diff options
author | CruiseDevice | 2020-09-05 16:31:29 +0530 |
---|---|---|
committer | ankitjavalkar | 2020-10-08 10:38:06 +0530 |
commit | e4a9897685b4958efb0d5bd86f57dc1584449619 (patch) | |
tree | 8cbda921879e957070f0023f1960bdb573210d2d | |
parent | f61351f6a4e7a6150e66ca39f23ac14f9a60de96 (diff) | |
download | online_test-e4a9897685b4958efb0d5bd86f57dc1584449619.tar.gz online_test-e4a9897685b4958efb0d5bd86f57dc1584449619.tar.bz2 online_test-e4a9897685b4958efb0d5bd86f57dc1584449619.zip |
Make Post model generic.
- Fix breaking links in forum after change in models.
-rw-r--r-- | yaksh/models.py | 16 | ||||
-rw-r--r-- | yaksh/templates/yaksh/post_comments.html | 8 | ||||
-rw-r--r-- | yaksh/templatetags/custom_filters.py | 1 | ||||
-rw-r--r-- | yaksh/views.py | 17 |
4 files changed, 29 insertions, 13 deletions
diff --git a/yaksh/models.py b/yaksh/models.py index b172e79..c6d6f7b 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -42,6 +42,7 @@ from django.template import Context, Template from django.conf import settings from django.forms.models import model_to_dict from django.db.models import Count + # Local Imports from yaksh.code_server import ( submit, get_result as get_result_from_code_server @@ -309,6 +310,9 @@ class Lesson(models.Model): help_text="Youtube id, vimeo id, others" ) + post = GenericRelation('Post', related_query_name='lessons') + + def __str__(self): return "{0}".format(self.name) @@ -919,6 +923,7 @@ class Course(models.Model): view_grade = models.BooleanField(default=False) learning_module = models.ManyToManyField(LearningModule, related_name='learning_module') + post = GenericRelation('Post', related_query_name='courses') # The start date of the course enrollment. start_enroll_time = models.DateTimeField( @@ -2753,8 +2758,15 @@ class ForumBase(models.Model): class Post(ForumBase): title = models.CharField(max_length=200) - course = models.ForeignKey(Course, - on_delete=models.CASCADE, related_name='post') + target_ct = models.ForeignKey(ContentType, + blank=True, + null=True, + related_name='target_obj', + on_delete=models.CASCADE) + target_id = models.PositiveIntegerField(null=True, + blank=True, + db_index=True) + target = GenericForeignKey('target_ct', 'target_id') def __str__(self): return self.title diff --git a/yaksh/templates/yaksh/post_comments.html b/yaksh/templates/yaksh/post_comments.html index b16b80c..f0f1593 100644 --- a/yaksh/templates/yaksh/post_comments.html +++ b/yaksh/templates/yaksh/post_comments.html @@ -6,7 +6,7 @@ {% block content %} <div class="container"> - <a class="btn btn-primary" href="{% url 'yaksh:course_forum' post.course.id %}"> + <a class="btn btn-primary" href="{% url 'yaksh:course_forum' post.target.id %}"> <i class="fa fa-arrow-left"></i> Back to Posts </a> <br> @@ -18,7 +18,7 @@ <small> <strong>{{post.creator.username}}</strong> {{post.created_at}} - {% if user.profile.is_moderator %}<a href="{% url 'yaksh:hide_post' post.course.id post.uid %}" class="pull-right btn btn-danger">Delete</a>{% endif %} + {% if user.profile.is_moderator %}<a href="{% url 'yaksh:hide_post' post.target.id post.uid %}" class="pull-right btn btn-danger">Delete</a>{% endif %} </small> </div> @@ -41,7 +41,7 @@ <strong class="text-muted">{{comment.creator.username}}</strong> </div> <div class="col-6 text-right"> - <small class="text-muted">{{comment.created_at}} {% if user.profile.is_moderator %} <a href="{% url 'yaksh:hide_comment' post.course.id comment.uid %}" class="btn btn-danger">Delete</a>{% endif %}</small> + <small class="text-muted">{{comment.created_at}} {% if user.profile.is_moderator %} <a href="{% url 'yaksh:hide_comment' post.target.id comment.uid %}" class="btn btn-danger">Delete</a>{% endif %}</small> </div> </div> <p class="card-text description">{{comment.description}}</p> @@ -59,7 +59,7 @@ <br> <div> <b><u>Add comment:</u></b> - <form action="{% url 'yaksh:post_comments' post.course.id post.uid %}" method="POST" enctype='multipart/form-data'> + <form action="" method="POST" enctype='multipart/form-data'> <div class="form-group"> {% csrf_token %} {{form}} diff --git a/yaksh/templatetags/custom_filters.py b/yaksh/templatetags/custom_filters.py index 57ec7dd..f297598 100644 --- a/yaksh/templatetags/custom_filters.py +++ b/yaksh/templatetags/custom_filters.py @@ -1,6 +1,7 @@ from django import template from django.template.defaultfilters import stringfilter from django.forms.fields import CheckboxInput +from django.contrib.contenttypes.models import ContentType from ast import literal_eval import os try: diff --git a/yaksh/views.py b/yaksh/views.py index 73979da..e9730d9 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 @@ -3447,6 +3448,7 @@ 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)) @@ -3455,9 +3457,10 @@ def course_forum(request, course_id): posts = course.post.get_queryset().filter( active=True, title__icontains=search_term) 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 +3468,7 @@ 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.save() return redirect('yaksh:post_comments', course_id=course.id, uuid=new_post.uid) @@ -3509,7 +3512,7 @@ def post_comments(request, course_id, uuid): 'comments': comments, 'base_template': base_template, 'form': form, - 'user': user + 'user': user, }) @@ -3518,7 +3521,7 @@ def post_comments(request, course_id, uuid): 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)): + if (not course.is_creator(user) or not course.is_teacher(user)): raise Http404('You are not enrolled in {0} course'.format(course.name)) post = get_object_or_404(Post, uid=uuid) post.comment.active = False @@ -3532,7 +3535,7 @@ def hide_post(request, course_id, uuid): 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)): + if (not course.is_creator(user) or not course.is_teacher(user)): raise Http404('You are not enrolled in {0} course'.format(course.name)) comment = get_object_or_404(Comment, uid=uuid) post_uid = comment.post_field.uid |