Commit ee753da3 authored by Jakob Lund Johannessen's avatar Jakob Lund Johannessen
Browse files

chore: refactor comments views

parent 27b6c728
Pipeline #171242 failed with stages
in 1 minute and 5 seconds
from calendar import c
from django.urls import path, include
from comments.views import CommentList, CommentDetail, LikeList, LikeDetail
from comments.views.comments import CommentList, CommentDetail
from comments.views.likes import LikeList, LikeDetail
likes_url_patterns = [
path("", LikeList.as_view(), name="like-list"),
......
from django.shortcuts import render
from rest_framework import generics, mixins
from comments.models import Comment, Like
from comments.models import Comment
from rest_framework import permissions
from comments.permissions import IsCommentVisibleToUser
from workouts.permissions import IsOwner, IsReadOnly
from comments.serializers import CommentSerializer, LikeSerializer
from comments.serializers import CommentSerializer
from django.db.models import Q
from rest_framework.filters import OrderingFilter
# Create your views here.
class CommentList(
mixins.ListModelMixin, mixins.CreateModelMixin, generics.GenericAPIView
):
# queryset = Comment.objects.all()
serializer_class = CommentSerializer
permission_classes = [permissions.IsAuthenticated]
filter_backends = [OrderingFilter]
......@@ -42,9 +39,11 @@ class CommentList(
- The comment is on a coach visibility workout and the user is the workout owner's coach
- The comment is on a workout owned by the user
"""
# The code below is kind of duplicate of the one in ./permissions.py
# We should replace it with a better solution.
# Or maybe not.
# Even though this is duplicated of ../permissions, I don't think it
# Should be replaced, as it's two different views it's consering.
# One could have different permissins per object
# than per list.
qs = Comment.objects.filter(
Q(workout__visibility="PU")
......@@ -58,8 +57,6 @@ class CommentList(
return qs
# Details of comment
class CommentDetail(
mixins.RetrieveModelMixin,
......@@ -82,44 +79,3 @@ class CommentDetail(
def delete(self, request, *args, **kwargs):
return self.destroy(request, *args, **kwargs)
# List of likes
class LikeList(mixins.ListModelMixin, mixins.CreateModelMixin,
generics.GenericAPIView):
serializer_class = LikeSerializer
permission_classes = [permissions.IsAuthenticated]
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)
def perform_create(self, serializer):
serializer.save(owner=self.request.user)
def get_queryset(self):
return Like.objects.filter(owner=self.request.user)
# Details of like
class LikeDetail(
mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin,
generics.GenericAPIView,
):
queryset = Like.objects.all()
serializer_class = LikeSerializer
permission_classes = [permissions.IsAuthenticated]
_Detail = []
def get(self, request, *args, **kwargs):
return self.retrieve(request, *args, **kwargs)
def put(self, request, *args, **kwargs):
return self.update(request, *args, **kwargs)
def delete(self, request, *args, **kwargs):
return self.destroy(request, *args, **kwargs)
from rest_framework import generics, mixins
from comments.models import Like
from rest_framework import permissions
from comments.serializers import LikeSerializer
class LikeList(mixins.ListModelMixin, mixins.CreateModelMixin,
generics.GenericAPIView):
serializer_class = LikeSerializer
permission_classes = [permissions.IsAuthenticated]
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)
def perform_create(self, serializer):
serializer.save(owner=self.request.user)
def get_queryset(self):
return Like.objects.filter(owner=self.request.user)
class LikeDetail(
mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin,
generics.GenericAPIView,
):
queryset = Like.objects.all()
serializer_class = LikeSerializer
permission_classes = [permissions.IsAuthenticated]
_Detail = []
def get(self, request, *args, **kwargs):
return self.retrieve(request, *args, **kwargs)
def put(self, request, *args, **kwargs):
return self.update(request, *args, **kwargs)
def delete(self, request, *args, **kwargs):
return self.destroy(request, *args, **kwargs)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment