Skip to content

Commit e15be7e

Browse files
update
1 parent 1675fb8 commit e15be7e

4 files changed

Lines changed: 52 additions & 30 deletions

File tree

base/models/content.py

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class Course(models.Model):
4242
description: short description of the course
4343
owner: people that may change the structure of the course
4444
"""
45+
4546
class Meta:
4647
verbose_name = _("Course")
4748
verbose_name_plural = _("Courses")
@@ -53,15 +54,17 @@ class Meta:
5354
creation_date = models.DateTimeField(verbose_name=_('Creation Date'), auto_now_add=True, blank=True)
5455

5556
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)
57+
topics = models.ManyToManyField("Topic", verbose_name=_("Topics"), through='CourseStructureEntry',
58+
related_name="courses", blank=True)
5759

5860
owners = models.ManyToManyField(Profile, related_name='owned_courses', verbose_name=_("Owners"))
5961
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)
62+
help_text=_("Is the course protected and can only be edited by the owners?"),
63+
blank=True, default=False)
6164

6265
category = models.ForeignKey(Category, verbose_name=_("Category"), related_name="courses", on_delete=models.CASCADE)
6366
period = models.ForeignKey(Period, verbose_name=_("Period"), related_name="courses",
64-
blank=True, null=True, on_delete=models.SET_NULL)
67+
blank=True, null=True, on_delete=models.SET_NULL)
6568

6669
def __str__(self):
6770
return self.title
@@ -74,6 +77,7 @@ class Topic(models.Model):
7477
title: Name of the topic
7578
category: category this topic belongs to
7679
"""
80+
7781
class Meta:
7882
verbose_name = _("Topic")
7983
verbose_name_plural = _("Topics")
@@ -125,6 +129,7 @@ class Content(models.Model):
125129
author: user that created the content
126130
parent_topic: the topic the content belongs to/describes
127131
"""
132+
128133
class Meta:
129134
verbose_name = _("Content")
130135
verbose_name_plural = _("Contents")
@@ -139,8 +144,10 @@ class Meta:
139144
language = models.CharField(verbose_name=_("Language"), max_length=30, choices=settings.LANGUAGES)
140145
tags = models.ManyToManyField(Tag, verbose_name=_("Tags"), related_name='contents', blank=True)
141146

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)
147+
readonly = models.BooleanField(verbose_name=_("Readonly"), help_text=_("Can this content be updated?"),
148+
default=False)
149+
public = models.BooleanField(verbose_name=_("Show in public courses?"), help_text=_(
150+
"May this content be displayed in courses that don't require registration?"), default=False)
144151

145152
creation_date = models.DateTimeField(verbose_name=_('Creation Date'), auto_now_add=True, blank=True)
146153
preview = models.ImageField(verbose_name=_("Rendered preview"), blank=True, null=True)
@@ -166,7 +173,9 @@ def get_rate(self):
166173
:return: rating
167174
:rtype: float
168175
"""
169-
rating = self.ratings.aggregate(Avg('rating'))['rating__avg']
176+
177+
rating = self.ratings.aggregate(Avg('rating'))['rating__avg'] # todo something wrong here
178+
print(self.ratings)
170179
if rating is None:
171180
return 0
172181
return round(rating, 2) # pylint: disable=no-member
@@ -196,25 +205,27 @@ def get_user_rate(self, user):
196205
:rtype: int
197206
"""
198207
if self.user_already_rated(user):
199-
# TODO FIX
200-
return 0 #self.ratings.get(user_id=user.pk).rating # pylint: disable=no-member
208+
content_id = self.id
209+
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
201211
return 0
202212

203-
def rate_content(self, user, rate):
213+
def rate_content(self, user):
204214
"""
205215
Rate content
206216
:param content: Content
207217
:param User user: user
208218
:param int rate: rating
209219
:return: nothing
210220
"""
211-
#print(self.ratings.)
212-
# self.ratings (Content) != Rating
213-
214-
#self.ratings.objects.create(user=user, content_id=self.pk, rating=rate) # pylint: disable=no-member
215-
216-
self.ratings.filter(user_id=user.user.pk, content=self).delete() # pylint: disable=no-member
217-
self.ratings.objects.create(user=user, content=self, rating=rate) # pylint: disable=no-member
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
227+
# pylint: disable=no-member
228+
self.save()
218229

219230
def get_index_in_course(self, course):
220231
"""
@@ -233,6 +244,7 @@ class CourseStructureEntry(models.Model):
233244
index: position that is meant (e.g. "1#2" -> second undertopic of the first topic)
234245
topic: topic at specified position/index
235246
"""
247+
236248
class Meta:
237249
verbose_name = _("Course Structure Entry")
238250
verbose_name_plural = _("Course Structure Entries")

base/models/profile.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
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
7+
68

79

810
class Profile(models.Model):
@@ -14,6 +16,9 @@ class Profile(models.Model):
1416
def __str__(self):
1517
return str(self.user)
1618

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

1823
@receiver(post_save, sender=User)
1924
def create_user_profile(sender, instance, created, **kwargs):
@@ -24,3 +29,4 @@ def create_user_profile(sender, instance, created, **kwargs):
2429
@receiver(post_save, sender=User)
2530
def save_user_profile(sender, instance, **kwargs):
2631
instance.profile.save()
32+

frontend/templates/frontend/content/detail.html

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,8 @@ <h5 class="modal-title" id="deleteContentModalLabel">{% trans "Delete" %} <i>{{
9898
{{ content.description|linebreaks }}
9999
</div>
100100
</div>
101-
ratings
102101
<h5><a name="rating">{% trans "Rating" %}</a></h5>
103-
<div class="float-right text-right"><a>{% trans "Current" %} {{ content.get_rate_num }}</a> </div>
102+
<div class="float-right text-right"><a>{% trans "Current" %} {{ content.get_rate }}</a> </div>
104103
<div class="starrating risingstar d-flex justify-content-end flex-row-reverse">
105104
{% for i in 5|rev_range %}
106105
<input type="radio" id="star{{ i }}" name="rating" value="{{ i }}"
@@ -109,7 +108,6 @@ <h5><a name="rating">{% trans "Rating" %}</a></h5>
109108
{% endfor %}
110109
</div>
111110

112-
113111
<h5 style="margin-top: 30px;"><a name="comments">{% trans "Comments" %}</a></h5>
114112
{% if user.is_authenticated %}
115113
<form action="{% url 'frontend:content' course.id topic.id content.id %}" method="post">

frontend/views/content.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -206,19 +206,25 @@ def rate_content(request, course_id, topic_id, content_id, pk):
206206
:rtype: HttpResponse
207207
"""
208208
# check if rating is valid
209-
rating = pk
210-
print(course_id,topic_id,content_id,rating)
211-
print(type(get_user(request)))
212209
content = get_object_or_404(Content, pk=content_id)
213210
profile = get_user(request)
214-
content.rate_content(profile, rating)
215-
216-
# check if content already has rating
217-
#Rating.objects.filter(user_id=profile, content_id=content_id).delete()
218-
#Rating.objects.create(user=profile, content=content, rating=rating) # user = profile; rating_obj =
219-
220-
content.ratings.add(profile)
221-
content.save()
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()
222228

223229
return HttpResponseRedirect(
224230
reverse_lazy('frontend:content', args=(course_id, topic_id, content_id,))

0 commit comments

Comments
 (0)