djangosnippets.org: Latest snippets tagged with 'decorator'https://djangosnippets.org/tags/decorator/2017-10-03T13:43:53.250581-05:00CBV decorator from view function decorator
2017-10-03T13:43:53.250581-05:00powderflaskhttps://djangosnippets.org/snippets/10624/<p>The Mixin approach for applying permissions to CBV views suffers from 3 issues:</p>
<ol>
<li>you need to read the code to see what permissions are being applied to a View</li>
<li>multiple bits of disparate code required to specify, e.g., a simple permission check</li>
<li>permissions set on a base class are overridden …</li></ol>
Freely redistributableDecorator to add placeholders to field
2017-05-12T15:12:40.064009-05:00stanihttps://djangosnippets.org/snippets/10611/<p>Decorator to automagically add placeholders to form widgets. <code>cls</code> can be any class derived from <code>django.forms.Form</code> or <code>django.forms.ModelForm</code>. The field labels are used as value for the placeholder. This will affect all form instances of this class.</p>
<ul>
<li>add_placeholders only to forms.TextInput and form.Textarea</li>
<li>add_placeholders_to_any_field adds placeholders to any field …</li></ul>
Freely redistributableEllipsis paginator decorator with first and last two items
2015-10-23T15:23:32.571887-05:00karolyihttps://djangosnippets.org/snippets/10535/<p>Use this template tag to get a paginator showing the first and last two pages w/ adjacent pages using ellipsis.</p>
<p>The <code>page</code> parameter is a page of a <code>Paginator</code> (typically the first but you can use whichever you want).</p>
<p>In case of 50 pages, while being on the 40th, it'll …</p>
Freely redistributable@group_required decorator
2015-07-09T14:46:33.820390-05:00sebastien247https://djangosnippets.org/snippets/10508/<p>Use :</p>
<p><code>@group_required(('toto', 'titi'))</code></p>
<p><code>def my_view(request):</code></p>
<p><code>...</code></p>
<p><code>@group_required('toto')</code></p>
<p><code>def my_view(request):</code></p>
<p><code>...</code></p>
<p>Note that group_required() also takes an optional login_url parameter
<code>@group_required('toto', login_url='/loginpage/')</code></p>
<p><code>def my_view(request):</code></p>
<p><code>...</code></p>
<p>As in the login_required() decorator, login_url defaults to settings.LOGIN_URL.</p>
<p>If the raise_exception parameter is given, the decorator will raise PermissionDenied, prompting the 403 (HTTP Forbidden) …</p>
Freely redistributableQuick line profiler decorator
2015-05-12T06:27:17.066313-05:00ibestuzhevhttps://djangosnippets.org/snippets/10483/<p>Requires <a href="https://pypi.python.org/pypi/line_profiler">line_profiler</a></p>
<pre><code>pip install line_profiler
</code></pre>
<p>Will print profile info into console</p>
<pre><code>@line_profiler
def my_view(request):
context = some_quick_func()
return some_heavy_func(context)
</code></pre>
<p>It does not work good when nested, so don't wrap <code>some_heavy_func</code>. If you want to profile also some nested call - use <code>extra_view</code> parameter:</p>
<pre><code>@line_profiler(extra_view=[some_heavy_func])
def my_view(request):
context = some_quick_func() …</code></pre>
Freely redistributableDecorator @not_login_required
2015-02-12T05:00:45.848000-06:00florent1933https://djangosnippets.org/snippets/10465/<p>This is a simple django snippet!</p>
<p>It is the <em>opposite of @login_required</em> decorator for Django views</p>
<p>Example</p>
<p><strong>@not_login_required</strong></p>
<p><code>def login_page(request):
...</code></p>
Freely redistributableMiddleware decoratores for classed based views
2014-08-27T05:01:06.289396-05:00dhkehttps://djangosnippets.org/snippets/3091/<p>Decorators to attach middleware to class based views w/o arguments.</p>
Freely redistributablevary_on_user
2014-04-28T16:23:10.450013-05:00TAHhttps://djangosnippets.org/snippets/3045/<p>@vary_on_user doesn't work properly if your site uses third-party cookies, since the cache key is created from <em>all</em> of the cookies in the request header. This decorator caches pages based on the user ID, so it works reliably.</p>
Freely redistributableDecorator to execute a method only once
2013-12-11T15:47:24.581181-06:00atodorov-otbhttps://djangosnippets.org/snippets/3010/<p>Beware if using Amazon Simple Queue Service to execute Celery tasks which send email messages! Sometimes SQS messages are duplicated which results in multiple copies of the messages being sent. This is a simple decorator which uses a cache backend to prevent the task from executing twice in a specified …</p>
Freely redistributableanonymous_required decorator
2013-07-04T07:13:22.246922-05:00yettyhttps://djangosnippets.org/snippets/2969/<p>Decorator to preserve view from logged users.</p>
Freely redistributableDecorator
2013-04-19T14:39:22.370819-05:00chachanhttps://djangosnippets.org/snippets/2912/<p>[Understanding Python Decorators in 12 Easy Steps!] (http://simeonfranklin.com/blog/2012/jul/1/python-decorators-in-12-steps/)</p>
Freely redistributableHeader view decorators
2013-02-19T05:55:54.198438-06:00ydmhttps://djangosnippets.org/snippets/2895/<p>This file includes two Django view decorators <code>header</code> and <code>headers</code> that provide an easy way to set response headers.</p>
<p>Also, because I have to work with a lot of cross domain requests, I include few shortcuts for convenience to set the Access-Control-Allow-Origin header appropriately.</p>
Freely redistributableJSON decorator for views handling ajax requests
2012-12-23T11:05:05.312451-06:00anilshanbhaghttps://djangosnippets.org/snippets/2869/<p>Sample usage for using decorator
<code>@json_response(ajax_required=True, login_required=True)</code>
<code>def subscribe(request):</code>
<code>return {"status":"success"}</code></p>
<p>Converts a function returning dict into json response. Does is_ajax check and user authenticated check if set in flags. When function returns HttpResponse does nothing.</p>
Freely redistributableFunction decorator for caching function results in local memory
2012-09-24T04:58:24.691794-05:00bikeshedderhttps://djangosnippets.org/snippets/2824/<pre><code>@cache(60)
def heavy_computation():
for x in xrange(10000000):
pass # just waste some time`
</code></pre>
<p>The result of <code>heavy_computation</code> calls will be cached for 60 seconds. This cache decorator does not use the Django cache backend and uses the local memory instead.</p>
Freely redistributableyet another render_to_response decorator
2012-09-09T16:00:38.806674-05:00alekshttps://djangosnippets.org/snippets/2812/<p>This one takes a template path as an argument. Return dictionary with template values from your view. It's simple as:</p>
<p>@render_to_template('posts/post_list.html')
def api_get_all(request):
return {'test': 'testing!'}</p>
Freely redistributable