Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / migrations / 0017_populate_resource_data.py @ 304acb60

History | View | Annotate | Download (15.2 kB)

1
# encoding: utf-8
2
import datetime
3
import sys
4

    
5
from south.db import db
6
from south.v2 import DataMigration
7
from django.db import models
8
from django.db.utils import IntegrityError
9
from collections import defaultdict
10
from django.db.models import Q
11

    
12
d = {
13
        'cyclades' : { 'vm' : 2 },
14
        'pithos+': {'diskspace' : 10  } 
15
    }
16
class Migration(DataMigration):
17

    
18
    def forwards(self, orm):
19
        "Write your forwards methods here."
20
        
21
        try:
22
            default = orm.AstakosGroup.objects.get(name='default')
23
        except orm.AstakosGroup.DoesNotExist:
24
            return
25
        
26
        def create_policies(sn, policy):
27
            s, created = orm.Service.objects.get_or_create(name = sn)
28
            if created:
29
                s.save()
30
            
31
            for rn, l in policy.iteritems():
32
                r, created = orm.Resource.objects.get_or_create (    
33
                                                                    service = s,
34
                                                                    name = rn
35
                                                                )
36
                if created:
37
                    r.save()
38
                
39
                q, created = orm.AstakosGroupQuota.objects.get_or_create(   
40
                                                                            group = default,
41
                                                                            resource = r,
42
                                                                            limit = l   
43
                                                                        )
44
                if created:
45
                    q.save()
46
                
47
        map(lambda i: create_policies(i[0], i[1]), d.iteritems())
48
            
49
    def backwards(self, orm):
50
        try:
51
            default = orm.AstakosGroup.objects.get(name='default')
52
        except orm.AstakosGroup.DoesNotExist:
53
            return
54
        
55
        def destroy_policies(sn, policy):
56
            for rn, l in policy.iteritems():
57
                try:
58
                    q = orm.AstakosGroupQuota.objects.get(group=default, resource__name=rn)
59
                    if q.limit == l:
60
                        q.delete()
61
                except orm.AstakosGroupQuota.DoesNotExist:
62
                    return
63
                
64
        map(lambda i: destroy_policies(i[0], i[1]), d.iteritems())
65
    
66
    models = {
67
        'auth.group': {
68
            'Meta': {'object_name': 'Group'},
69
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
70
            'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
71
            'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
72
        },
73
        'auth.permission': {
74
            'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
75
            'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
76
            'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
77
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
78
            'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
79
        },
80
        'auth.user': {
81
            'Meta': {'object_name': 'User'},
82
            'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
83
            'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
84
            'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
85
            'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
86
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
87
            'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
88
            'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
89
            'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
90
            'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
91
            'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
92
            'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
93
            'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
94
            'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'})
95
        },
96
        'contenttypes.contenttype': {
97
            'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
98
            'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
99
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
100
            'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
101
            'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
102
        },
103
        'im.additionalmail': {
104
            'Meta': {'object_name': 'AdditionalMail'},
105
            'email': ('django.db.models.fields.EmailField', [], {'max_length': '75'}),
106
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
107
            'owner': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['im.AstakosUser']"})
108
        },
109
        'im.approvalterms': {
110
            'Meta': {'object_name': 'ApprovalTerms'},
111
            'date': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime(2012, 8, 9, 11, 14, 9, 289091)', 'db_index': 'True'}),
112
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
113
            'location': ('django.db.models.fields.CharField', [], {'max_length': '255'})
114
        },
115
        'im.astakosgroup': {
116
            'Meta': {'object_name': 'AstakosGroup', '_ormbases': ['auth.Group']},
117
            'approval_date': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}),
118
            'creation_date': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime(2012, 8, 9, 11, 14, 9, 283154)'}),
119
            'desc': ('django.db.models.fields.TextField', [], {'null': 'True'}),
120
            'estimated_participants': ('django.db.models.fields.PositiveIntegerField', [], {'null': 'True'}),
121
            'expiration_date': ('django.db.models.fields.DateTimeField', [], {'null': 'True'}),
122
            'group_ptr': ('django.db.models.fields.related.OneToOneField', [], {'to': "orm['auth.Group']", 'unique': 'True', 'primary_key': 'True'}),
123
            'issue_date': ('django.db.models.fields.DateTimeField', [], {'null': 'True'}),
124
            'kind': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['im.GroupKind']"}),
125
            'moderation_enabled': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
126
            'policy': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'to': "orm['im.Resource']", 'null': 'True', 'through': "orm['im.AstakosGroupQuota']", 'blank': 'True'})
127
        },
128
        'im.astakosgroupquota': {
129
            'Meta': {'unique_together': "(('resource', 'group'),)", 'object_name': 'AstakosGroupQuota'},
130
            'group': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['im.AstakosGroup']", 'blank': 'True'}),
131
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
132
            'limit': ('django.db.models.fields.PositiveIntegerField', [], {}),
133
            'resource': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['im.Resource']"})
134
        },
