mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2025-01-03 05:48:44 +00:00
Adds model and beginning of boilerplate for tags
This commit is contained in:
parent
9c645c6ab6
commit
13b512b569
8 changed files with 57 additions and 3 deletions
|
@ -54,3 +54,9 @@ class EditUserForm(ModelForm):
|
||||||
fields = ['avatar', 'name', 'summary']
|
fields = ['avatar', 'name', 'summary']
|
||||||
help_texts = {f: None for f in fields}
|
help_texts = {f: None for f in fields}
|
||||||
|
|
||||||
|
class TagForm(ModelForm):
|
||||||
|
class Meta:
|
||||||
|
model = models.Tag
|
||||||
|
fields = ['name']
|
||||||
|
help_texts = {f: None for f in fields}
|
||||||
|
|
||||||
|
|
|
@ -47,6 +47,13 @@ class Review(Status):
|
||||||
super().save(*args, **kwargs)
|
super().save(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
class Tag(FedireadsModel):
|
||||||
|
''' freeform tags for books '''
|
||||||
|
users = models.ManyToManyField('User')
|
||||||
|
books = models.ManyToManyField('Book')
|
||||||
|
name = models.CharField(max_length=140, unique=True)
|
||||||
|
|
||||||
|
|
||||||
class Favorite(FedireadsModel):
|
class Favorite(FedireadsModel):
|
||||||
''' fav'ing a post '''
|
''' fav'ing a post '''
|
||||||
user = models.ForeignKey('User', on_delete=models.PROTECT)
|
user = models.ForeignKey('User', on_delete=models.PROTECT)
|
||||||
|
|
|
@ -7,7 +7,7 @@ from urllib.parse import urlencode
|
||||||
|
|
||||||
from fedireads import activitypub
|
from fedireads import activitypub
|
||||||
from fedireads import models
|
from fedireads import models
|
||||||
from fedireads.status import create_review, create_status
|
from fedireads.status import create_review, create_status, create_tag
|
||||||
from fedireads.remote_user import get_or_create_remote_user
|
from fedireads.remote_user import get_or_create_remote_user
|
||||||
from fedireads.broadcast import get_recipients, broadcast
|
from fedireads.broadcast import get_recipients, broadcast
|
||||||
|
|
||||||
|
@ -160,6 +160,11 @@ def handle_review(user, book, name, content, rating):
|
||||||
other_recipients = get_recipients(user, 'public', limit='other')
|
other_recipients = get_recipients(user, 'public', limit='other')
|
||||||
broadcast(user, article_create_activity, other_recipients)
|
broadcast(user, article_create_activity, other_recipients)
|
||||||
|
|
||||||
|
def handle_tag(user, book, name):
|
||||||
|
tag = create_tag(user, book, name)
|
||||||
|
|
||||||
|
tag_activity = activitypub.get_tag(tag)
|
||||||
|
book_object = activitypub.get_book(book)
|
||||||
|
|
||||||
def handle_comment(user, review, content):
|
def handle_comment(user, review, content):
|
||||||
''' respond to a review or status '''
|
''' respond to a review or status '''
|
||||||
|
|
|
@ -45,6 +45,21 @@ def create_status(user, content, reply_parent=None, mention_books=None):
|
||||||
return status
|
return status
|
||||||
|
|
||||||
|
|
||||||
|
def create_tag(user, possible_book, name):
|
||||||
|
''' add a tag to a book '''
|
||||||
|
book = get_or_create_book(possible_book)
|
||||||
|
|
||||||
|
try:
|
||||||
|
# check for an existing tag with this text
|
||||||
|
tag = models.Tag.objects.get(name=name)
|
||||||
|
except models.Tag.DoesNotExist():
|
||||||
|
# create a new one if there isn't an existing one
|
||||||
|
tag = models.Tag.objects.create(name=name)
|
||||||
|
tag.users.add(user)
|
||||||
|
tag.books.add(book)
|
||||||
|
return tag
|
||||||
|
|
||||||
|
|
||||||
def sanitize(content):
|
def sanitize(content):
|
||||||
''' remove invalid html from free text '''
|
''' remove invalid html from free text '''
|
||||||
parser = InputHtmlParser()
|
parser = InputHtmlParser()
|
||||||
|
|
|
@ -12,6 +12,12 @@
|
||||||
{% if not reviews %}
|
{% if not reviews %}
|
||||||
<p>No reviews yet!</p>
|
<p>No reviews yet!</p>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
<form class="tag-form" name="tag" action="/tag/" method="post">
|
||||||
|
{% csrf_token %}
|
||||||
|
<input type="hidden" name="book" value="{{ book.openlibrary_key }}"></input>
|
||||||
|
{{ tag_form.as_p }}
|
||||||
|
<button type="submit">Add tag</button>
|
||||||
|
</form>
|
||||||
<form class="review-form" name="review" action="/review/" method="post">
|
<form class="review-form" name="review" action="/review/" method="post">
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
<input type="hidden" name="book" value="{{ book.openlibrary_key }}"></input>
|
<input type="hidden" name="book" value="{{ book.openlibrary_key }}"></input>
|
||||||
|
|
|
@ -51,6 +51,7 @@ urlpatterns = [
|
||||||
|
|
||||||
# internal action endpoints
|
# internal action endpoints
|
||||||
re_path(r'^review/?$', views.review),
|
re_path(r'^review/?$', views.review),
|
||||||
|
re_path(r'^tag/?$', views.tag),
|
||||||
re_path(r'^comment/?$', views.comment),
|
re_path(r'^comment/?$', views.comment),
|
||||||
re_path(r'^favorite/(?P<status_id>\d+)/?$', views.favorite),
|
re_path(r'^favorite/(?P<status_id>\d+)/?$', views.favorite),
|
||||||
re_path(
|
re_path(
|
||||||
|
|
|
@ -11,7 +11,7 @@ class FedireadsModel(models.Model):
|
||||||
def absolute_id(self):
|
def absolute_id(self):
|
||||||
''' constructs the absolute reference to any db object '''
|
''' constructs the absolute reference to any db object '''
|
||||||
base_path = 'https://%s' % DOMAIN
|
base_path = 'https://%s' % DOMAIN
|
||||||
if self.user:
|
if hasattr(self, 'user'):
|
||||||
base_path = self.user.absolute_id
|
base_path = self.user.absolute_id
|
||||||
model_name = type(self).__name__.lower()
|
model_name = type(self).__name__.lower()
|
||||||
return '%s/%s/%d' % (base_path, model_name, self.id)
|
return '%s/%s/%d' % (base_path, model_name, self.id)
|
||||||
|
|
|
@ -50,7 +50,8 @@ def home_tab(request, tab):
|
||||||
if tab == 'home':
|
if tab == 'home':
|
||||||
# people you follow and direct mentions
|
# people you follow and direct mentions
|
||||||
activities = activities.filter(
|
activities = activities.filter(
|
||||||
Q(user__in=following, privacy='public') | Q(mention_users=request.user)
|
Q(user__in=following, privacy='public') | \
|
||||||
|
Q(mention_users=request.user)
|
||||||
)
|
)
|
||||||
elif tab == 'local':
|
elif tab == 'local':
|
||||||
# everyone on this instance
|
# everyone on this instance
|
||||||
|
@ -272,6 +273,19 @@ def review(request):
|
||||||
return redirect('/book/%s' % book_identifier)
|
return redirect('/book/%s' % book_identifier)
|
||||||
|
|
||||||
|
|
||||||
|
@login_required
|
||||||
|
def tag(request):
|
||||||
|
''' tag a book '''
|
||||||
|
form = forms.ReviewForm(request.POST)
|
||||||
|
book_identifier = request.POST.get('book')
|
||||||
|
if not form.is_valid():
|
||||||
|
return redirect('/book/%s' % book_identifier)
|
||||||
|
|
||||||
|
name = form.data.get('name')
|
||||||
|
outgoing.handle_tag(request.user, book_identifier, name)
|
||||||
|
return redirect('/book/%s' % book_identifier)
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
def comment(request):
|
def comment(request):
|
||||||
''' respond to a book review '''
|
''' respond to a book review '''
|
||||||
|
|
Loading…
Reference in a new issue