Commit 0b4ddde4 authored by Morten Nordseth's avatar Morten Nordseth
Browse files

smell - speculative generality

parent cd8a6a4a
Pipeline #128562 canceled with stages
......@@ -30,6 +30,7 @@ ENV GROUPID=${GROUPID}
RUN python manage.py test
# Initialize Django
RUN python manage.py makemigrations
RUN python manage.py migrate
# Import credential variables
......
......@@ -29,19 +29,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
......@@ -72,41 +72,3 @@ class CommentDetail(
def delete(self, request, *args, **kwargs):
return self.destroy(request, *args, **kwargs)
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)
......@@ -49,8 +49,7 @@ def api_root(request, format=None):
"workout-files": reverse(
"workout-file-list", request=request, format=format
),
"comments": reverse("comment-list", request=request, format=format),
"likes": reverse("like-list", request=request, format=format),
"comments": reverse("comment-list", request=request, format=format)
}
)
......
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