Skip to content

Instantly share code, notes, and snippets.

/views.py
Created May 1, 2016

Embed
What would you like to do?
class WriteReview(StudentOnlyMixin, CreateView):
model = CourseReview
form_class = ReviewForm
template_name = 'academy/write_review.html'
def dispatch(self, request, *args, **kwargs):
# check if review exists
try:
course = get_object_or_404(Course, slug=self.kwargs['slug'])
self.course = course
old_review = CourseReview.objects.filter(author=self.request.user, course=course).first()
except IndexError:
pass
else:
return HttpResponseRedirect()
return super(WriteReview, self).dispatch(request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super(WriteReview, self).get_context_data(**kwargs)
context['course'] = get_object_or_404(Course, slug=self.kwargs['slug'])
return context
def form_valid(self, form):
course = get_object_or_404(Course, slug=self.kwargs['slug'])
form.save(commit=False)
form.instance.course = course
form.instance.author = self.request.user
messages.success(self.request, 'Your review has been saved!')
return super(WriteReview, self).form_valid(form)
def get_success_url(self):
return "{}?#LECTURES".format(reverse('course.landing.page', args={self.kwargs['slug']}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.