~widelands-dev/widelands-website/trunk

« back to all changes in this revision

Viewing changes to pybb/migrations/002_post_count.py

  • Committer: Holger Rapp
  • Date: 2010-01-01 21:35:23 UTC
  • mto: (173.3.2 widelands)
  • mto: This revision was merged to the branch mainline in revision 176.
  • Revision ID: rapp@mrt.uka.de-20100101213523-53rcapbemm69ep6u
Made the site compatible to django 1.1 and all the various packages

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from django.db import connection
 
2
 
 
3
from pybb.models import Forum, Topic
 
4
 
 
5
DESCRIPTION = 'Add and populate post_count fields to Forum and Topic models'
 
6
 
 
7
def migrate():
 
8
    cur = connection.cursor()
 
9
 
 
10
    print 'Altering forum table'
 
11
    cur.execute("ALTER TABLE pybb_forum ADD post_count INT NOT NULL DEFAULT 0")
 
12
 
 
13
    print 'Altering topic table'
 
14
    cur.execute("ALTER TABLE pybb_topic ADD post_count INT NOT NULL DEFAULT 0")
 
15
 
 
16
    print 'Populating post_count of topics'
 
17
    for topic in Topic.objects.all():
 
18
        topic.post_count = topic.posts.all().count()
 
19
        topic.save()
 
20
 
 
21
    print 'Populating post_count of forums'
 
22
    for forum in Forum.objects.all():
 
23
        forum.post_count = sum(x.post_count for x in forum.topics.all())
 
24
        forum.save()