Merge pull request #448 from mouse-reeve/tags-error

Errors when trying to tag a book
This commit is contained in:
Mouse Reeve 2020-12-31 08:58:51 -08:00 committed by GitHub
commit 12feecb9ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 98 additions and 21 deletions

View file

@ -129,7 +129,7 @@ class Work(OrderedCollectionPageMixin, Book):
def to_edition_list(self, **kwargs): def to_edition_list(self, **kwargs):
''' an ordered collection of editions ''' ''' an ordered collection of editions '''
return self.to_ordered_collection( return self.to_ordered_collection(
self.editions.all(), self.editions.order_by('-updated_date').all(),
remote_id='%s/editions' % self.remote_id, remote_id='%s/editions' % self.remote_id,
**kwargs **kwargs
) )

View file

@ -17,7 +17,9 @@ class Tag(OrderedCollectionMixin, BookWyrmModel):
@classmethod @classmethod
def book_queryset(cls, identifier): def book_queryset(cls, identifier):
''' county of books associated with this tag ''' ''' county of books associated with this tag '''
return cls.objects.filter(identifier=identifier) return cls.objects.filter(
identifier=identifier
).order_by('-updated_date')
@property @property
def collection_queryset(self): def collection_queryset(self):
@ -64,7 +66,7 @@ class UserTag(BookWyrmModel):
id='%s#remove' % self.remote_id, id='%s#remove' % self.remote_id,
actor=user.remote_id, actor=user.remote_id,
object=self.book.to_activity(), object=self.book.to_activity(),
target=self.to_activity(), target=self.remote_id,
).serialize() ).serialize()

View file

@ -283,21 +283,6 @@ def to_markdown(content):
return sanitizer.get_output() return sanitizer.get_output()
def handle_tag(user, tag):
''' tag a book '''
broadcast(user, tag.to_add_activity(user))
def handle_untag(user, book, name):
''' tag a book '''
book = models.Book.objects.get(id=book)
tag = models.Tag.objects.get(name=name, book=book, user=user)
tag_activity = tag.to_remove_activity(user)
tag.delete()
broadcast(user, tag_activity)
def handle_favorite(user, status): def handle_favorite(user, status):
''' a user likes a status ''' ''' a user likes a status '''
try: try:

View file

@ -41,6 +41,9 @@ class ViewActions(TestCase):
content='Test status', content='Test status',
remote_id='https://example.com/status/1', remote_id='https://example.com/status/1',
) )
self.work = models.Work.objects.create(title='Test Work')
self.book = models.Edition.objects.create(
title='Test Book', parent_work=self.work)
self.settings = models.SiteSettings.objects.create(id=1) self.settings = models.SiteSettings.objects.create(id=1)
self.factory = RequestFactory() self.factory = RequestFactory()
@ -351,3 +354,43 @@ class ViewActions(TestCase):
author.refresh_from_db() author.refresh_from_db()
self.assertEqual(author.name, 'Test Author') self.assertEqual(author.name, 'Test Author')
self.assertEqual(resp.template_name, 'edit_author.html') self.assertEqual(resp.template_name, 'edit_author.html')
def test_tag(self):
''' add a tag to a book '''
request = self.factory.post(
'', {
'name': 'A Tag!?',
'book': self.book.id,
})
request.user = self.local_user
with patch('bookwyrm.broadcast.broadcast_task.delay'):
actions.tag(request)
tag = models.Tag.objects.get()
user_tag = models.UserTag.objects.get()
self.assertEqual(tag.name, 'A Tag!?')
self.assertEqual(tag.identifier, 'A+Tag%21%3F')
self.assertEqual(user_tag.user, self.local_user)
self.assertEqual(user_tag.book, self.book)
def test_untag(self):
''' remove a tag from a book '''
tag = models.Tag.objects.create(name='A Tag!?')
user_tag = models.UserTag.objects.create(
user=self.local_user, book=self.book, tag=tag)
request = self.factory.post(
'', {
'user': self.local_user.id,
'book': self.book.id,
'name': tag.name,
})
request.user = self.local_user
with patch('bookwyrm.broadcast.broadcast_task.delay'):
actions.untag(request)
self.assertTrue(models.Tag.objects.filter(name='A Tag!?').exists())
self.assertFalse(models.UserTag.objects.exists())

View file

@ -196,3 +196,43 @@ class Views(TestCase):
result = views.editions_page(request, self.work.id) result = views.editions_page(request, self.work.id)
self.assertIsInstance(result, JsonResponse) self.assertIsInstance(result, JsonResponse)
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
def test_author_page(self):
''' there are so many views, this just makes sure it LOADS '''
author = models.Author.objects.create(name='Jessica')
request = self.factory.get('')
with patch('bookwyrm.views.is_api_request') as is_api:
is_api.return_value = False
result = views.author_page(request, author.id)
self.assertIsInstance(result, TemplateResponse)
self.assertEqual(result.template_name, 'author.html')
self.assertEqual(result.status_code, 200)
request = self.factory.get('')
with patch('bookwyrm.views.is_api_request') as is_api:
is_api.return_value = True
result = views.author_page(request, author.id)
self.assertIsInstance(result, JsonResponse)
self.assertEqual(result.status_code, 200)
def test_tag_page(self):
''' there are so many views, this just makes sure it LOADS '''
tag = models.Tag.objects.create(name='hi there')
models.UserTag.objects.create(
tag=tag, user=self.local_user, book=self.book)
request = self.factory.get('')
with patch('bookwyrm.views.is_api_request') as is_api:
is_api.return_value = False
result = views.tag_page(request, tag.identifier)
self.assertIsInstance(result, TemplateResponse)
self.assertEqual(result.template_name, 'tag.html')
self.assertEqual(result.status_code, 200)
request = self.factory.get('')
with patch('bookwyrm.views.is_api_request') as is_api:
is_api.return_value = True
result = views.tag_page(request, tag.identifier)
self.assertIsInstance(result, JsonResponse)
self.assertEqual(result.status_code, 200)

View file

@ -577,14 +577,14 @@ def tag(request):
tag_obj, created = models.Tag.objects.get_or_create( tag_obj, created = models.Tag.objects.get_or_create(
name=name, name=name,
) )
user_tag = models.UserTag.objects.get_or_create( user_tag, _ = models.UserTag.objects.get_or_create(
user=request.user, user=request.user,
book=book, book=book,
tag=tag_obj, tag=tag_obj,
) )
if created: if created:
outgoing.handle_tag(request.user, user_tag) broadcast(request.user, user_tag.to_add_activity(request.user))
return redirect('/book/%s' % book_id) return redirect('/book/%s' % book_id)
@ -593,9 +593,16 @@ def tag(request):
def untag(request): def untag(request):
''' untag a book ''' ''' untag a book '''
name = request.POST.get('name') name = request.POST.get('name')
tag = get_object_or_404(models.Tag, name=name)
book_id = request.POST.get('book') book_id = request.POST.get('book')
book = get_object_or_404(models.Edition, id=book_id)
outgoing.handle_untag(request.user, book_id, name) tag = get_object_or_404(
models.UserTag, tag=tag, book=book, user=request.user)
tag_activity = tag.to_remove_activity(request.user)
tag.delete()
broadcast(request.user, tag_activity)
return redirect('/book/%s' % book_id) return redirect('/book/%s' % book_id)