def apply_decorators( # noqa: C901 view=None, login=False, staff=False, perms=None, cache=None, gzip=False, xframe=None, csrf=None, decorators=(), ): """ Apply decorators to view function. Can also be used as a decorator. """ if view is None: kwargs = locals() kwargs.pop("view") return lambda view: apply_decorators(view, **kwargs) # Cache control if cache is False: view = never_cache(view) elif cache is not None: view = cache_control(**cache)(view) # Permissions # (We keep the implementation here, but those options are not handled by # this decorator anymore). if login: view = login_required(view) if perms: view = permission_required(perms)(view) if staff: view = staff_required(view) # Compression if gzip: view = gzip_page(view) # Security if xframe is False: view = xframe_options_exempt(view) elif xframe == "deny": view = xframe_options_deny(view) elif xframe == "sameorigin": view = xframe_options_sameorigin(view) if csrf is False: view = csrf_exempt(view) elif csrf == "cookie": view = ensure_csrf_cookie(view) elif csrf == "token": view = requires_csrf_token(view) elif csrf is True: view = csrf_protect(view) # Apply final decorators for decorator in reversed(decorators): view = decorator(view) return view
def as_view(cls, **kwargs): """ Optionally decorates the base view with django.views.decorators.gzip.gzip_page(). """ view = super(HomeFormView, cls).as_view(**kwargs) return gzip.gzip_page(view) if cls.gzip_page else view
def as_view(cls, **kwargs): """ Optionally decorates the base view with django.views.decorators.gzip.gzip_page(). """ view = super(GZipPage, cls).as_view(**kwargs) return gzip.gzip_page(view) if cls.gzip_page else view
def apply_decorators( view=None, login=False, staff=False, perms=None, # noqa: C901 cache=None, gzip=False, xframe=None, csrf=None, decorators=()): """ Apply decorators to view function. Can also be used as a decorator. """ if view is None: kwargs = locals() kwargs.pop('view') return lambda view: apply_decorators(view, **kwargs) # Cache control if cache is False: view = never_cache(view) elif cache is not None: view = cache_control(**cache)(view) # Permissions if login: view = login_required(view) if perms: view = permission_required(perms)(view) if staff: view = staff_required(view) # Compression if gzip: view = gzip_page(view) # Security if xframe is False: view = xframe_options_exempt(view) elif xframe == 'deny': view = xframe_options_deny(view) elif xframe == 'sameorigin': view = xframe_options_sameorigin(view) if csrf is False: view = csrf_exempt(view) elif csrf == 'cookie': view = ensure_csrf_cookie(view) elif csrf == 'token': view = requires_csrf_token(view) elif csrf is True: view = csrf_protect(view) # Apply final decorators for decorator in reversed(decorators): view = decorator(view) return view
def gzip_page_ajax(func): """ Smarter version of django's gzip_page: - If settings.DEBUG not set, will always gzip - If settings.DEBUG set, will gzip only if request is an ajax request This allows you to use django-debug-toolbar in DEBUG mode (if you gzip a response then the debug toolbar middleware won't run) """ gzipped_func = gzip_page(func) if not settings.DEBUG: return gzipped_func @wraps(func, assigned=available_attrs(func)) def conditional_gzip_func(request, *args, **kwargs): if request.is_ajax(): return gzipped_func(request, *args, **kwargs) return func(request, *args, **kwargs) return conditional_gzip_func
from django.conf import urls from django.views.decorators.gzip import gzip_page from otree_redwood.views import DebugView, EventsJsonAPI urlpatterns = [ urls.url(DebugView.url_pattern, DebugView.as_view(), name=DebugView.url_name), urls.url(EventsJsonAPI.url_pattern, gzip_page(EventsJsonAPI.as_view()), name=EventsJsonAPI.url_name), ]
from erp.models import PRODUCT from uuid import uuid4 as uuid_random from datetime import datetime import os.path import socket import sys import os import re ################################################################################ ################################################################################ gzip_page = lambda fn: fn if settings.DEBUG else gzip.gzip_page (fn) ################################################################################ ################################################################################ @gzip_page def main (request, page='home'): if request.session.has_key ('timestamp') and 'refresh' not in request.GET: request.session['timestamp'] = datetime.now () request.session.save () else: request.session['timestamp'] = datetime.now () request.session.save () init (request)
GeoCodeMethodListView.as_view(), name='geo_code_methods_list'), url(r'^geo_code_methods/(?P<pk>[^/]+)/$', GeoCodeMethodDetailView.as_view(), name='geo_code_method_detail'), url(r'^facility_coordinates/(?P<pk>[^/]+)/$', FacilityCoordinatesCreationAndDetail.as_view(), name='facility_coordinates_simple_detail'), url(r'^facility_coordinates/$', FacilityCoordinatesCreationAndListing.as_view(), name='facility_coordinates_simple_list'), url(r'^coordinates/$', gzip_page( cache_page(coordinates_cache_seconds) (FacilityCoordinatesListView.as_view())), name='facility_coordinates_list'), url(r'^coordinates/(?P<pk>[^/]+)/$', gzip_page( cache_page(coordinates_cache_seconds) (FacilityCoordinatesDetailView.as_view())), name='facility_coordinates_detail'), url(r'^country_borders/$', gzip_page( cache_page(cache_seconds) (WorldBorderListView.as_view())), name='world_borders_list'), url(r'^country_borders/(?P<pk>[^/]+)/$', gzip_page(
def wrapper(request, *args, **kwargs): display_length = getattr(request.REQUEST, 'iDisplayLength', None) if not display_length or display_length > 0: return func(request, *args, **kwargs) else: return gzip_page(func)(request, *args, **kwargs)
urlpatterns = patterns( '', # Shipments url(r'^list/$', views.ShipmentsListView.as_view(), name='shipments_list'), url(r'^map/$', views.ShipmentsDashboardView.as_view(), name='shipments_dashboard'), url(r'^map/package/(?P<pk>\d+)/$', views.ShipmentsPackageMapView.as_view(), name='shipments_package_map'), url(r'^new/$', views.ShipmentCreateView.as_view(), name='create_shipment'), url(r'^shipment/(?P<pk>\d+)/$', views.ShipmentUpdateView.as_view(), name='edit_shipment'), url(r'^shipment/(?P<pk>\d+)/packages/$', gzip_page(csrf_exempt(views.ShipmentPackagesView.as_view())), name='shipment_packages'), url(r'^shipment/$', views.ShipmentUpdateView.as_view(), name='edit_shipment_no_pk'), url(r'^shipment/delete/(?P<pk>\d+)/$', views.ShipmentDeleteView.as_view(), name='delete_shipment'), url(r'^shipment/lost/(?P<pk>\d+)/$', views.ShipmentLostView.as_view(), name='lost_shipment'), url(r'^shipment/editlost/(?P<pk>\d+)/$', views.ShipmentEditLostView.as_view(), name='edit_lost_note'), # Packages url(r'^new/pkg/(?P<pk>\d+)/$', views.NewPackageModalView.as_view(), name='new_package'), url(r'^edit/pkg/(?P<pk>\d+)/$', views.EditPackageModalView.as_view(), name='edit_package'), url(r'^pkg_from_kit/(?P<pk>\d+)/$',
def as_view(cls, **initkwargs): view = super(GzipPageMixin, cls).as_view(**initkwargs) return gzip_page(view)
from django.contrib import admin from django.contrib.sitemaps import views as sitemap_views from django.urls import include, path from django.views.decorators.gzip import gzip_page from django.views.i18n import JavaScriptCatalog from dictionary.sitemaps import sitemaps urlpatterns = [ path("", include("dictionary.urls")), path("graphql/", include("dictionary_graph.urls")), path("admin/", admin.site.urls), # i18n path("jsi18n/", JavaScriptCatalog.as_view(packages=["dictionary"]), name="javascript-catalog"), path("i18n/", include("django.conf.urls.i18n")), # Sitemap path("sitemap.xml", gzip_page(sitemap_views.index), {"sitemaps": sitemaps}), path( "sitemap-<section>.xml", gzip_page(sitemap_views.sitemap), {"sitemaps": sitemaps}, name="django.contrib.sitemaps.views.sitemap", ), ] # Will consider this near release: # https://docs.djangoproject.com/en/3.0/topics/i18n/translation/#note-on-performance
'', # Shipments url(r'^list/$', views.ShipmentsListView.as_view(), name='shipments_list'), url(r'^map/$', views.ShipmentsDashboardView.as_view(), name='shipments_dashboard'), url(r'^map/package/(?P<pk>\d+)/$', views.ShipmentsPackageMapView.as_view(), name='shipments_package_map'), url(r'^new/$', views.ShipmentCreateView.as_view(), name='create_shipment'), url(r'^shipment/(?P<pk>\d+)/$', views.ShipmentUpdateView.as_view(), name='edit_shipment'), url(r'^shipment/(?P<pk>\d+)/packages/$', gzip_page(csrf_exempt(views.ShipmentPackagesView.as_view())), name='shipment_packages'), url(r'^shipment/$', views.ShipmentUpdateView.as_view(), name='edit_shipment_no_pk'), url(r'^shipment/delete/(?P<pk>\d+)/$', views.ShipmentDeleteView.as_view(), name='delete_shipment'), url(r'^shipment/lost/(?P<pk>\d+)/$', views.ShipmentLostView.as_view(), name='lost_shipment'), url(r'^shipment/editlost/(?P<pk>\d+)/$', views.ShipmentEditLostView.as_view(), name='edit_lost_note'), # Packages
Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: path('', views.home, name='home') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') Including another URLconf 1. Import the include() function: from django.urls import include, path 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.conf import settings from django.contrib import admin from django.urls import path from django.conf.urls import url from django.views.decorators.cache import never_cache from django.views.decorators.gzip import gzip_page from django.views.generic import TemplateView from django.views.static import serve urlpatterns = [ path('admin/', admin.site.urls), url(r'^static/(?P<path>.*)$', gzip_page(serve), kwargs=dict(document_root=settings.FRONTEND_STATIC_ROOT)), url(r'^$', never_cache(gzip_page( TemplateView.as_view(template_name='index.html'))), name='main'), ]
from django.urls import path from app02.views import test, test_signal from django.views.decorators.gzip import gzip_page urlpatterns = [ path("test/", gzip_page(test), name="test"), path("test_signal/", gzip_page(test_signal), name="test_signal"), ]
from django.urls import path # Gzip from django.views.decorators.gzip import gzip_page from app01.views import Index, TornAsyncioView, Testasync, Testsync from app01.views import current_datetime, async_celery from app01.views import tran_handler, request_def urlpatterns = [ path("index/", gzip_page(Index.as_view()), name="index"), path("example/", current_datetime, name="example"), path("async/", Testasync.as_view(), name="async"), path("sync/", Testsync.as_view(), name="sync"), path("torn/", TornAsyncioView.as_view(), name="torn"), path("async_celery/", async_celery, name="async_celery"), path("tran/", tran_handler, name="tran_handler"), path("request/", gzip_page(request_def), name="request_def"), ]
from django.conf.urls import patterns, include, url from django.views.decorators.gzip import gzip_page from . import chart_views, views urlpatterns = patterns('', url(r'^$', views.Landing.as_view(), name="home"), url(r'^building/$', gzip_page(views.BuildingsList.as_view()), name="building_list"), url(r'^building/(?P<elbi>\d+)-(?P<slug>[-\w]+)/$', views.BuildingView.as_view(), name="building_detail"), url(r'^about/$', views.About.as_view(), name='about'), # chart url patterns url(r'^chart/', include(patterns('', url(r'^elevatorlist/(?P<data>data.json)?$', gzip_page(chart_views.ElevatorList.as_view()), name='elevatorlist'), url(r'^locator/(?P<data>data.json)?$', gzip_page(chart_views.Locator.as_view()), name='locator'), url(r'^search/(?P<data>data.json)?$', gzip_page(chart_views.Search.as_view()), name='search'), ), namespace='chart')), )