From 12d37073df51b099d17d43d3df16bc98ac603efd Mon Sep 17 00:00:00 2001 From: etienne Date: Mon, 8 Dec 2008 22:45:38 +0000 Subject: Erase old polls --- poll_cleaning.py | 23 +++++++++++++++++++++++ polls/models.py | 25 +++++++++++++++++++++++++ settings.py | 2 ++ 3 files changed, 50 insertions(+) create mode 100755 poll_cleaning.py 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'), -- cgit v1.2.3