Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / migrations / 0016_populate_group_data.py @ 304acb60

History | View | Annotate | Download (16.1 kB)

1
# encoding: utf-8
2
import datetime
3
from south.db import db
4
from south.v2 import DataMigration
5
from django.db import models
6
from django.db.models import Count
7

    
8
class Migration(DataMigration):
9
    def forwards(self, orm):
10
        
11
        def _create_groupkind(name):
12
            try:
13
                orm.GroupKind(name=name).save()
14
            except:
15
                pass
16
                                        
17
        t = ('default', 'course', 'project', 'laboratory', 'organization')
18
        map(_create_groupkind, t)
19
        
20
        default = orm.GroupKind.objects.get(name='default')
21
        
22
        groups = {}
23
        def _create_astakogroup(name):
24
            try:
25
                groups[name] = orm.AstakosGroup.objects.get(name=name)
26
            except orm.AstakosGroup.DoesNotExist:
27
                try:
28
                    g = orm['auth.Group'].objects.get(name=name)
29
                    groups[name] = extended = orm.AstakosGroup(group_ptr_id=g.pk)
30
                    extended.__dict__.update(g.__dict__)
31
                    extended.kind = default
32
                    extended.approval_date = datetime.datetime.now()
33
                    extended.issue_date = datetime.datetime.now()
34
                    extended.moderation_enabled = False
35
                    extended.save()
36
                    map(lambda u:orm.Membership(    group=extended,
37
                                                    person=orm.AstakosUser.objects.get(id=u.id),
38
                                                    date_joined=datetime.datetime.now()
39
                                                ).save(),
40
                        g.user_set.all())
41
                except orm['auth.Group'].DoesNotExist:
42
                    groups[name] = orm.AstakosGroup(    name=name,
43
                                                        kind=default,
44
                                                        approval_date=datetime.datetime.now(),
45
                                                        issue_date=datetime.datetime.now(),
46
                                                        moderation_enabled=False
47
                                                    )
48
                    groups[name].save()
49
        
50
        # catch integrate 
51
        t = ('default', 'shibboleth', 'helpdesk', 'faculty', 'ugrad', 'grad', 'researcher', 'associate')
52
        map(_create_astakogroup, t)
53
        
54
        orphans = orm.AstakosUser.objects.annotate(num_groups=Count('astakos_groups')).filter(num_groups = 0)
55
        map ( lambda u: orm.Membership(group=groups['default'], person=u, date_joined=datetime.datetime.now()).save(), orphans )
56
        
57
    def backwards(self, orm):
58
        def _delete_groupkind(name):
59
            try:
60
                orm.GroupKind.objects.get(name=name).delete()
61
            except orm.GroupKind.DoesNotExist:
62
                pass
63
        
64
        def _delete_astakosgroup(name):
65
            try:
66
                orm.AstakosGroup.objects.get(name=name).delete()
67
            except orm.AstakosGroup.DoesNotExist:
68
                pass
69
        
70
        t = ('default', 'shibboleth', 'helpdesk', 'faculty', 'ugrad', 'grad', 'researcher', 'associate')
71
        map(_delete_astakosgroup, t)
72
        
73
        t = ('default', 'course', 'project', 'laboratory', 'organization')
74
        map(_delete_groupkind, t)
75

    
76
    models = {
77
        'auth.group': {
78
            'Meta': {'object_name': 'Group'},
79
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
80
            'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
81
            'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
82
        },
83
        'auth.permission': {
84
            'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
85
            'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
86
            'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
87
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
88
            'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
89
        },
90
        'auth.user': {
91
            'Meta': {'object_name': 'User'},
92
            'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
93
            'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
94
            'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
95
            'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
96
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
97
            'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
98
            'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
99
            'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
100
            'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
101
            'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
102
            'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
103
            'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
104
            'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'})
105
        },
106
        'contenttypes.contenttype': {
107
            'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
108
            'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
109
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
110
            'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
111
            'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
112
        },
113
        'im.additionalmail': {
114
            'Meta': {'object_name': 'AdditionalMail'},
115
            'email': ('django.db.models.fields.EmailField', [], {'max_length': '75'}),
116
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
117
            'owner': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['im.AstakosUser']"})
118
        },
119
        'im.approvalterms': {
120
            'Meta': {'object_name': 'ApprovalTerms'},
121
            'date': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime(2012, 8, 8, 12, 40, 8, 181485)', 'db_index': 'True'}),
122
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
123
            'location': ('django.db.models.fields.CharField', [], {'max_length': '255'})
124
        },
125
        'im.astakosgroup': {
126
            'Meta': {'object_name': 'AstakosGroup', '_ormbases': ['auth.Group']},
127
            'approval_date': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}),
128
            'creation_date': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime(2012, 8, 8, 12, 40, 8, 175548)'}),
129
            'desc': ('django.db.models.fields.TextField', [], {'null': 'True'}),
130
            'estimated_participants': ('django.db.models.fields.PositiveIntegerField', [], {'null': 'True'}),
131
            'expiration_date': ('django.db.models.fields.DateTimeField', [], {'null': 'True'}),
132
            'group_ptr': ('django.db.models.fields.related.OneToOneField', [], {'to': "orm['auth.Group']", 'unique': 'True', 'primary_key': 'True'}),
133
            'issue_date': ('django.db.models.fields.DateTimeField', [], {'null': 'True'}),
134
            'kind': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['im.GroupKind']"}),
135
            'moderation_enabled': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
136
            'policy': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'to': "orm['im.Resource']", 'null': 'True', 'through': "orm['im.AstakosGroupQuota']", 'blank': 'True'})
