Debian Codegraph @

sortedm2m/operations.py

raw symbols SCIP
1 from django.db import models
2 from django.db.migrations.operations import AlterField
3
4 from .compat import get_apps_from_state, get_field, get_rel
5
6
7 class AlterSortedManyToManyField(AlterField):
8 """A migration operation to transform a ManyToManyField into a
9 SortedManyToManyField and vice versa."""
10
11 def database_forwards(self, app_label, schema_editor, from_state, to_state):
12 to_apps = get_apps_from_state(to_state)
13 to_model = to_apps.get_model(app_label, self.model_name)
14 if self.allow_migrate_model(schema_editor.connection.alias, to_model):
15 to_field = get_field(to_model, self.name)
16
17 from_apps = get_apps_from_state(from_state)
18 from_model = from_apps.get_model(app_label, self.model_name)
19 from_field = get_field(from_model, self.name)
20
21 to_m2m_model = get_rel(to_field).through
22 from_m2m_model = get_rel(from_field).through
23
24 # M2M -> SortedM2M
25 if getattr(to_field, 'sorted', False):
26 self.add_sort_value_field(schema_editor, to_m2m_model)
27 # SortedM2M -> M2M
28 elif getattr(from_field, 'sorted', False):
29 self.remove_sort_value_field(schema_editor, from_m2m_model)
30 else:
31 raise TypeError(
32 '{operation} should only be used when changing a '
33 'SortedManyToManyField into a ManyToManyField or a '
34 'ManyToManyField into a SortedManyToManyField.'
35 .format(operation=self.__class__.__name__))
36
37 def database_backwards(self, app_label, schema_editor, from_state, to_state):
38 from_apps = get_apps_from_state(from_state)
39 from_model = from_apps.get_model(app_label, self.model_name)
40 from_field = get_field(from_model, self.name)
41
42 to_apps = get_apps_from_state(to_state)
43 to_model = to_apps.get_model(app_label, self.model_name)
44
45 if self.allow_migrate_model(schema_editor.connection.alias, to_model):
46 to_field = get_field(to_model, self.name)
47 from_m2m_model = get_rel(from_field).through
48 to_m2m_model = get_rel(to_field).through
49
50 # The `to_state` is the OLDER state.
51
52 # M2M -> SortedM2M (backwards)
53 if getattr(to_field, 'sorted', False):
54 self.add_sort_value_field(schema_editor, to_m2m_model)
55 # SortedM2M -> M2M (backwards)
56 elif getattr(from_field, 'sorted', False):
57 self.remove_sort_value_field(schema_editor, from_m2m_model)
58 else:
59 raise TypeError(
60 '{operation} should only be used when changing a '
61 'SortedManyToManyField into a ManyToManyField or a '
62 'ManyToManyField into a SortedManyToManyField.'
63 .format(operation=self.__class__.__name__))
64
65 def add_sort_value_field(self, schema_editor, model):
66 field = self.make_sort_by_field(model)
67 schema_editor.add_field(model, field)
68
69 @staticmethod
70 def remove_sort_value_field(schema_editor, model):
71 field = get_field(model, model._sort_field_name) # pylint: disable=protected-access
72 schema_editor.remove_field(model, field)
73
74 @staticmethod
75 def make_sort_by_field(model):
76 field_name = model._sort_field_name # pylint: disable=protected-access
77 field = models.IntegerField(name=field_name, default=0)
78 field.set_attributes_from_name(field_name)
79 return field