This is a rewrite of [snippet #1006](http://www.djangosnippets.org/snippets/1006/) to use the moderation features available in Django's comments framework. This is more customizable than the signals approach and works well if other moderation features are being used. If you want to make comments that are flagged as spam become hidden instead of deleted, change the allow() method to moderate(). [See the blog post here](http://sciyoshi.com/blog/2009/jul/17/prevent-django-newcomments-spam-akismet-reloaded/)
On a busy site it can be nice to have a summary of admin activity. Running this command (I call it "adminlog") generates output like this:
2009-07-10 18:06:19: pbx changed flat page: "/yay/ -- Let's All Say Yay"
By default it shows the last five actions; pass it a numerical arg to show more or fewer.
Run this as a cron job and you can follow a site's admin-side activity without even logging in!
A one-liner that I use all the time: Set `upload_to` to something based on the slug and the filename's extension.
Just add this function to the top of your models and use it like this:
image = models.FileField(upload_to=slug_filename('people'))
and the `upload_to` path will end up like this for eg `myfile.jpg`:
people/this-is-the-slug.jpg
Enjoy!
Simple Python snippet to detect if any word in a list of words is inside your string. Use for profanity checking (my use case), auto tag detection, scoring, etc.
This will return an empty list if the word is not in the list. Assumes everything in words_to_find is lower case. Can probably be done cleaner with regular expressions but this method is extremely readable for those that prefer none regex solutions.
The widget for FileField and ImageField has a problem: it doesn't supports clear its value and it doesn't delete the old file when you replace it for a new one.
This is a solution for this. It is just for Admin, but you can make changes to be compatible with common forms.
The jQuery code will put an **<input type="checkbox">** tag next to every **<input type="file">** and user can check it to clear the field value.
When a user just replace the current file for a new one, the old file will be deleted.
This is a very simple way of getting authenticated RSS feeds in django, by slightly changing the django.contrib.syndication.views
1) copy django/contrib/syndication/views.py into mysite/feeds/views.py
2) replace the contents of that file with the snippet
3) any feeds which you require login just add them to the auth_required list. In this case I require login for /feeds/mystuff/ so I make auth_required = ['mystuff']
My directory structure is like this:
mysite/feeds/
views.py
feedmodels.py
feedmodels.py is just where you make your feed models (see http://docs.djangoproject.com/en/dev/ref/contrib/syndication/ where they give an example of "LatestEntries")
[urls.py] - this is what I add in urls.py
from mysite.feeds.feedmodels import Latest,MyPersonalStuff
feeds = {
'latest':Latest,
'mystuff':MyPersonalStuff,
}
(r'^feeds/(?P<url>.*)/$', 'mysite.feeds.views.feed', {'feed_dict': feeds}),
###########################################################################
Ever wished you could have pretty SQL-like output for a python object (e.g., a list of dicts) while you're debugging your code? This function will do just that. Simply pass it an object that is an iterable of dictionaries and it returns it in an easy-to-read table, similar to the output of commandline SQL.
Example:
from tablelize import tableize
from django.contrib.auth.models import User
print(tableize(User.objects.values('email', 'first_name', 'last_name')))
+------------+-----------+-------------------+
| first_name | last_name | email |
+------------+-----------+-------------------+
| Test | User | [email protected] |
| Another | User | [email protected] |
+------------+-----------+-------------------+
Custom serialization, the poor try to make something like [django full serializers](http://code.google.com/p/wadofstuff/wiki/DjangoFullSerializers)
Usage:
you need two files, goodjson.py and goodpython.py, for example, in the root of application named "core". Then, add two lines into your settings.py:
SERIALIZATION_MODULES = {'goodjson' : 'core.goodjson',
'goodpython': 'core.goodpython'}
This thing does only serialization, not deserialization. You were warned.
DateTimeWidget using [JSCal2](http://www.dynarch.com/projects/calendar/)
Duplicate of [this snippet](http://www.djangosnippets.org/snippets/391/), but for latest 1.5 version of DHTML Calendar.
Also here is **fixed problem of previous widget** linked to *form.changed_data* and *EntryLog.message*. This is fixed by adding own, little modified *_has_changed()* method
This tag is designed to facilitate pagination in the case where both the page number and other parameters (eg. search criteria) are passed via GET.
It takes one argument - a dictionary of GET variables to be added to the current url
Example usage:
{% for page_num in results.paginator.page_range %}
<a href="{% append_to_get p=page_num %}">{{ page_num }}</a>
{% endfor %}
Note that the passed arguments are evaluated within the template context.
If you have a model with foreign key to User, you can use this manager to show (i.e. in admin interface) only objects, that are related to currently logged-in user. Superuser sees all objects, not only his.
Requires: [ThreadlocalsMiddleware](http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser)
This is another foreign key to User model. User is automatically associated before save.
Requires: [ThreadlocalsMiddleware](http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser)
Inspired by: [snippet 509](http://www.djangosnippets.org/snippets/509/)
Simple function that tests whether a given IP address is in a list of IP addresses or subnets.
Requires `ipaddr`. Comes with Python 2.7 or 3.1, [downloadable here](http://code.google.com/p/ipaddr-py/) for earlier versions.
More info on `ipaddr` [in Python 3.1 docs](http://docs.python.org/dev/py3k/library/ipaddr.html).