55from django .utils .translation import gettext_lazy as _
66from 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
910from 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