Server IP : 85.214.239.14 / Your IP : 3.139.86.74 Web Server : Apache/2.4.62 (Debian) System : Linux h2886529.stratoserver.net 4.9.0 #1 SMP Tue Jan 9 19:45:01 MSK 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.18 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : OFF | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /proc/3/task/3/cwd/srv/modoboa/env/lib/python3.5/site-packages/braces/views/ |
Upload File : |
from django.core.exceptions import ImproperlyConfigured from django.shortcuts import redirect try: from django.utils.encoding import force_str as force_string except ImportError: from django.utils.encoding import force_text as force_string try: from django.urls import resolve except ImportError: from django.core.urlresolvers import resolve class SetHeadlineMixin(object): """ Mixin allows you to set a static headline through a static property on the class or programmatically by overloading the get_headline method. """ headline = None # Default the headline to none def get_context_data(self, **kwargs): kwargs = super(SetHeadlineMixin, self).get_context_data(**kwargs) # Update the existing context dict with the provided headline. kwargs.update({"headline": self.get_headline()}) return kwargs def get_headline(self): if self.headline is None: # If no headline was provided as a view # attribute and this method wasn't # overridden raise a configuration error. raise ImproperlyConfigured( '{0} is missing a headline. ' 'Define {0}.headline, or override ' '{0}.get_headline().'.format(self.__class__.__name__)) return force_string(self.headline) class StaticContextMixin(object): """ Mixin allows you to set static context through a static property on the class. """ static_context = None def get_context_data(self, **kwargs): kwargs = super(StaticContextMixin, self).get_context_data(**kwargs) try: kwargs.update(self.get_static_context()) except (TypeError, ValueError): raise ImproperlyConfigured( '{0}.static_context must be a dictionary or container ' 'of two-tuples.'.format(self.__class__.__name__)) else: return kwargs def get_static_context(self): if self.static_context is None: raise ImproperlyConfigured( '{0} is missing the static_context property. Define ' '{0}.static_context, or override ' '{0}.get_static_context()'.format(self.__class__.__name__) ) return self.static_context class CanonicalSlugDetailMixin(object): """ A mixin that enforces a canonical slug in the url. If a urlpattern takes a object's pk and slug as arguments and the slug url argument does not equal the object's canonical slug, this mixin will redirect to the url containing the canonical slug. """ def dispatch(self, request, *args, **kwargs): # Set up since we need to super() later instead of earlier. self.request = request self.args = args self.kwargs = kwargs # Get the current object, url slug, and # urlpattern name (namespace aware). obj = self.get_object() slug = self.kwargs.get(self.slug_url_kwarg, None) match = resolve(request.path_info) url_parts = match.namespaces url_parts.append(match.url_name) current_urlpattern = ":".join(url_parts) # Figure out what the slug is supposed to be. if hasattr(obj, "get_canonical_slug"): canonical_slug = obj.get_canonical_slug() else: canonical_slug = self.get_canonical_slug() # If there's a discrepancy between the slug in the url and the # canonical slug, redirect to the canonical slug. if canonical_slug != slug: params = {self.pk_url_kwarg: obj.pk, self.slug_url_kwarg: canonical_slug, 'permanent': True} return redirect(current_urlpattern, **params) return super(CanonicalSlugDetailMixin, self).dispatch( request, *args, **kwargs) def get_canonical_slug(self): """ Override this method to customize what slug should be considered canonical. Alternatively, define the get_canonical_slug method on this view's object class. In that case, this method will never be called. """ return self.get_object().slug class AllVerbsMixin(object): """Call a single method for all HTTP verbs. The name of the method should be specified using the class attribute ``all_handler``. The default value of this attribute is 'all'. """ all_handler = 'all' def dispatch(self, request, *args, **kwargs): if not self.all_handler: raise ImproperlyConfigured( '{0} requires the all_handler attribute to be set.'.format( self.__class__.__name__)) handler = getattr(self, self.all_handler, self.http_method_not_allowed) return handler(request, *args, **kwargs) class HeaderMixin(object): """ Add arbitrary HTTP headers to a response by specifying them in the ``headers`` attribute or by overriding the ``get_headers()`` method. """ headers = {} def get_headers(self, request): return self.headers def dispatch(self, request, *args, **kwargs): """ Override this method to customize the way additional headers are retrieved. It is mandatory that the returned value supports the ``.items()`` method. """ response = super(HeaderMixin, self).dispatch(request, *args, **kwargs) for key, value in self.get_headers(request).items(): if key not in response: response[key] = value return response