~ubuntu-branches/ubuntu/raring/schooltool.intervention/raring

« back to all changes in this revision

Viewing changes to src/schooltool/intervention/generations/evolve7.py

  • Committer: Gediminas Paulauskas
  • Date: 2011-09-19 16:56:45 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: menesis@pov.lt-20110919165645-718diuud5tc4mjsx
Tags: 0.5.0-0ubuntu1
* New upstream release.
* debian/rules: move gradebook to Suggests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# SchoolTool - common information systems platform for school administration
 
3
# Copyright (c) 2008 Shuttleworth Foundation
 
4
#
 
5
# This program is free software; you can redistribute it and/or modify
 
6
# it under the terms of the GNU General Public License as published by
 
7
# the Free Software Foundation; either version 2 of the License, or
 
8
# (at your option) any later version.
 
9
#
 
10
# This program is distributed in the hope that it will be useful,
 
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
# GNU General Public License for more details.
 
14
#
 
15
# You should have received a copy of the GNU General Public License
 
16
# along with this program; if not, write to the Free Software
 
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
#
 
19
"""
 
20
Evolve database to generation 7.
 
21
 
 
22
Move message and goal id lists to new contact relationsship properties.
 
23
"""
 
24
 
 
25
import pytz
 
26
 
 
27
from zope.app.generations.utility import findObjectsProviding
 
28
from zope.app.generations.utility import getRootFolder
 
29
from zope.component.hooks import getSite, setSite
 
30
 
 
31
from schooltool.app.interfaces import ISchoolToolApplication
 
32
from schooltool.contact.interfaces import IContact, IContactable
 
33
 
 
34
 
 
35
def getContacts(ids, persons):
 
36
    for id in ids:
 
37
        if len(id) > 2 and id[-2] == ':':
 
38
            index = int(id[-1])
 
39
            id = id[:-2]
 
40
        else:
 
41
            index = 0
 
42
        if id not in persons:
 
43
            continue
 
44
        person = persons[id]
 
45
        if index:
 
46
            contacts = [contact for contact in IContactable(person).contacts]
 
47
            if index <= len(contacts):
 
48
                yield contacts[index - 1]
 
49
        else:
 
50
            yield IContact(person)
 
51
 
 
52
 
 
53
def fixMessage(message, persons):
 
54
    created = message.created  # activate __dict__
 
55
    if 'sender' in message.__dict__:
 
56
        message.created = created    # activate persistence
 
57
        sender_id = message.__dict__['sender']
 
58
        del message.__dict__['sender']
 
59
        for contact in getContacts([sender_id], persons):
 
60
            if contact not in message.sender:
 
61
                message.sender.add(contact)
 
62
 
 
63
    if 'recipients' in message.__dict__:
 
64
        message.created = created    # activate persistence
 
65
        recipient_ids = message.__dict__['recipients']
 
66
        del message.__dict__['recipients']
 
67
        for contact in getContacts(recipient_ids, persons):
 
68
            if contact not in message.recipients:
 
69
                message.recipients.add(contact)
 
70
 
 
71
 
 
72
def fixGoal(goal, persons):
 
73
    created = goal.created  # activate __dict__
 
74
    if 'creator' in goal.__dict__:
 
75
        goal.created = created    # activate persistence
 
76
        creator_id = goal.__dict__['creator']
 
77
        del goal.__dict__['creator']
 
78
        for contact in getContacts([creator_id], persons):
 
79
            if contact not in goal.creator:
 
80
                goal.creator.add(contact)
 
81
 
 
82
    if '_persons_responsible' in goal.__dict__:
 
83
        goal.created = created    # activate persistence
 
84
        responsible_ids = goal.__dict__['_persons_responsible']
 
85
        del goal.__dict__['_persons_responsible']
 
86
        for contact in getContacts(responsible_ids, persons):
 
87
            if contact not in goal._persons_responsible:
 
88
                goal._persons_responsible.add(contact)
 
89
 
 
90
    if 'at_one_time_responsible' in goal.__dict__:
 
91
        at_one_time_ids = goal.__dict__['at_one_time_responsible']
 
92
        del goal.__dict__['at_one_time_responsible']
 
93
        for contact in getContacts(at_one_time_ids, persons):
 
94
            goal.at_one_time_responsible.add(contact)
 
95
 
 
96
 
 
97
def evolve(context):
 
98
    root = getRootFolder(context)
 
99
    old_site = getSite()
 
100
    apps = findObjectsProviding(root, ISchoolToolApplication)
 
101
 
 
102
    for app in apps:
 
103
        setSite(app)
 
104
        interventionRoot = app.get(u'schooltool.interventions', {})
 
105
        persons = app.get('persons', {})
 
106
        for schoolYear in interventionRoot.values():
 
107
            for student in schoolYear.values():
 
108
                for message in student['messages'].values():
 
109
                    fixMessage(message, persons)
 
110
                for goal in student['goals'].values():
 
111
                    fixGoal(goal, persons)
 
112