Skip to content

Commit 97f4887

Browse files
Merge pull request #41 from DataManagementLab/#5_Rating
#5 rating
2 parents 7aa0568 + 2e89f66 commit 97f4887

16 files changed

Lines changed: 113 additions & 49 deletions

File tree

base/locale/de_DE/LC_MESSAGES/django.po

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ msgid ""
88
msgstr ""
99
"Project-Id-Version: PACKAGE VERSION\n"
1010
"Report-Msgid-Bugs-To: \n"
11-
"POT-Creation-Date: 2020-06-05 11:48+0000\n"
11+
"POT-Creation-Date: 2020-06-22 14:54+0000\n"
1212
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1313
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1414
"Language-Team: LANGUAGE <LL@li.org>\n"

base/models/content.py

Lines changed: 28 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):
@@ -42,6 +43,7 @@ class Course(models.Model):
4243
description: short description of the course
4344
owner: people that may change the structure of the course
4445
"""
46+
4547
class Meta:
4648
verbose_name = _("Course")
4749
verbose_name_plural = _("Courses")
@@ -53,15 +55,17 @@ class Meta:
5355
creation_date = models.DateTimeField(verbose_name=_('Creation Date'), auto_now_add=True, blank=True)
5456

5557
image = models.ImageField(verbose_name=_("Title Image"), blank=True, upload_to='uploads/courses/%Y/%m/%d/')
56-
topics = models.ManyToManyField("Topic", verbose_name=_("Topics"), through='CourseStructureEntry', related_name="courses", blank=True)
58+
topics = models.ManyToManyField("Topic", verbose_name=_("Topics"), through='CourseStructureEntry',
59+
related_name="courses", blank=True)
5760

5861
owners = models.ManyToManyField(Profile, related_name='owned_courses', verbose_name=_("Owners"))
5962
restrict_changes = models.BooleanField(verbose_name=_("Edit Restriction"),
60-
help_text=_("Is the course protected and can only be edited by the owners?"), blank=True, default=False)
63+
help_text=_("Is the course protected and can only be edited by the owners?"),
64+
blank=True, default=False)
6165

6266
category = models.ForeignKey(Category, verbose_name=_("Category"), related_name="courses", on_delete=models.CASCADE)
6367
period = models.ForeignKey(Period, verbose_name=_("Period"), related_name="courses",
64-
blank=True, null=True, on_delete=models.SET_NULL)
68+
blank=True, null=True, on_delete=models.SET_NULL)
6569

6670
def __str__(self):
6771
return self.title
@@ -74,6 +78,7 @@ class Topic(models.Model):
7478
title: Name of the topic
7579
category: category this topic belongs to
7680
"""
81+
7782
class Meta:
7883
verbose_name = _("Topic")
7984
verbose_name_plural = _("Topics")
@@ -125,6 +130,7 @@ class Content(models.Model):
125130
author: user that created the content
126131
parent_topic: the topic the content belongs to/describes
127132
"""
133+
128134
class Meta:
129135
verbose_name = _("Content")
130136
verbose_name_plural = _("Contents")
@@ -139,8 +145,10 @@ class Meta:
139145
language = models.CharField(verbose_name=_("Language"), max_length=30, choices=settings.LANGUAGES)
140146
tags = models.ManyToManyField(Tag, verbose_name=_("Tags"), related_name='contents', blank=True)
141147

142-
readonly = models.BooleanField(verbose_name=_("Readonly"), help_text=_("Can this content be updated?"), default=False)
143-
public = models.BooleanField(verbose_name=_("Show in public courses?"), help_text=_("May this content be displayed in courses that don't require registration?"), default=False)
148+
readonly = models.BooleanField(verbose_name=_("Readonly"), help_text=_("Can this content be updated?"),
149+
default=False)
150+
public = models.BooleanField(verbose_name=_("Show in public courses?"), help_text=_(
151+
"May this content be displayed in courses that don't require registration?"), default=False)
144152

145153
creation_date = models.DateTimeField(verbose_name=_('Creation Date'), auto_now_add=True, blank=True)
146154
preview = models.ImageField(verbose_name=_("Rendered preview"), blank=True, null=True)
@@ -161,15 +169,10 @@ def get_rate_num(self):
161169
return self.get_rate()
162170

163171
def get_rate(self):
164-
"""
165-
return the average rating and 0 if there are no ratings
166-
:return: rating
167-
:rtype: float
168-
"""
169-
rating = self.ratings.aggregate(Avg('rating'))['rating__avg']
170-
if rating is None:
171-
return 0
172-
return round(rating, 2) # pylint: disable=no-member
172+
rating = Rating.objects.filter(content_id=self.id).aggregate(Avg('rating'))['rating__avg']
173+
if rating is not None:
174+
return rating
175+
return -1
173176

174177
def get_rate_count(self):
175178
"""
@@ -196,19 +199,23 @@ def get_user_rate(self, user):
196199
:rtype: int
197200
"""
198201
if self.user_already_rated(user):
199-
# TODO FIX
200-
return 0 #self.ratings.get(user_id=user.pk).rating # pylint: disable=no-member
202+
content_id = self.id
203+
return self.ratings.get(user=user).rating_set.first().rating
201204
return 0
202205

203-
def rate_content(self, user, rate):
206+
def rate_content(self, user, rating):
204207
"""
205208
Rate content
209+
:param rating: Rating of Content by User
210+
:param content: Content
206211
:param User user: user
207-
:param int rate: rating
208212
:return: nothing
209213
"""
210-
self.ratings.filter(user_id=user.pk, content_id=self.id).delete() # pylint: disable=no-member
211-
self.ratings.objects.create(user=user, content_id=self.pk, rating=rate) # pylint: disable=no-member
214+
Rating.objects.filter(user_id=user.user.id, content_id=self.id).delete()
215+
rating = Rating.objects.create(user=user, content=self, rating=rating) # user = profile
216+
rating.save()
217+
# pylint: disable=no-member
218+
self.save()
212219

213220
def get_index_in_course(self, course):
214221
"""
@@ -227,6 +234,7 @@ class CourseStructureEntry(models.Model):
227234
index: position that is meant (e.g. "1#2" -> second undertopic of the first topic)
228235
topic: topic at specified position/index
229236
"""
237+
230238
class Meta:
231239
verbose_name = _("Course Structure Entry")
232240
verbose_name_plural = _("Course Structure Entries")

base/models/profile.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from django.utils.translation import gettext_lazy as _
66

77

8+
89
class Profile(models.Model):
910
user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
1011
bio = models.TextField(verbose_name=_("Biography"), blank=True)
@@ -24,3 +25,4 @@ def create_user_profile(sender, instance, created, **kwargs):
2425
@receiver(post_save, sender=User)
2526
def save_user_profile(sender, instance, **kwargs):
2627
instance.profile.save()
28+

collab_coursebook/locale/de_DE/LC_MESSAGES/django.po

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ msgid ""
88
msgstr ""
99
"Project-Id-Version: PACKAGE VERSION\n"
1010
"Report-Msgid-Bugs-To: \n"
11-
"POT-Creation-Date: 2020-06-05 11:48+0000\n"
11+
"POT-Creation-Date: 2020-06-22 14:54+0000\n"
1212
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1313
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1414
"Language-Team: LANGUAGE <LL@li.org>\n"

content/locale/de_DE/LC_MESSAGES/django.po

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ msgid ""
88
msgstr ""
99
"Project-Id-Version: PACKAGE VERSION\n"
1010
"Report-Msgid-Bugs-To: \n"
11-
"POT-Creation-Date: 2020-06-05 11:48+0000\n"
11+
"POT-Creation-Date: 2020-06-22 14:54+0000\n"
1212
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1313
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1414
"Language-Team: LANGUAGE <LL@li.org>\n"

frontend/forms/content.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ class TranslateForm(forms.Form):
1313
widget=forms.Select(choices=TRANSLATE_CHOICE,
1414
attrs={'class': 'form-control',
1515
'style': 'width:auto',
16-
'onchange': 'this.form.submit();'}))
16+
'onchange': 'this.form.submit();'}))

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/locale/de_DE/LC_MESSAGES/django.po

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ msgid ""
88
msgstr ""
99
"Project-Id-Version: PACKAGE VERSION\n"
1010
"Report-Msgid-Bugs-To: \n"
11-
"POT-Creation-Date: 2020-06-05 11:48+0000\n"
11+
"POT-Creation-Date: 2020-06-22 14:54+0000\n"
1212
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1313
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1414
"Language-Team: LANGUAGE <LL@li.org>\n"
@@ -144,6 +144,7 @@ msgstr "Inhalte hinzufügen"
144144
msgid "Duplicate Course"
145145
msgstr "Kurs duplizieren"
146146

147+
147148
#: frontend/templates/frontend/course/edit.html:11
148149
msgid "Edit course"
149150
msgstr "Kurs bearbeiten"

frontend/templates/frontend/content/detail.html

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

2121
<div class="dropdown-menu dropdown-menu-right" aria-labelledby="dropdownMenuLink">
22+
2223
<a class="dropdown-item" href="
2324
{% url 'frontend:content-reading-mode' course.id topic.id content.id %}{% if ending %}{{ ending }}{% endif %}">{% fa5_icon 'eye' 'far' %}
2425
Reading Mode</a>
@@ -98,20 +99,23 @@ <h5 class="modal-title" id="deleteContentModalLabel">{% trans "Delete" %} <i>{{
9899
{% include content.type|content_view %}
99100

100101
<div class="mt-5">
101-
{{ content.description|linebreaks }}
102+
{{ content.description|linebreaks }} <!-- resolves to s -->
102103
</div>
103104
</div>
104-
105-
<h5><a name="rating">{% trans "Rating" %}</a></h5>
106-
<div class="starrating risingstar d-flex justify-content-end flex-row-reverse">
107-
{% for i in 5|rev_range %}
108-
<input type="radio" id="star{{ i }}" name="rating" value="{{ i }}"
109-
{% if i <= user_rate %}class="active" {% endif %}/>
110-
{# TODO Replace with POST action and use proper url reversing #}
111-
<label for="star{{ i }}" onclick="window.location.href ='rate/{{ i }}/#rating'"></label>
112-
{% endfor %}
113-
</div>
114-
105+
<h5><a name="rating">{% trans "Rating" %}</a>
106+
{% if content.get_rate != -1 %}
107+
<span class="badge float-right text-right">{{ content.get_rate }}/5</span>
108+
{% else %}
109+
<span class="badge float-right text-right">No rating yet</span>
110+
{% endif %}
111+
</h5>
112+
<div class="starrating risingstar d-flex justify-content-end flex-row-reverse">
113+
{% for i in 5|rev_range %}
114+
<input type="radio" id="star{{ i }}" name="rating" value="{{ i }}"
115+
{% if i <= content.get_rate %}class="active" {% endif %}/>
116+
<label for="star{{ i }}" onclick="window.location.href='{% url 'frontend:rating' course.id topic.id content.id i %}'"></label>
117+
{% endfor %}
118+
</div>
115119
<h5 style="margin-top: 30px;"><a name="comments">{% trans "Comments" %}</a></h5>
116120
{% if user.is_authenticated %}
117121
<form action="{% url 'frontend:content' course.id topic.id content.id %}" class="post-form" method="post">

frontend/templates/frontend/course/topic_contents.html

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@
2020
<div class="card-footer">
2121
<span class="badge badge-primary">{{ content.language }}</span>
2222
{% if content.tags.count > 0 %}
23-
&nbsp;&middot;&nbsp;
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 %}
28+
{% if content.get_rate != -1 %}
2829
&nbsp;&middot;&nbsp;
29-
{% trans "Rating" %}: {{ content.get_rate }}
30+
<span class="badge badge-info">{{ content.get_rate}}</span>
31+
{% endif %}
3032
</div>
3133
</div>
3234
</div>

0 commit comments

Comments
 (0)