There are great things in django - generic.views that allow you to list, create and edit models in a few lines. But, unfortunately, ordinary wrappers are not applicable to them, which, for example, check if the user is logged in.
urls.py:
url(r'^network/$', ComputerListView.as_view(), name='computer_list'),
views.py:
class ComputerListView(SiteCommonView, ListView): # класс SiteCommonView передан сюда, чтобы брать оттуда общий контекст для всех представлений model = Computer queryset = Computer.objects.all() #@auth_user_required #def as_view(self, *args, **kwargs): # return super(ComputerListView, self).as_view(self, *args, **kwargs) def get_context_data(self, **kwargs): c = super(ComputerListView, self).get_context_data(**kwargs) c.update(self.get_context()) # это единственное для чего нужен SiteCommonView c['title'] = u'Компьютеры в сети' return c # обертка def auth_user_required(*args_, **kwargs_): def wrapper(func): def tmp(*args, **kwargs): request = args[0] if request.user: profile = get_user_profile(request) if profile: kwargs['profile'] = profile return func(*args, **kwargs) raise Http403() return tmp return wrapper
So, if we uncomment
#def as_view(self, *args, **kwargs): # return super(ComputerListView, self).as_view(self, *args, **kwargs)
We TypeError at unbound method as_view() must be called with ComputerListView instance as first argument (got nothing instead)
error: TypeError at unbound method as_view() must be called with ComputerListView instance as first argument (got nothing instead)
If you uncomment
#@auth_user_required
we get TypeError at /network/ wrapper() got an unexpected keyword argument 'object_list'
How to write wrappers for class methods in this case?