Test tag and list add
This commit is contained in:
parent
6e6bcb2f48
commit
4d0e52bf51
|
@ -82,7 +82,8 @@ class ActivityObject:
|
||||||
if activity_objects:
|
if activity_objects:
|
||||||
value = naive_parse(activity_objects, value)
|
value = naive_parse(activity_objects, value)
|
||||||
else:
|
else:
|
||||||
value = naive_parse(activity_objects, value, serializer=field.type)
|
value = naive_parse(
|
||||||
|
activity_objects, value, serializer=field.type)
|
||||||
|
|
||||||
except KeyError:
|
except KeyError:
|
||||||
if field.default == MISSING and \
|
if field.default == MISSING and \
|
||||||
|
|
|
@ -330,7 +330,7 @@ class OrderedCollectionMixin(OrderedCollectionPageMixin):
|
||||||
def to_activity(self, **kwargs):
|
def to_activity(self, **kwargs):
|
||||||
''' an ordered collection of the specified model queryset '''
|
''' an ordered collection of the specified model queryset '''
|
||||||
return self.to_ordered_collection(
|
return self.to_ordered_collection(
|
||||||
self.collection_queryset, **kwargs).serialize()
|
self.collection_queryset, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
class CollectionItemMixin(ActivitypubMixin):
|
class CollectionItemMixin(ActivitypubMixin):
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
''' models for storing different kinds of Activities '''
|
''' models for storing different kinds of Activities '''
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
|
|
||||||
|
from django.apps import apps
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
from bookwyrm import activitypub
|
from bookwyrm import activitypub
|
||||||
|
@ -15,17 +16,15 @@ class Tag(OrderedCollectionMixin, BookWyrmModel):
|
||||||
name = fields.CharField(max_length=100, unique=True)
|
name = fields.CharField(max_length=100, unique=True)
|
||||||
identifier = models.CharField(max_length=100)
|
identifier = models.CharField(max_length=100)
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def book_queryset(cls, identifier):
|
|
||||||
''' county of books associated with this tag '''
|
|
||||||
return cls.objects.filter(
|
|
||||||
identifier=identifier
|
|
||||||
).order_by('-updated_date')
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def collection_queryset(self):
|
def books(self):
|
||||||
''' books associated with this tag '''
|
''' count of books associated with this tag '''
|
||||||
return self.book_queryset(self.identifier)
|
edition_model = apps.get_model('bookwyrm.Edition', require_ready=True)
|
||||||
|
return edition_model.objects.filter(
|
||||||
|
usertag__tag__identifier=self.identifier
|
||||||
|
).order_by('-created_date').distinct()
|
||||||
|
|
||||||
|
collection_queryset = books
|
||||||
|
|
||||||
def get_remote_id(self):
|
def get_remote_id(self):
|
||||||
''' tag should use identifier not id in remote_id '''
|
''' tag should use identifier not id in remote_id '''
|
||||||
|
|
|
@ -612,30 +612,6 @@ class Inbox(TestCase):
|
||||||
self.assertEqual(shelf.books.first(), book)
|
self.assertEqual(shelf.books.first(), book)
|
||||||
|
|
||||||
|
|
||||||
# def test_handle_tag_book(self):
|
|
||||||
# ''' tagging a book '''
|
|
||||||
# work = models.Work.objects.create(title='work title')
|
|
||||||
# book = models.Edition.objects.create(
|
|
||||||
# title='Test', remote_id='https://bookwyrm.social/book/37292',
|
|
||||||
# parent_work=work)
|
|
||||||
#
|
|
||||||
# activity = {
|
|
||||||
# "id": "https://bookwyrm.social/shelfbook/6189#add",
|
|
||||||
# "type": "Add",
|
|
||||||
# "actor": "https://example.com/users/rat",
|
|
||||||
# "object": {
|
|
||||||
# "type": "Edition",
|
|
||||||
# "title": "Test Title",
|
|
||||||
# "work": work.remote_id,
|
|
||||||
# "id": "https://bookwyrm.social/book/37292",
|
|
||||||
# },
|
|
||||||
# "target": "",
|
|
||||||
# "@context": "https://www.w3.org/ns/activitystreams"
|
|
||||||
# }
|
|
||||||
# views.inbox.activity_task(activity)
|
|
||||||
# self.assertEqual(shelf.books.first(), book)
|
|
||||||
|
|
||||||
|
|
||||||
@responses.activate
|
@responses.activate
|
||||||
def test_handle_add_book_to_list(self):
|
def test_handle_add_book_to_list(self):
|
||||||
''' listing a book '''
|
''' listing a book '''
|
||||||
|
@ -687,6 +663,49 @@ class Inbox(TestCase):
|
||||||
self.assertEqual(booklist.books.first(), book)
|
self.assertEqual(booklist.books.first(), book)
|
||||||
|
|
||||||
|
|
||||||
|
@responses.activate
|
||||||
|
def test_handle_tag_book(self):
|
||||||
|
''' listing a book '''
|
||||||
|
work = models.Work.objects.create(title='work title')
|
||||||
|
book = models.Edition.objects.create(
|
||||||
|
title='Test', remote_id='https://bookwyrm.social/book/37292',
|
||||||
|
parent_work=work)
|
||||||
|
|
||||||
|
responses.add(
|
||||||
|
responses.GET,
|
||||||
|
'https://www.example.com/tag/cool-tag',
|
||||||
|
json={
|
||||||
|
"id": "https://1b1a78582461.ngrok.io/tag/tag",
|
||||||
|
"type": "OrderedCollection",
|
||||||
|
"totalItems": 0,
|
||||||
|
"first": "https://1b1a78582461.ngrok.io/tag/tag?page=1",
|
||||||
|
"last": "https://1b1a78582461.ngrok.io/tag/tag?page=1",
|
||||||
|
"name": "cool tag",
|
||||||
|
"@context": "https://www.w3.org/ns/activitystreams"
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
activity = {
|
||||||
|
"id": "https://bookwyrm.social/listbook/6189#add",
|
||||||
|
"type": "Add",
|
||||||
|
"actor": "https://example.com/users/rat",
|
||||||
|
"object": {
|
||||||
|
"type": "Edition",
|
||||||
|
"title": "Test Title",
|
||||||
|
"work": work.remote_id,
|
||||||
|
"id": "https://bookwyrm.social/book/37292",
|
||||||
|
},
|
||||||
|
"target": "https://www.example.com/tag/cool-tag",
|
||||||
|
"@context": "https://www.w3.org/ns/activitystreams"
|
||||||
|
}
|
||||||
|
views.inbox.activity_task(activity)
|
||||||
|
|
||||||
|
tag = models.Tag.objects.get()
|
||||||
|
self.assertFalse(models.List.objects.exists())
|
||||||
|
self.assertEqual(tag.name, 'cool tag')
|
||||||
|
self.assertEqual(tag.books.first(), book)
|
||||||
|
|
||||||
|
|
||||||
def test_handle_update_user(self):
|
def test_handle_update_user(self):
|
||||||
''' update an existing user '''
|
''' update an existing user '''
|
||||||
# we only do this with remote users
|
# we only do this with remote users
|
||||||
|
|
|
@ -59,6 +59,21 @@ class TagViews(TestCase):
|
||||||
self.assertEqual(result.status_code, 200)
|
self.assertEqual(result.status_code, 200)
|
||||||
|
|
||||||
|
|
||||||
|
def test_tag_page_activitypub_page(self):
|
||||||
|
''' there are so many views, this just makes sure it LOADS '''
|
||||||
|
view = views.Tag.as_view()
|
||||||
|
with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
|
||||||
|
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('', {'page': 1})
|
||||||
|
with patch('bookwyrm.views.tag.is_api_request') as is_api:
|
||||||
|
is_api.return_value = True
|
||||||
|
result = view(request, tag.identifier)
|
||||||
|
self.assertIsInstance(result, ActivitypubResponse)
|
||||||
|
self.assertEqual(result.status_code, 200)
|
||||||
|
|
||||||
|
|
||||||
def test_tag(self):
|
def test_tag(self):
|
||||||
''' add a tag to a book '''
|
''' add a tag to a book '''
|
||||||
view = views.AddTag.as_view()
|
view = views.AddTag.as_view()
|
||||||
|
|
|
@ -16,12 +16,11 @@ class Tag(View):
|
||||||
''' tag page '''
|
''' tag page '''
|
||||||
def get(self, request, tag_id):
|
def get(self, request, tag_id):
|
||||||
''' see books related to a tag '''
|
''' see books related to a tag '''
|
||||||
tag_obj = models.Tag.objects.filter(identifier=tag_id).first()
|
tag_obj = get_object_or_404(models.Tag, identifier=tag_id)
|
||||||
if not tag_obj:
|
|
||||||
return HttpResponseNotFound()
|
|
||||||
|
|
||||||
if is_api_request(request):
|
if is_api_request(request):
|
||||||
return ActivitypubResponse(tag_obj.to_activity(**request.GET))
|
return ActivitypubResponse(
|
||||||
|
tag_obj.to_activity(**request.GET), safe=False)
|
||||||
|
|
||||||
books = models.Edition.objects.filter(
|
books = models.Edition.objects.filter(
|
||||||
usertag__tag__identifier=tag_id
|
usertag__tag__identifier=tag_id
|
||||||
|
|
Loading…
Reference in New Issue