mirror of
https://github.com/codingforentrepreneurs/DjangoFlix.git
synced 2022-03-27 20:26:53 +03:00
55 - Ratings Form
This commit is contained in:
@@ -26,6 +26,8 @@ from playlists.views import (
|
||||
FeaturedPlaylistListView
|
||||
)
|
||||
|
||||
from ratings.views import rate_object_view
|
||||
|
||||
'''
|
||||
str - everything but /
|
||||
int - 0 and up
|
||||
@@ -51,4 +53,5 @@ urlpatterns = [
|
||||
path('shows/<slug:slug>/', TVShowDetailView.as_view()),
|
||||
path('shows/', TVShowListView.as_view()),
|
||||
path('tags/', include('tags.urls')),
|
||||
path('object-rate/', rate_object_view)
|
||||
]
|
||||
|
||||
10
src/ratings/forms.py
Normal file
10
src/ratings/forms.py
Normal file
@@ -0,0 +1,10 @@
|
||||
from django import forms
|
||||
|
||||
|
||||
from .models import RatingChoices
|
||||
|
||||
class RatingForm(forms.Form):
|
||||
rating = forms.ChoiceField(label='Rate', choices=RatingChoices.choices)
|
||||
object_id = forms.IntegerField(widget=forms.HiddenInput)
|
||||
content_type_id = forms.IntegerField(widget=forms.HiddenInput)
|
||||
next = forms.CharField(widget=forms.HiddenInput)
|
||||
@@ -3,6 +3,7 @@ from django.contrib.contenttypes.models import ContentType
|
||||
from django.contrib.contenttypes.fields import GenericForeignKey
|
||||
from django.db import models
|
||||
from django.db.models import Avg
|
||||
from django.db.models.signals import post_save
|
||||
|
||||
User = settings.AUTH_USER_MODEL # "auth.User"
|
||||
|
||||
@@ -12,7 +13,7 @@ class RatingChoices(models.IntegerChoices):
|
||||
THREE = 3
|
||||
FOUR = 4
|
||||
FIVE = 5
|
||||
__empty__ = 'Unknown'
|
||||
__empty__ = 'Rate this'
|
||||
|
||||
class RatingQuerySet(models.QuerySet):
|
||||
def rating(self):
|
||||
@@ -31,4 +32,17 @@ class Rating(models.Model):
|
||||
object_id = models.PositiveIntegerField()
|
||||
content_object = GenericForeignKey("content_type", "object_id")
|
||||
|
||||
objects = RatingManager()
|
||||
objects = RatingManager()
|
||||
|
||||
|
||||
def rating_post_save(sender, instance, created, *args, **kwargs):
|
||||
if created:
|
||||
# trigger new content_object calculation
|
||||
content_type = instance.content_type
|
||||
user = instance.user
|
||||
qs = Rating.objects.filter(user=user, content_type=content_type, object_id=instance.object_id).exclude(pk=instance.pk)
|
||||
if qs.exists():
|
||||
qs.delete()
|
||||
|
||||
|
||||
post_save.connect(rating_post_save, sender=Rating)
|
||||
@@ -1 +1,7 @@
|
||||
Rating: {% if value %}{{ value }}{% else %}~{% endif %}
|
||||
Rating: {% if value %}{{ value }}{% else %}~{% endif %}
|
||||
{% if form %}
|
||||
<form method='POST' action='/object-rate/'>{% csrf_token %}
|
||||
{{ form.as_p }}
|
||||
<input type='submit' value='Rate' />
|
||||
</form>
|
||||
{% endif %}
|
||||
@@ -1,6 +1,7 @@
|
||||
from django import template
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
|
||||
from ratings.forms import RatingForm
|
||||
from ratings.models import Rating
|
||||
|
||||
register = template.Library()
|
||||
@@ -17,16 +18,27 @@ def rating(context, *args, **kwargs):
|
||||
user = None
|
||||
if request.user.is_authenticated:
|
||||
user = request.user
|
||||
|
||||
app_label = obj._meta.app_label # playlists
|
||||
model_name = obj._meta.model_name # Playlist
|
||||
model_name = obj._meta.model_name
|
||||
if app_label == "playlists":
|
||||
if model_name == 'movieproxy' or 'tvshowproxy':
|
||||
model_name = 'playlist'
|
||||
c_type = ContentType.objects.get(app_label=app_label,model=model_name)
|
||||
avg_rating = Rating.objects.filter(content_type=c_type, object_id=obj.id).rating()
|
||||
context = {
|
||||
'value': avg_rating,
|
||||
'form': None
|
||||
}
|
||||
|
||||
display_form = False
|
||||
if user is not None:
|
||||
display_form = True
|
||||
if rating_only is True:
|
||||
display_form = False
|
||||
return {
|
||||
"value": avg_rating
|
||||
}
|
||||
if display_form:
|
||||
context['form'] = RatingForm(initial={
|
||||
"object_id": obj.id,
|
||||
"content_type_id": c_type.id,
|
||||
"next": request.path,
|
||||
})
|
||||
return context
|
||||
@@ -1,3 +1,26 @@
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.shortcuts import render
|
||||
|
||||
# Create your views here.
|
||||
from .forms import RatingForm
|
||||
from .models import Rating
|
||||
|
||||
def rate_object_view(request):
|
||||
if not request.user.is_authenticated:
|
||||
return HttpResponseRedirect('/')
|
||||
if request.method == "POST":
|
||||
form = RatingForm(request.POST)
|
||||
if form.is_valid():
|
||||
object_id = form.cleaned_data.get('object_id')
|
||||
rating = form.cleaned_data.get('rating')
|
||||
content_type_id = form.cleaned_data.get('content_type_id')
|
||||
c_type = ContentType.objects.get_for_id(content_type_id)
|
||||
obj = Rating.objects.create(
|
||||
content_type=c_type,
|
||||
object_id=object_id,
|
||||
value=rating,
|
||||
user=request.user
|
||||
)
|
||||
next_path = form.cleaned_data.get('next') # detail view
|
||||
return HttpResponseRedirect(next_path)
|
||||
return HttpResponseRedirect('/')
|
||||
Reference in New Issue
Block a user