Skip to content
This repository has been archived by the owner on Aug 24, 2019. It is now read-only.

Commit

Permalink
Tagging links
Browse files Browse the repository at this point in the history
  • Loading branch information
enisbt committed Aug 19, 2018
1 parent 0f74947 commit 3ebadfb
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 10 deletions.
32 changes: 29 additions & 3 deletions factlist/perspective/serializers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from django.core.cache import cache
from rest_framework import serializers

from factlist.users.serializers import MinimalUserSerializer
from factlist.claims.serializers import LinkSerializer
from .models import Topic, TopicLink, Tag
from factlist.claims.models import Link
from .models import Topic, TopicLink, Tag, LinkTag


class TagSerializer(serializers.ModelSerializer):
Expand All @@ -17,6 +18,31 @@ class Meta:
)


class LinkSerializer(serializers.ModelSerializer):
tags = serializers.SerializerMethodField()
embed = serializers.SerializerMethodField()

class Meta:
model = Link
fields = (
"id",
"link",
"created_at",
"updated_at",
"tags",
'embed',
)

def get_tags(self, link):
tag_ids = list(LinkTag.objects.filter(link=link).values_list("tag_id", flat=True))
tags = Tag.objects.filter(id__in=tag_ids)
tags = TagSerializer(tags, many=True)
return tags.data

def get_embed(self, link):
return cache.get(link.link)


class TopicSerializer(serializers.ModelSerializer):
user = MinimalUserSerializer(read_only=True)
tags = TagSerializer(many=True)
Expand All @@ -40,5 +66,5 @@ class CreateTopicSerializer(serializers.Serializer):
link = serializers.CharField(required=True)


class UpdateTopicSerializer(serializers.Serializer):
class TitleSerializer(serializers.Serializer):
title = serializers.CharField(required=True)
86 changes: 86 additions & 0 deletions factlist/perspective/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,89 @@ def test_create_a_link(self):
}
response = enis_client.post("/api/v1/topics/%s/links/" % topic_id, data=data)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)

def test_get_list_of_links(self):
enis, enis_client = self.create_user_and_user_client()

data = {
'title': 'Test topic',
'link': "https://github.com",
}
response = enis_client.post('/api/v1/topics/', data=data)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)

topic_id = response.data["id"]

data = {
"link": "https://twitter.com"
}
response = enis_client.post("/api/v1/topics/%s/links/" % topic_id, data=data)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)

response = enis_client.get('/api/v1/topics/%s/links/' % topic_id)
self.assertEqual(response.status_code, status.HTTP_200_OK)

data = {
"link": "https://github.com"
}
response = enis_client.post("/api/v1/topics/%s/links/" % topic_id, data=data)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)

response = enis_client.get('/api/v1/topics/%s/links/' % topic_id)
self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_tag_links(self):
enis, enis_client = self.create_user_and_user_client()

data = {
'title': 'Test topic',
'link': "https://github.com",
}
response = enis_client.post('/api/v1/topics/', data=data)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)

topic_id = response.data["id"]

data = {
"link": "https://twitter.com"
}
response = enis_client.post("/api/v1/topics/%s/links/" % topic_id, data=data)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
first_link_id = response.data['id']

data = {
"link": "https://github.com"
}
response = enis_client.post("/api/v1/topics/%s/links/" % topic_id, data=data)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
second_link_id = response.data['id']

data = {
'title': 'tag1'
}
response = enis_client.post('/api/v1/topics/%s/links/%s/tags/' % (topic_id, first_link_id), data=data)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)

response = enis_client.get('/api/v1/topics/%s/tags/' % topic_id)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['count'], 1)

data = {
'title': 'tag2'
}
response = enis_client.post('/api/v1/topics/%s/links/%s/tags/' % (topic_id, first_link_id), data=data)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)

response = enis_client.get('/api/v1/topics/%s/tags/' % topic_id)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['count'], 2)

data = {
'title': 'tag1'
}
response = enis_client.post('/api/v1/topics/%s/links/%s/tags/' % (topic_id, second_link_id), data=data)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)

response = enis_client.get('/api/v1/topics/%s/tags/' % topic_id)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['count'], 2)
4 changes: 3 additions & 1 deletion factlist/perspective/urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from django.urls import path

from .views import ListAndCreateTopicView, TopicView, CreateLinkView
from .views import ListAndCreateTopicView, TopicView, CreateLinkView, TagLinkView, ListTagsOfTopic

urlpatterns = [
path('topics/', ListAndCreateTopicView.as_view()),
path('topics/<int:pk>/', TopicView.as_view()),
path('topics/<int:pk>/links/', CreateLinkView.as_view()),
path('topics/<int:topic_pk>/links/<int:pk>/tags/', TagLinkView.as_view()),
path('topics/<int:pk>/tags/', ListTagsOfTopic.as_view()),
]
40 changes: 34 additions & 6 deletions factlist/perspective/views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from rest_framework.generics import ListCreateAPIView, RetrieveUpdateDestroyAPIView, CreateAPIView
from rest_framework.generics import ListCreateAPIView, RetrieveUpdateDestroyAPIView, ListAPIView
from rest_framework.permissions import IsAuthenticated, AllowAny
from rest_framework.response import Response
from rest_framework import status
from rest_framework.views import APIView

from factlist.claims.models import Link
from factlist.claims.serializers import LinkSerializer
from .serializers import TopicSerializer, CreateTopicSerializer, UpdateTopicSerializer
from .models import Topic, TopicLink, Tag
from .serializers import TopicSerializer, CreateTopicSerializer, TitleSerializer, TagSerializer, LinkSerializer
from .models import Topic, TopicLink, Tag, LinkTag


class ListAndCreateTopicView(ListCreateAPIView):
Expand Down Expand Up @@ -64,17 +64,45 @@ def get_serializer_class(self):

def update(self, request, *args, **kwargs):
instance = self.get_object()
serializer = UpdateTopicSerializer(data=request.data)
serializer = TitleSerializer(data=request.data)
if serializer.is_valid(raise_exception=True):
if "title" in serializer.data:
instance.title = serializer.data["title"]
return Response(TopicSerializer(instance).data, status=status.HTTP_200_OK)


class CreateLinkView(CreateAPIView):
class CreateLinkView(ListCreateAPIView):
serializer_class = LinkSerializer
permission_classes = [IsAuthenticated]

def perform_create(self, serializer):
serializer.save()
TopicLink.objects.create(topic_id=self.kwargs["pk"], link_id=serializer.data["id"])

def get_queryset(self):
link_ids = list(TopicLink.objects.filter(topic_id=self.kwargs["pk"]).values_list("link_id", flat=True))
return Link.objects.filter(id__in=link_ids)


class TagLinkView(APIView):
permission_classes = [IsAuthenticated]

def post(self, request, *args, **kwargs):
serializer = TitleSerializer(data=request.data)
if serializer.is_valid(raise_exception=True):
title = serializer.data['title']
tags = Tag.objects.filter(title=title)
if tags.exists():
tags = tags.first()
else:
tags = Tag.objects.create(title=title, topic_id=self.kwargs['topic_pk'])
LinkTag.objects.create(link_id=self.kwargs['pk'], tag=tags)
return Response(TagSerializer(tags).data, status=status.HTTP_201_CREATED)


class ListTagsOfTopic(ListAPIView):
permission_classes = [AllowAny]
serializer_class = TagSerializer

def get_queryset(self):
return Tag.objects.filter(topic_id=self.kwargs['pk'])

0 comments on commit 3ebadfb

Please sign in to comment.