A middleware which will protect from page hammering using flexible spanning time windows using the cache backend.
Please read the Docstring of the class for details.
should probably be migrated to an inclusion tag to allow a source timezone that isn't the site specific TIME_ZONE.
This code assumes that your database stores dates according to the django.conf.settings.TIME_ZONE variable.
Yes.. this assumes that dates are stored in the database according to system time. On my systems the system time of a server is always UTC therefore avoiding problems with datetime (no tz info) columns in backend databases having no timezone information and stored according to the database or system timezone information. I find it a good practice to always use UTC for any stored information and always retrieve information as UTC and localize the date during display.
Based on discussion on [http://thebuild.com/blog/2010/12/14/using-server-side-postgresql-cursors-in-django/](http://thebuild.com/blog/2010/12/14/using-server-side-postgresql-cursors-in-django/) and [http://thebuild.com/blog/2010/12/13/very-large-result-sets-in-django-using-postgresql/](http://thebuild.com/blog/2010/12/13/very-large-result-sets-in-django-using-postgresql/) but instead implements them via extending the psycopg2 backend which allows you to use all of django's machinery without having to resort to using raw cursors.
Usage:
qs = Model.objects.all()
with server_side_cursors(qs, itersize=100):
for item in qs.iterator():
item.value
if random_reason_to_break:
break
Setup:
In your own project create the package hierarchy myproject.db.backends.postgresql_psycopg2 and place the code in base.py.
In your settings.py set the database ENGINE to be 'myproject.db.backends.postgresql_psycopg2'.
If you using south you'll have to let it know its a postgresql_psycopg2 backend by adding to SOUTH_DATABASE_ADAPTERS (see south documentation).
Note:
Make sure your using psycopg >= 2.4 for efficient named (server side) cursor iteration.
Nice to name your constant multiple choice fields in models, this is one way of doing that. Sorry I haven't looked into existing alternatives. But this approach worked for me.
Given an unbound form, determine what data would be generated from POSTing the form unchanged. The goal is to end up with a dict such that, passed into another form constructor as its data kwarg, form.changed_data == [].
Adds to Django 1.2 tag `{% elif %}`
{% if user.nick == "guest" %}
Hello guest!
{% elif user.nick == "admin" or user.is_admin %}
Hello admin!
{% elif user %}
You are registered user
{% else %}
Login to site
{% endif %}
Snipped designed for [gaeframework.com](http://www.gaeframework.com)
Inspired by snippets: [#1572](http://djangosnippets.org/snippets/1572/) and [#2243](http://djangosnippets.org/snippets/2243/)
usage :-
put it in python path and refer to it from settings.py
`THUMBNAIL_BACKEND = 'full.import.path.to.SEOThumbnailBackend'`
Took me a bit to figure it out since i couldn't find an existing example code for it.
**You can save these codes into a templatetag file in your django app. Then use codes like these in your template files:**
****************************************************************************
{% load *yourtags* %}
...
<img src="{% thumb *yourmodel.picturefield* 200 300 0 %}"/>
<img src="{% thumb *yourmodel.picturefield* 500 400 yes %}"/>
...
The parameters are:
imagefield ImageField
width Integer
height Integer
rescale [1, yes, true, 0, no, false]
**Some codes come from <djangosnippets.org>**
I needed to overwrite files on update (not create new ones) but also a validation which would prevent to upload 2 files with the same name.
The CustomCheckFiled triggers a validation passing the filename and model instance. If the validation returns false, the validation error_message will be displayed in admin.
The OverwriteStorage is needed because the default storage alters the name if such name already exists. Enjoy.
How to validate your model at save using the pre_save signal.
from http://groups.google.com/group/django-developers/browse_thread/thread/eb2f760e4c8d7911/482d8fd36fba4596?hl=en&lnk=gst&q=problem+with+Model.objects.create#482d8fd36fba4596
Adds drag-and-drop ordering of rows in the admin list view for [Grappelli](http://code.google.com/p/django-grappelli/). This is based on [Snippet #2057](http://djangosnippets.org/snippets/2057/) and fixes some bugs as well as switching to jQuery/jQuery UI provided by Grappelli. No additional files need to be installed.
The model needs to have a field holding the position and that field has to be made list_editable in the ModelAdmin. The changes of the ordering are applied after clicking 'Save'.
This is adapted from [rodnsi's version](http://djangosnippets.org/snippets/1818/) to work with Django 1.2 (as django.contrib.admin came with jQuery built in as of 1.2).
This snipped provides a subclass of the syndication Feed class that supports HTTP authentication (basic auth). Feeds that should support authentication can inherit from this class instead from the Feed class. It is basically the implementation of [Snippet #243](http://djangosnippets.org/snippets/243/) ported to the new-style syndication framework feeds (for which decorators don't work).
Usage:
class ArticleFeed(HTTPAuthFeed):
def items(self, obj):
return Article.objects.all().order_by('-created')[:10]
def item_title(self, item):
return item.title
def item_description(self, item):
return item.description
Generic function to merge model instances. Useful when you need to merge duplicate models together, e.g. for users.
Based on http://djangosnippets.org/snippets/382/, with several enhancements:
* *Type checking*: only Model subclasses can be used and testing that all instances are of same model class
* *Handles symmetrical many-to-may*: original snippet failed in that case
* *Filling up blank attrs of original when duplicate has it filled*
* *Prepared to use outside of command-line*
This is an expanded version of ["Resolve URLs to view name"](http://djangosnippets.org/snippets/1378/) without the monkey-patching.
Simply pass in a URL such as '/events/rsvp/some_conference/' and you'll get back the view name or function (if name isn't available) and the arguments to it, eg 'events_rsvp', [], {'event_slug':'some_conference'}.
Example (blatantly copied from previous snippet):
=== urlconf ====
urlpatterns = patterns(''
(r'/some/url', 'app.views.view'),
url(r'/some/other/(?P<url>\w+)', 'app.views.other.view', name='this_is_a_named_view'),
url(r'/yet/another/(?P<url>\w+)/(\d+)', 'app.views.yetanother.view', name='one_with_args'),
)
=== example usage in interpreter ===
>>> from some.where import resolve_to_name
>>> print resolve_to_name('/some/url')
('app.views.view',[],{})
>>> print resolve_to_name('/some/other/url')
('this_is_a_named_view',[],{'url':'url'})
>>> print resolve_to_name('/yet/another/url/5')
('one_with_args',[5],{'url':'url'})
From [fahhem.com](http://fahhem.com/) and [Recrec Labs](http://recreclabs.com/)
You're looking at the top-rated snippets currently on the site; if you'd like to contribute, sign up for an account and you'll be able to rate any snippet you see.