From 4f02068e12f45bcc6c6add9ce2ea15bdb4b54766 Mon Sep 17 00:00:00 2001 From: Anna Grund Date: Sat, 12 Aug 2023 15:09:29 +0300 Subject: [PATCH] Update --- ...tionalternativecategory_unique_together.py | 17 +++++++++ .../migrations/0031_auto_20230809_1940.py | 21 +++++++++++ ...odelsuggestionalternativecategory_table.py | 17 +++++++++ .../migrations/0033_auto_20230809_1944.py | 21 +++++++++++ ...tionalternativecategory_unique_together.py | 17 +++++++++ ajapaik/ajapaik/models.py | 28 ++++++++++++++- .../object_categorization_service.py | 35 ++++++++++++++++++- 7 files changed, 154 insertions(+), 2 deletions(-) create mode 100644 ajapaik/ajapaik/migrations/0030_alter_photomodelsuggestionalternativecategory_unique_together.py create mode 100644 ajapaik/ajapaik/migrations/0031_auto_20230809_1940.py create mode 100644 ajapaik/ajapaik/migrations/0032_alter_photomodelsuggestionalternativecategory_table.py create mode 100644 ajapaik/ajapaik/migrations/0033_auto_20230809_1944.py create mode 100644 ajapaik/ajapaik/migrations/0034_alter_photomodelsuggestionalternativecategory_unique_together.py diff --git a/ajapaik/ajapaik/migrations/0030_alter_photomodelsuggestionalternativecategory_unique_together.py b/ajapaik/ajapaik/migrations/0030_alter_photomodelsuggestionalternativecategory_unique_together.py new file mode 100644 index 000000000..dafe318cc --- /dev/null +++ b/ajapaik/ajapaik/migrations/0030_alter_photomodelsuggestionalternativecategory_unique_together.py @@ -0,0 +1,17 @@ +# Generated by Django 3.2.7 on 2023-08-09 16:34 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('ajapaik', '0029_photomodelsuggestionresult'), + ] + + operations = [ + migrations.AlterUniqueTogether( + name='photomodelsuggestionalternativecategory', + unique_together={('proposer', 'photo_id', 'viewpoint_elevation_alternation', 'scene_alternation')}, + ), + ] diff --git a/ajapaik/ajapaik/migrations/0031_auto_20230809_1940.py b/ajapaik/ajapaik/migrations/0031_auto_20230809_1940.py new file mode 100644 index 000000000..538e35c5b --- /dev/null +++ b/ajapaik/ajapaik/migrations/0031_auto_20230809_1940.py @@ -0,0 +1,21 @@ +# Generated by Django 3.2.7 on 2023-08-09 16:40 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('ajapaik', '0030_alter_photomodelsuggestionalternativecategory_unique_together'), + ] + + operations = [ + migrations.AlterUniqueTogether( + name='photomodelsuggestionalternativecategory', + unique_together={('proposer', 'photo_id')}, + ), + migrations.AlterModelTable( + name='photomodelsuggestionalternativecategory', + table='ajapaik_photomodelsuggestionalternativecategory', + ), + ] diff --git a/ajapaik/ajapaik/migrations/0032_alter_photomodelsuggestionalternativecategory_table.py b/ajapaik/ajapaik/migrations/0032_alter_photomodelsuggestionalternativecategory_table.py new file mode 100644 index 000000000..f15fdf450 --- /dev/null +++ b/ajapaik/ajapaik/migrations/0032_alter_photomodelsuggestionalternativecategory_table.py @@ -0,0 +1,17 @@ +# Generated by Django 3.2.7 on 2023-08-09 16:41 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('ajapaik', '0031_auto_20230809_1940'), + ] + + operations = [ + migrations.AlterModelTable( + name='photomodelsuggestionalternativecategory', + table='photomodelsuggestionalternativecategory', + ), + ] diff --git a/ajapaik/ajapaik/migrations/0033_auto_20230809_1944.py b/ajapaik/ajapaik/migrations/0033_auto_20230809_1944.py new file mode 100644 index 000000000..6d7fb49ae --- /dev/null +++ b/ajapaik/ajapaik/migrations/0033_auto_20230809_1944.py @@ -0,0 +1,21 @@ +# Generated by Django 3.2.7 on 2023-08-09 16:44 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('ajapaik', '0032_alter_photomodelsuggestionalternativecategory_table'), + ] + + operations = [ + migrations.AlterUniqueTogether( + name='photomodelsuggestionalternativecategory', + unique_together={('proposer', 'photo_id', 'scene_alternation')}, + ), + migrations.AlterModelTable( + name='photomodelsuggestionalternativecategory', + table='ajapaik_photomodelsuggestionalternativecategory', + ), + ] diff --git a/ajapaik/ajapaik/migrations/0034_alter_photomodelsuggestionalternativecategory_unique_together.py b/ajapaik/ajapaik/migrations/0034_alter_photomodelsuggestionalternativecategory_unique_together.py new file mode 100644 index 000000000..48e75c554 --- /dev/null +++ b/ajapaik/ajapaik/migrations/0034_alter_photomodelsuggestionalternativecategory_unique_together.py @@ -0,0 +1,17 @@ +# Generated by Django 3.2.7 on 2023-08-09 17:13 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('ajapaik', '0033_auto_20230809_1944'), + ] + + operations = [ + migrations.AlterUniqueTogether( + name='photomodelsuggestionalternativecategory', + unique_together={('proposer', 'photo_id', 'viewpoint_elevation_alternation'), ('proposer', 'photo_id', 'scene_alternation')}, + ), + ] diff --git a/ajapaik/ajapaik/models.py b/ajapaik/ajapaik/models.py index f362d04c7..7157b6705 100644 --- a/ajapaik/ajapaik/models.py +++ b/ajapaik/ajapaik/models.py @@ -25,7 +25,7 @@ from django.contrib.gis.geos import Point from django.contrib.postgres.fields import ArrayField from django.core.cache import cache -from django.core.exceptions import ObjectDoesNotExist +from django.core.exceptions import ObjectDoesNotExist, ValidationError from django.core.files.uploadedfile import InMemoryUploadedFile from django.core.validators import MaxValueValidator, MinValueValidator from django.db.models import CASCADE, DateField, FileField, Lookup, Transform, OneToOneField, Q, F, Sum, Index @@ -2128,6 +2128,32 @@ class PhotoModelSuggestionAlternativeCategory(Suggestion): proposer = ForeignKey('Profile', blank=True, null=True, related_name='photo_scene_suggestions_alternation', on_delete=CASCADE) + def validate_unique(self, exclude=None): + super().validate_unique(exclude) + + queryset = self.__class__._default_manager.filter( + Q(scene_alternation=0) | Q(scene_alternation=1), + proposer=self.proposer, + photo_id=self.photo_id + ).exclude(pk=self.pk) + + print(f"QUERY SET IS {queryset}") + print(type(self.scene_alternation)) + print(self.scene_alternation in ['0', '1']) + print(queryset.exists()) + + if self.scene_alternation in ['0', '1'] and queryset.exists(): + raise ValidationError('Only one record with scene_alternation 0 or 1 is allowed.') + + def save(self, *args, **kwargs): + self.validate_unique() + super().save(*args, **kwargs) + + class Meta: + db_table = 'ajapaik_photomodelsuggestionalternativecategory' + unique_together = (('proposer', 'photo_id', 'scene_alternation'), + ('proposer', 'photo_id', 'viewpoint_elevation_alternation')) + class PhotoFlipSuggestion(Suggestion): proposer = ForeignKey('Profile', blank=True, null=True, related_name='photo_flip_suggestions', on_delete=CASCADE) diff --git a/ajapaik/ajapaik_object_categorization/service/object_categorization/object_categorization_service.py b/ajapaik/ajapaik_object_categorization/service/object_categorization/object_categorization_service.py index 6fe8b2590..be887b4c7 100644 --- a/ajapaik/ajapaik_object_categorization/service/object_categorization/object_categorization_service.py +++ b/ajapaik/ajapaik_object_categorization/service/object_categorization/object_categorization_service.py @@ -1,5 +1,6 @@ import json +from django.db.models import Max from django.http import HttpRequest from ajapaik.ajapaik.models import PhotoSceneSuggestion, PhotoViewpointElevationSuggestion, \ @@ -84,9 +85,39 @@ def post_image_category_result_table(request: HttpRequest): return result +# def aggregate_category_data(): +# """ +# Methods aggregates data from both Alternative table + User's categories input. +# """ +# +# one_week_ago = datetime.datetime.now() - datetime.timedelta(days=7) +# +# # scene_category = PhotoSceneSuggestion.objects.filter(created__gte=one_week_ago).order_by('-created') +# # view_point_category = PhotoViewpointElevationSuggestion.objects.filter(created__gte=one_week_ago).order_by( +# # '-created') +# +# alternative_category = PhotoModelSuggestionAlternativeCategory.objects.filter(created__gte=one_week_ago).order_by( +# '-created') +# +# result_categories = {} +# +# # if scene_category.exists(): +# # scene_category_dict = serializers.serialize('python', scene_category) +# # result_categories['scene_category_data'] = scene_category_dict +# # +# # if view_point_category.exists(): +# # view_point_dict = serializers.serialize('python', view_point_category) +# # result_categories['view_point_category_data'] = view_point_dict +# +# if alternative_category.exists(): +# confirm_reject_dict = serializers.serialize('python', alternative_category) +# result_categories['alternative_category_data'] = confirm_reject_dict +# +# return result_categories + def aggregate_category_data(): """ - Methods aggregates data from both Alternative table + User's categories input. + Method aggregates data from both Alternative table + User's categories input. """ one_week_ago = datetime.datetime.now() - datetime.timedelta(days=7) @@ -112,6 +143,8 @@ def aggregate_category_data(): confirm_reject_dict = serializers.serialize('python', alternative_category) result_categories['alternative_category_data'] = confirm_reject_dict + print("=====") + print(result_categories) return result_categories