Advertisement
serrggee

Untitled

Jul 31st, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.95 KB | None | 0 0
  1. url(r'^$', StaffListView.as_view(), name="staffmember_list"),
  2.  
  3. class StaffListView(ListView):
  4.     model = StaffMember
  5.     queryset = StaffMember.objects.order_by('order').all()
  6.  
  7.     def render_to_response(self, context, **response_kwargs):
  8.         # Shim to affect the CMS Toolbar only
  9.         if self.request.toolbar and self.request.toolbar.edit_mode:
  10.             menu = self.request.toolbar.get_or_create_menu('staff-list-menu', 'Leadership')
  11.             menu.add_sideframe_item(u'Seniority List', url=reverse('admin:staff_seniority_changelist'))
  12.             menu.add_modal_item('Add new Seniority', url="%s" % (reverse('admin:staff_seniority_add'), ))
  13.             menu.add_break()
  14.             menu.add_sideframe_item(u'Staff List', url=reverse('admin:staff_staffmember_changelist'))
  15.             menu.add_modal_item('Add new Staff Member', url="%s" % (reverse('admin:staff_staffmember_add'), ))
  16.  
  17.         return super(StaffListView, self).render_to_response(context, **response_kwargs)
  18.  
  19. class View(object):
  20.     """
  21.    Intentionally simple parent class for all views. Only implements
  22.    dispatch-by-method and simple sanity checking.
  23.    """
  24.  
  25.     http_method_names = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace']
  26.  
  27.     def __init__(self, **kwargs):
  28.         """
  29.        Constructor. Called in the URLconf; can contain helpful extra
  30.        keyword arguments, and other things.
  31.        """
  32.         # Go through keyword arguments, and either save their values to our
  33.         # instance, or raise an error.
  34.         for key, value in six.iteritems(kwargs):
  35.             setattr(self, key, value)
  36.  
  37.     @classonlymethod
  38.     def as_view(cls, **initkwargs):
  39.         """
  40.        Main entry point for a request-response process.
  41.        """
  42.         # sanitize keyword arguments
  43.         for key in initkwargs:
  44.             if key in cls.http_method_names:
  45.                 raise TypeError("You tried to pass in the %s method name as a "
  46.                                 "keyword argument to %s(). Don't do that."
  47.                                 % (key, cls.__name__))
  48.             if not hasattr(cls, key):
  49.                 raise TypeError("%s() received an invalid keyword %r. as_view "
  50.                                 "only accepts arguments that are already "
  51.                                 "attributes of the class." % (cls.__name__, key))
  52.  
  53.         def view(request, *args, **kwargs):
  54.             self = cls(**initkwargs)
  55.             if hasattr(self, 'get') and not hasattr(self, 'head'):
  56.                 self.head = self.get
  57.             self.request = request
  58.             self.args = args
  59.             self.kwargs = kwargs
  60.             return self.dispatch(request, *args, **kwargs)
  61.  
  62.         # take name and docstring from class
  63.         update_wrapper(view, cls, updated=())
  64.  
  65.         # and possible attributes set by decorators
  66.         # like csrf_exempt from dispatch
  67.         update_wrapper(view, cls.dispatch, assigned=())
  68.         return view
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement