create destroy mixin

This commit is contained in:
alex 2019-11-22 15:07:50 +03:00
parent 801f5fce24
commit 429825804a
3 changed files with 33 additions and 15 deletions

View File

@ -196,6 +196,7 @@ class News(GalleryModelMixin, BaseAttributes, TranslatedFieldsMixin, HasTagsMixi
views_count = models.OneToOneField('rating.ViewCount', blank=True, null=True, on_delete=models.SET_NULL)
ratings = generic.GenericRelation(Rating)
favorites = generic.GenericRelation(to='favorites.Favorites')
carousels = generic.GenericRelation(to='main.Carousel')
agenda = models.ForeignKey('news.Agenda', blank=True, null=True,
on_delete=models.SET_NULL,
verbose_name=_('agenda'))

View File

@ -13,4 +13,4 @@ urlpatterns = [
name='gallery-list'),
path('<int:pk>/gallery/<int:image_id>/', views.NewsBackOfficeGalleryCreateDestroyView.as_view(),
name='gallery-create-destroy'),
]
]

View File

@ -85,7 +85,7 @@ class JWTGenericViewMixin:
value=cookie.value,
secure=cookie.secure,
httponly=cookie.http_only,
max_age=cookie.max_age,)
max_age=cookie.max_age, )
return response
def _get_tokens_from_cookies(self, request, cookies: dict = None):
@ -126,9 +126,8 @@ class CreateDestroyGalleryViewMixin(generics.CreateAPIView,
return Response(status=status.HTTP_204_NO_CONTENT)
class FavoritesCreateDestroyMixinView(generics.CreateAPIView,
generics.DestroyAPIView):
"""Favorites Create Destroy mixin."""
class BaseCreateDestroyMixinView(generics.CreateAPIView, generics.DestroyAPIView):
"""Base Create Destroy mixin."""
_model = None
serializer_class = None
@ -137,16 +136,6 @@ class FavoritesCreateDestroyMixinView(generics.CreateAPIView,
def get_base_object(self):
return get_object_or_404(self._model, slug=self.kwargs['slug'])
def get_object(self):
"""
Returns the object the view is displaying.
"""
obj = self.get_base_object()
favorites = get_object_or_404(obj.favorites.filter(user=self.request.user))
# May raise a permission denied
self.check_object_permissions(self.request, favorites)
return favorites
def es_update_base_object(self):
es_update(self.get_base_object())
@ -159,6 +148,34 @@ class FavoritesCreateDestroyMixinView(generics.CreateAPIView,
self.es_update_base_object()
class FavoritesCreateDestroyMixinView(BaseCreateDestroyMixinView):
"""Favorites Create Destroy mixin."""
def get_object(self):
"""
Returns the object the view is displaying.
"""
obj = self.get_base_object()
favorites = get_object_or_404(obj.favorites.filter(user=self.request.user))
# May raise a permission denied
self.check_object_permissions(self.request, favorites)
return favorites
class CarouselCreateDestroyMixinView(BaseCreateDestroyMixinView):
"""Carousel Create Destroy mixin."""
def get_object(self):
"""
Returns the object the view is displaying.
"""
obj = self.get_base_object()
carousels = get_object_or_404(obj.carousels.filter(user=self.request.user))
# May raise a permission denied
self.check_object_permissions(self.request, carousels)
return carousels
# BackOffice user`s views & viewsets
class BindObjectMixin:
"""Bind object mixin."""