diff --git a/herodotus/urls.py b/herodotus/urls.py index a960bd3..ca242f8 100644 --- a/herodotus/urls.py +++ b/herodotus/urls.py @@ -15,7 +15,7 @@ """ from django.contrib import admin from django.urls import path, re_path, include -from .views import ContentViewSet, ScrapeArticle, CheckToken, UserViewSet, SearchContent, IndexArticles, FeedViewSet, VersionInformation +from .views import ImportView, ExportView, ContentViewSet, ScrapeArticle, CheckToken, UserViewSet, SearchContent, IndexArticles, FeedViewSet, VersionInformation from rest_framework.urlpatterns import format_suffix_patterns from rest_framework.routers import DefaultRouter @@ -29,6 +29,8 @@ path('scrapearticle/', ScrapeArticle.as_view()), path('indexarticles/', IndexArticles.as_view()), path('version/', VersionInformation.as_view()), + path('export/', ExportView.as_view()), + path('import/', ImportView.as_view()), path('search/', SearchContent.as_view()), path('checktoken/', CheckToken.as_view()), path('', include(router.urls)), diff --git a/herodotus/views.py b/herodotus/views.py index e335458..7129f5c 100644 --- a/herodotus/views.py +++ b/herodotus/views.py @@ -9,6 +9,8 @@ from django.db.models import Case, When import meilisearch from django.conf import settings +from django.http import HttpResponse +import json class ContentViewSet(viewsets.ModelViewSet): @@ -17,11 +19,13 @@ class ContentViewSet(viewsets.ModelViewSet): pagination_class = ContentPagination permission_classes = [permissions.IsAuthenticatedOrReadOnly] + class FeedViewSet(viewsets.ModelViewSet): queryset = Feed.objects.order_by('-id') serializer_class = FeedSerializer permission_classes = [permissions.IsAuthenticatedOrReadOnly] + class SearchContent(generics.ListCreateAPIView): pagination_class = ContentPagination serializer_class = ContentSerializer @@ -31,16 +35,19 @@ def list(self, request): search_ids = [] search = request.GET.get('q') - - client = meilisearch.Client(os.environ['MEILI_SEARCH_URL'], os.environ['MEILI_SEARCH_MASTER_KEY']) - index = client.get_or_create_index('article', {'primaryKey': 'article_id'}) + + client = meilisearch.Client( + os.environ['MEILI_SEARCH_URL'], os.environ['MEILI_SEARCH_MASTER_KEY']) + index = client.get_or_create_index( + 'article', {'primaryKey': 'article_id'}) results = index.search(search) for result in results['hits']: search_ids.append(result['article_id']) - preserved = Case(*[When(pk=pk, then=pos) for pos, pk in enumerate(search_ids)]) + preserved = Case(*[When(pk=pk, then=pos) + for pos, pk in enumerate(search_ids)]) queryset = self.get_queryset().filter(id__in=search_ids).order_by(preserved) paginator = ContentPagination() @@ -48,8 +55,6 @@ def list(self, request): serializer = ContentSerializer(page, many=True) return paginator.get_paginated_response(serializer.data) - - class ScrapeArticle(views.APIView): @@ -72,23 +77,26 @@ def get(self, request): results = ScrapedArticleSerializer(data, many=False).data return Response(results) + class IndexArticles(views.APIView): permission_classes = [permissions.IsAuthenticated] def get(self, request): - client = meilisearch.Client(os.environ['MEILI_SEARCH_URL'], os.environ['MEILI_SEARCH_MASTER_KEY']) - index = client.get_or_create_index('article', {'primaryKey': 'article_id'}) + client = meilisearch.Client( + os.environ['MEILI_SEARCH_URL'], os.environ['MEILI_SEARCH_MASTER_KEY']) + index = client.get_or_create_index( + 'article', {'primaryKey': 'article_id'}) documents = [] contentQuerySet = Content.objects.all() for article in contentQuerySet: - documents.append({'article_id': article.id, 'content': article.content, 'title': article.title, 'author': article.author, 'publisher': article.publisher, 'date': str(article.date)}) + documents.append({'article_id': article.id, 'content': article.content, 'title': article.title, + 'author': article.author, 'publisher': article.publisher, 'date': str(article.date)}) index.delete_all_documents() index.add_documents(documents) - return Response({'completed': True}) @@ -101,6 +109,43 @@ class UserViewSet(viewsets.ModelViewSet): serializer_class = UserSerializer permission_classes = [permissions.IsAdminUser] + class VersionInformation(views.APIView): - def get(self, request): - return Response({'version': settings.VERSION}) \ No newline at end of file + def get(self, request): + return Response({'version': settings.VERSION}) + +class ExportView(views.APIView): + permission_classes = [permissions.IsAuthenticated] + + def get(self, request): + contentQuerySet = Content.objects.all() + + contentArray = [{ + 'content_type': article.content_type, + 'url': article.url if article.url else None, + 'author': article.author if article.author else None, + 'publisher': article.publisher if article.publisher else None, + 'date': str(article.date) if article.date else None, + 'title': article.title, + 'content': article.content, + 'richtext': article.richtext, + } for article in contentQuerySet] + + response = HttpResponse(json.dumps(contentArray), content_type='text/json') + response['Content-Disposition'] = "attachment; filename=herodotus_export.json" + + return response + +class ImportView(views.APIView): + permission_classes = [permissions.IsAuthenticated] + + def post(self, request): + uploadedContent = request.FILES.get("content").read().decode('utf-8').replace("\n", '') + parsed = json.loads(uploadedContent) + + for article in parsed: + print(article['title']) + contentObj = Content(title=article['title'], url=article['url'], content_type=article['content_type'], author=article['author'], publisher=article['publisher'], date=article['date'], content=article['content'], richtext=article['richtext']) + contentObj.save() + + return HttpResponse("success")