Skip to content
Snippets Groups Projects
Commit 38a1f6bb authored by hollum's avatar hollum
Browse files

code smell - speculative generality

parent e417702a
Branches speculative_generality
No related tags found
No related merge requests found
Pipeline #170028 passed
......@@ -30,19 +30,3 @@ class Comment(models.Model):
class Meta:
ordering = ["-timestamp"]
class Like(models.Model):
"""Django model for a reaction to a comment.
Attributes:
owner: Who liked the comment
comment: The comment that was liked
timestamp: When the like occurred.
"""
owner = models.ForeignKey(
get_user_model(), on_delete=models.CASCADE, related_name="likes"
)
comment = models.ForeignKey(Comment, on_delete=models.CASCADE, related_name="likes")
timestamp = models.DateTimeField(auto_now_add=True)
from rest_framework import serializers
from rest_framework.serializers import HyperlinkedRelatedField
from comments.models import Comment, Like
from comments.models import Comment
from workouts.models import Workout
......@@ -13,14 +13,3 @@ class CommentSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Comment
fields = ["url", "id", "owner", "workout", "content", "timestamp"]
class LikeSerializer(serializers.HyperlinkedModelSerializer):
owner = serializers.ReadOnlyField(source="owner.username")
comment = HyperlinkedRelatedField(
queryset=Comment.objects.all(), view_name="comment-detail"
)
class Meta:
model = Like
fields = ["url", "id", "owner", "comment", "timestamp"]
from django.urls import path, include
from comments.models import Comment, Like
from comments.views import CommentList, CommentDetail, LikeList, LikeDetail
from comments.models import Comment
from comments.views import CommentList, CommentDetail
from rest_framework.urlpatterns import format_suffix_patterns
urlpatterns = [
path("api/comments/", CommentList.as_view(), name="comment-list"),
path("api/comments/<int:pk>/", CommentDetail.as_view(), name="comment-detail"),
path("api/likes/", LikeList.as_view(), name="like-list"),
path("api/likes/<int:pk>/", LikeDetail.as_view(), name="like-detail"),
]
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
from workouts.models import Visibility
......@@ -78,43 +78,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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment