Mercurial > public > sg101
annotate wiki/tasks.py @ 629:f4c043cf55ac
Wiki integration. Requests don't always have sessions.
In particular this occurs when a request is made without a trailing slash.
The Common middleware redirects when this happens, and the middleware
process_request() processing stops before a session can get added.
So just set an attribute on the request object for each operation.
This seemed weird to me at first, but there are plenty of examples of this
in the Django code base already.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Tue, 13 Nov 2012 13:50:06 -0600 |
parents | a4300639c6e7 |
children | aeafbf3ecebf |
rev | line source |
---|---|
bgneal@627 | 1 """ |
bgneal@627 | 2 Celery tasks for the wiki app. |
bgneal@627 | 3 |
bgneal@627 | 4 """ |
bgneal@627 | 5 import datetime |
bgneal@627 | 6 import logging |
bgneal@627 | 7 import time |
bgneal@627 | 8 |
bgneal@627 | 9 from celery.task import task |
bgneal@627 | 10 from django.conf import settings |
bgneal@627 | 11 import redis |
bgneal@627 | 12 |
bgneal@627 | 13 from core.services import get_redis_connection |
bgneal@627 | 14 |
bgneal@627 | 15 |
bgneal@627 | 16 logger = logging.getLogger(__name__) |
bgneal@627 | 17 |
bgneal@627 | 18 |
bgneal@627 | 19 @task |
bgneal@627 | 20 def expire_cookies(): |
bgneal@627 | 21 """ |
bgneal@627 | 22 Periodically run this task to remove expired cookies from the Redis set |
bgneal@627 | 23 that is shared between this Django application & the MoinMoin wiki for |
bgneal@627 | 24 authentication. |
bgneal@627 | 25 |
bgneal@627 | 26 """ |
bgneal@627 | 27 now = datetime.datetime.utcnow() |
bgneal@627 | 28 cutoff = now - datetime.timedelta(seconds=settings.WIKI_COOKIE_AGE) |
bgneal@627 | 29 min_score = time.mktime(cutoff.utctimetuple()) |
bgneal@627 | 30 |
bgneal@627 | 31 conn = get_redis_connection() |
bgneal@627 | 32 |
bgneal@627 | 33 set_name = settings.WIKI_REDIS_SET |
bgneal@627 | 34 try: |
bgneal@627 | 35 count = conn.zcard(set_name) |
bgneal@627 | 36 except redis.RedisError: |
bgneal@627 | 37 logger.error("Error getting zcard") |
bgneal@627 | 38 return |
bgneal@627 | 39 |
bgneal@627 | 40 try: |
bgneal@627 | 41 removed = conn.zremrangebyscore(set_name, 0.0, min_score) |
bgneal@627 | 42 except redis.RedisError: |
bgneal@627 | 43 logger.error("Error removing by score") |
bgneal@627 | 44 return |
bgneal@627 | 45 |
bgneal@627 | 46 total = count - removed |
bgneal@627 | 47 logger.info("Expire wiki cookies: removed %d, total is now %d", |
bgneal@627 | 48 removed, total) |