Merge branch 'feature/back-office-collections' into 'develop'

Feature/back office collections

See merge request gm/gm-backend!180
This commit is contained in:
Олег Хаятов 2019-12-16 19:41:59 +00:00
commit c456e43f80
2 changed files with 10 additions and 7 deletions

View File

@ -112,19 +112,22 @@ class Collection(ProjectBaseMixin, CollectionDateMixin,
@property
def related_object_names(self) -> list:
"""Return related object names."""
raw_object_names = {}
for related_object in [(related_object.id, related_object.name) for related_object in self._related_objects]:
instances = getattr(self, f'{related_object[1]}')
raw_objects = []
for related_object in [related_object.name for related_object in self._related_objects]:
instances = getattr(self, f'{related_object}')
if instances.exists():
for instance in instances.all():
raw_object_names[related_object[0]] = instance.slug if hasattr(instance, 'slug') else None
raw_object = (instance.id, instance.slug) if hasattr(instance, 'slug') else (
instance.id, None
)
raw_objects.append(raw_object)
# parse slugs
related_objects = []
object_names = set()
re_pattern = r'[\w]+'
for object_id in raw_object_names:
result = re.findall(re_pattern, raw_object_names[object_id])
for object_id, raw_name, in raw_objects:
result = re.findall(re_pattern, raw_name)
if result:
name = ' '.join(result).capitalize()
if name not in object_names:

View File

@ -21,7 +21,7 @@ class CollectionBackOfficeSerializer(CollectionBaseSerializer):
source='get_collection_type_display', read_only=True)
country = CountrySimpleSerializer(read_only=True)
count_related_objects = serializers.IntegerField(read_only=True)
related_object_names = serializers.JSONField(read_only=True)
related_object_names = serializers.ListField(read_only=True)
class Meta:
model = models.Collection