Skip to content

Commit

Permalink
Merge pull request #244 from volunteers-for-city-projects/fix/favorit…
Browse files Browse the repository at this point in the history
…e_project

Fixed the lack of project id verification
  • Loading branch information
ArtemKAF authored Nov 26, 2023
2 parents 0603eb4 + a721301 commit 8181106
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 45 deletions.
15 changes: 5 additions & 10 deletions backend/api/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from projects.models import Organization, Volunteer

from .utils import modify_errors
from .utils import get_modify_validation_errors


class DestroyUserMixin:
Expand Down Expand Up @@ -36,14 +36,9 @@ def is_valid(self, *, raise_exception=False):
try:
super().is_valid(raise_exception=True)
except ValidationError as error:
errors_db, errors_valid = modify_errors(
error.get_full_details(), {}
)
errors_db.update({'ValidationErrors': errors_valid})
raise ValidationError(
{
self.__class__.__name__:
errors_db
}
raise get_modify_validation_errors(
self.__class__.__name__,
error.get_full_details(),
{},
)
return not bool(self._errors)
45 changes: 45 additions & 0 deletions backend/api/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from django.http import Http404
from django.shortcuts import get_object_or_404
from djoser.compat import get_user_email
from djoser.conf import settings
from drf_extra_fields.fields import Base64ImageField
Expand Down Expand Up @@ -87,3 +89,46 @@ def modify_errors(details, errors_valid):
[]
).append(str(details.get(key)[i].get('message')))
return errors_db, errors_valid


def get_modify_validation_errors(class_name, details, errors_db):
errors_db, errors_valid = modify_errors(details, {})
errors_db.update(
{'ValidationErrors': errors_valid},
)
return ValidationError(
{class_name: errors_db},
)


def get_instance(model, user, project, serializer_name):
try:
instance = get_object_or_404(model, user=user, project=project)
except ValueError as error:
raise get_modify_validation_errors(
serializer_name,
{
'id': ValidationError(error.args[0]).get_full_details()
},
{},
)
except Http404 as error:
raise get_modify_validation_errors(
serializer_name,
{
'not_exist':
ValidationError(
error.args[0], code='not_exist',
).get_full_details()
},
{},
)
except Exception as error:
raise get_modify_validation_errors(
serializer_name,
{
'error': ValidationError(error.args[0]).get_full_details()
},
{},
)
return instance
52 changes: 17 additions & 35 deletions backend/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
VolunteerGetSerializer,
VolunteerUpdateSerializer,
)
from .utils import is_correct_status_change
from .utils import get_instance, is_correct_status_change

# from taggit.serializers import TaggitSerializer

Expand Down Expand Up @@ -246,28 +246,6 @@ def destroy(self, request, *args, **kwargs):
instance.delete()
return Response(status=status.HTTP_204_NO_CONTENT)

def create_favorite(self, serializer_class, user, project):
"""
Добавить проект в избранное.
"""
serializer = serializer_class(
data={'user': user, 'project': project, }
)
if serializer.is_valid():
serializer.save()
return Response(status=status.HTTP_204_NO_CONTENT)
return Response(
serializer.errors,
status=status.HTTP_400_BAD_REQUEST,
)

def delete_favorite(self, model, user, project):
"""
Удалить проект из избранного.
"""
model.objects.filter(user=user, project=project).delete()
return Response(status=status.HTTP_204_NO_CONTENT)

@action(
['POST', 'DELETE'],
detail=True,
Expand All @@ -280,18 +258,20 @@ def favorite(self, request, **kwargs):
---
"""
project = get_object_or_404(Project, pk=kwargs.get('pk'))
if request.method == 'POST':
return self.create_favorite(
serializer_class=self.serializer_class,
user=request.user.pk,
project=project.pk
)
return self.delete_favorite(
model=ProjectFavorite,
user=request.user.pk,
project=kwargs.get('pk'),
serializer = self.serializer_class(
data={'user': request.user.pk, 'project': kwargs.get('pk'), }
)
if request.method == 'POST' and serializer.is_valid():
serializer.save()
return Response(status=status.HTTP_204_NO_CONTENT)
instance = get_instance(
ProjectFavorite,
request.user.pk,
kwargs.get('pk'),
serializer.__class__.__name__,
)
instance.delete()
return Response(status=status.HTTP_204_NO_CONTENT)

@action(
methods=['POST'],
Expand Down Expand Up @@ -596,7 +576,9 @@ class ProjectMeViewSet(viewsets.GenericViewSet, mixins.ListModelMixin):

def get_queryset(self):
if self.request.user.is_volunteer:
# volunteer = get_object_or_404(Volunteer, user=self.request.user.id)
# volunteer = get_object_or_404(
# Volunteer, user=self.request.user.id,
# )
# from_volunteer_favorite = ProjectFavorite.objects.filter(
# project=OuterRef('pk'), volunteer=volunteer
# )
Expand Down

0 comments on commit 8181106

Please sign in to comment.