coding utf-8 from __future__ import unicode_literals from django db im

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
def convert_projects_to_sm_relations(apps, schema_editor):
Task = apps.get_model("tasks", "Task")
ContentType = apps.get_model("contenttypes", "ContentType")
Project = apps.get_model("pekama", "Project")
SubMatter = apps.get_model("legal", "SubMatter")
Event = apps.get_model("legal", "Event")
matter_ct = ContentType.objects.get(app_label="legal", model="matter")
submatter_ct = ContentType.objects.get(app_label="legal", model="submatter")
event_ct = ContentType.objects.get(app_label="legal", model="event")
for task in Task.objects.all():
project = Project.objects.get(pk=task.project.id)
if project.content_type == matter_ct:
matter_id = project.object_id
first_submatter = SubMatter.objects.filter(matter_id=matter_id).first()
if first_submatter:
task.sub_matter = first_submatter
if project.content_type == submatter_ct:
sub_matter = SubMatter.objects.get(pk=project.object_id)
task.sub_matter = sub_matter
if project.content_type == event_ct:
event = Event.objects.get(pk=project.object_id)
task.sub_matter = event.sub_matter
task.event = event
task.save()
class Migration(migrations.Migration):
dependencies = [
('tasks', '0002_auto_20150213_1706'),
]
operations = [
migrations.RunPython(convert_projects_to_sm_relations, lambda x, y: None),
]