Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / migrations / 0030_populate_resource_data.py @ 1f3ad141

History | View | Annotate | Download (16 kB)

1
# encoding: utf-8
2

    
3
from south.v2 import DataMigration
4

    
5
from astakos.im.settings import SERVICES
6

    
7
import logging
8

    
9
logger = logging.getLogger(__name__)
10

    
11
class Migration(DataMigration):
12

    
13
    def forwards(self, orm):
14
        "Write your forwards methods here."
15

    
16
        try:
17
            default = orm.AstakosGroup.objects.get(name='default')
18
        except orm.AstakosGroup.DoesNotExist:
19
            return
20

    
21
        def create_policies(args):
22
            sn, dict = args
23
            url = dict.get('url')
24
            resources = dict.get('resources') or ()
25
            s, created = orm.Service.objects.get_or_create(name=sn,
26
                                                           defaults={'url': url})
27
            if not created and not s.url:
28
                s.url = url
29
            s.save()
30

    
31
            for r in resources:
32
                try:
33
                    rn = r.pop('name', '')
34
                    uplimit = r.pop('uplimit', None)
35
                    r, created = orm.Resource.objects.get_or_create(
36
                        service=s,
37
                        name=rn,
38
                        defaults=r)
39
                except Exception, e:
40
                    print "Cannot create resource ", rn
41
                    continue
42

    
43
                q, created = orm.AstakosGroupQuota.objects.get_or_create(
44
                    group=default,
45
                    resource=r,
46
                    uplimit=uplimit,
47
                    limit=0)
48
        map(create_policies, SERVICES.iteritems())
49

    
50
    def backwards(self, orm):
51
        try:
52
            default = orm.AstakosGroup.objects.get(name='default')
53
        except orm.AstakosGroup.DoesNotExist:
54
            return
55

    
56
        def destroy_policies(args):
57
            sn, dict = args
58
            url = dict.get('url')
59
            resources = dict.get('resources') or ()
60
            for r in resources:
61
                rn = r.get('name', '')
62
                try:
63
                    q = orm.AstakosGroupQuota.objects.get(
64
                        group=default,
65
                        resource__name=rn)
66
                    q.delete()
67
                    q = orm.Resource.objects.get(service__name=sn, name=rn)
68
                    q.delete()
69
                except Exception, e:
70
                    print "Cannot create resource ", rn
71
                    continue
72

    
73
        map(destroy_policies, SERVICES.iteritems())
74

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

    
238
    complete_apps = ['im']