def process_exception(self, request, exception): if isinstance(exception, RecordModifiedError): got_request_exception.send(sender=self, request=request) callback = get_callable(conf.HANDLER409) return callback(request, target=exception.target) else: # pragma: no cover pass
def get_file(request, app_label, model_name, field_name, object_id, filename): handler = get_callable( getattr(settings, 'PRIVATE_DOWNLOAD_HANDLER', 'private_files.handlers.basic')) model = apps.get_model(app_label, model_name) instance = get_object_or_404(model, pk=unquote(object_id)) condition = getattr(instance, field_name).condition single_use = getattr(instance, field_name).single_use if single_use: value = cache.get(request.GET.get('access-key', 'no-access-key'), None) cache.delete(request.GET.get('access-key', 'no-access-key')) if value != '%s-%s-%s-%s-%s' % (app_label, model_name, field_name, object_id, filename): raise PermissionDenied() if not model: raise Http404("") if not hasattr(instance, field_name): raise Http404("") if condition(request, instance): pre_download.send(sender=model, instance=instance, field_name=field_name, request=request) return handler(request, instance, field_name) else: raise PermissionDenied()
def render(self, context): if 'request' not in context: return '' request = context['request'] view_name = Variable(self.view_name).resolve(context) args = [Variable(arg).resolve(context) for arg in self.args] kwargs = {} for key, value in self.kwargs.items(): kwargs[key] = Variable(value).resolve(context) try: url = reverse(view_name, args=args, kwargs=kwargs) url = url.replace('%40', '@') match = resolve(url) view = match.func except NoReverseMatch: view = get_callable(view_name) if hasattr(view, 'as_view'): view = view.as_view() url = request.path if callable(view): old_path = request.path try: request.path = url v = view(request, *args, **kwargs) try: content = v.rendered_content except AttributeError: content = v.content.decode() return content finally: request.path = old_path raise ValueError('%r is not callable' % view)
def recalculate_total(self, save=True): """ Recalculates totals, discounts, taxes. """ items = list(self.items.all()) shared_state = {} processor_classes = [ get_callable(processor) for processor in plata.settings.PLATA_ORDER_PROCESSORS ] for p in (cls(shared_state) for cls in processor_classes): p.process(self, items) if save: self.save() [item.save() for item in items]
# using as many characters as needed to make a natural replacement AUTOSLUG_SLUGIFY_FUNCTION = 'autoslug.utils.translit_long' # using the minimum number of characters to make a replacement AUTOSLUG_SLUGIFY_FUNCTION = 'autoslug.utils.translit_short' # only performing single character replacements AUTOSLUG_SLUGIFY_FUNCTION = 'autoslug.utils.translit_one' .. _Unidecode: http://pypi.python.org/pypi/Unidecode .. _pytils: http://pypi.python.org/pypi/pytils .. _translitcodec: http://pypi.python.org/pypi/translitcodec `AUTOSLUG_MODELTRANSLATION_ENABLE` Django-autoslug support of modeltranslation_ is still experimental. If you wish to enable it, please set this option to `True` in your project settings. Default is `False`. .. _modeltranslation: http://django-modeltranslation.readthedocs.org """ from django.conf import settings from django.urls.utils import get_callable # use custom slugifying function if any slugify_function_path = getattr(settings, 'AUTOSLUG_SLUGIFY_FUNCTION', 'autoslug.utils.slugify') slugify = get_callable(slugify_function_path) # enable/disable modeltranslation support autoslug_modeltranslation_enable = getattr(settings, 'AUTOSLUG_MODELTRANSLATION_ENABLE', False)
def getEditorClass(): global _EditorClass if not _EditorClass: _EditorClass = get_callable(settings.EDITOR) return _EditorClass
def setup_default_urls(module_name): """ Setup default url patterns by adding additional url patterns to the given list of url patterns. Such additional patterns are concerned about serving media in DEBUG mode and adding default 404 and 500 handlers in PRODUCTION mode. """ # get urls module urls = get_module_by_name(module_name) # create empty urlpatterns if not there yet if not hasattr(urls, 'urlpatterns'): urls.urlpatterns = [] # django debug toolbar (debug only) if settings.DEBUG and settings.DEBUG_TOOLBAR: import debug_toolbar urls.urlpatterns += [ url(r'^__debug__/', include(debug_toolbar.urls)), ] # 404 and 500 pages (PRODUCTION only) if not settings.DEBUG: urls.handler404 = settings.HANDLER_404 urls.handler500 = settings.HANDLER_500 else: # pragma: no cover urls.urlpatterns += [ url(r'^404/$', get_callable(settings.HANDLER_404)), url(r'^500/$', get_callable(settings.HANDLER_500)), ] # enable postcode view if postcode in installed apps if 'cubane.postcode' in settings.INSTALLED_APPS: from cubane.postcode import views as postcode_views urls.urlpatterns += [ url(r'^postcode-lookup/$', postcode_views.postcode_lookup, name='cubane.postcode_lookup') ] # serve media assets through django dev server (DEBUG only) if settings.DEBUG: # pragma: no cover # uploaded media files urls.urlpatterns += [ url( r'^%s(?P<path>.*)$' % settings.MEDIA_URL, static.serve, {'document_root': settings.MEDIA_ROOT} ) ] # serve media assets through frontend media api to allow for on-demand # media customisation and for downloading SVG icon sets if 'cubane.media' in settings.INSTALLED_APPS: from cubane.media import views as media_views # shapes urls.urlpatterns += [ url( r'^%sshapes/(?P<shape>[-_\w]+)/(?P<size>[-_\w]+)/(?P<bucket>\d+)/(?P<pk>\d+)/(?P<filename>.*?)$' % settings.MEDIA_API_URL, media_views.media_api, name='cubane.media_api.shape' ) ] # originals urls.urlpatterns += [ url( r'^%soriginals/(?P<bucket>\d+)/(?P<pk>\d+)/(?P<filename>.*?)$' % settings.MEDIA_API_URL, media_views.media_api_original, name='cubane.media_api.original' ) ] # by identifier alone urls.urlpatterns += [ url( r'^%spk/(?P<pk>\d+)/$' % settings.MEDIA_API_URL, media_views.media_api_pk, name='cubane.media_api.pk' ) ] # SVG icons sets (DEBUG only) if settings.DEBUG and 'cubane.svgicons' in settings.INSTALLED_APPS: from cubane.svgicons import views as svgicons_views urls.urlpatterns += [ # individual icon url( r'^%ssvgicons/(?P<target>[-_\w\d]+)/(?P<name>[-_\w\d]+)\.svg$' % settings.MEDIA_API_URL, svgicons_views.media_api_svgicons ), # full icon set url( r'^%ssvgicons/(?P<target>[-_\w\d]+)\.svg$' % settings.MEDIA_API_URL, svgicons_views.media_api_svgicons, kwargs={'name': None} ) ]