-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from daveminer/tag-filters
Tag filters
- Loading branch information
Showing
7 changed files
with
114 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -143,7 +143,7 @@ | |
|
||
USE_I18N = True | ||
|
||
USE_TZ = True | ||
USE_TZ = False | ||
|
||
|
||
# Static files (CSS, JavaScript, Images) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from .callbackview import * | ||
from .createview import * | ||
from .detailview import * | ||
from .listview import * | ||
from .listview import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,71 @@ | ||
from datetime import datetime, timedelta | ||
from django.core.exceptions import BadRequest | ||
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage | ||
from django.db.models import Q | ||
from django.db.models.query import QuerySet | ||
from django.http import HttpResponse, JsonResponse | ||
from django.http import HttpResponse, JsonResponse, QueryDict | ||
from django.views.generic import ListView | ||
from ..models import Sentiment | ||
|
||
|
||
class SentimentList(ListView): | ||
model = Sentiment | ||
template_name: str = '../templates/list.html' | ||
paginate_by: int = 10 # Number of items per page | ||
default_page_size: int = 100 | ||
|
||
def get_queryset(self) -> QuerySet[Sentiment]: | ||
def get_queryset(self, params: QueryDict) -> QuerySet[Sentiment]: | ||
queryset = Sentiment.objects.all().order_by('-created_at') | ||
tag = self.request.GET.get('tag') | ||
if tag: | ||
queryset = queryset.filter(tags__icontains=tag) | ||
tags = params.getlist('tags') | ||
if tags: | ||
query = Q() | ||
for tag in tags: | ||
query |= Q(tags__icontains=tag) | ||
queryset = queryset.filter(query) | ||
period = params.get('period') | ||
if period: | ||
try: | ||
period = int(period) | ||
except ValueError: | ||
raise BadRequest("The 'period' query parameter must be an integer.") | ||
else: | ||
period = 30 # Default period value if not provided | ||
|
||
timestamp = datetime.now() - timedelta(days=period) | ||
queryset = queryset.filter(created_at__gte=timestamp) | ||
return queryset | ||
|
||
def get(self, request, *args, **kwargs) -> HttpResponse | JsonResponse: | ||
if 'application/json' in request.META.get('HTTP_ACCEPT'): | ||
self.object_list = self.get_queryset() | ||
context = self.get_context_data() | ||
return JsonResponse(list( | ||
context['object_list'].values('created_at', 'label', 'score', 'tags', 'text') | ||
), safe=False) | ||
|
||
return super().get(request, *args, **kwargs) | ||
self.object_list = self.get_queryset(self.request.GET) | ||
|
||
# Pagination | ||
page_size = request.GET.get('page_size', self.default_page_size) | ||
try: | ||
page_size = int(page_size) | ||
except ValueError: | ||
page_size = self.default_page_size | ||
|
||
page = request.GET.get('page') | ||
try: | ||
page = int(page) | ||
except (TypeError, ValueError): | ||
page = 1 | ||
|
||
paginator = Paginator(self.object_list, page_size) | ||
paginated_queryset = paginator.page(page) | ||
|
||
if 'application/json' in request.META.get('HTTP_ACCEPT', ''): | ||
object_list = [ | ||
{ | ||
'created_at': obj.created_at, | ||
'label': obj.label, | ||
'score': obj.score, | ||
'tags': obj.tags, | ||
'text': obj.text | ||
} | ||
for obj in paginated_queryset.object_list | ||
] | ||
|
||
return JsonResponse(object_list, safe=False) | ||
|
||
context = self.get_context_data(object_list=paginated_queryset.object_list) | ||
return self.render_to_response(context) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import json | ||
import numpy | ||
import pytest | ||
import random | ||
from django.test import Client, override_settings | ||
from sentiment.models import Sentiment | ||
|
||
@pytest.mark.django_db | ||
def test_get(mocker): | ||
client = Client() | ||
__create_sentiment(label='test-label', text='test-text-two', tags=['test-tag']) | ||
__create_sentiment(label='test-label-two', text='test-text-two', tags=['test-tag', 'other-test-tag']) | ||
|
||
response = client.get("/sentiment/", {'tags': ['test-tag', 'another-tag'], 'period': 30}, HTTP_ACCEPT='application/json') | ||
|
||
assert response.status_code == 200 | ||
|
||
json = response.json() | ||
assert json[0]['label'] == 'test-label-two' | ||
assert json[0]['score'] < 1.0 and json[0]['score'] > -1.0 | ||
assert json[1]['label'] == 'test-label' | ||
|
||
|
||
def __create_sentiment(label, text, score=None, tags=None): | ||
if score is None: | ||
score = random.uniform(-1.0, 1.0) # Generate a random score between -1.0 and 1.0 | ||
if tags is None: | ||
tags = [] # Set tags to an empty list if not provided | ||
|
||
Sentiment.objects.create( | ||
label=label, | ||
score=score, | ||
tags=tags, | ||
text=text | ||
) |