Skip to content

Commit

Permalink
Merge pull request #3 from alaskanpuffin/development
Browse files Browse the repository at this point in the history
Add Import/Export
  • Loading branch information
alaskanpuffin authored Oct 2, 2020
2 parents 1dbd473 + 7576c7a commit 9528379
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 13 deletions.
4 changes: 3 additions & 1 deletion herodotus/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)),
Expand Down
69 changes: 57 additions & 12 deletions herodotus/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand All @@ -31,25 +35,26 @@ 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()
page = paginator.paginate_queryset(queryset, request)

serializer = ContentSerializer(page, many=True)
return paginator.get_paginated_response(serializer.data)




class ScrapeArticle(views.APIView):
Expand All @@ -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})


Expand All @@ -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})
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")

0 comments on commit 9528379

Please sign in to comment.