Adds ratings
This commit is contained in:
parent
f7cb3d9444
commit
a27effd05d
|
@ -26,6 +26,12 @@ class RegisterForm(ModelForm):
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class RatingForm(ModelForm):
|
||||||
|
class Meta:
|
||||||
|
model = models.Review
|
||||||
|
fields = ['rating']
|
||||||
|
|
||||||
|
|
||||||
class ReviewForm(ModelForm):
|
class ReviewForm(ModelForm):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = models.Review
|
model = models.Review
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
# Generated by Django 3.0.3 on 2020-04-03 18:35
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('fedireads', '0028_auto_20200401_1824'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='review',
|
||||||
|
name='name',
|
||||||
|
field=models.CharField(max_length=255, null=True),
|
||||||
|
),
|
||||||
|
]
|
|
@ -58,7 +58,7 @@ class Comment(Status):
|
||||||
|
|
||||||
class Review(Status):
|
class Review(Status):
|
||||||
''' a book review '''
|
''' a book review '''
|
||||||
name = models.CharField(max_length=255)
|
name = models.CharField(max_length=255, null=True)
|
||||||
book = models.ForeignKey('Edition', on_delete=models.PROTECT)
|
book = models.ForeignKey('Edition', on_delete=models.PROTECT)
|
||||||
rating = models.IntegerField(
|
rating = models.IntegerField(
|
||||||
default=None,
|
default=None,
|
||||||
|
|
|
@ -10,7 +10,7 @@ from fedireads import activitypub
|
||||||
from fedireads import models
|
from fedireads import models
|
||||||
from fedireads.broadcast import get_recipients, broadcast
|
from fedireads.broadcast import get_recipients, broadcast
|
||||||
from fedireads.status import create_review, create_status, create_comment
|
from fedireads.status import create_review, create_status, create_comment
|
||||||
from fedireads.status import create_tag, create_notification
|
from fedireads.status import create_tag, create_notification, create_rating
|
||||||
from fedireads.remote_user import get_or_create_remote_user
|
from fedireads.remote_user import get_or_create_remote_user
|
||||||
|
|
||||||
|
|
||||||
|
@ -188,6 +188,12 @@ def handle_import_books(user, items):
|
||||||
broadcast(user, create_activity, recipients)
|
broadcast(user, create_activity, recipients)
|
||||||
|
|
||||||
|
|
||||||
|
def handle_rate(user, book, rating):
|
||||||
|
''' a review that's just a rating '''
|
||||||
|
review = create_rating(user, book, rating)
|
||||||
|
# TODO: serialize and broadcast
|
||||||
|
|
||||||
|
|
||||||
def handle_review(user, book, name, content, rating):
|
def handle_review(user, book, name, content, rating):
|
||||||
''' post a review '''
|
''' post a review '''
|
||||||
# validated and saves the review in the database so it has an id
|
# validated and saves the review in the database so it has an id
|
||||||
|
|
|
@ -314,6 +314,34 @@ button .icon {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* star ratings */
|
||||||
|
.stars {
|
||||||
|
letter-spacing: -0.15em;
|
||||||
|
display: inline-block;
|
||||||
|
}
|
||||||
|
.rate-stars .icon {
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
.rate-stars form {
|
||||||
|
display: inline;
|
||||||
|
width: min-content;
|
||||||
|
}
|
||||||
|
.rate-stars form button.icon {
|
||||||
|
background: none;
|
||||||
|
border: none;
|
||||||
|
padding: 0;
|
||||||
|
margin: 0;
|
||||||
|
color: black;
|
||||||
|
}
|
||||||
|
.rate-stars form > .icon:before {
|
||||||
|
content: '\e9d7';
|
||||||
|
}
|
||||||
|
.rate-stars:hover .icon:before {
|
||||||
|
content: '\e9d9';
|
||||||
|
}
|
||||||
|
.rate-stars form:hover ~ form .icon:before{
|
||||||
|
content: '\e9d7';
|
||||||
|
}
|
||||||
|
|
||||||
/* re-usable tab styles */
|
/* re-usable tab styles */
|
||||||
.tabs {
|
.tabs {
|
||||||
|
@ -406,6 +434,20 @@ button .icon {
|
||||||
margin-bottom: 1em;
|
margin-bottom: 1em;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
dl {
|
||||||
|
font-size: 0.9em;
|
||||||
|
margin-top: 0.5em;
|
||||||
|
}
|
||||||
|
dt {
|
||||||
|
float: left;
|
||||||
|
margin-right: 0.5em;
|
||||||
|
}
|
||||||
|
dd {
|
||||||
|
margin-bottom: 0.25em;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
.all-shelves {
|
.all-shelves {
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: row;
|
flex-direction: row;
|
||||||
|
|
|
@ -25,6 +25,17 @@ def create_review_from_activity(author, activity):
|
||||||
return review
|
return review
|
||||||
|
|
||||||
|
|
||||||
|
def create_rating(user, book, rating):
|
||||||
|
''' a review that's just a rating '''
|
||||||
|
if not rating or rating < 1 or rating > 5:
|
||||||
|
raise ValueError('Invalid rating')
|
||||||
|
return models.Review.objects.create(
|
||||||
|
user=user,
|
||||||
|
book=book,
|
||||||
|
rating=rating,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def create_review(user, book, name, content, rating):
|
def create_review(user, book, name, content, rating):
|
||||||
''' a book review has been added '''
|
''' a book review has been added '''
|
||||||
name = sanitize(name)
|
name = sanitize(name)
|
||||||
|
|
|
@ -40,7 +40,8 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="column">
|
<div class="column">
|
||||||
<h3>{{ active_tab }} rating: {{ rating | stars }}</h3>
|
{% include 'snippets/rate_action.html' with user=request.user book=book %}
|
||||||
|
<h3>{{ active_tab }} rating: {% include 'snippets/stars.html' with rating=rating %}</h3>
|
||||||
|
|
||||||
{% include 'snippets/book_description.html' %}
|
{% include 'snippets/book_description.html' %}
|
||||||
|
|
||||||
|
|
|
@ -2,11 +2,22 @@
|
||||||
{% load humanize %}
|
{% load humanize %}
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<div class="content-container">
|
<div class="content-container">
|
||||||
<h2>Edit "{{ book.title }}"</h2>
|
<h2>
|
||||||
<div class="book-preview">
|
Edit "{{ book.title }}"
|
||||||
{% include 'snippets/book_cover.html' with book=book size="small" %}
|
<a href="/book/{{ book.fedireads_key }}">
|
||||||
<p>Added: {{ book.created_date | naturaltime }}</p>
|
<span class="edit-link icon icon-close">
|
||||||
<p>Updated: {{ book.updated_date | naturaltime }}</p>
|
<span class="hidden-text">Close</span>
|
||||||
|
</span>
|
||||||
|
</a>
|
||||||
|
</h2>
|
||||||
|
<div class="book-preview row">
|
||||||
|
<div class="cover-container">
|
||||||
|
{% include 'snippets/book_cover.html' with book=book size="small" %}
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<p>Added: {{ book.created_date | naturaltime }}</p>
|
||||||
|
<p>Updated: {{ book.updated_date | naturaltime }}</p>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
@ -54,7 +65,6 @@
|
||||||
<p><label for="id_first_published_date">First published date:</label> {{ form.first_published_date }} </p>
|
<p><label for="id_first_published_date">First published date:</label> {{ form.first_published_date }} </p>
|
||||||
<p><label for="id_published_date">Published date:</label> {{ form.published_date }} </p>
|
<p><label for="id_published_date">Published date:</label> {{ form.published_date }} </p>
|
||||||
</div>
|
</div>
|
||||||
<button type="submit">Update book</button>
|
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
{% load fr_display %}
|
||||||
|
<span class="hidden-text">Leave a rating</span>
|
||||||
|
<div class="stars rate-stars">
|
||||||
|
{% for i in '12345'|make_list %}
|
||||||
|
<form name="rate" action="/rate/" method="POST" onsubmit="interact(event)">
|
||||||
|
{% csrf_token %}
|
||||||
|
<input type="hidden" name="book" value="{{ book.fedireads_key }}"></input>
|
||||||
|
<input type="hidden" name="rating" value="{{ forloop.counter }}"></input>
|
||||||
|
<button type="submit" class="icon icon-star-{% if book|rating:user < forloop.counter %}empty{% else %}full{% endif %}">
|
||||||
|
<span class="hidden-text">{{ forloop.counter }} star{{ forloop.counter | pluralize }}</span>
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
|
@ -0,0 +1,9 @@
|
||||||
|
<span class="hidden-text">Leave a rating</span>
|
||||||
|
<div class="stars">
|
||||||
|
{% for i in '12345'|make_list %}
|
||||||
|
<span class="icon icon-star-{% if rating < forloop.counter %}empty{% else %}full{% endif %}">
|
||||||
|
<span class="hidden-text">{{ forloop.counter }} star{{ forloop.counter | pluralize }}</span>
|
||||||
|
</span>
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
|
|
@ -1,5 +1,15 @@
|
||||||
{% load fr_display %}
|
{% load fr_display %}
|
||||||
|
|
||||||
|
{% if status.status_type == 'Review' and not status.name or not status.content %}
|
||||||
|
<div class="post rating">
|
||||||
|
<h2>
|
||||||
|
{% include 'snippets/status_header.html' with status=status %}
|
||||||
|
{% include 'snippets/stars.html' with book=book rating=status.rating %}
|
||||||
|
</h2>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{% else %}
|
||||||
|
|
||||||
<div class="post {{ status.status_type | lower }} depth-{{ depth }} {% if main %}main{% else %}reply{% endif %}">
|
<div class="post {{ status.status_type | lower }} depth-{{ depth }} {% if main %}main{% else %}reply{% endif %}">
|
||||||
|
|
||||||
<h2>
|
<h2>
|
||||||
|
@ -22,3 +32,4 @@
|
||||||
{% include 'snippets/interaction.html' with activity=status %}
|
{% include 'snippets/interaction.html' with activity=status %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
|
{% endif %}
|
||||||
|
|
|
@ -5,6 +5,8 @@
|
||||||
|
|
||||||
{% if status.status_type == 'Update' %}
|
{% if status.status_type == 'Update' %}
|
||||||
{{ status.content | safe }}
|
{{ status.content | safe }}
|
||||||
|
{% elif status.status_type == 'Review' and not status.name and not status.content%}
|
||||||
|
rated <a href="{{ status.book.absolute_id }}">{{ status.book.title }}</a>
|
||||||
{% elif status.status_type == 'Review' %}
|
{% elif status.status_type == 'Review' %}
|
||||||
reviewed {{ status.book.title }}
|
reviewed {{ status.book.title }}
|
||||||
{% elif status.status_type == 'Comment' %}
|
{% elif status.status_type == 'Comment' %}
|
||||||
|
|
|
@ -12,16 +12,18 @@ def dict_key(d, k):
|
||||||
return d.get(k) or 0
|
return d.get(k) or 0
|
||||||
|
|
||||||
|
|
||||||
@register.filter(name='stars')
|
@register.filter(name='rating')
|
||||||
def stars(number):
|
def get_rating(book, user):
|
||||||
''' turn integers into stars '''
|
''' get a user's rating of a book '''
|
||||||
try:
|
rating = models.Review.objects.filter(
|
||||||
number = int(number)
|
user=user,
|
||||||
except (ValueError, TypeError):
|
book=book,
|
||||||
number = 0
|
rating__isnull=False,
|
||||||
if not number:
|
).order_by('-published_date').first()
|
||||||
return ''
|
print(rating.rating, '\n\n\n')
|
||||||
return ('★' * number) + '☆' * (5 - number)
|
if rating:
|
||||||
|
return rating.rating
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
@register.filter(name='description')
|
@register.filter(name='description')
|
||||||
|
|
|
@ -78,6 +78,7 @@ urlpatterns = [
|
||||||
re_path(r'^edit_book/(?P<book_id>\d+)/?', actions.edit_book),
|
re_path(r'^edit_book/(?P<book_id>\d+)/?', actions.edit_book),
|
||||||
re_path(r'^upload_cover/(?P<book_id>\d+)/?', actions.upload_cover),
|
re_path(r'^upload_cover/(?P<book_id>\d+)/?', actions.upload_cover),
|
||||||
|
|
||||||
|
re_path(r'^rate/?$', actions.rate),
|
||||||
re_path(r'^review/?$', actions.review),
|
re_path(r'^review/?$', actions.review),
|
||||||
re_path(r'^comment/?$', actions.comment),
|
re_path(r'^comment/?$', actions.comment),
|
||||||
re_path(r'^tag/?$', actions.tag),
|
re_path(r'^tag/?$', actions.tag),
|
||||||
|
|
|
@ -186,6 +186,24 @@ def shelve(request):
|
||||||
return redirect('/')
|
return redirect('/')
|
||||||
|
|
||||||
|
|
||||||
|
@login_required
|
||||||
|
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
|
||||||
|
import pdb;pdb.set_trace()
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
def review(request):
|
def review(request):
|
||||||
''' create a book review '''
|
''' create a book review '''
|
||||||
|
|
Loading…
Reference in New Issue