Skip to content

Commit 655bbe9

Browse files
add url reverse; add new view
1 parent 37e0aa1 commit 655bbe9

4 files changed

Lines changed: 60 additions & 5 deletions

File tree

frontend/templates/frontend/content/detail.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,7 @@ <h5><a name="rating">{% trans "Rating" %}</a></h5>
104104
{% for i in 5|rev_range %}
105105
<input type="radio" id="star{{ i }}" name="rating" value="{{ i }}"
106106
{% if i <= user_rate %}class="active" {% endif %}/>
107-
{# TODO Replace with POST action and use proper url reversing #}
108-
<label for="star{{ i }}" onclick="window.location.href ='rate/{{ i }}/#rating'"></label>
107+
<label for="star{{ i }}" onclick="window.location.href='{% url 'frontend:rating' course.id topic.id content.id i %}'"></label>
109108
{% endfor %}
110109
</div>
111110

frontend/urls.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
path('<int:course_id>/topic/<int:topic_id>/content/', include([
2323
path('<int:content_id>/comment/<int:pk>/delete/', views.DeleteComment.as_view(), name='comment-delete'),
2424
path('<int:content_id>/comment/<int:pk>/edit/', views.EditComment.as_view(), name='comment-edit'),
25+
path('<int:content_id>/rate/<int:pk>/', views.rate_content, name='rating'),
26+
2527
path('<pk>/', views.ContentView.as_view(), name='content'),
2628
])),
2729
path('add/', views.AddCourseView.as_view(), name='add-course'),

frontend/views/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
from .course import CourseView, AddCourseView, CourseDeleteView
55
from .content import ContentView
66
from .comment import EditComment, DeleteComment
7+
from .content import RateContentView, rate_content

frontend/views/content.py

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
from django.utils.translation import gettext_lazy as _
66
from django.views.generic import DetailView
77

8-
from base.models import Content, Comment, Course, Topic, Favorite
8+
from base.models import Content, Comment, Course, Topic, Favorite, Rating, Profile
9+
from base.utils import get_user
910
from frontend.forms import CommentForm, TranslateForm
1011

1112

@@ -15,7 +16,8 @@ class ContentView(DetailView): # pylint: disable=too-many-ancestors
1516
"""
1617
model = Content
1718
template_name = "frontend/content/detail.html"
18-
#form_class = Comment
19+
20+
# form_class = Comment
1921

2022
def post(self, request, *args, **kwargs): # pylint: disable=unused-argument
2123
"""
@@ -147,7 +149,8 @@ def get_context_data(self, **kwargs):
147149
topic = Topic.objects.get(pk=topic_id)
148150
if self.request.GET.get('coursebook'):
149151
course = get_object_or_404(Course, {"pk": self.kwargs['course_id']})
150-
contents = [f.content for f in Favorite.objects.filter(course=course, user=self.request.user.profile)] #models.get_coursebook_flat(get_user(self.request), course)
152+
contents = [f.content for f in Favorite.objects.filter(course=course,
153+
user=self.request.user.profile)] # models.get_coursebook_flat(get_user(self.request), course)
151154
else:
152155
contents = topic.get_contents(self.request.GET.get('s'), self.request.GET.get('f'))
153156

@@ -169,3 +172,53 @@ def get_context_data(self, **kwargs):
169172
context['ending'] = '?s=' + self.request.GET.get('s') + "&f=" + \
170173
self.request.GET.get('f')
171174
return context
175+
176+
177+
class RateContentView(DetailView):
178+
model = Content
179+
180+
#def get_context_data(self, **kwargs):
181+
#None
182+
183+
#def dispatch(self, request, *args, **kwargs):
184+
#None
185+
186+
def post(self, request, *args, **kwargs): # pylint: disable=unused-argument
187+
#None
188+
form = RateForm(request.POST)
189+
if form.is_valid():
190+
post = form.save(commit=False)
191+
192+
193+
#def get(self, request, *args, **kwargs):
194+
#form = RateForm()
195+
196+
197+
def rate_content(request, course_id, topic_id, content_id, pk):
198+
"""
199+
Let's the user rate content
200+
:param int topic_id: id of the topic
201+
:param HttpRequest request: request
202+
:param int course_id: course id
203+
:param int content_id: id of the content which gets rated
204+
:param int pk: the user rating (should be in [ 1, 2, 3, 4, 5])
205+
:return: redirect to content page
206+
:rtype: HttpResponse
207+
"""
208+
# check if rating is valid
209+
rating = pk
210+
print(course_id,topic_id,content_id,rating)
211+
print(type(get_user(request)))
212+
content = get_object_or_404(Content, pk=content_id)
213+
#print(Content.ratings.filter(user_id=user.pk, content_id=content.pk))
214+
profile = get_user(request)
215+
Rating.objects.filter(user_id=profile, content_id=content_id).delete()
216+
rating_obj = Rating.objects.create(user=profile, content=content, rating=rating) # user = profile
217+
content.ratings.add(rating_obj)
218+
# check if content already has rating
219+
#content.rate_content()
220+
content.save()
221+
222+
return HttpResponseRedirect(
223+
reverse_lazy('frontend:content', args=(course_id, topic_id, content_id,))
224+
+ '#rating')

0 commit comments

Comments
 (0)