Skip to content
Snippets Groups Projects
Commit 2b615fe8 authored by Jan Bernoth's avatar Jan Bernoth
Browse files

Created a top media apiview

parent 3131d393
No related branches found
No related tags found
1 merge request!122User centric api
......@@ -13,6 +13,10 @@ After every container has started you can use the following links:
Custom functions could be seen in the [Custom Admin Functions](#custom-admin-functions)
* http://localhost:5555/ - Flower Dashboard. See all Celery tasks and their results.
Possibility to test in docker environment:
`docker exec -e PATH=/usr/local/bin oneup-django-web-1 python manage.py test --no-input --settings=one_
api.settings_prod`
## Custom Admin Functions
......
......@@ -228,3 +228,15 @@ class FileSerializer(serializers.ModelSerializer):
""" Definition of model and used fields """
model = Files
fields = ['name']
class TopMediaSerializer(serializers.Serializer):
""" Serializer class to shows how many articles are in each medium """
medium_name = serializers.CharField(max_length=200)
article_count = serializers.IntegerField()
def update(self, instance, validated_data):
""" not yet needed """
def create(self, validated_data):
""" not yet needed """
......@@ -106,33 +106,38 @@ class ArticleTest(TransactionTestCase):
self.assertEqual(Article.objects.count(), 2)
def create_articles_with_tags():
""" prepare test data """
tag_1 = Tag(name="Test1")
tag_1.save()
tag_2 = Tag(name="Test2")
tag_2.save()
conf = ArticlesConfig.create('articles.apps.ArticlesConfig')
conf.ready()
with mock.patch('articles.signals.extract_information_from_file', autospec=True) as mocked_post_save:
post_save.connect(mocked_post_save, sender=Files, dispatch_uid='extract_information_from_file')
Files.objects.create(upload_file='testfile.pdf', upload_date='18.11.2021')
file = Files.objects.get(upload_file='testfile.pdf')
input_data = {
'medium_name': 'MAZ',
'medium_type': 'Zeitung',
'name': 'Testartikel',
'source_file': file,
'reach': 1000,
'date_of_publication': datetime.datetime.today(),
'text': 'Dies ist ein beispielhafter Beispieltext',
'pages': 2,
'wrongly_parsed': True,
'tags': [tag_1.name, tag_2.name]
}
write_article_to_db(**input_data)
class OverviewTest(TransactionTestCase):
""" test functions for REST API Overview"""
def setUp(self):
tag_1 = Tag(name="Test1")
tag_1.save()
tag_2 = Tag(name="Test2")
tag_2.save()
conf = ArticlesConfig.create('articles.apps.ArticlesConfig')
conf.ready()
with mock.patch('articles.signals.extract_information_from_file', autospec=True) as mocked_post_save:
post_save.connect(mocked_post_save, sender=Files, dispatch_uid='extract_information_from_file')
Files.objects.create(upload_file='testfile.pdf', upload_date='18.11.2021')
file = Files.objects.get(upload_file='testfile.pdf')
input_data = {
'medium_name': 'MAZ',
'medium_type': 'Zeitung',
'name': 'Testartikel',
'source_file': file,
'reach': 1000,
'date_of_publication': datetime.datetime.today(),
'text': 'Dies ist ein beispielhafter Beispieltext',
'pages': 2,
'wrongly_parsed': True,
'tags': [tag_1.name, tag_2.name]
}
write_article_to_db(**input_data)
create_articles_with_tags()
def test_create_user_app_conf(self):
""" tests if user were created if config is ready"""
......@@ -171,3 +176,70 @@ class OverviewTest(TransactionTestCase):
"date_of_publication": today_str,
"reach": "1000"}
])
class TopMediaTest(TransactionTestCase):
""" Tests the top media api"""
def setUp(self):
create_articles_with_tags()
def test_top_media_blank_api_wo_user(self):
""" tests if the link works and returns the exact article count """
response = self.client.get('/articles/top/media')
self.assertEqual(response.status_code, 200)
self.assertJSONEqual(str(response.content, encoding='utf8'), [{'article_count': 1, 'medium_name': 'maz'}])
def test_top_media_with_extra_articles(self):
""" test the setting with multiple media """
file = Files.objects.get(upload_file='testfile.pdf')
tag_1 = Tag.objects.get(name="Test1")
tag_2 = Tag.objects.get(name="Test2")
input_data = {
'medium_name': 'MOZ',
'medium_type': 'Zeitung',
'name': 'Testartikel2',
'source_file': file,
'reach': 1111,
'date_of_publication': datetime.datetime.today(),
'text': 'Dies ist ein beispielhafter Beispieltext',
'pages': 2,
'wrongly_parsed': True,
'tags': [tag_1.name]
}
write_article_to_db(**input_data)
input_data['name'] = 'Testartikel3'
input_data['tags'] = [tag_2.name]
write_article_to_db(**input_data)
response = self.client.get('/articles/top/media')
self.assertEqual(response.status_code, 200)
self.assertJSONEqual(str(response.content, encoding='utf8'),
[{'article_count': 2, 'medium_name': 'moz'}, {'article_count': 1, 'medium_name': 'maz'}])
def test_top_media_with_extra_tag(self):
""" tests how the api is dealing with tags """
file = Files.objects.get(upload_file='testfile.pdf')
tag3_name = "Test3"
input_data = {
'medium_name': 'MOZ',
'medium_type': 'Zeitung',
'name': 'Testartikel2',
'source_file': file,
'reach': 1111,
'date_of_publication': datetime.datetime.today(),
'text': 'Dies ist ein beispielhafter Beispieltext',
'pages': 2,
'wrongly_parsed': True,
'tags': [tag3_name]
}
write_article_to_db(**input_data)
response = self.client.get('/articles/top/media')
self.assertEqual(response.status_code, 200)
self.assertJSONEqual(str(response.content, encoding='utf8'), [{'article_count': 1, 'medium_name': 'maz'}])
user = User.objects.get(id_tag=1)
tag3 = Tag.objects.get(name=tag3_name)
user.favorite_tags.add(tag3)
response = self.client.get('/articles/top/media')
self.assertEqual(response.status_code, 200)
self.assertJSONEqual(str(response.content, encoding='utf8'),
[{'article_count': 1, 'medium_name': 'maz'}, {'article_count': 1, 'medium_name': 'moz'}])
......@@ -14,6 +14,7 @@ article_urlpatterns = [
path('mediumtype/<int:pk>', views.MediumTypeDetail.as_view()),
path('byTags', views.TimelineList.as_view()),
path('overview', views.OverviewList.as_view()),
path('top/media', views.TopMediaView.as_view()),
]
wizard_urlpatterns = [
......
......@@ -3,7 +3,7 @@ import datetime
import logging
from django.core.exceptions import ObjectDoesNotExist
from django.db.models import Q
from django.db.models import Q, Count
from django.http import JsonResponse
from django.shortcuts import render
from django.utils import timezone
......@@ -17,7 +17,7 @@ from rest_framework.views import APIView
from articles.forms import FileUploadForm
from articles.models import Article, Timeline, Medium, MediumType, Overview, TopMedia, Tag, TopTags, Files, User
from articles.serializers import ArticleSerializer, TimelineSerializer, MediumSerializer, MediumTypeSerializer, \
OverviewSerializer
OverviewSerializer, TopMediaSerializer
from articles.tasks import read_all_media_files
logger = logging.getLogger(__name__)
......@@ -86,16 +86,8 @@ class MediumTypeDetail(generics.RetrieveUpdateDestroyAPIView): # pylint: disabl
serializer_class = MediumTypeSerializer
class TimelineList(generics.ListAPIView):
""" List all timeline objects """
today = datetime.datetime.today()
datetime_recent = today - datetime.timedelta(days=365)
queryset = Timeline.objects.filter(
Q(year=today.year, month__lte=today.month) | Q(year=datetime_recent.year, month__gte=datetime_recent.month))
serializer_class = TimelineSerializer
def user_tags(id_tag):
""" get favorite tags by generated user id """
try:
user = User.objects.get(id_tag=id_tag)
except ObjectDoesNotExist:
......@@ -107,14 +99,20 @@ def user_tags(id_tag):
return tag_names
class TimelineList(APIView):
""" List all timeline objects """
today = datetime.datetime.today()
datetime_recent = today - datetime.timedelta(days=365)
queryset = Timeline.objects.filter(
Q(year=today.year, month__lte=today.month) | Q(year=datetime_recent.year, month__gte=datetime_recent.month))
serializer_class = TimelineSerializer
class OverviewList(APIView):
""" List all overview objects """
today = datetime.datetime.today()
datetime_recent = today - datetime.timedelta(days=90)
# queryset = Overview.objects.filter(date_of_publication__range=[datetime_recent, today])
# serializer_class = OverviewSerializer
def get(self, request):
""" get overview, a user can be selected with user=user_id """
tag_names = user_tags(1)
......@@ -235,3 +233,23 @@ class UploadView(View):
file = Files(upload_file=name_, upload_date=timezone.now())
file.save()
return render(request, self.template_name, {'documents': Files.objects.all(), 'form': form})
class TopMediaView(APIView):
""" gets for a specific user and his favorite tags the top media platforms """
def get(self, request):
""" get method """
tag_names = user_tags(1)
if request.query_params.__contains__('user'):
tag_names = user_tags(request.query_params['user'])
# Counts the number of articles in a medium depending on the tags
top_media = Medium.objects.filter(article__tags__name__in=tag_names).annotate(
article_count=Count('article', distinct=True)).order_by('-article_count')
raw_media_data = []
for medium in top_media.all():
raw_media_data.append({'medium_name': medium.name, 'article_count': medium.article_count})
if len(raw_media_data) > 5:
break
top_media_serializer = TopMediaSerializer(raw_media_data, many=True)
return JsonResponse(top_media_serializer.data, safe=False)
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