Example #1
0
 def get_book_from_title_author(self):
     ''' search by title and author '''
     search_term = construct_search_term(self.line['Title'],
                                         self.line['Author'])
     search_results = books_manager.search(search_term)
     if search_results:
         return books_manager.get_or_create_book(search_results[0].key)
Example #2
0
def edit_book_page(request, book_identifier):
    ''' info about a book '''
    book = books_manager.get_or_create_book(book_identifier)
    if not book.description:
        book.description = book.parent_work.description
    data = {'book': book, 'form': forms.EditionForm(instance=book)}
    return TemplateResponse(request, 'edit_book.html', data)
Example #3
0
 def get_book_from_title_author(self):
     ''' search by title and author '''
     search_term = construct_search_term(
         self.data['Title'],
         self.data['Author']
     )
     search_result = books_manager.first_search_result(search_term)
     if search_result:
         return books_manager.get_or_create_book(search_result.key)
Example #4
0
def create_tag(user, possible_book, name):
    ''' add a tag to a book '''
    book = get_or_create_book(possible_book)

    try:
        tag = models.Tag.objects.create(name=name, book=book, user=user)
    except IntegrityError:
        return models.Tag.objects.get(name=name, book=book, user=user)
    return tag
Example #5
0
def create_comment(user, possible_book, content):
    ''' a book comment has been added '''
    # throws a value error if the book is not found
    book = get_or_create_book(possible_book)
    content = sanitize(content)

    return models.Comment.objects.create(
        user=user,
        book=book,
        content=content,
    )
Example #6
0
def create_comment_from_activity(author, activity):
    ''' parse an activity json blob into a status '''
    book_id = activity['inReplyToBook']
    book = get_or_create_book(book_id)
    content = activity.get('content')
    published = activity.get('published')
    remote_id = activity['id']

    comment = create_comment(author, book, content)
    comment.published_date = published
    comment.remote_id = remote_id
    comment.save()
    return comment
Example #7
0
def create_quotation(user, possible_book, content, quote):
    ''' a quotation has been added '''
    # throws a value error if the book is not found
    book = get_or_create_book(possible_book)
    content = sanitize(content)
    quote = sanitize(quote)

    return models.Quotation.objects.create(
        user=user,
        book=book,
        content=content,
        quote=quote,
    )
Example #8
0
def rate(request):
    ''' just a star rating for a book '''
    form = forms.RatingForm(request.POST)
    book_identifier = request.POST.get('book')
    # TODO: better failure behavior
    if not form.is_valid():
        return redirect('/book/%s' % book_identifier)

    rating = form.cleaned_data.get('rating')
    # throws a value error if the book is not found
    book = get_or_create_book(book_identifier)

    outgoing.handle_rate(request.user, book, rating)
    return redirect('/book/%s' % book_identifier)
Example #9
0
def create_quotation_from_activity(author, activity):
    ''' parse an activity json blob into a status '''
    book_id = activity['inReplyToBook']
    book = get_or_create_book(book_id)
    quote = activity.get('quote')
    content = activity.get('content')
    published = activity.get('published')
    remote_id = activity['id']

    quotation = create_quotation(author, book, content, quote)
    quotation.published_date = published
    quotation.remote_id = remote_id
    quotation.save()
    return quotation
Example #10
0
def create_review_from_activity(author, activity):
    ''' parse an activity json blob into a status '''
    book_id = activity['inReplyToBook']
    book = get_or_create_book(book_id)
    name = activity.get('name')
    rating = activity.get('rating')
    content = activity.get('content')
    published = activity.get('published')
    remote_id = activity['id']

    review = create_review(author, book, name, content, rating)
    review.published_date = published
    review.remote_id = remote_id
    review.save()
    return review
Example #11
0
def review(request):
    ''' create a book review '''
    form = forms.ReviewForm(request.POST)
    book_identifier = request.POST.get('book')
    # TODO: better failure behavior
    if not form.is_valid():
        return redirect('/book/%s' % book_identifier)

    # TODO: validation, htmlification
    name = form.cleaned_data.get('name')
    content = form.cleaned_data.get('content')
    rating = form.data.get('rating', None)
    try:
        rating = int(rating)
    except ValueError:
        rating = None

    # throws a value error if the book is not found
    book = get_or_create_book(book_identifier)

    outgoing.handle_review(request.user, book, name, content, rating)
    return redirect('/book/%s' % book_identifier)
