drf-appkit 1.2.2__tar.gz → 1.2.3__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.
Files changed (75) hide show
  1. drf-appkit-1.2.3/MANIFEST.in +1 -0
  2. {drf-appkit-1.2.2/appkit/drf_appkit.egg-info → drf-appkit-1.2.3}/PKG-INFO +1 -1
  3. drf-appkit-1.2.3/appkit/debug.py +42 -0
  4. {drf-appkit-1.2.2 → drf-appkit-1.2.3/appkit/drf_appkit.egg-info}/PKG-INFO +1 -1
  5. drf-appkit-1.2.3/appkit/drf_appkit.egg-info/top_level.txt +1 -0
  6. drf-appkit-1.2.3/appkit/email.py +48 -0
  7. drf-appkit-1.2.3/appkit/managers.py +181 -0
  8. drf-appkit-1.2.3/appkit/migrations/__init__.py +0 -0
  9. drf-appkit-1.2.3/appkit/models.py +445 -0
  10. drf-appkit-1.2.3/appkit/settings.py +39 -0
  11. drf-appkit-1.2.3/appkit/shortcuts.py +306 -0
  12. drf-appkit-1.2.3/appkit/storages.py +11 -0
  13. drf-appkit-1.2.3/appkit/util.py +337 -0
  14. drf-appkit-1.2.3/appkit/views.py +22 -0
  15. drf-appkit-1.2.3/drf_appkit.egg-info/PKG-INFO +29 -0
  16. drf-appkit-1.2.3/drf_appkit.egg-info/SOURCES.txt +71 -0
  17. drf-appkit-1.2.3/drf_appkit.egg-info/dependency_links.txt +1 -0
  18. drf-appkit-1.2.3/drf_appkit.egg-info/top_level.txt +1 -0
  19. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/setup.cfg +5 -1
  20. drf-appkit-1.2.3/setup.py +3 -0
  21. drf-appkit-1.2.2/MANIFEST.in +0 -1
  22. drf-appkit-1.2.2/appkit/drf_appkit.egg-info/top_level.txt +0 -5
  23. drf-appkit-1.2.2/setup.py +0 -7
  24. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/LICENSE +0 -0
  25. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/README.md +0 -0
  26. {drf-appkit-1.2.2/appkit/api → drf-appkit-1.2.3/appkit}/__init__.py +0 -0
  27. {drf-appkit-1.2.2/appkit/drf → drf-appkit-1.2.3/appkit/api}/__init__.py +0 -0
  28. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/api/filters.py +0 -0
  29. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/api/metadata.py +0 -0
  30. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/api/mixins.py +0 -0
  31. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/api/pagination.py +0 -0
  32. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/api/relations.py +0 -0
  33. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/api/renderers.py +0 -0
  34. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/api/serializers.py +0 -0
  35. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/api/tests.py +0 -0
  36. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/api/views.py +0 -0
  37. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/auth/__init__.py +0 -0
  38. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/auth/backends.py +0 -0
  39. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/auth/decorators.py +0 -0
  40. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/auth/email.py +0 -0
  41. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/auth/permissions.py +0 -0
  42. {drf-appkit-1.2.2/appkit/forms → drf-appkit-1.2.3/appkit/drf}/__init__.py +0 -0
  43. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/drf/authentication.py +0 -0
  44. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/drf/backends.py +0 -0
  45. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/drf/exceptions.py +0 -0
  46. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/drf/fields.py +0 -0
  47. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/drf/filters.py +0 -0
  48. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/drf/metadata.py +0 -0
  49. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/drf/mixins.py +0 -0
  50. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/drf/permissions.py +0 -0
  51. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/drf/serializers.py +0 -0
  52. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/drf_appkit.egg-info/SOURCES.txt +0 -0
  53. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/drf_appkit.egg-info/dependency_links.txt +0 -0
  54. {drf-appkit-1.2.2/appkit/migrations → drf-appkit-1.2.3/appkit/forms}/__init__.py +0 -0
  55. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/forms/widgets.py +0 -0
  56. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/jinja2/appkit/forms/widgets/money_widget.html +0 -0
  57. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/migrations/0001_initial.py +0 -0
  58. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/migrations/0002_arrangement_tags.py +0 -0
  59. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/migrations/0003_auto_20210221_0852.py +0 -0
  60. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/migrations/0004_auto_20210309_0655.py +0 -0
  61. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/migrations/0005_auto_20210324_1046.py +0 -0
  62. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/migrations/0006_sitealias.py +0 -0
  63. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/migrations/0007_auto_20211021_1901.py +0 -0
  64. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/migrations/0008_update_autofields.py +0 -0
  65. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/migrations/0009_alter_arrangement_unique_together_and_more.py +0 -0
  66. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/migrations/0010_arrangement_name.py +0 -0
  67. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/migrations/0011_alter_sitealias_options.py +0 -0
  68. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/migrations/0012_alter_sitealias_site.py +0 -0
  69. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/migrations/0013_usergroup.py +0 -0
  70. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/migrations/0014_remove_note_notes_note_content_type_note_object_id.py +0 -0
  71. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/migrations/0015_alter_note_content_type_alter_note_object_id.py +0 -0
  72. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/migrations/0016_alter_arrangementitem_unique_together_and_more.py +0 -0
  73. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/migrations/0017_alter_imageattachment_image.py +0 -0
  74. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/migrations/0018_place_geoposition.py +0 -0
  75. {drf-appkit-1.2.2 → drf-appkit-1.2.3}/appkit/migrations/0019_alter_place_geoposition.py +0 -0
@@ -0,0 +1 @@
1
+ recursive-include appkit/jinja2 *
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: drf-appkit
3
- Version: 1.2.2
3
+ Version: 1.2.3
4
4
  Summary: A Django app providing generic CRUD functionality
5
5
  Author: Allan Hart
6
6
  License: BSD
@@ -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)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: drf-appkit
3
- Version: 1.2.2
3
+ Version: 1.2.3
4
4
  Summary: A Django app providing generic CRUD functionality
5
5
  Author: Allan Hart
6
6
  License: BSD
@@ -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