-
Notifications
You must be signed in to change notification settings - Fork 58
/
0005_create_nested_plugins.py
98 lines (79 loc) · 3.28 KB
/
0005_create_nested_plugins.py
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from django.db import migrations, models
def add_plugin(apps, plugin_type, plugin_model, language, placeholder, parent, **data): # pragma: no cover
from cms.models import CMSPlugin
from treebeard.mp_tree import MP_AddChildHandler
# This is a CMSPlugin instance which represents the model's
# current state for the current migration
plugin = apps.get_model('cms', 'CMSPlugin')(
parent=parent,
placeholder=placeholder,
language=language,
plugin_type=plugin_type,
position=(parent.cmsplugin_set.count() + 1),
)
# This is a CMSPlugin instance which represents the current
# state of the model class itself, does not respect migrations.
# Needed because of the treebeard operations that rely on internal methods
concrete_parent = (
CMSPlugin
.objects
.only('numchild', 'depth', 'path')
.get(pk=parent.pk)
)
MP_AddChildHandler(concrete_parent, instance=plugin).process()
bound_plugin = plugin_model(**data)
bound_plugin.cmsplugin_ptr = plugin
for attr in ('parent_id', 'placeholder', 'language', 'plugin_type', 'creation_date', 'depth', 'path',
'numchild', 'pk', 'position'):
setattr(bound_plugin, attr, getattr(plugin, attr))
bound_plugin.save()
return bound_plugin
def create_marker_and_route(apps, schema_editor): # pragma: no cover
Placeholder = apps.get_model('cms', 'Placeholder')
CMSPlugin = apps.get_model('cms', 'CMSPlugin')
GoogleMap = apps.get_model('djangocms_googlemap', 'GoogleMap')
GoogleMapMarker = apps.get_model('djangocms_googlemap', 'GoogleMapMarker')
GoogleMapRoute = apps.get_model('djangocms_googlemap', 'GoogleMapRoute')
plugins = GoogleMap.objects.all()
for gmap in plugins:
if (gmap.content or gmap.address or gmap.zipcode or gmap.city or
gmap.lat or gmap.lng):
placeholder = Placeholder.objects.get(pk=gmap.placeholder_id)
target = CMSPlugin.objects.get(pk=gmap.pk)
address = '{0}, {1}, {2}'.format(
gmap.address, gmap.zipcode, gmap.city
)
# add nested marker plugin
add_plugin(
apps=apps,
plugin_type='GoogleMapMarkerPlugin',
plugin_model=GoogleMapMarker,
language=gmap.language,
placeholder=placeholder,
parent=target,
# custom fields
address=address,
lat=gmap.lat,
lng=gmap.lng,
info_content=gmap.content,
)
if gmap.route_planer:
# add nested route plugin
add_plugin(
apps=apps,
plugin_type='GoogleMapRoutePlugin',
plugin_model=GoogleMapRoute,
language=gmap.language,
placeholder=placeholder,
parent=target,
# custom fields
title=gmap.route_planer_title or '',
destination=address,
)
class Migration(migrations.Migration):
dependencies = [
('djangocms_googlemap', '0004_adapted_fields'),
]
operations = [
migrations.RunPython(create_marker_and_route),
]