diff options
Diffstat (limited to 'yaksh/views.py')
-rw-r--r-- | yaksh/views.py | 88 |
1 files changed, 76 insertions, 12 deletions
diff --git a/yaksh/views.py b/yaksh/views.py index 73979da..69a7414 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -10,6 +10,7 @@ from django.db import models from django.views.decorators.csrf import csrf_exempt from django.contrib.auth.decorators import login_required from django.contrib.auth.models import Group +from django.contrib.contenttypes.models import ContentType from django.forms.models import inlineformset_factory from django.forms import fields from django.utils import timezone @@ -2803,11 +2804,40 @@ def show_lesson(request, lesson_id, module_id, course_id): if not learn_unit.is_prerequisite_complete(user, learn_module, course): msg = "You have not completed previous Lesson/Quiz/Exercise" return view_module(request, learn_module.id, course_id, msg=msg) + + lesson_ct = ContentType.objects.get_for_model(learn_unit.lesson) + title = learn_unit.lesson.name + try: + post = Post.objects.get( + target_ct=lesson_ct, target_id=learn_unit.lesson.id, + active=True, title=title + ) + except Post.DoesNotExist: + post = Post.objects.create( + target_ct=lesson_ct, target_id=learn_unit.lesson.id, + active=True, title=title, creator=user, + description=f'Discussion on {title} lesson', + ) + if request.method == "POST": + form = CommentForm(request.POST, request.FILES) + if form.is_valid(): + new_comment = form.save(commit=False) + new_comment.creator = request.user + new_comment.post_field = post + new_comment.anonymous = request.POST.get('anonymous', '') == 'on' + new_comment.save() + return redirect(request.path_info) + else: + raise Http404(f'Post does not exist for lesson {title}') + else: + form = CommentForm() + comments = post.comment.filter(active=True) context = {'lesson': learn_unit.lesson, 'user': user, 'course': course, 'state': "lesson", "all_modules": all_modules, 'learning_units': learning_units, "current_unit": learn_unit, 'learning_module': learn_module, 'toc': toc, - 'contents_by_time': contents_by_time} + 'contents_by_time': contents_by_time, + 'comments': comments, 'form': form, 'post': post} return my_render_to_response(request, 'yaksh/show_video.html', context) @@ -3447,17 +3477,22 @@ def course_forum(request, course_id): base_template = 'manage.html' moderator = True course = get_object_or_404(Course, id=course_id) + course_ct = ContentType.objects.get_for_model(course) if (not course.is_creator(user) and not course.is_teacher(user) and not course.is_student(user)): raise Http404('You are not enrolled in {0} course'.format(course.name)) search_term = request.GET.get('search_post') if search_term: - posts = course.post.get_queryset().filter( - active=True, title__icontains=search_term) + posts = Post.objects.filter( + Q(title__icontains=search_term) | + Q(description__icontains=search_term), + target_ct=course_ct, target_id=course.id, active=True + ) else: - posts = course.post.get_queryset().filter( - active=True).order_by('-modified_at') - paginator = Paginator(posts, 1) + posts = Post.objects.filter( + target_ct=course_ct, target_id=course.id, active=True + ).order_by('-modified_at') + paginator = Paginator(posts, 10) page = request.GET.get('page') posts = paginator.get_page(page) if request.method == "POST": @@ -3465,7 +3500,8 @@ def course_forum(request, course_id): if form.is_valid(): new_post = form.save(commit=False) new_post.creator = user - new_post.course = course + new_post.target = course + new_post.anonymous = request.POST.get('anonymous', '') == 'on' new_post.save() return redirect('yaksh:post_comments', course_id=course.id, uuid=new_post.uid) @@ -3484,6 +3520,27 @@ def course_forum(request, course_id): @login_required @email_verified +def lessons_forum(request, course_id): + user = request.user + base_template = 'user.html' + moderator = False + if is_moderator(user): + base_template = 'manage.html' + moderator = True + course = get_object_or_404(Course, id=course_id) + course_ct = ContentType.objects.get_for_model(course) + lesson_posts = course.get_lesson_posts() + return render(request, 'yaksh/lessons_forum.html', { + 'user': user, + 'base_template': base_template, + 'moderator': moderator, + 'course': course, + 'posts': lesson_posts, + }) + + +@login_required +@email_verified def post_comments(request, course_id, uuid): user = request.user base_template = 'user.html' @@ -3502,6 +3559,7 @@ def post_comments(request, course_id, uuid): new_comment = form.save(commit=False) new_comment.creator = request.user new_comment.post_field = post + new_comment.anonymous = request.POST.get('anonymous', '') == 'on' new_comment.save() return redirect(request.path_info) return render(request, 'yaksh/post_comments.html', { @@ -3509,7 +3567,8 @@ def post_comments(request, course_id, uuid): 'comments': comments, 'base_template': base_template, 'form': form, - 'user': user + 'user': user, + 'course': course }) @@ -3519,7 +3578,9 @@ def hide_post(request, course_id, uuid): user = request.user course = get_object_or_404(Course, id=course_id) if (not course.is_creator(user) and not course.is_teacher(user)): - raise Http404('You are not enrolled in {0} course'.format(course.name)) + raise Http404( + 'Only a course creator or a teacher can delete the post.' + ) post = get_object_or_404(Post, uid=uuid) post.comment.active = False post.active = False @@ -3531,9 +3592,12 @@ def hide_post(request, course_id, uuid): @email_verified def hide_comment(request, course_id, uuid): user = request.user - course = get_object_or_404(Course, id=course_id) - if (not course.is_creator(user) and not course.is_teacher(user)): - raise Http404('You are not enrolled in {0} course'.format(course.name)) + if course_id: + course = get_object_or_404(Course, id=course_id) + if (not course.is_creator(user) and not course.is_teacher(user)): + raise Http404( + 'Only a course creator or a teacher can delete the comments' + ) comment = get_object_or_404(Comment, uid=uuid) post_uid = comment.post_field.uid comment.active = False |