1 from django.core.management.base import BaseCommand, CommandError
2 from forum.models import Node, NodeRevision
6 # Used to activate the latest revision connected to some node
7 def activate_latest_revision(node):
8 # We're adding a new try-except block just in case that function has been called incorrectly.
10 # The latest revision is the one that was added the last.
11 rev = node.revisions.all().order_by('-pk')[0]
12 node.active_revision_id = rev.id
17 logging.error("Incorrect attempt to activate the latest revision of a node \
18 that has no revisions at all has been made.")
21 # Used to create a new NodeRevision object according to the node content
22 def create_revision(node):
24 author_id = node.author_id,
27 revised_at = node.added_at,
29 summary = 'Initial revision',
30 tagnames = node.tagnames,
38 class Command(BaseCommand):
40 def handle(self,*args, **options):
41 print 'Running MaintainDb'
43 nodes = Node.objects.all()
46 if node.active_revision is None:
47 print "Node #%(node_id)d: NodeRevision doesn't exist" % dict(node_id=node.id)
49 # We currently don't have any active revision for this Node. Let's check if there are any revisions
50 # at all for it. If there are any we activate the last.
51 if node.revisions.all().count() > 0:
52 print " We have revisions for Node #%(node_id)d." % dict(node_id=node.id)
54 # If there are already some revisions connected to the current node, we activate the latest
55 activate_latest_revision(node)
57 print " We don't have revisions for Node #%(node_id)d. We're "\
58 "going to create a new one from the current node content."% dict(node_id=node.id)
60 # First of all we're going to create a new revision according to the current node data...
63 # ...and after that we're going to activate it
64 activate_latest_revision(node)
68 if node.node_type == "question":
69 # Reset the answer count cache
70 node.reset_answer_count_cache()
71 print "Question #%(question_id)d: Answer count cache has been reset" % { 'question_id' : node.id }
73 # Reset the accepted count cache
74 node.reset_accepted_count_cache()
75 print "Question #%(question_id)d: Resetting the accepted count cache" % { 'question_id' : node.id }