forked from mirrors/bookwyrm
f4008eb8c8
Fixes #70
80 lines
2.1 KiB
Python
80 lines
2.1 KiB
Python
''' Handle user activity '''
|
|
from fedireads import models
|
|
from fedireads.books_manager import get_or_create_book
|
|
from fedireads.sanitize_html import InputHtmlParser
|
|
from django.db import IntegrityError
|
|
|
|
|
|
def create_review(user, possible_book, name, content, rating, published):
|
|
''' a book review has been added '''
|
|
# throws a value error if the book is not found
|
|
book = get_or_create_book(possible_book)
|
|
|
|
content = sanitize(content)
|
|
|
|
# no ratings outside of 0-5
|
|
rating = rating if 0 <= rating <= 5 else 0
|
|
|
|
review = models.Review(
|
|
user=user,
|
|
book=book,
|
|
name=name,
|
|
rating=rating,
|
|
content=content,
|
|
)
|
|
if published:
|
|
review.published_date = published
|
|
review.save()
|
|
return review
|
|
|
|
|
|
def create_status(user, content, reply_parent=None, mention_books=None):
|
|
''' a status update '''
|
|
# TODO: handle @'ing users
|
|
|
|
# sanitize input html
|
|
parser = InputHtmlParser()
|
|
parser.feed(content)
|
|
content = parser.get_output()
|
|
|
|
status = models.Status.objects.create(
|
|
user=user,
|
|
content=content,
|
|
reply_parent=reply_parent,
|
|
)
|
|
|
|
if mention_books:
|
|
for book in mention_books:
|
|
status.mention_books.add(book)
|
|
|
|
return status
|
|
|
|
|
|
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
|
|
|
|
|
|
def create_notification(user, notification_type, related_user=None, \
|
|
related_book=None, related_status=None):
|
|
''' let a user know when someone interacts with their content '''
|
|
models.Notification.objects.create(
|
|
user=user,
|
|
related_book=related_book,
|
|
related_user=related_user,
|
|
related_status=related_status,
|
|
notification_type=notification_type,
|
|
)
|
|
|
|
|
|
def sanitize(content):
|
|
''' remove invalid html from free text '''
|
|
parser = InputHtmlParser()
|
|
parser.feed(content)
|
|
return parser.get_output()
|