drf-appkit 1.2.2__tar.gz → 1.2.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- drf-appkit-1.2.4/MANIFEST.in +1 -0
- {drf-appkit-1.2.2/appkit/drf_appkit.egg-info → drf-appkit-1.2.4}/PKG-INFO +1 -1
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/api/views.py +5 -3
- drf-appkit-1.2.4/appkit/debug.py +42 -0
- drf-appkit-1.2.4/appkit/drf/adapters.py +7 -0
- drf-appkit-1.2.4/appkit/drf/endpoints.py +51 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4/appkit/drf_appkit.egg-info}/PKG-INFO +1 -1
- drf-appkit-1.2.4/appkit/drf_appkit.egg-info/top_level.txt +1 -0
- drf-appkit-1.2.4/appkit/email.py +48 -0
- drf-appkit-1.2.4/appkit/managers.py +181 -0
- drf-appkit-1.2.4/appkit/migrations/__init__.py +0 -0
- drf-appkit-1.2.4/appkit/models.py +445 -0
- drf-appkit-1.2.4/appkit/settings.py +39 -0
- drf-appkit-1.2.4/appkit/shortcuts.py +306 -0
- drf-appkit-1.2.4/appkit/storages.py +11 -0
- drf-appkit-1.2.4/appkit/util.py +337 -0
- drf-appkit-1.2.4/appkit/views.py +22 -0
- drf-appkit-1.2.4/drf_appkit.egg-info/PKG-INFO +29 -0
- drf-appkit-1.2.4/drf_appkit.egg-info/SOURCES.txt +71 -0
- drf-appkit-1.2.4/drf_appkit.egg-info/dependency_links.txt +1 -0
- drf-appkit-1.2.4/drf_appkit.egg-info/top_level.txt +1 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/setup.cfg +5 -1
- drf-appkit-1.2.4/setup.py +3 -0
- drf-appkit-1.2.2/MANIFEST.in +0 -1
- drf-appkit-1.2.2/appkit/api/metadata.py +0 -20
- drf-appkit-1.2.2/appkit/drf/metadata.py +0 -100
- drf-appkit-1.2.2/appkit/drf_appkit.egg-info/top_level.txt +0 -5
- drf-appkit-1.2.2/setup.py +0 -7
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/LICENSE +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/README.md +0 -0
- {drf-appkit-1.2.2/appkit/api → drf-appkit-1.2.4/appkit}/__init__.py +0 -0
- {drf-appkit-1.2.2/appkit/drf → drf-appkit-1.2.4/appkit/api}/__init__.py +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/api/filters.py +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/api/mixins.py +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/api/pagination.py +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/api/relations.py +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/api/renderers.py +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/api/serializers.py +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/api/tests.py +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/auth/__init__.py +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/auth/backends.py +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/auth/decorators.py +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/auth/email.py +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/auth/permissions.py +0 -0
- {drf-appkit-1.2.2/appkit/forms → drf-appkit-1.2.4/appkit/drf}/__init__.py +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/drf/authentication.py +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/drf/backends.py +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/drf/exceptions.py +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/drf/fields.py +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/drf/filters.py +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/drf/mixins.py +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/drf/permissions.py +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/drf/serializers.py +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/drf_appkit.egg-info/SOURCES.txt +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/drf_appkit.egg-info/dependency_links.txt +0 -0
- {drf-appkit-1.2.2/appkit/migrations → drf-appkit-1.2.4/appkit/forms}/__init__.py +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/forms/widgets.py +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/jinja2/appkit/forms/widgets/money_widget.html +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/migrations/0001_initial.py +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/migrations/0002_arrangement_tags.py +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/migrations/0003_auto_20210221_0852.py +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/migrations/0004_auto_20210309_0655.py +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/migrations/0005_auto_20210324_1046.py +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/migrations/0006_sitealias.py +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/migrations/0007_auto_20211021_1901.py +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/migrations/0008_update_autofields.py +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/migrations/0009_alter_arrangement_unique_together_and_more.py +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/migrations/0010_arrangement_name.py +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/migrations/0011_alter_sitealias_options.py +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/migrations/0012_alter_sitealias_site.py +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/migrations/0013_usergroup.py +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/migrations/0014_remove_note_notes_note_content_type_note_object_id.py +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/migrations/0015_alter_note_content_type_alter_note_object_id.py +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/migrations/0016_alter_arrangementitem_unique_together_and_more.py +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/migrations/0017_alter_imageattachment_image.py +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/migrations/0018_place_geoposition.py +0 -0
- {drf-appkit-1.2.2 → drf-appkit-1.2.4}/appkit/migrations/0019_alter_place_geoposition.py +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
recursive-include appkit/jinja2 *
|
|
@@ -27,8 +27,7 @@ from ..drf.filters import (
|
|
|
27
27
|
MasterDetailViewFilterBackend,
|
|
28
28
|
SearchFilter,
|
|
29
29
|
)
|
|
30
|
-
|
|
31
|
-
from ..drf.metadata import DRFFieldDict
|
|
30
|
+
from ..drf.endpoints import DRFAutoEndpoint
|
|
32
31
|
from ..drf.mixins import (
|
|
33
32
|
DestroyModelMixin,
|
|
34
33
|
DynamicSerializerMixin,
|
|
@@ -124,7 +123,6 @@ class MasterDetailViewSet(
|
|
|
124
123
|
that is common across all ModelViewSets
|
|
125
124
|
"""
|
|
126
125
|
allow_duplicate_records = False
|
|
127
|
-
metadata_fielddict_class = DRFFieldDict
|
|
128
126
|
|
|
129
127
|
edit_actions = ('create', 'partial_update', 'update',)
|
|
130
128
|
detail_actions = ('retrieve',)
|
|
@@ -132,6 +130,10 @@ class MasterDetailViewSet(
|
|
|
132
130
|
filter_class = FilterSet
|
|
133
131
|
search_filter_class = SearchFilter
|
|
134
132
|
|
|
133
|
+
@property
|
|
134
|
+
def endpoint(self):
|
|
135
|
+
return DRFAutoEndpoint(self.queryset.model, viewset=self)
|
|
136
|
+
|
|
135
137
|
@property
|
|
136
138
|
def filter_backends(self):
|
|
137
139
|
backends = [MasterDetailViewFilterBackend] + api_settings.DEFAULT_FILTER_BACKENDS
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import inspect
|
|
2
|
+
import logging
|
|
3
|
+
import os
|
|
4
|
+
import traceback
|
|
5
|
+
|
|
6
|
+
from django.conf import settings
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def log(message, level=logging.INFO):
|
|
10
|
+
calling_frame = traceback.extract_stack(limit=2)[0]
|
|
11
|
+
calling_module_path, _ = os.path.splitext(calling_frame.filename)
|
|
12
|
+
relative_module_path = calling_module_path[calling_module_path.index('/app/') + 1:]
|
|
13
|
+
namespace = f"{relative_module_path.replace('/', '.')}.{calling_frame.name}"
|
|
14
|
+
|
|
15
|
+
logger = logging.getLogger(namespace)
|
|
16
|
+
logger.log(level, f'{namespace} | {message}')
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def trace(message, level=logging.DEBUG, depth=0):
|
|
20
|
+
frames = inspect.stack()
|
|
21
|
+
|
|
22
|
+
current_frame = frames[1]
|
|
23
|
+
root_path = str(settings.DJANGO_ROOT_PATH)
|
|
24
|
+
|
|
25
|
+
message = '({0}:{1}:{2}): {3}'.format(
|
|
26
|
+
current_frame[1].replace(root_path, ''),
|
|
27
|
+
current_frame[3],
|
|
28
|
+
current_frame[2],
|
|
29
|
+
message,
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
if depth > 0:
|
|
33
|
+
for i in range(2, min(depth+2, len(frames))):
|
|
34
|
+
frame = frames[i]
|
|
35
|
+
message += '\n\t{0}, line {1}, in {2}'.format(
|
|
36
|
+
frame[1].replace(root_path, ''),
|
|
37
|
+
frame[2],
|
|
38
|
+
frame[3]
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
logger = logging.getLogger("debug")
|
|
42
|
+
logger.log(level, message)
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
try:
|
|
2
|
+
from collections import Iterable
|
|
3
|
+
except ImportError:
|
|
4
|
+
from collections.abc import Iterable
|
|
5
|
+
|
|
6
|
+
from rest_framework import fields, relations
|
|
7
|
+
|
|
8
|
+
from drf_auto_endpoint.endpoints import Endpoint
|
|
9
|
+
from drf_auto_endpoint.get_field_dict import GetFieldDict
|
|
10
|
+
|
|
11
|
+
from drf_extra_fields.fields import DateTimeRangeField
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class DRFGetFieldDict(GetFieldDict):
|
|
15
|
+
def get_base_dict_for_field(self, name, field_instance, translated_fields, serializer_instance):
|
|
16
|
+
base_dict_for_field = super().get_base_dict_for_field(name, field_instance, translated_fields, serializer_instance)
|
|
17
|
+
|
|
18
|
+
widget = 'textfield'
|
|
19
|
+
|
|
20
|
+
if isinstance(field_instance, relations.ManyRelatedField):
|
|
21
|
+
widget = 'itemlist'
|
|
22
|
+
elif isinstance(field_instance, relations.HyperlinkedRelatedField):
|
|
23
|
+
widget = 'select'
|
|
24
|
+
elif field_instance.style.get('base_template') == 'textarea.html':
|
|
25
|
+
widget = 'textarea'
|
|
26
|
+
elif isinstance(field_instance, fields.DateField):
|
|
27
|
+
widget = 'date'
|
|
28
|
+
elif isinstance(field_instance, fields.DateTimeField):
|
|
29
|
+
widget = 'datetime'
|
|
30
|
+
elif isinstance(field_instance, DateTimeRangeField):
|
|
31
|
+
widget = 'datetimerange'
|
|
32
|
+
|
|
33
|
+
base_dict_for_field['ui']['widget'] = widget
|
|
34
|
+
|
|
35
|
+
return base_dict_for_field
|
|
36
|
+
|
|
37
|
+
get_field_dict = DRFGetFieldDict()
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
# ------------------------------------------------------------------------------
|
|
41
|
+
class DRFAutoEndpoint(Endpoint):
|
|
42
|
+
def _get_field_dict(self, field, serializer_instance=None):
|
|
43
|
+
foreign_key_as_list = (isinstance(self.foreign_key_as_list, Iterable) and field in self.foreign_key_as_list) \
|
|
44
|
+
or (not isinstance(self.foreign_key_as_list, Iterable) and self.foreign_key_as_list)
|
|
45
|
+
|
|
46
|
+
if serializer_instance is None:
|
|
47
|
+
serializer_instance = self.get_serializer()()
|
|
48
|
+
|
|
49
|
+
return get_field_dict(field, serializer_instance, self.get_translated_fields(),
|
|
50
|
+
self.fields_annotation, self.model, foreign_key_as_list=foreign_key_as_list)
|
|
51
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
appkit
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
from django.conf import settings
|
|
2
|
+
from django.templatetags.static import static
|
|
3
|
+
|
|
4
|
+
from mail_templated import EmailMessage
|
|
5
|
+
|
|
6
|
+
from .settings import appkit_settings
|
|
7
|
+
from .shortcuts import site_url_base
|
|
8
|
+
|
|
9
|
+
get_current_site = appkit_settings.CURRENT_SITE_ACCESSOR
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class AppkitEmailMessage(EmailMessage):
|
|
13
|
+
sender = settings.DEFAULT_FROM_EMAIL_SENDER
|
|
14
|
+
|
|
15
|
+
def __init__(self, template_name=None, site=None, context=None, *args, **kwargs):
|
|
16
|
+
email_context = dict(context) if context else {}
|
|
17
|
+
|
|
18
|
+
app_site_url = site_url_base(None)
|
|
19
|
+
email_context.update({
|
|
20
|
+
'app_name': settings.PROJECT_NAME,
|
|
21
|
+
'app_site_url': app_site_url,
|
|
22
|
+
'app_icon_url': f'{app_site_url}{static("images/icon/android-chrome-192x192.png")}',
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
if site:
|
|
26
|
+
email_context.update({
|
|
27
|
+
'site_name': site.name,
|
|
28
|
+
'site_url': site_url_base(site),
|
|
29
|
+
})
|
|
30
|
+
if site.profile.icon:
|
|
31
|
+
email_context['site_icon_url'] = site.profile.icon.image.thumbnail['192x192']
|
|
32
|
+
|
|
33
|
+
super().__init__(template_name, email_context, *args, **kwargs)
|
|
34
|
+
|
|
35
|
+
def send(self, *args, **kwargs):
|
|
36
|
+
if not self.sender and 'site' in self.context:
|
|
37
|
+
self.sender = self.context['site'].name
|
|
38
|
+
|
|
39
|
+
self.from_email = f"{self.sender} <{self.from_email}>"
|
|
40
|
+
|
|
41
|
+
self.to = list(*args)
|
|
42
|
+
self.cc = kwargs.pop('cc', [])
|
|
43
|
+
self.bcc = kwargs.pop('bcc', [])
|
|
44
|
+
self.reply_to = kwargs.pop('reply_to', [])
|
|
45
|
+
|
|
46
|
+
self.context['allow_reply'] = True if self.reply_to else False
|
|
47
|
+
|
|
48
|
+
super(AppkitEmailMessage, self).send(*args, **kwargs)
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
from django.contrib.contenttypes.fields import GenericRelation
|
|
2
|
+
from django.db import models, transaction
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class DocumentManager(models.Manager):
|
|
6
|
+
# --------------------------------------------------------------------------
|
|
7
|
+
# Document Duplicating
|
|
8
|
+
def post_process_duplicated_document(self, document):
|
|
9
|
+
return document
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def duplicate(self, document):
|
|
13
|
+
DocumentClass = self.model
|
|
14
|
+
model_fields = DocumentClass._meta.get_fields()
|
|
15
|
+
|
|
16
|
+
with transaction.atomic():
|
|
17
|
+
duplicated_document = document.clone()
|
|
18
|
+
|
|
19
|
+
generic_relation_field_names = set()
|
|
20
|
+
foreign_key_relation_field_names = set()
|
|
21
|
+
|
|
22
|
+
for field in model_fields:
|
|
23
|
+
field_name = field.name
|
|
24
|
+
if isinstance(field, GenericRelation):
|
|
25
|
+
generic_relation_field_names.add(field_name)
|
|
26
|
+
continue
|
|
27
|
+
elif field.one_to_many:
|
|
28
|
+
if field.related_name:
|
|
29
|
+
foreign_key_relation_field_names.add(field.related_name)
|
|
30
|
+
else:
|
|
31
|
+
foreign_key_relation_field_names.add('{}_set'.format(field_name))
|
|
32
|
+
continue
|
|
33
|
+
if field.auto_created or field.many_to_many or field.one_to_one:
|
|
34
|
+
continue
|
|
35
|
+
|
|
36
|
+
# Duplicate the one-to-many relations
|
|
37
|
+
for field_name in foreign_key_relation_field_names:
|
|
38
|
+
duplicated_fk_relation = getattr(duplicated_document, field_name)
|
|
39
|
+
fk_related_record_manager = getattr(document, field_name)
|
|
40
|
+
for item in fk_related_record_manager.all():
|
|
41
|
+
# Create a copy of the related item and add it to the
|
|
42
|
+
# foreign-key relation
|
|
43
|
+
duplicated_fk_relation.add(item.clone())
|
|
44
|
+
|
|
45
|
+
# Duplicate the M2M relation
|
|
46
|
+
for field in model_fields:
|
|
47
|
+
if field.many_to_many:
|
|
48
|
+
field_name = field.name
|
|
49
|
+
related_record_manager = getattr(document, field_name)
|
|
50
|
+
duplicated_related_records = getattr(duplicated_document, field_name)
|
|
51
|
+
related_records = related_record_manager.all()
|
|
52
|
+
duplicated_related_records.add(*related_records)
|
|
53
|
+
|
|
54
|
+
return self.post_process_duplicated_document(duplicated_document)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
# --------------------------------------------------------------------------
|
|
58
|
+
# Document Merging
|
|
59
|
+
# --------------------------------------------------------------------------
|
|
60
|
+
@staticmethod
|
|
61
|
+
def longest(a, b):
|
|
62
|
+
return a if len(a) >= len(b) else b
|
|
63
|
+
|
|
64
|
+
@staticmethod
|
|
65
|
+
def concat(a, b):
|
|
66
|
+
return '{}\n\n\n{}'.format(a, b)
|
|
67
|
+
|
|
68
|
+
def create_merged_document(self, documents):
|
|
69
|
+
return self.model()
|
|
70
|
+
|
|
71
|
+
def post_process_merged_document(self, document):
|
|
72
|
+
return document
|
|
73
|
+
|
|
74
|
+
def merge(self, records):
|
|
75
|
+
DocumentClass = self.model
|
|
76
|
+
model_fields = DocumentClass._meta.get_fields()
|
|
77
|
+
|
|
78
|
+
merge_strategy = DocumentClass.merge_strategy()
|
|
79
|
+
"""
|
|
80
|
+
For Fields:
|
|
81
|
+
1) If a field value exists on only one model, use it.
|
|
82
|
+
2) If a field exists on both models:
|
|
83
|
+
a) If a merge strategy is explicitly specified, use it.
|
|
84
|
+
b) Choose the more recent value.
|
|
85
|
+
|
|
86
|
+
For M2M Relationships:
|
|
87
|
+
1) TODO: If a merge strategy is explicitly specified, use it.
|
|
88
|
+
2) Take the union of both sets
|
|
89
|
+
"""
|
|
90
|
+
#
|
|
91
|
+
with transaction.atomic():
|
|
92
|
+
merged_document = self.create_merged_document(records)
|
|
93
|
+
|
|
94
|
+
# Order the records by date_modified so that as we traverse them
|
|
95
|
+
# we can take the newer value when no merging strategy is specified
|
|
96
|
+
ordering = merge_strategy.get('ordering', 'date_modified')
|
|
97
|
+
records = records.order_by(ordering)
|
|
98
|
+
generic_relation_field_names = set()
|
|
99
|
+
foreign_key_relation_field_names = set()
|
|
100
|
+
|
|
101
|
+
for field in model_fields:
|
|
102
|
+
field_name = field.name
|
|
103
|
+
if isinstance(field, GenericRelation):
|
|
104
|
+
generic_relation_field_names.add(field_name)
|
|
105
|
+
continue
|
|
106
|
+
elif field.one_to_many:
|
|
107
|
+
if field.related_name:
|
|
108
|
+
foreign_key_relation_field_names.add(field.related_name)
|
|
109
|
+
else:
|
|
110
|
+
foreign_key_relation_field_names.add('{}_set'.format(field_name))
|
|
111
|
+
continue
|
|
112
|
+
if field.auto_created or field.many_to_many or field.one_to_one:
|
|
113
|
+
continue
|
|
114
|
+
|
|
115
|
+
merge_method = merge_strategy.get(field_name, None)
|
|
116
|
+
if callable(merge_method):
|
|
117
|
+
setattr(merged_document, field_name, merge_method(records))
|
|
118
|
+
else:
|
|
119
|
+
for record in records:
|
|
120
|
+
current_value = getattr(merged_document, field_name)
|
|
121
|
+
record_value = getattr(record, field_name)
|
|
122
|
+
|
|
123
|
+
# Attribute is not yet set on the merged document so just use this value
|
|
124
|
+
if record_value and current_value and merge_method:
|
|
125
|
+
# If a merge strategy is explicitly specified we'll use it.
|
|
126
|
+
# Otherwise take the more recently modified value
|
|
127
|
+
merge_func = getattr(self, merge_method)
|
|
128
|
+
merged_value = merge_func(record_value, current_value)
|
|
129
|
+
else:
|
|
130
|
+
merged_value = record_value
|
|
131
|
+
|
|
132
|
+
if merged_value:
|
|
133
|
+
setattr(merged_document, field_name, merged_value)
|
|
134
|
+
|
|
135
|
+
merged_document.save()
|
|
136
|
+
|
|
137
|
+
# Update ALL foreign key relations to reference merged document
|
|
138
|
+
for record in records:
|
|
139
|
+
for field_name in foreign_key_relation_field_names:
|
|
140
|
+
merged_fk_relation = getattr(merged_document, field_name)
|
|
141
|
+
fk_related_record_manager = getattr(record, field_name)
|
|
142
|
+
for item in fk_related_record_manager.all():
|
|
143
|
+
try:
|
|
144
|
+
with transaction.atomic():
|
|
145
|
+
merged_fk_relation.add(item)
|
|
146
|
+
except IntegrityError:
|
|
147
|
+
pass
|
|
148
|
+
|
|
149
|
+
# Update the generic relations
|
|
150
|
+
for record in records:
|
|
151
|
+
for field_name in generic_relation_field_names:
|
|
152
|
+
generic_relation = getattr(record, field_name)
|
|
153
|
+
for item in generic_relation.all():
|
|
154
|
+
item.object_id = merged_document.id
|
|
155
|
+
item.save()
|
|
156
|
+
|
|
157
|
+
# Take the union of any M2M relationships
|
|
158
|
+
for record in records:
|
|
159
|
+
for field in model_fields:
|
|
160
|
+
if field.many_to_many:
|
|
161
|
+
field_name = field.name
|
|
162
|
+
related_record_manager = getattr(record, field_name)
|
|
163
|
+
model_manager = related_record_manager.through.objects
|
|
164
|
+
|
|
165
|
+
merge_method_name = 'merge_{}'.format(field_name)
|
|
166
|
+
if hasattr(self, merge_method_name):
|
|
167
|
+
# If a merge method for this field exists, use it
|
|
168
|
+
merge_method = getattr(self, merge_method_name)
|
|
169
|
+
merge_method(record, merged_document)
|
|
170
|
+
elif model_manager and hasattr(model_manager, 'merge_records'):
|
|
171
|
+
merge_method = getattr(model_manager, 'merge_records')
|
|
172
|
+
merge_method(record, merged_document)
|
|
173
|
+
else:
|
|
174
|
+
merged_related_records = getattr(merged_document, field_name)
|
|
175
|
+
related_records = related_record_manager.all()
|
|
176
|
+
merged_related_records.add(*related_records)
|
|
177
|
+
# Delete the merged source record
|
|
178
|
+
record.delete()
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
return self.post_process_merged_document(merged_document)
|
|
File without changes
|