Skip to content

Commit

Permalink
Merge pull request #240 from ZUS666/develop
Browse files Browse the repository at this point in the history
a
  • Loading branch information
Arseny13 authored Oct 27, 2023
2 parents 812a9bf + 6779ce8 commit 3f2b603
Show file tree
Hide file tree
Showing 32 changed files with 907 additions and 241 deletions.
14 changes: 14 additions & 0 deletions api_spot/api/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,17 @@ class AddSpotsError(exceptions.ValidationError):
default_detail = {
'error': 'Название уже есть в локации'
}


class SubscribedUserError(exceptions.ValidationError):
default_code = 'subscribe exists'
default_detail = {
'error': 'Вы уже подписаны.'
}


class NotSubscribedUserError(exceptions.ValidationError):
default_code = 'subscribe does not exists'
default_detail = {
'error': 'Вы не подписаны.'
}
7 changes: 6 additions & 1 deletion api_spot/api/serializers/review.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,16 @@ class ReviewGetSerializer(serializers.ModelSerializer):
source='booked_spot.user.last_name',
read_only=True
)
image = serializers.ImageField(
read_only=True,
source='booked_spot.user.avatar.image'
)

class Meta:
"""Класс мета для модели Review."""
model = Review
fields = (
'id', 'description', 'rating',
'first_name', 'last_name', 'pub_date'
'first_name', 'last_name', 'pub_date',
'image'
)
5 changes: 3 additions & 2 deletions api_spot/api/serializers/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ class Meta:
'phone',
'birth_date',
'occupation',
'image'
'is_subscribed',
'image',
)
read_only_fields = ('email',)
read_only_fields = ('email', 'is_subscribed')


class EmailSerializer(serializers.Serializer):
Expand Down
6 changes: 6 additions & 0 deletions api_spot/api/services/subscribe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def subscribe_service(self, error, bool):
user = self.request.user
if user.is_subscribed is bool:
raise error
user.is_subscribed = bool
user.save(update_fields=['is_subscribed'])
3 changes: 2 additions & 1 deletion api_spot/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
FavoriteViewSet, LocationMapListAPIView, LocationShortListAPIView,
LocationViewSet, OrderGetViewSet, OrderViewSet, PayView, PlanPhotoAPIView,
QuestionViewSet, ReviewCreateViewSet, ReviewGetViewSet, RuleViewSet,
SpotViewSet, UserViewSet,
SpotViewSet, SubscireAPIView, UserViewSet,
)


Expand Down Expand Up @@ -77,6 +77,7 @@
),
path('short_locations/', LocationShortListAPIView.as_view()),
path('map_locations/', LocationMapListAPIView.as_view()),
path('subscribe/', SubscireAPIView.as_view(),),
re_path(
r'locations/(?P<location_id>\d+)/plan_photo/',
PlanPhotoAPIView.as_view()
Expand Down
2 changes: 2 additions & 0 deletions api_spot/api/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from .review import ReviewCreateViewSet, ReviewGetViewSet
from .rule import RuleViewSet
from .spot import SpotViewSet
from .subscribe import SubscireAPIView
from .users import UserViewSet


Expand All @@ -33,4 +34,5 @@
ReviewGetViewSet
RuleViewSet
SpotViewSet
SubscireAPIView
UserViewSet
2 changes: 1 addition & 1 deletion api_spot/api/views/review.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ReviewGetViewSet(RetrieveListViewSet):
Представление для вывода отзывов по локациям.
"""
queryset = Review.objects.select_related(
'booked_spot__user',
'booked_spot__user', 'booked_spot__user__avatar'
).all()
serializer_class = ReviewGetSerializer
permission_classes = (AllowAny,)
Expand Down
30 changes: 30 additions & 0 deletions api_spot/api/views/subscribe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from drf_spectacular.utils import extend_schema
from rest_framework import status
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView

from api.exceptions import NotSubscribedUserError, SubscribedUserError
from api.services.subscribe import subscribe_service


@extend_schema(
tags=('subscribe',)
)
class SubscireAPIView(APIView):
"""
Представление подписки и отписки на новостную рассылку.
"""
permission_classes = (IsAuthenticated,)

def post(self, request):
subscribe_service(self, SubscribedUserError, True)
return Response(
{'message': 'Вы успешно подписались'}, status=status.HTTP_200_OK
)

def delete(self, request):
subscribe_service(self, NotSubscribedUserError, False)
return Response(
{'message': 'Вы успешно отписались'}, status=status.HTTP_200_OK
)
4 changes: 4 additions & 0 deletions api_spot/api_spot/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@
'task': 'spots.tasks.repeat_orders_finish',
'schedule': crontab(hour='*/1', minute=1),
},
'every_day_check_email': {
'task': 'promo.tasks.every_day_check_today_email_task',
'schedule': crontab(hour=15, minute=30),
},
}
Loading

0 comments on commit 3f2b603

Please sign in to comment.