summaryrefslogtreecommitdiff
path: root/yaksh/models.py
diff options
context:
space:
mode:
authorPalaparthy Adityachandra2020-04-27 13:51:06 +0530
committerGitHub2020-04-27 13:51:06 +0530
commit53a0c4ad3e733f3960000527f83565f2fd8fc412 (patch)
treeb3111bb5f317ec1ca12b173a3bce8b6a7e1b9c62 /yaksh/models.py
parent01c9faa0abeedb748600c35a35bd6cf8124bd64d (diff)
parent2116310ed81ed81035a25ccc5746e8114b6a7b24 (diff)
downloadonline_test-53a0c4ad3e733f3960000527f83565f2fd8fc412.tar.gz
online_test-53a0c4ad3e733f3960000527f83565f2fd8fc412.tar.bz2
online_test-53a0c4ad3e733f3960000527f83565f2fd8fc412.zip
Merge pull request #690 from CruiseDevice/forum
Discussion Forum for Yaksh
Diffstat (limited to 'yaksh/models.py')
-rw-r--r--yaksh/models.py53
1 files changed, 52 insertions, 1 deletions
diff --git a/yaksh/models.py b/yaksh/models.py
index 5d4d453..9bcb132 100644
--- a/yaksh/models.py
+++ b/yaksh/models.py
@@ -1,5 +1,6 @@
from __future__ import unicode_literals, division
from datetime import datetime, timedelta
+import uuid
import json
import random
import ruamel.yaml
@@ -10,6 +11,7 @@ from collections import Counter, defaultdict
from django.db import models
from django.contrib.auth.models import User, Group, Permission
+from django.core.exceptions import ValidationError
from django.contrib.contenttypes.models import ContentType
from taggit.managers import TaggableManager
from django.utils import timezone
@@ -232,6 +234,19 @@ def render_template(template_path, data=None):
return render
+def validate_image(image):
+ file_size = image.file.size
+ limit_mb = 30
+ if file_size > limit_mb * 1024 * 1024:
+ raise ValidationError("Max size of file is {0} MB".format(limit_mb))
+
+
+def get_image_dir(instance, filename):
+ return os.sep.join((
+ 'post_%s' % (instance.uid), filename
+ ))
+
+
###############################################################################
class CourseManager(models.Manager):
@@ -1162,7 +1177,9 @@ class CourseStatus(models.Model):
if self.is_course_complete():
self.calculate_percentage()
if self.course.grading_system is None:
- grading_system = GradingSystem.objects.get(name__contains='default')
+ grading_system = GradingSystem.objects.get(
+ name__contains='default'
+ )
else:
grading_system = self.course.grading_system
grade = grading_system.get_grade(self.percentage)
@@ -2632,4 +2649,38 @@ class TestCaseOrder(models.Model):
# Order of the test case for a question.
order = models.TextField()
+
##############################################################################
+class ForumBase(models.Model):
+ uid = models.UUIDField(unique=True, default=uuid.uuid4, editable=False)
+ creator = models.ForeignKey(User, on_delete=models.CASCADE)
+ description = models.TextField()
+ created_at = models.DateTimeField(auto_now_add=True)
+ modified_at = models.DateTimeField(auto_now=True)
+ image = models.ImageField(upload_to=get_image_dir, blank=True,
+ null=True, validators=[validate_image])
+ active = models.BooleanField(default=True)
+
+
+class Post(ForumBase):
+ title = models.CharField(max_length=200)
+ course = models.ForeignKey(Course,
+ on_delete=models.CASCADE, related_name='post')
+
+ def __str__(self):
+ return self.title
+
+ def get_last_comment(self):
+ return self.comment.last()
+
+ def get_comments_count(self):
+ return self.comment.filter(active=True).count()
+
+
+class Comment(ForumBase):
+ post_field = models.ForeignKey(Post, on_delete=models.CASCADE,
+ related_name='comment')
+
+ def __str__(self):
+ return 'Comment by {0}: {1}'.format(self.creator.username,
+ self.post_field.title)