Skip to content

Commit 57275fc

Browse files
add sort rating; implement get_rate method
1 parent e15be7e commit 57275fc

11 files changed

Lines changed: 48 additions & 85 deletions

File tree

base/models/content.py

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from fontawesome_5.fields import IconField
66

77
from base.models import Profile
8+
from .social import Rating
89

910

1011
class Category(models.Model):
@@ -168,17 +169,7 @@ def get_rate_num(self):
168169
return self.get_rate()
169170

170171
def get_rate(self):
171-
"""
172-
return the average rating and 0 if there are no ratings
173-
:return: rating
174-
:rtype: float
175-
"""
176-
177-
rating = self.ratings.aggregate(Avg('rating'))['rating__avg'] # todo something wrong here
178-
print(self.ratings)
179-
if rating is None:
180-
return 0
181-
return round(rating, 2) # pylint: disable=no-member
172+
return Rating.objects.filter(content_id=self.id).aggregate(Avg('rating'))['rating__avg']
182173

183174
def get_rate_count(self):
184175
"""
@@ -207,23 +198,19 @@ def get_user_rate(self, user):
207198
if self.user_already_rated(user):
208199
content_id = self.id
209200
return self.ratings.get(user=user).rating_set.first().rating
210-
#user.get_rating_from_content(content_id).rating # todo may change back to ratings
211201
return 0
212202

213-
def rate_content(self, user):
203+
def rate_content(self, user, rating):
214204
"""
215205
Rate content
206+
:param rating: Rating of Content by User
216207
:param content: Content
217208
:param User user: user
218-
:param int rate: rating
219209
:return: nothing
220210
"""
221-
# todo delete
222-
#self.ratings.get(user=user).delete()
223-
#self.ratings.add(user)
224-
#self.ratings.filter(user_id=user.pk).delete() # pylint: disable=no-member
225-
# create function won't work
226-
#self.ratings.add
211+
Rating.objects.filter(user_id=user.user.id, content_id=self.id).delete()
212+
rating = Rating.objects.create(user=user, content=self, rating=rating) # user = profile
213+
rating.save()
227214
# pylint: disable=no-member
228215
self.save()
229216

base/models/profile.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from django.db.models.signals import post_save
44
from django.dispatch import receiver
55
from django.utils.translation import gettext_lazy as _
6-
from .social import Rating
76

87

98

@@ -16,9 +15,6 @@ class Profile(models.Model):
1615
def __str__(self):
1716
return str(self.user)
1817

19-
def get_rating_from_content(self, content_id):
20-
return Rating.objects.get(user=self, content_id=content_id)
21-
2218

2319
@receiver(post_save, sender=User)
2420
def create_user_profile(sender, instance, created, **kwargs):

frontend/forms/course.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ class FilterAndSortForm(forms.Form):
2525
The form for entering filter and sorting Options
2626
"""
2727