135
        'im.astakosuser': {
136
            'Meta': {'unique_together': "(('provider', 'third_party_identifier'),)", 'object_name': 'AstakosUser', '_ormbases': ['auth.User']},
137
            'activation_sent': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}),
138
            'affiliation': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}),
139
            'astakos_groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['im.AstakosGroup']", 'symmetrical': 'False', 'through': "orm['im.Membership']", 'blank': 'True'}),
140
            'auth_token': ('django.db.models.fields.CharField', [], {'max_length': '32', 'null': 'True', 'blank': 'True'}),
141
            'auth_token_created': ('django.db.models.fields.DateTimeField', [], {'null': 'True'}),
142
            'auth_token_expires': ('django.db.models.fields.DateTimeField', [], {'null': 'True'}),
143
            'date_signed_terms': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}),
144
            'email_verified': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
145
            'has_credits': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
146
            'has_signed_terms': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
147
            'invitations': ('django.db.models.fields.IntegerField', [], {'default': '100'}),
148
            'is_verified': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
149
            'level': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
150
            'owner': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'related_name': "'owner'", 'null': 'True', 'to': "orm['im.AstakosGroup']"}),
151
            'policy': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['im.Resource']", 'null': 'True', 'through': "orm['im.AstakosUserQuota']", 'symmetrical': 'False'}),
152
            'provider': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}),
153
            'third_party_identifier': ('django.db.models.fields.CharField', [], {'max_length': '255', 'null': 'True', 'blank': 'True'}),
154
            'updated': ('django.db.models.fields.DateTimeField', [], {}),
155
            'user_ptr': ('django.db.models.fields.related.OneToOneField', [], {'to': "orm['auth.User']", 'unique': 'True', 'primary_key': 'True'})
156
        },
157
        'im.astakosuserquota': {
158
            'Meta': {'unique_together': "(('resource', 'user'),)", 'object_name': 'AstakosUserQuota'},
159
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
160
            'limit': ('django.db.models.fields.PositiveIntegerField', [], {}),
161
            'resource': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['im.Resource']"}),
162
            'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['im.AstakosUser']"})
163
        },
164
        'im.emailchange': {
165
            'Meta': {'object_name': 'EmailChange'},
166
            'activation_key': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '40', 'db_index': 'True'}),
167
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
168
            'new_email_address': ('django.db.models.fields.EmailField', [], {'max_length': '75'}),
169
            'requested_at': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime(2012, 8, 9, 11, 14, 9, 290713)'}),
170
            'user': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'emailchange_user'", 'unique': 'True', 'to': "orm['im.AstakosUser']"})
171
        },
172
        'im.groupkind': {
173
            'Meta': {'object_name': 'GroupKind'},
174
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
175
            'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255', 'db_index': 'True'})
176
        },
177
        'im.invitation': {
178
            'Meta': {'object_name': 'Invitation'},
179
            'code': ('django.db.models.fields.BigIntegerField', [], {'db_index': 'True'}),
180
            'consumed': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}),
181
            'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
182
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
183
            'inviter': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'invitations_sent'", 'null': 'True', 'to': "orm['im.AstakosUser']"}),
184
            'is_consumed': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
185
            'realname': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
186
            'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255'})
187
        },
188
        'im.membership': {
189
            'Meta': {'unique_together': "(('person', 'group'),)", 'object_name': 'Membership'},
190
            'date_joined': ('django.db.models.fields.DateField', [], {'db_index': 'True', 'null': 'True', 'blank': 'True'}),
191
            'date_requested': ('django.db.models.fields.DateField', [], {'default': 'datetime.datetime(2012, 8, 9, 11, 14, 9, 286925)', 'blank': 'True'}),
192
            'group': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['im.AstakosGroup']"}),
193
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
194
            'person': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['im.AstakosUser']"})
195
        },
196
        'im.resource': {
197
            'Meta': {'object_name': 'Resource'},
198
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
199
            'meta': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['im.ResourceMetadata']", 'symmetrical': 'False'}),
200
            'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255', 'db_index': 'True'}),
201
            'service': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['im.Service']"})
202
        },
203
        'im.resourcemetadata': {
204
            'Meta': {'object_name': 'ResourceMetadata'},
205
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
206
            'key': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255', 'db_index': 'True'}),
207
            'value': ('django.db.models.fields.CharField', [], {'max_length': '255'})
208
        },
209
        'im.service': {
210
            'Meta': {'object_name': 'Service'},
211
            'auth_token': ('django.db.models.fields.CharField', [], {'max_length': '32', 'null': 'True', 'blank': 'True'}),
212
            'auth_token_created': ('django.db.models.fields.DateTimeField', [], {'null': 'True'}),
213
            'auth_token_expires': ('django.db.models.fields.DateTimeField', [], {'null': 'True'}),
214
            'icon': ('django.db.models.fields.FilePathField', [], {'max_length': '100', 'blank': 'True'}),
215
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
216
            'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255', 'db_index': 'True'}),
217
            'url': ('django.db.models.fields.FilePathField', [], {'max_length': '100'})
218
        }
219
    }
220

    
221
    complete_apps = ['im']