~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: 2009-02-26 22:38:49 UTC
  • Revision ID: sirver@kallisto.local-20090226223849-1563ij0uuw0lz0zu
First version of widelands online help

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()