137
        },
138
        'im.astakosgroupquota': {
139
            'Meta': {'unique_together': "(('resource', 'group'),)", 'object_name': 'AstakosGroupQuota'},
140
            'group': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['im.AstakosGroup']", 'blank': 'True'}),
141
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
142
            'limit': ('django.db.models.fields.PositiveIntegerField', [], {}),
143
            'resource': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['im.Resource']"})
144
        },
145
        'im.astakosuser': {
146
            'Meta': {'unique_together': "(('provider', 'third_party_identifier'),)", 'object_name': 'AstakosUser', '_ormbases': ['auth.User']},
147
            'activation_sent': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}),
148
            'affiliation': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}),
149
            'astakos_groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['im.AstakosGroup']", 'symmetrical': 'False', 'through': "orm['im.Membership']", 'blank': 'True'}),
150
            'auth_token': ('django.db.models.fields.CharField', [], {'max_length': '32', 'null': 'True', 'blank': 'True'}),
151
            'auth_token_created': ('django.db.models.fields.DateTimeField', [], {'null': 'True'}),
152
            'auth_token_expires': ('django.db.models.fields.DateTimeField', [], {'null': 'True'}),
153
            'date_signed_terms': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}),
154
            'email_verified': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
155
            'has_credits': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
156
            'has_signed_terms': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
157
            'invitations': ('django.db.models.fields.IntegerField', [], {'default': '100'}),
158
            'is_verified': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
159
            'level': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
160
            'owner': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'related_name': "'owner'", 'null': 'True', 'to': "orm['im.AstakosGroup']"}),
161
            'policy': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['im.Resource']", 'null': 'True', 'through': "orm['im.AstakosUserQuota']", 'symmetrical': 'False'}),
162
            'provider': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}),
163
            'third_party_identifier': ('django.db.models.fields.CharField', [], {'max_length': '255', 'null': 'True', 'blank': 'True'}),
164
            'updated': ('django.db.models.fields.DateTimeField', [], {}),
165
            'user_ptr': ('django.db.models.fields.related.OneToOneField', [], {'to': "orm['auth.User']", 'unique': 'True', 'primary_key': 'True'})
166
        },
167
        'im.astakosuserquota': {
168
            'Meta': {'unique_together': "(('resource', 'user'),)", 'object_name': 'AstakosUserQuota'},
169
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
170
            'limit': ('django.db.models.fields.PositiveIntegerField', [], {}),
171
            'resource': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['im.Resource']"}),
172
            'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['im.AstakosUser']"})
173
        },
174
        'im.emailchange': {
175
            'Meta': {'object_name': 'EmailChange'},
176
            'activation_key': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '40', 'db_index': 'True'}),
177
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
178
            'new_email_address': ('django.db.models.fields.EmailField', [], {'max_length': '75'}),
179
            'requested_at': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime(2012, 8, 8, 12, 40, 8, 183025)'}),
180
            'user': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'emailchange_user'", 'unique': 'True', 'to': "orm['im.AstakosUser']"})
181
        },
182
        'im.groupkind': {
183
            'Meta': {'object_name': 'GroupKind'},
184
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
185
            'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255', 'db_index': 'True'})
186
        },
187
        'im.invitation': {
188
            'Meta': {'object_name': 'Invitation'},
189
            'code': ('django.db.models.fields.BigIntegerField', [], {'db_index': 'True'}),
190
            'consumed': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}),
191
            'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
192
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
193
            'inviter': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'invitations_sent'", 'null': 'True', 'to': "orm['im.AstakosUser']"}),
194
            'is_consumed': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
195
            'realname': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
196
            'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255'})
197
        },
198
        'im.membership': {
199
            'Meta': {'unique_together': "(('person', 'group'),)", 'object_name': 'Membership'},
200
            'date_joined': ('django.db.models.fields.DateField', [], {'db_index': 'True', 'null': 'True', 'blank': 'True'}),
201
            'date_requested': ('django.db.models.fields.DateField', [], {'default': 'datetime.datetime(2012, 8, 8, 12, 40, 8, 179349)', 'blank': 'True'}),
202
            'group': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['im.AstakosGroup']"}),
203
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
204
            'person': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['im.AstakosUser']"})
205
        },
206
        'im.resource': {
207
            'Meta': {'object_name': 'Resource'},
208
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
209
            'meta': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['im.ResourceMetadata']", 'symmetrical': 'False'}),
210
            'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255', 'db_index': 'True'}),
211
            'service': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['im.Service']"})
212
        },
213
        'im.resourcemetadata': {
214
            'Meta': {'object_name': 'ResourceMetadata'},
215
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
216
            'key': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255', 'db_index': 'True'}),
217
            'value': ('django.db.models.fields.CharField', [], {'max_length': '255'})
218
        },
219
        'im.service': {
220
            'Meta': {'object_name': 'Service'},
221
            'auth_token': ('django.db.models.fields.CharField', [], {'max_length': '32', 'null': 'True', 'blank': 'True'}),
222
            'auth_token_created': ('django.db.models.fields.DateTimeField', [], {'null': 'True'}),
223
            'auth_token_expires': ('django.db.models.fields.DateTimeField', [], {'null': 'True'}),
224
            'icon': ('django.db.models.fields.FilePathField', [], {'max_length': '100', 'blank': 'True'}),
225
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
226
            'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255', 'db_index': 'True'}),
227
            'url': ('django.db.models.fields.FilePathField', [], {'max_length': '100'})
228
        }
229
    }
230

    
231
    complete_apps = ['im']