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