Merge branch 'master' of https://github.com/melinath/philo
[philo.git] / migrations / 0006_move_attribute_and_relationship_values.py
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
7 class Migration(DataMigration):
8
9         def forwards(self, orm):
10                 "Write your forwards methods here."
11                 ContentType = orm['contenttypes.ContentType']
12                 json_ct = ContentType.objects.get(app_label='philo', model='jsonvalue')
13                 fk_ct = ContentType.objects.get(app_label='philo', model='foreignkeyvalue')
14                 
15                 for attribute in orm.Attribute.objects.all():
16                         value = orm.JSONValue(value_json=attribute.value_json)
17                         value.save()
18                         attribute.value_content_type = json_ct
19                         attribute.value_object_id = value.id
20                         attribute.save()
21                 
22                 for relationship in orm.Relationship.objects.all():
23                         attribute = orm.Attribute(entity_content_type=relationship.entity_content_type, entity_object_id=relationship.entity_object_id, key=relationship.key)
24                         value = orm.ForeignKeyValue(content_type=relationship.value_content_type, object_id=relationship.value_object_id)
25                         value.save()
26                         attribute.value_content_type = fk_ct
27                         attribute.value_object_id = value.id
28                         attribute.save()
29                         relationship.delete()
30
31
32         def backwards(self, orm):
33                 "Write your backwards methods here."
34                 if orm.ManyToManyValue.objects.count():
35                         raise NotImplementedError("ManyToManyValue objects cannot be unmigrated.")
36                 
37                 ContentType = orm['contenttypes.ContentType']
38                 json_ct = ContentType.objects.get(app_label='philo', model='jsonvalue')
39                 fk_ct = ContentType.objects.get(app_label='philo', model='foreignkeyvalue')
40                 
41                 for attribute in orm.Attribute.objects.all():
42                         if attribute.value_content_type == json_ct:
43                                 value = orm.JSONValue.objects.get(pk=attribute.value_object_id)
44                                 attribute.value_json = value.value_json
45                                 attribute.save()
46                         elif attribute.value_content_type == fk_ct:
47                                 value = orm.ForeignKeyValue.objects.get(pk=attribute.value_object_id)
48                                 relationship = orm.Relationship(value_content_type=value.content_type, value_object_id=value.object_id, key=attribute.key, entity_content_type=attribute.entity_content_type, entity_object_id=attribute.entity_object_id)
49                                 relationship.save()
50                                 attribute.delete()
51                         else:
52                                 ct = attribute.value_content_type
53                                 raise NotImplementedError("Class cannot be converted: %s.%s" % (ct.app_label, ct.model))
54
55
56         models = {
57                 'contenttypes.contenttype': {
58                         'Meta': {'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
59                         'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
60                         'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
61                         'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
62                         'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
63                 },
64                 'philo.attribute': {
65                         'Meta': {'unique_together': "(('key', 'entity_content_type', 'entity_object_id'), ('value_content_type', 'value_object_id'))", 'object_name': 'Attribute'},
66                         'entity_content_type': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'attribute_entity_set'", 'to': "orm['contenttypes.ContentType']"}),
67                         'entity_object_id': ('django.db.models.fields.PositiveIntegerField', [], {}),
68                         'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
69                         'key': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
70                         'value_content_type': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'attribute_value_set'", 'null': 'True', 'to': "orm['contenttypes.ContentType']"}),
71                         'value_object_id': ('django.db.models.fields.PositiveIntegerField', [], {'null': 'True', 'blank': 'True'}),
72             'value': ('philo.models.fields.JSONField', [], {})
73                 },
74                 'philo.collection': {
75                         'Meta': {'object_name': 'Collection'},
76                         'description': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}),
77                         'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
78                         'name': ('django.db.models.fields.CharField', [], {'max_length': '255'})
79                 },
80                 'philo.collectionmember': {
81                         'Meta': {'object_name': 'CollectionMember'},
82                         'collection': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'members'", 'to': "orm['philo.Collection']"}),
83                         'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
84                         'index': ('django.db.models.fields.PositiveIntegerField', [], {'null': 'True', 'blank': 'True'}),
85                         'member_content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
86                         'member_object_id': ('django.db.models.fields.PositiveIntegerField', [], {})
87                 },
88                 'philo.contentlet': {
89                         'Meta': {'object_name': 'Contentlet'},
90                         'content': ('philo.models.fields.TemplateField', [], {}),
91                         'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
92                         'name': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
93                         'page': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'contentlets'", 'to': "orm['philo.Page']"})
94                 },
95                 'philo.contentreference': {
96                         'Meta': {'object_name': 'ContentReference'},
97                         'content_id': ('django.db.models.fields.PositiveIntegerField', [], {'null': 'True', 'blank': 'True'}),
98                         'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
99                         'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
100                         'name': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
101                         'page': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'contentreferences'", 'to': "orm['philo.Page']"})
102                 },
103                 'philo.file': {
104                         'Meta': {'object_name': 'File'},
105                         'file': ('django.db.models.fields.files.FileField', [], {'max_length': '100'}),
106                         'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
107                         'mimetype': ('django.db.models.fields.CharField', [], {'max_length': '255'})
108                 },
109                 'philo.foreignkeyvalue': {
110                         'Meta': {'object_name': 'ForeignKeyValue'},
111                         'content_type': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'foreign_key_value_set'", 'null': 'True', 'to': "orm['contenttypes.ContentType']"}),
112                         'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
113                         'object_id': ('django.db.models.fields.PositiveIntegerField', [], {'null': 'True', 'blank': 'True'})
114                 },
115                 'philo.jsonvalue': {
116                         'Meta': {'object_name': 'JSONValue'},
117                         'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
118                         'value': ('philo.models.fields.JSONField', [], {})
119                 },
120                 'philo.manytomanyvalue': {
121                         'Meta': {'object_name': 'ManyToManyValue'},
122                         'content_type': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'many_to_many_value_set'", 'null': 'True', 'to': "orm['contenttypes.ContentType']"}),
123                         'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
124                         'object_ids': ('django.db.models.fields.CommaSeparatedIntegerField', [], {'max_length': '300', 'null': 'True', 'blank': 'True'})
125                 },
126                 'philo.node': {
127                         'Meta': {'object_name': 'Node'},
128                         'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
129                         'parent': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'children'", 'null': 'True', 'to': "orm['philo.Node']"}),
130                         'slug': ('django.db.models.fields.SlugField', [], {'max_length': '255', 'db_index': 'True'}),
131                         'view_content_type': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'node_view_set'", 'to': "orm['contenttypes.ContentType']"}),
132                         'view_object_id': ('django.db.models.fields.PositiveIntegerField', [], {})
133                 },
134                 'philo.page': {
135                         'Meta': {'object_name': 'Page'},
136                         'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
137                         'template': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'pages'", 'to': "orm['philo.Template']"}),
138                         'title': ('django.db.models.fields.CharField', [], {'max_length': '255'})
139                 },
140                 'philo.redirect': {
141                         'Meta': {'object_name': 'Redirect'},
142                         'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
143                         'status_code': ('django.db.models.fields.IntegerField', [], {'default': '302'}),
144                         'target': ('django.db.models.fields.CharField', [], {'max_length': '200'})
145                 },
146         'philo.relationship': {
147             'Meta': {'unique_together': "(('key', 'entity_content_type', 'entity_object_id'),)", 'object_name': 'Relationship'},
148             'entity_content_type': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'relationship_entity_set'", 'to': "orm['contenttypes.ContentType']"}),
149             'entity_object_id': ('django.db.models.fields.PositiveIntegerField', [], {}),
150             'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
151             'key': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
152             'value_content_type': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'relationship_value_set'", 'null': 'True', 'to': "orm['contenttypes.ContentType']"}),
153             'value_object_id': ('django.db.models.fields.PositiveIntegerField', [], {'null': 'True', 'blank': 'True'})
154         },
155                 'philo.tag': {
156                         'Meta': {'object_name': 'Tag'},
157                         'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
158                         'name': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
159                         'slug': ('django.db.models.fields.SlugField', [], {'unique': 'True', 'max_length': '255', 'db_index': 'True'})
160                 },
161                 'philo.template': {
162                         'Meta': {'object_name': 'Template'},
163                         'code': ('philo.models.fields.TemplateField', [], {}),
164                         'documentation': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}),
165                         'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
166                         'mimetype': ('django.db.models.fields.CharField', [], {'default': "'text/html'", 'max_length': '255'}),
167                         'name': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
168                         'parent': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'children'", 'null': 'True', 'to': "orm['philo.Template']"}),
169                         'slug': ('django.db.models.fields.SlugField', [], {'max_length': '255', 'db_index': 'True'})
170                 }
171         }
172
173         complete_apps = ['philo']