Skip to content

Commit

Permalink
Convert letter sentiment view into class-based view
Browse files Browse the repository at this point in the history
  • Loading branch information
clairempr committed Apr 15, 2022
1 parent bfbd894 commit 290e125
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
4 changes: 2 additions & 2 deletions letterpress/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.conf.urls import include, url
from django.contrib.auth import views as auth_views
from letters.views import export, GetStatsView, get_text_sentiment, GetWordCloudView, letter_by_id, \
letter_sentiment_view, LettersView, logout_view, place_by_id, places_view, random_letter, search, search_places, \
LetterSentimentView, LettersView, logout_view, place_by_id, places_view, random_letter, search, search_places, \
SentimentView, StatsView, text_sentiment_view, WordCloudView
from letterpress.views import HomeView

Expand All @@ -20,7 +20,7 @@
url(r'^accounts/logout/$', logout_view, name='logout'),
url(r'^export/', export, name='export'),
url(r'^letters/(?P<letter_id>[0-9]+)/sentiment/(?P<sentiment_id>[0-9]+)/$',
letter_sentiment_view, name='letter_sentiment_view'),
LetterSentimentView.as_view(), name='letter_sentiment_view'),
url(r'^letters/(?P<letter_id>[0-9]+)/$', letter_by_id, name='letter_by_id'),
url(r'^letters/', LettersView.as_view(), name='letters_view'),
url(r'^search_places/', search_places, name='search_places'),
Expand Down
12 changes: 6 additions & 6 deletions letters/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,35 +279,35 @@ def test_sentiment_view(self, mock_get_sentiments_for_sort_by_list, mock_get_ini

class LetterSentimentViewTestCase(TestCase):
"""
Test letter_sentiment_view()
Test LetterSentimentView
"""

@patch('letters.views.letter_search.get_letter_sentiments', autospec=True)
def test_letter_sentiment_view(self, mock_get_letter_sentiments):
"""
If letter exists, letter_sentiment_view() should return response with list of sentiments
If letter exists, LetterSentimentView should return response with list of sentiments
For some reason, object_not_found() and show_letter_sentiment() can't be successfully mocked,
so actually call them
"""

# If Letter with letter_id not found, letter_sentiment_view() should return object_not_found()
# If Letter with letter_id not found, LetterSentimentView should return object_not_found()
response = self.client.get(reverse('letter_sentiment_view',
kwargs={'letter_id': '1', 'sentiment_id': '1'}), follow=True)
expected = {'title': 'Letter not found', 'object_id': '1', 'object_type': 'Letter'}
for key in expected.keys():
self.assertEqual(response.context[key], expected[key],
"letter_sentiment_view() context '{}' should be '{}', if letter not found".format(key, expected[key]))
"LetterSentimentView context '{}' should be '{}', if letter not found".format(key, expected[key]))

# If Letter with letter_id found, letter_sentiment_view() should return show_letter_sentiment()
# If Letter with letter_id found, LetterSentimentView should return show_letter_sentiment()
response = self.client.get(reverse('letter_sentiment_view',
kwargs={'letter_id': LetterFactory().pk, 'sentiment_id': '1'}), follow=True)
self.assertTemplateUsed(response, 'letter_sentiment.html')

expected = {'title': 'Letter Sentiment', 'nbar': 'sentiment'}
for key in expected.keys():
self.assertEqual(response.context[key], expected[key],
"letter_sentiment_view() context '{}' should be '{}', if letter found".format(key, expected[key]))
"LetterSentimentView context '{}' should be '{}', if letter found".format(key, expected[key]))


class ShowLetterSentimentTestCase(TestCase):
Expand Down
25 changes: 15 additions & 10 deletions letters/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from django.template.loader import render_to_string
from django.utils.html import mark_safe
from django.views.generic.base import TemplateView, View
from django.views.generic.detail import DetailView
from letter_sentiment.custom_sentiment import get_custom_sentiment_for_text, highlight_for_custom_sentiment
from letter_sentiment.sentiment import get_sentiment, highlight_text_for_sentiment

Expand Down Expand Up @@ -216,18 +217,22 @@ def get_context_data(self, **kwargs):
return context


# view to show one letter by id, with highlights for selected sentiment
def letter_sentiment_view(request, letter_id, sentiment_id):
assert isinstance(request, HttpRequest)
try:
letter = Letter.objects.get(pk=letter_id)
except Letter.DoesNotExist:
return object_not_found(request, letter_id, 'Letter')
class LetterSentimentView(View):
"""
View to show one letter by id, with highlights for selected sentiment
"""

def get(self, request, *args, **kwargs):
pk = self.kwargs.get('letter_id')
try:
letter = Letter.objects.get(pk=pk)
except Letter.DoesNotExist:
return object_not_found(self.request, pk, 'Letter')

# sentiments is a list of tuples (id, value)
sentiments = letter_search.get_letter_sentiments(letter, sentiment_id)
# sentiments is a list of tuples (id, value)
sentiments = letter_search.get_letter_sentiments(letter, self.kwargs.get('sentiment_id'))

return show_letter_sentiment(request, letter, title='Letter Sentiment', nbar='sentiment', sentiments=sentiments)
return show_letter_sentiment(request, letter, title='Letter Sentiment', nbar='sentiment', sentiments=sentiments)


# show particular letter with sentiment highlights
Expand Down

0 comments on commit 290e125

Please sign in to comment.