summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoretienne2008-12-08 22:45:38 +0000
committeretienne2008-12-08 22:45:38 +0000
commit12d37073df51b099d17d43d3df16bc98ac603efd (patch)
tree34f3894ed6f24586e219af5273eff2538fa9a401
parenta0e093c353140308fc2fac24f1ec179e304ab3ba (diff)
Erase old polls
-rwxr-xr-xpoll_cleaning.py23
-rw-r--r--polls/models.py25
-rw-r--r--settings.py2
3 files changed, 50 insertions, 0 deletions
diff --git a/poll_cleaning.py b/poll_cleaning.py
new file mode 100755
index 0000000..993a5b4
--- /dev/null
+++ b/poll_cleaning.py
@@ -0,0 +1,23 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+'''
+Clean the old polls
+'''
+
+import os
+import sys
+
+# django settings path
+os.environ['DJANGO_SETTINGS_MODULE'] = 'papillon.settings'
+
+# add the parent path to sys.path
+curdir = os.path.abspath(os.curdir)
+sep = os.path.sep
+sys.path.append(sep.join(curdir.split(sep)[:-1]))
+
+
+from papillon.polls.models import Poll
+
+for poll in Poll.objects.all():
+ poll.checkForErasement()
diff --git a/polls/models.py b/polls/models.py
index dabb27e..4ca5036 100644
--- a/polls/models.py
+++ b/polls/models.py
@@ -21,9 +21,13 @@
Models management
'''
+import datetime
+
from django.db import models
from django.utils.translation import gettext_lazy as _
+from papillon.settings import DAYS_TO_LIVE
+
class Category(models.Model):
name = models.CharField(max_length=100)
description = models.TextField()
@@ -56,6 +60,27 @@ class Poll(models.Model):
def getTypeLabel(self):
idx = [type[0] for type in self.TYPE].index(self.type)
return Poll.TYPE[idx][1]
+
+ def checkForErasement(self):
+ '''Check if the poll has to be deleted'''
+ if not DAYS_TO_LIVE:
+ return
+ now = datetime.datetime.now()
+ dtl = datetime.timedelta(days=DAYS_TO_LIVE)
+ if self.modification_date + dtl > now:
+ return
+ voters = Voter.objects.filter(poll=self)
+ for voter in voters:
+ if voter.modification_date + dtl > now:
+ return
+ for voter in voters:
+ voter.user.delete()
+ voter.delete()
+ comments = Comment.objects.filter(poll=self)
+ for comment in comments:
+ comment.delete()
+ self.delete()
+
class Admin:
pass
class Meta:
diff --git a/settings.py b/settings.py
index a97d0b5..52b37ec 100644
--- a/settings.py
+++ b/settings.py
@@ -7,6 +7,8 @@ DEBUG = True
TEMPLATE_DEBUG = DEBUG
BASE_SITE = 'http://localhost:8000/papillon'
+# time to live in days
+DAYS_TO_LIVE = 30
ADMINS = (
# ('Your Name', 'your_email@domain.com'),