55from fontawesome_5 .fields import IconField
66
77from base .models import Profile
8+ from .social import Rating
89
910
1011class 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" )
0 commit comments