As the last part of the chapter, we describe briefly some possible admin management of the model and the implementation of an API endpoint to retrieve the data processed by the application. In the pages
folder, we can set two admin interfaces in the admin.py
file to check the data collected by the SearchTerm
and Page
models:
from django.contrib import admin from django_markdown.admin import MarkdownField, AdminMarkdownWidget from pages.models import SearchTerm,Page,Link class SearchTermAdmin(admin.ModelAdmin): formfield_overrides = {MarkdownField: {'widget': AdminMarkdownWidget}} list_display = ['id', 'term', 'num_reviews'] ordering = ['-id'] class PageAdmin(admin.ModelAdmin): formfield_overrides = {MarkdownField: {'widget': AdminMarkdownWidget}} list_display = ['id', 'searchterm', 'url','title','content'] ordering = ['-id','-new_rank'] admin.site.register(SearchTerm,SearchTermAdmin) admin.site.register(Page,PageAdmin) admin.site.register(Link)
Note that both SearchTermAdmin
and PageAdmin
display objects with decreasing ID (and new_rank
in the case of PageAdmin
). The following screenshot is an example:
Note that although it is not necessary, the Link
model has also been included in the admin interface (admin.site.register(Link)
). More interestingly, we can set up an API endpoint to retrieve the sentiment counts related to a movie's title. In the api.py
file inside the pages folder, we can have the following:
from rest_framework import views,generics from rest_framework.permissions import AllowAny from rest_framework.response import Response from rest_framework.pagination import PageNumberPagination from pages.serializers import SearchTermSerializer from pages.models import SearchTerm,Page class LargeResultsSetPagination(PageNumberPagination): page_size = 1000 page_size_query_param = 'page_size' max_page_size = 10000 class SearchTermsList(generics.ListAPIView): serializer_class = SearchTermSerializer permission_classes = (AllowAny,) pagination_class = LargeResultsSetPagination def get_queryset(self): return SearchTerm.objects.all() class PageCounts(views.APIView): permission_classes = (AllowAny,) def get(self,*args, **kwargs): searchid=self.kwargs['pk'] reviewpages = Page.objects.filter(searchterm=searchid).filter(review=True) npos = len([p for p in reviewpages if p.sentiment==1]) nneg = len(reviewpages)-npos return Response({'npos':npos,'nneg':nneg})
The PageCounts
class takes as input the ID of the search (the movie's title) and it returns the sentiments, that is, positive and negative counts, for the movie's reviews. To get the ID of earchTerm
from a movie's title, you can either look at the admin interface or use the other API endpoint SearchTermsList
; this simply returns the list of the movies' titles together with the associated ID. The serializer is set on the serializers.py
file:
from pages.models import SearchTerm from rest_framework import serializers class SearchTermSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = SearchTerm fields = ('id', 'term')
To call these endpoints, we can again use the swagger interface (see Chapter 6, Getting Started with Django) or use the curl
command in the terminal to make these calls. For instance:
curl -X GET localhost:8000/search-list/ {"count":7,"next":null,"previous":null,"results":[{"id":24,"term":"the martian"},{"id":27,"term":"steve jobs"},{"id":29,"term":"suffragette"},{"id":39,"term":"southpaw"},{"id":40,"term":"vacation"},{"id":67,"term":"the revenant"},{"id":68,"term":"batman vs superman dawn of justice"}]}
and
curl -X GET localhost:8000/pages-sentiment/68/ {"nneg":3,"npos":15}
3.131.38.14