Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

« Newer Snippets
Older Snippets »
6 total  XML / RSS feed 

LightTPD subdomain rewrite, Backpack/Basecamp style.

Rewrites to

This example show the values being passed to FCGI for use by Django.

$HTTP["host"] =~ "([^.]+)\.example\.com" {
  server.document-root = "/users/home/username/domains/"
  server.errorlog = "/users/home/username/var/log/lighttpd.example_com.error.log" 
  accesslog.filename = "/users/home/username/var/log/lighttpd.example_com.access.log" 

  fastcgi.server = (
    "/main.fcgi" => (
      "main" => (
        "socket" => "/users/home/username/tmp/django/example_com.socket"

  url.rewrite-once = ( "^(/.*)$" => "/main.fcgi/%1/$1" )
  server.error-handler-404 = "/main.fcgi" 

Implement automatic comment moderation queue in Django

Assuming you're using latest trunk (i.e., post-magic-removal merge), you need a few things:

1. A method on your content object which returns whether a comment should go to moderation or not.
2. A method on the FreeComment model which checks this and sets the 'approved' field appropriately.
3. A list filter for the FreeComment admin to show comments in need of moderation.

For the first bit, we borrow from my method for automatically closing comments (

def auto_moderate(self):
    return - datetime.timedelta(30) >= self.pub_date

This returns False if the object is less than 30 days old, True if it's 30 days old or older.

For the second bit, you'll need to hack on the FreeComment model; it's located in django/contrib/comments/ Add this:

def save(self):
    if self.get_content_object().auto_moderate:
        self.approved = False
    super(FreeComment, self).save()

And for the third bit, just add 'approved' to the list of properties the admin can filter on, and you'll be able to click and get a list of all comments which have not yet been approved.

In your templates, you can then manually check whether each comment is approved or not with {% if comment.approved %} or, if you're feeling adventurous, you can add a custom manager to the FreeComment model which only returns comments which have been approved.

For ubernostrum

Template (entries_archive_month.html):

<div id="content">
<h2>Archive: <a href="/blog/{{ month|date:"Y" }}/">{{ month|date:"Y" }}a> > {{ month|date:"F" }}</h2>
{% if object_list %}
    {% for entry in object_list %} <li><a href="{{ entry.get_absolute_url }}">{{ entry.title|title }}a></li> {% endfor %}
{% else %
} <p>No entries are available.p> {% endif %} </div>

from django.conf.urls.defaults import *

info_dict = {
    'app_label': 'blog',
    'module_name': 'entries',
    'date_field' : 'date_pub',

urlpatterns = patterns('',
    (r'^$', 'django.views.generic.date_based.archive_index', dict(info_dict,num_latest=5)),
    #(r'^$', ''),
    (r'^(?P\d{4})/(?P[a-z]{3})/(?P\d{2})/(?P[a-z0-9-]+)/$', 'django.views.generic.date_based.object_detail', dict(info_dict, slug_field='slug')),
    (r'^(?P\d{4})/(?P[a-z]{3})/$', 'django.views.generic.date_based.archive_month', info_dict),
    (r'^(?P\d{4})/$', 'django.views.generic.date_based.archive_year', info_dict),
    (r'^(?P\d{4})/(?P[a-z]{3})/(?P\d{2})/(?P[a-z0-9-]+)/comment/$', ''),
    (r'^category/(?P\w+)/$', ''),

For pkit

entries.get_list(pub_date__year=2005, pub_date__month=11)

lighttpd.conf update for use with Django

If you grabbed my sample lighttpd.conf before I made this post in the forum, then open up your lighttpd.conf, scroll down to the url.rewrite section, and change this:

"^(/[^media]/.*)$" => "/main.fcgi$1"

To this:

"^(/[^media].*)$" => "/main.fcgi$1"

Close comments after a set time in Django apps

If you're using Django's bundled comments application, you might want to have comments for objects closed after a set period of time; to do this, just add a method to the model of the object which will be getting the comments (e.g., the 'Entry' class if you have a weblog), like so (this assumes a DateTimeField called 'pub_date' which represents the object's date of publication):

def allow_comments(self):
    return - datetime.timedelta(30) >= self.pub_date

Change the timedelta value to however many days you'd like to leave comments open after publication, and now you'll be able to selectively display the comment form only when comments are open, by adding this to your template (assuming the object is being referenced by the name 'entry'):

{% if entry.allow_comments %}
... display the comment form here ...
{% endif %}
« Newer Snippets
Older Snippets »
6 total  XML / RSS feed