28-
FILTER_CHOICE = [('None', '------')] # + Content.STYLE # TODO Use
29-
SORTING_CHOICE = [('None', '-----'), ('creation_date', 'Date')] # , ('rating', 'Rating')]
28+
FILTER_CHOICE = [('None', '------')] # + Content.STYLE
29+
SORTING_CHOICE = [('None', '-----'), ('creation_date', 'Date'), ('rating', 'Rating')]
3030
filter = forms.CharField(label='Filter by',
3131
widget=forms.Select(choices=FILTER_CHOICE,
3232
attrs={'class': 'form-control',

frontend/templates/frontend/content/detail.html

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
</button>
2020

2121
<div class="dropdown-menu dropdown-menu-right" aria-labelledby="dropdownMenuLink">
22-
<a class="dropdown-item" href="
23-
{# url 'readingmode' course.id topic.id content.id #}{% if ending %}{{ ending }}{% endif %}">{% fa5_icon 'eye' 'far' %}
22+
<a class="dropdown-item" href="{# url 'readingmode' course.id topic.id content.id #}{% if ending %}{{ ending }}{% endif %}">{% fa5_icon 'eye' 'far' %}
2423
Reading Mode</a>
2524
{% if user.is_authenticated %}
2625
<a class="dropdown-item" href="{# url 'favourite_content' course.id topic.id content.id #}?onDetailPage=True">
@@ -95,19 +94,17 @@ <h5 class="modal-title" id="deleteContentModalLabel">{% trans "Delete" %} <i>{{
9594
{% include content.type|content_view %}
9695

9796
<div class="mt-5">
98-
{{ content.description|linebreaks }}
97+
{{ content.description|linebreaks }} <!-- resolves to s -->
9998
</div>
10099
</div>
101-
<h5><a name="rating">{% trans "Rating" %}</a></h5>
102-
<div class="float-right text-right"><a>{% trans "Current" %} {{ content.get_rate }}</a> </div>
100+
<h5><a name="rating">{% trans "Rating" %}</a><span class="badge float-right text-right">{{ content.get_rate }}/5</span></h5>
103101
<div class="starrating risingstar d-flex justify-content-end flex-row-reverse">
104102
{% for i in 5|rev_range %}
105103
<input type="radio" id="star{{ i }}" name="rating" value="{{ i }}"
106104
{% if i <= user_rate %}class="active" {% endif %}/>
107105
<label for="star{{ i }}" onclick="window.location.href='{% url 'frontend:rating' course.id topic.id content.id i %}'"></label>
108106
{% endfor %}
109107
</div>
110-
111108
<h5 style="margin-top: 30px;"><a name="comments">{% trans "Comments" %}</a></h5>
112109
{% if user.is_authenticated %}
113110
<form action="{% url 'frontend:content' course.id topic.id content.id %}" method="post">

frontend/templates/frontend/course/topic_contents.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
</a>
2020
<div class="card-footer">
2121
<span class="badge badge-primary">{{ content.language }}</span>
22+
&nbsp;&middot;&nbsp;
2223
{% if content.tags.count > 0 %}
23-
&nbsp;&middot;&nbsp;
2424
{% for tag in content.tags.all %}
25-
<span class="badge badge-info">{{ tag }}</span>
25+
<span class="badge badge-secondary">{{ tag }}</span>
2626
{% endfor %}
2727
{% endif %}
2828
&nbsp;&middot;&nbsp;
29-
{% trans "Rating" %}: {{ content.get_rate }}
29+
<span class="badge badge-info">{{ content.get_rate}}</span>
3030
</div>
3131
</div>
3232
</div>

frontend/templates/frontend/course/view.html

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,29 @@ <h5 style="margin-top: 20px;">
7373
{% for entry in structure %}
7474
<div style="margin-top: 30px;">
7575
{% with forloop.counter as outer_index %}
76-
<h3 class="text-info">{{ outer_index }}. {{ entry.topic.title }}</h3>
77-
78-
{% with entry.topic.contents.all as topic_contents %}
76+
<div class="float-right text-right">
77+
<form id="filter+sort" class="form-inline" method="post"
78+
action="{% url 'frontend:course' course.id %}">
79+
{% csrf_token %}
80+
{% bootstrap_form form %}
81+
</form>
82+
</div>
83+
<h3 class="text-info">{{ outer_index }}. {{ entry.topic.title }} {% if sorting != "None" %}({{ sorting }}){% endif %}</h3>
84+
{% with entry.topic_contents as topic_contents %}
7985
{% include "frontend/course/topic_contents.html" %}
8086
{% endwith %}
8187
{% if entry.subtopics %}
8288
{% for subtopic in entry.subtopics %}
8389
<div style="margin-top: 15px;">
84-
<h4 class="text-info">{{ outer_index }}.{{ forloop.counter }}. {{ subtopic.title }}</h4>
85-
{% with subtopic.contents.all as topic_contents %}
90+
<div class="float-right text-right">
91+
<form id="filter+sort" class="form-inline" method="post"
92+
action="{% url 'frontend:course' course.id %}">
93+
{% csrf_token %}
94+
{% bootstrap_form form %}
95+
</form>
96+
</div>
97+
<h4 class="text-info">{{ outer_index }}.{{ forloop.counter }}. {{ subtopic.subtopic.title }} {% if sorting != "None" %}({{ sorting }}) {% endif %}</h4>
98+
{% with subtopic.topic_contents as topic_contents %}
8699
{% include "frontend/course/topic_contents.html" %}
87100
{% endwith %}
88101
</div>

frontend/urls.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
path('profile/edit/', views.ProfileEditView.as_view(), name="profile-edit"),
1313

1414
path('courses/', include([
15-
re_path(r'^(?P<sort>date-new|date-old|title-a|title-z)/$', views.CourseListView.as_view(),
16-
name='courses-sort'),
15+
re_path(r'^(?P<sort>date-new|date-old|title-a|title-z)/$',
16+
views.CourseListView.as_view(), name='courses-sort'),
1717
path('', views.CourseListView.as_view(), name='courses'),
1818
path('<int:pk>/', include([
1919
path('', views.CourseView.as_view(), name='course'),

frontend/views/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +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
7+
from .content import rate_content

frontend/views/content.py

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -174,29 +174,9 @@ def get_context_data(self, **kwargs):
174174
return context
175175

176176

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-
197177
def rate_content(request, course_id, topic_id, content_id, pk):
198178
"""
199-
Let's the user rate content
179+
Let the user rate content
200180
:param int topic_id: id of the topic
201181
:param HttpRequest request: request
202182
:param int course_id: course id
@@ -205,26 +185,9 @@ def rate_content(request, course_id, topic_id, content_id, pk):
205185
:return: redirect to content page
206186
:rtype: HttpResponse
207187
"""
208-
# check if rating is valid
209188
content = get_object_or_404(Content, pk=content_id)
210189
profile = get_user(request)
211-
#content.rate_content(request.user, rating)
212-
213-
# create or update rating
214-
Rating.objects.filter(user_id=profile, content_id=content_id).delete()
215-
print("pk: ", pk)
216-
rating = Rating.objects.create(user=profile, content=content, rating=pk) # user = profile
217-
rating.save()
218-
219-
#content.rate_content(user=profile)
220-
221-
# update content rating
222-
#content.rate_content(user=get_user(request), rate=rating)
223-
# profile = get_user(request)
224-
#profile
225-
#content.ratings.filter(user=get_user(request)).delete()
226-
print(content.get_user_rate(get_user(request)))
227-
# content.save()
190+
content.rate_content(user=profile, rating=pk)
228191

229192
return HttpResponseRedirect(
230193
reverse_lazy('frontend:content', args=(course_id, topic_id, content_id,))

frontend/views/course.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,22 @@ def get_context_data(self, **kwargs):
9292
index_split = entry.index.split('/')
9393
# Topic
9494
if len(index_split) == 1:
95-
current_topic = {'topic': entry.topic, 'subtopics': []}
95+
current_topic = {'topic': entry.topic, 'subtopics': [],
96+
'topic_contents': entry.topic.get_contents(self.sorted_by, self.filtered_by)}
9697
topics_recursive.append(current_topic)
9798
# Subtopic
9899
# Only handle up to one subtopic level
99100
else:
100-
current_topic["subtopics"].append(entry.topic)
101+
current_topic["subtopics"].append({'subtopic': entry.topic, 'topic_contents': entry.topic
102+
.get_contents(self.sorted_by, self.filtered_by)})
101103

102104
context["structure"] = topics_recursive
103105

106+
if self.sorted_by is not None:
107+
context['sorting'] = self.sorted_by
108+
if self.filtered_by is not None:
109+
context['filtering'] = self.filtered_by
110+
104111

105112
"""# create a list of topics ordered by (sub-)topic and index
106113
flat_topic_list = create_topic_and_subtopic_list(topics, super().get_object())

0 commit comments

Comments
 (0)