[damned-lies] Add cleaning of old archived actions
- From: Claude Paroz <claudep src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [damned-lies] Add cleaning of old archived actions
- Date: Mon, 24 Jan 2011 21:50:29 +0000 (UTC)
commit 2cc6bfbf95dc88f59cfc2f5d35cd9464e5321d2f
Author: Claude Paroz <claude 2xlibre net>
Date: Mon Jan 24 22:49:29 2011 +0100
Add cleaning of old archived actions
stats/management/commands/run-maintenance.py | 3 +-
vertimus/models.py | 44 +++++++++++++-------------
2 files changed, 24 insertions(+), 23 deletions(-)
---
diff --git a/stats/management/commands/run-maintenance.py b/stats/management/commands/run-maintenance.py
index 71ed2f3..9242b65 100644
--- a/stats/management/commands/run-maintenance.py
+++ b/stats/management/commands/run-maintenance.py
@@ -2,6 +2,7 @@ from django.core.management.base import BaseCommand
from people.models import Person
from teams.models import Role
+from vertimus.models import ActionDbArchived
class Command(BaseCommand):
help = "Run maintenance tasks"
@@ -9,4 +10,4 @@ class Command(BaseCommand):
def handle(self, *args, **options):
Person.clean_unactivated_accounts()
Role.inactivate_unused_roles()
-
+ ActionDbArchived.clean_old_actions(365)
diff --git a/vertimus/models.py b/vertimus/models.py
index 2956b00..baa4110 100644
--- a/vertimus/models.py
+++ b/vertimus/models.py
@@ -19,14 +19,15 @@
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import os, sys
+from datetime import datetime, timedelta
-from django.db import models
-from django.utils.translation import get_language, activate, ugettext, ugettext_lazy as _
-from django.core import mail, urlresolvers
-from django.contrib.sites.models import Site
from django.conf import settings
+from django.contrib.sites.models import Site
+from django.core import mail, urlresolvers
+from django.db import models
+from django.db.models import Max
from django.db.models.signals import post_save, pre_delete
-from django.db import connection
+from django.utils.translation import get_language, activate, ugettext, ugettext_lazy as _
from stats.models import Branch, Domain, Statistics
from stats.signals import pot_has_changed
@@ -138,24 +139,12 @@ class StateAbstract(object):
def get_action_sequence_from_level(self, level):
"""Get the sequence corresponding to the requested level.
The first level is 1."""
- if level <= 0:
- raise Exception("Level must be greater than 0")
-
- # Raw SQL in waiting for Django 1.1.
- query = """
- SELECT sequence
- FROM action_archived
- WHERE state_db_id = %s
- GROUP BY sequence
- ORDER BY sequence DESC
- LIMIT 1 OFFSET %s"""
- cursor = connection.cursor()
- cursor.execute(query, (self._state_db.id, level - 1))
- try:
- sequence = cursor.fetchone()[0]
- except:
- sequence = None
+ assert level > 0, "Level must be greater than 0"
+ query = ActionDbArchived.objects.filter(state_db=self._state_db).values('sequence').distinct().order_by('-sequence')[level-1:level]
+ sequence = None
+ if len(query) > 0:
+ sequence = query[0]['sequence']
return sequence
def save(self):
@@ -423,6 +412,17 @@ class ActionDbArchived(models.Model):
def get_action_history(cls, sequence):
return action_db_get_action_history(cls, state_db=None, sequence=sequence)
+ @classmethod
+ def clean_old_actions(cls, days):
+ """ Delete old archived actions after some (now-days) time """
+ # In each sequence, test date of the latest action, to delete whole sequences instead of individual actions
+ for action in ActionDbArchived.objects.values('sequence'
+ ).annotate(max_created=Max('created')
+ ).filter(max_created__lt=datetime.now()-timedelta(days=days)):
+ # Call each action delete() so as file is also deleted
+ for act in ActionDbArchived.objects.filter(sequence=action['sequence']):
+ action.delete()
+
class ActionAbstract(object):
"""Abstract class"""
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]