Example #12
0
 def get_book_from_isbn(self):
     ''' search by isbn '''
     search_results = books_manager.search(self.isbn)
     if search_results:
         return books_manager.get_or_create_book(search_results[0].key)
Example #13
0
def book_page(request, book_identifier, tab='friends'):
    ''' info about a book '''
    book = books_manager.get_or_create_book(book_identifier)

    if is_api_request(request):
        return JsonResponse(activitypub.get_book(book))

    if isinstance(book, models.Work):
        book = book.default_edition
    if not book:
        return HttpResponseNotFound()

    work = book.parent_work
    if not work:
        return HttpResponseNotFound()

    book_reviews = models.Review.objects.filter(
        book__in=work.edition_set.all())

    if request.user.is_authenticated:
        user_reviews = book_reviews.filter(user=request.user, ).all()

        reviews = get_activity_feed(request.user, tab, model=book_reviews)

        try:
            # TODO: books can be on multiple shelves
            shelf = models.Shelf.objects.filter(user=request.user,
                                                edition=book).first()
        except models.Shelf.DoesNotExist:
            shelf = None

        user_tags = models.Tag.objects.filter(
            book=book, user=request.user).values_list('identifier', flat=True)
    else:
        tab = 'public'
        reviews = book_reviews.filter(privacy='public')
        shelf = None
        user_reviews = []
        user_tags = []

    rating = reviews.aggregate(Avg('rating'))
    tags = models.Tag.objects.filter(book=book).values(
        'book', 'name', 'identifier').distinct().all()

    data = {
        'book':
        book,
        'shelf':
        shelf,
        'user_reviews':
        user_reviews,
        'reviews':
        reviews.distinct(),
        'rating':
        rating['rating__avg'],
        'tags':
        tags,
        'user_tags':
        user_tags,
        'review_form':
        forms.ReviewForm(),
        'tag_form':
        forms.TagForm(),
        'feed_tabs': [{
            'id': 'friends',
            'display': 'Friends'
        }, {
            'id': 'local',
            'display': 'Local'
        }, {
            'id': 'federated',
            'display': 'Federated'
        }],
        'active_tab':
        tab,
        'path':
        '/book/%s' % book_identifier,
        'cover_form':
        forms.CoverForm(instance=book),
        'info_fields': [
            {
                'name': 'ISBN',
                'value': book.isbn
            },
            {
                'name': 'OCLC number',
                'value': book.oclc_number
            },
            {
                'name': 'OpenLibrary ID',
                'value': book.openlibrary_key
            },
            {
                'name': 'Goodreads ID',
                'value': book.goodreads_key
            },
            {
                'name': 'Format',
                'value': book.physical_format
            },
            {
                'name': 'Pages',
                'value': book.pages
            },
        ],
    }
    return TemplateResponse(request, 'book.html', data)
Example #14
0
def resolve_book(request):
    ''' figure out the local path to a book from a remote_id '''
    remote_id = request.POST.get('remote_id')
    book = books_manager.get_or_create_book(remote_id)
    return redirect('/book/%d' % book.id)
Example #15
0
 def get_book_from_isbn(self):
     ''' search by isbn '''
     search_result = books_manager.first_search_result(self.isbn)
     if search_result:
         return books_manager.get_or_create_book(search_result.key)
Example #16
0
User.objects.create_user('mouse', '*****@*****.**', 'password123')
User.objects.create_user(
    'rat', '*****@*****.**', 'ratword',
    manually_approves_followers=True
)

User.objects.get(id=1).followers.add(User.objects.get(id=2))

Connector.objects.create(
    identifier='openlibrary.org',
    connector_file='openlibrary',
    base_url='https://openlibrary.org',
    covers_url='https://covers.openlibrary.org',
    search_url='https://openlibrary.org/search?q=',
    key_name='openlibrary_key',
)

Connector.objects.create(
    identifier=DOMAIN,
    connector_file='self_connector',
    base_url='https://%s/book' % DOMAIN,
    covers_url='https://%s/images/covers' % DOMAIN,
    search_url='https://%s/search?q=' % DOMAIN,
    key_name='openlibrary_key',
)


get_or_create_book('OL1715344W')
get_or_create_book('OL102749W')