allow bind or remove award for establishment
This commit is contained in:
parent
ec1caeced3
commit
a3a66a88e6
|
|
@ -34,7 +34,7 @@ from utils.models import (
|
|||
BaseAttributes, FavoritesMixin, FileMixin, GalleryMixin, HasTagsMixin,
|
||||
IntermediateGalleryModelMixin, ProjectBaseMixin, TJSONField, TranslatedFieldsMixin,
|
||||
TypeDefaultImageMixin, URLImageMixin, default_menu_bool_array, PhoneModelMixin,
|
||||
)
|
||||
AwardsModelMixin)
|
||||
|
||||
|
||||
# todo: establishment type&subtypes check
|
||||
|
|
@ -549,7 +549,7 @@ class EstablishmentQuerySet(models.QuerySet):
|
|||
|
||||
|
||||
class Establishment(GalleryMixin, ProjectBaseMixin, URLImageMixin,
|
||||
TranslatedFieldsMixin, HasTagsMixin, FavoritesMixin):
|
||||
TranslatedFieldsMixin, HasTagsMixin, FavoritesMixin, AwardsModelMixin):
|
||||
"""Establishment model."""
|
||||
|
||||
ABANDONED = 0
|
||||
|
|
@ -1149,7 +1149,7 @@ class EmployeeQuerySet(models.QuerySet):
|
|||
)
|
||||
|
||||
|
||||
class Employee(PhoneModelMixin, BaseAttributes):
|
||||
class Employee(PhoneModelMixin, AwardsModelMixin, BaseAttributes):
|
||||
"""Employee model."""
|
||||
|
||||
user = models.OneToOneField('account.User', on_delete=models.PROTECT,
|
||||
|
|
@ -1225,11 +1225,6 @@ class Employee(PhoneModelMixin, BaseAttributes):
|
|||
)
|
||||
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):
|
||||
"""QuerySet for model EstablishmentSchedule"""
|
||||
|
|
|
|||
|
|
@ -21,6 +21,8 @@ from utils.decorators import with_base_attributes
|
|||
from utils.methods import string_random
|
||||
from utils.serializers import ImageBaseSerializer, ProjectModelSerializer, TimeZoneChoiceField, \
|
||||
PhoneMixinSerializer
|
||||
from main import models as main_models
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
|
||||
|
||||
def phones_handler(phones_list, establishment):
|
||||
|
|
@ -948,3 +950,24 @@ class TeamMemberSerializer(serializers.ModelSerializer):
|
|||
'username',
|
||||
'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>/<int:user_id>', views.TeamMemberDeleteView.as_view(),
|
||||
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',)
|
||||
]
|
||||
|
|
|
|||
|
|
@ -16,6 +16,8 @@ from timetable.serialziers import ScheduleCreateSerializer, ScheduleRUDSerialize
|
|||
from utils.methods import get_permission_classes
|
||||
from utils.permissions import (IsEstablishmentAdministrator, IsEstablishmentManager)
|
||||
from utils.views import CreateDestroyGalleryViewMixin
|
||||
from main import models as main_models
|
||||
from main import serializers as main_serializers
|
||||
|
||||
|
||||
class MenuRUDMixinViews:
|
||||
|
|
@ -938,3 +940,25 @@ class TeamMemberDeleteView(generics.DestroyAPIView):
|
|||
def get_object(self):
|
||||
return UserRole.objects.get(role__role=Role.ESTABLISHMENT_ADMINISTRATOR, user_id=self.kwargs['user_id'],
|
||||
establishment_id=self.kwargs['establishment_id'])
|
||||
|
||||
|
||||
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)
|
||||
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.jsonb import KeyTextTransform
|
||||
from django.core.validators import FileExtensionValidator
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.utils import timezone
|
||||
from django.utils.html import mark_safe
|
||||
from django.utils.translation import ugettext_lazy as _, get_language
|
||||
|
|
@ -526,3 +527,12 @@ class PhoneModelMixin:
|
|||
"""Return phone national number from from PhonеNumberField."""
|
||||
if hasattr(self, 'phone') and (self.phone and hasattr(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