Merge branch 'feature/bind-award-for-establishment' into 'develop'
allow bind or remove award for establishment See merge request gm/gm-backend!262
This commit is contained in:
commit
3edf197b81
|
|
@ -34,7 +34,7 @@ from utils.models import (
|
||||||
BaseAttributes, FavoritesMixin, FileMixin, GalleryMixin, HasTagsMixin,
|
BaseAttributes, FavoritesMixin, FileMixin, GalleryMixin, HasTagsMixin,
|
||||||
IntermediateGalleryModelMixin, ProjectBaseMixin, TJSONField, TranslatedFieldsMixin,
|
IntermediateGalleryModelMixin, ProjectBaseMixin, TJSONField, TranslatedFieldsMixin,
|
||||||
TypeDefaultImageMixin, URLImageMixin, default_menu_bool_array, PhoneModelMixin,
|
TypeDefaultImageMixin, URLImageMixin, default_menu_bool_array, PhoneModelMixin,
|
||||||
)
|
AwardsModelMixin)
|
||||||
|
|
||||||
|
|
||||||
# todo: establishment type&subtypes check
|
# todo: establishment type&subtypes check
|
||||||
|
|
@ -549,7 +549,7 @@ class EstablishmentQuerySet(models.QuerySet):
|
||||||
|
|
||||||
|
|
||||||
class Establishment(GalleryMixin, ProjectBaseMixin, URLImageMixin,
|
class Establishment(GalleryMixin, ProjectBaseMixin, URLImageMixin,
|
||||||
TranslatedFieldsMixin, HasTagsMixin, FavoritesMixin):
|
TranslatedFieldsMixin, HasTagsMixin, FavoritesMixin, AwardsModelMixin):
|
||||||
"""Establishment model."""
|
"""Establishment model."""
|
||||||
|
|
||||||
ABANDONED = 0
|
ABANDONED = 0
|
||||||
|
|
@ -1149,7 +1149,7 @@ class EmployeeQuerySet(models.QuerySet):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class Employee(PhoneModelMixin, BaseAttributes):
|
class Employee(PhoneModelMixin, AwardsModelMixin, BaseAttributes):
|
||||||
"""Employee model."""
|
"""Employee model."""
|
||||||
|
|
||||||
user = models.OneToOneField('account.User', on_delete=models.PROTECT,
|
user = models.OneToOneField('account.User', on_delete=models.PROTECT,
|
||||||
|
|
@ -1225,11 +1225,6 @@ class Employee(PhoneModelMixin, BaseAttributes):
|
||||||
)
|
)
|
||||||
return image_property
|
return image_property
|
||||||
|
|
||||||
def remove_award(self, award_id: int):
|
|
||||||
from main.models import Award
|
|
||||||
award = get_object_or_404(Award, pk=award_id)
|
|
||||||
self.awards.remove(award)
|
|
||||||
|
|
||||||
|
|
||||||
class EstablishmentScheduleQuerySet(models.QuerySet):
|
class EstablishmentScheduleQuerySet(models.QuerySet):
|
||||||
"""QuerySet for model EstablishmentSchedule"""
|
"""QuerySet for model EstablishmentSchedule"""
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,8 @@ from utils.decorators import with_base_attributes
|
||||||
from utils.methods import string_random
|
from utils.methods import string_random
|
||||||
from utils.serializers import ImageBaseSerializer, ProjectModelSerializer, TimeZoneChoiceField, \
|
from utils.serializers import ImageBaseSerializer, ProjectModelSerializer, TimeZoneChoiceField, \
|
||||||
PhoneMixinSerializer
|
PhoneMixinSerializer
|
||||||
|
from main import models as main_models
|
||||||
|
from django.contrib.contenttypes.models import ContentType
|
||||||
|
|
||||||
|
|
||||||
def phones_handler(phones_list, establishment):
|
def phones_handler(phones_list, establishment):
|
||||||
|
|
@ -948,3 +950,24 @@ class TeamMemberSerializer(serializers.ModelSerializer):
|
||||||
'username',
|
'username',
|
||||||
'email',
|
'email',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class BackEstablishmentAwardCreateSerializer(serializers.ModelSerializer):
|
||||||
|
"""Award, The Creator."""
|
||||||
|
award_type = serializers.PrimaryKeyRelatedField(required=True, queryset=main_models.AwardType.objects.all())
|
||||||
|
title = serializers.CharField(write_only=True)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = models.Award
|
||||||
|
fields = (
|
||||||
|
'id',
|
||||||
|
'award_type',
|
||||||
|
'title',
|
||||||
|
'vintage_year',
|
||||||
|
)
|
||||||
|
|
||||||
|
def validate(self, attrs):
|
||||||
|
attrs['object_id'] = self.context.get('request').parser_context.get('kwargs')['establishment_id']
|
||||||
|
attrs['content_type'] = ContentType.objects.get_for_model(models.Establishment)
|
||||||
|
attrs['title'] = {self.context.get('request').locale: attrs['title']}
|
||||||
|
return attrs
|
||||||
|
|
|
||||||
|
|
@ -83,5 +83,10 @@ urlpatterns = [
|
||||||
path('team/<int:establishment_id>', views.TeamMemberListView.as_view(), name='establishment-team-members-list'),
|
path('team/<int:establishment_id>', views.TeamMemberListView.as_view(), name='establishment-team-members-list'),
|
||||||
path('team/<int:establishment_id>/<int:user_id>', views.TeamMemberDeleteView.as_view(),
|
path('team/<int:establishment_id>/<int:user_id>', views.TeamMemberDeleteView.as_view(),
|
||||||
name='establishment-team-member-delete'),
|
name='establishment-team-member-delete'),
|
||||||
|
path('awards/create-and-bind/<int:establishment_id>',
|
||||||
|
views.EstablishmentAwardCreateAndBind.as_view(),
|
||||||
|
name='establishment-awards-create-and-bind'),
|
||||||
|
path('awards/create-and-bind/<int:establishment_id>/<int:award_id>',
|
||||||
|
views.EstablishmentAwardCreateAndBind.as_view(),
|
||||||
|
name='establishment-awards-create-and-bind',)
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
"""Establishment app views."""
|
"""Establishment app views."""
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
from django.contrib.contenttypes.models import ContentType
|
||||||
from django.db import transaction
|
from django.db import transaction
|
||||||
from django.db.models.query_utils import Q
|
from django.db.models.query_utils import Q
|
||||||
from django.http import Http404
|
from django.http import Http404
|
||||||
|
|
@ -17,6 +18,8 @@ from timetable.serialziers import ScheduleCreateSerializer, ScheduleRUDSerialize
|
||||||
from utils.methods import get_permission_classes
|
from utils.methods import get_permission_classes
|
||||||
from utils.permissions import (IsEstablishmentAdministrator, IsEstablishmentManager)
|
from utils.permissions import (IsEstablishmentAdministrator, IsEstablishmentManager)
|
||||||
from utils.views import CreateDestroyGalleryViewMixin
|
from utils.views import CreateDestroyGalleryViewMixin
|
||||||
|
from main import models as main_models
|
||||||
|
from main import serializers as main_serializers
|
||||||
|
|
||||||
|
|
||||||
class MenuRUDMixinViews:
|
class MenuRUDMixinViews:
|
||||||
|
|
@ -948,3 +951,28 @@ class TeamMemberDeleteView(generics.DestroyAPIView):
|
||||||
team_role_revoked.delay(self.kwargs['user_id'], self.request.country_code, establishment.name)
|
team_role_revoked.delay(self.kwargs['user_id'], self.request.country_code, establishment.name)
|
||||||
else:
|
else:
|
||||||
team_role_revoked(self.kwargs['user_id'], self.request.country_code, establishment.name)
|
team_role_revoked(self.kwargs['user_id'], self.request.country_code, establishment.name)
|
||||||
|
|
||||||
|
|
||||||
|
class EstablishmentAwardCreateAndBind(generics.CreateAPIView, generics.DestroyAPIView):
|
||||||
|
queryset = main_models.Award.objects.with_base_related().all()
|
||||||
|
permission_classes = get_permission_classes()
|
||||||
|
serializer_class = serializers.BackEstablishmentAwardCreateSerializer
|
||||||
|
|
||||||
|
def _award_list_for_establishment(self, establishment_id: int, status: int) -> Response:
|
||||||
|
awards = main_models.Award.objects.with_base_related().filter(
|
||||||
|
object_id=establishment_id,
|
||||||
|
content_type=ContentType.objects.get_for_model(models.Establishment)
|
||||||
|
)
|
||||||
|
response_serializer = main_serializers.AwardBaseSerializer(awards, many=True)
|
||||||
|
headers = self.get_success_headers(response_serializer.data)
|
||||||
|
return Response(response_serializer.data, status=status, headers=headers)
|
||||||
|
|
||||||
|
def create(self, request, *args, **kwargs):
|
||||||
|
"""Overridden create method."""
|
||||||
|
super().create(request, args, kwargs)
|
||||||
|
return self._award_list_for_establishment(kwargs['establishment_id'], status.HTTP_201_CREATED)
|
||||||
|
|
||||||
|
def delete(self, request, *args, **kwargs):
|
||||||
|
establishment = get_object_or_404(models.Establishment, id=kwargs['establishment_id'])
|
||||||
|
establishment.remove_award(kwargs['award_id'])
|
||||||
|
return self._award_list_for_establishment(kwargs['establishment_id'], status.HTTP_200_OK)
|
||||||
|
|
@ -9,6 +9,7 @@ from django.contrib.postgres.aggregates import ArrayAgg
|
||||||
from django.contrib.postgres.fields import JSONField
|
from django.contrib.postgres.fields import JSONField
|
||||||
from django.contrib.postgres.fields.jsonb import KeyTextTransform
|
from django.contrib.postgres.fields.jsonb import KeyTextTransform
|
||||||
from django.core.validators import FileExtensionValidator
|
from django.core.validators import FileExtensionValidator
|
||||||
|
from django.shortcuts import get_object_or_404
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django.utils.html import mark_safe
|
from django.utils.html import mark_safe
|
||||||
from django.utils.translation import ugettext_lazy as _, get_language
|
from django.utils.translation import ugettext_lazy as _, get_language
|
||||||
|
|
@ -526,3 +527,12 @@ class PhoneModelMixin:
|
||||||
"""Return phone national number from from PhonеNumberField."""
|
"""Return phone national number from from PhonеNumberField."""
|
||||||
if hasattr(self, 'phone') and (self.phone and hasattr(self.phone, 'national_number')):
|
if hasattr(self, 'phone') and (self.phone and hasattr(self.phone, 'national_number')):
|
||||||
return self.phone.national_number
|
return self.phone.national_number
|
||||||
|
|
||||||
|
|
||||||
|
class AwardsModelMixin:
|
||||||
|
def remove_award(self, award_id: int):
|
||||||
|
from main.models import Award
|
||||||
|
award = get_object_or_404(Award, pk=award_id)
|
||||||
|
|
||||||
|
if hasattr(self, 'awards'):
|
||||||
|
self.awards.remove(award)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user