This is a simplest approach possible. `as_view()` is replaced, so
that it applies the given decorator before returning.
In this approach, decorators are always put on top - that means it's not
possible to have functions called in this order:
B.dispatch, login_required, A.dispatch
NOTE: By default this modifies the given class, so be careful when doing this:
TemplateView = view_decorator(login_required)(TemplateView)
Because it will modify the TemplateView class. Instead create a fresh
class first and apply the decorator there. A shortcut for this is
specifying the ``subclass`` argument. But this is also dangerous. Consider:
@view_decorator(login_required, subclass=True)
class MyView(View):
def get_context_data(self):
data = super(MyView, self).get_context_data()
data["foo"] = "bar"
return data
This looks like a normal Python code, but there is a hidden infinite
recursion, because of how `super()` works in Python 2.x; By the time
`get_context_data()` is invoked, MyView refers to a subclass created in
the decorator. super() looks at the next class in the MRO of MyView,
which is the original MyView class we created, so it contains the
`get_context_data()` method. Which is exactly the method that was just
called. BOOM!
This code publishes an iCal file that can be subscribed to in Google Calendar. They change the way they interpret iCal data occasionally, so this may break, I'll try to keep it up to date.
There is some crazy string replace stuff going on there, I haven't yet convinced vObject to format things properly.
Feedback welcome.
*Note: this works for my existing feeds, but if I add a new feed to GCal, the timezones are incorrect, I'm working on that.
If you inherit from ValidatedModel instead of from models.Model, then full_clean() will be called before save().
So, add validators to your field definitions, and all your fields will be validated before they go to the database.
The same thing can be accomplished with a pre_save signal, but the code is quite a bit messier than the simple inheritance above.
The code here will take an EmailMessage from django.core.mail and replace the sourcing of any images served by the application with attached image content. Note: This expects a valid closing tag of **/>** on img elements, it will not properly handle filenames with **'** characters in it, and it does not handle if invalid image sources are listed.
It's often useful to dynamically create filter criteria, and Q objects are useful for that, but sometimes you need to make a combined Q composed of various alternates. This bit of code eases the awkwardness of creating the first Q so that there's a combiner, plus the odd case of no criteria.
The canonical notion of urls ending in slashes dates from a web where urls were used to access documents and files this is no longer the case so keeping your urls witouth trailing slashes makes them prettier. The problem is that many people/blogs/spiders/browsers could end up with a url with slashes which can be problematic for you SEO, or confuse users.
This script is for sites with no trailing slash dicipline in their urls, and to prevent everybody getting a horrible 404 for a simple slash you just got to remove it and issue a permanent redirect (301) and you'll get your pretty urls your cake and eat it too.
I must stress, you will have to edit all your public urls removing the slashes like so:
url(r'^login$', login,}
If you forget, to edit them and visit the url, your browser will remember the redirect and you'll have to clean the browsing history to fix it.
Template filter that truncates the text when it exceeds a certain number of characters.
It deletes the last word only if partial.
Adds '...' at the end of the text, only if truncated.
Examples (text == 'Lorem ipsum dolor sit amet', len(text) == 26)
{{ text|truncatewords_by_chars:30 }}
'Lorem ipsum dolor sit amet'
{{ text|truncatewords_by_chars:25 }}
'Lorem ipsum dolor sit...'
{{ text|truncatewords_by_chars:21 }}
'Lorem ipsum dolor sit...'
{{ text|truncatewords_by_chars:20 }}
'Lorem ipsum dolor...'
By Davide Muzzarelli
Template filter that divides a list into an exact number of columns.
The number of columns is guaranteed.
Example (list == [1,2,3,4,5,6,7,8,9,10]):
{% for column in list|columns:3 %}
<ul>
{% for item in column %}
<li>{{ item }}</li>
{% endfor %}
</ul>
{% endfor %}
Result:
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
<ul>
<li>5</li>
<li>6</li>
<li>7</li>
</ul>
<ul>
<li>8</li>
<li>9</li>
<li>10</li>
</ul>
By Davide Muzzarelli
1. Define a subclass extends forms.ModelForm
2. Add the __init__ method
3. Define your ModelForm extends this class
4. Define the CSS for "large","xlarge" and "xxlarge",or define yourself.
Dead simple snippet. Paste it in some models (i use project_specific/models.py), and you don't need to run update_index anymore. When a model is deleted from the database: it is deleted from the index. When a model is saved (created or modified): it is updated in the index.
Define validator `multiple_email_validator` that splits value by commas and calls `validate_email` validator for each element found.
Then define MultipleEmailField with this default validator and augmented max_length.
Then ... use it!
Based on [#1879](http://djangosnippets.org/snippets/1879/) and [#2356](http://djangosnippets.org/snippets/2356/)
Works in Django 1.3
Hopefully it's generic enough to implement a compact (sparse) version of whatever custom filter you need.