summaryrefslogtreecommitdiff
path: root/yaksh/models.py
diff options
context:
space:
mode:
authorPalaparthy Adityachandra2021-03-15 19:02:27 +0530
committerGitHub2021-03-15 19:02:27 +0530
commit6fda19daaa06482b8eb52eeb62f9b0a15d0a3da6 (patch)
treea589f0dfae0cd118432f7e45899439cfc2a98a13 /yaksh/models.py
parent85a2b6b61c74e2ed42d6c78f6b502a3c72dcf9ca (diff)
parent1793f445ec0cbc67df4b4cd3151859e01ed4b2a0 (diff)
downloadonline_test-6fda19daaa06482b8eb52eeb62f9b0a15d0a3da6.tar.gz
online_test-6fda19daaa06482b8eb52eeb62f9b0a15d0a3da6.tar.bz2
online_test-6fda19daaa06482b8eb52eeb62f9b0a15d0a3da6.zip
Merge pull request #822 from CruiseDevice/fix_file_upload_assignments
Fix file upload assignments
Diffstat (limited to 'yaksh/models.py')
-rw-r--r--yaksh/models.py84
1 files changed, 53 insertions, 31 deletions
diff --git a/yaksh/models.py b/yaksh/models.py
index 77b3684..4798b23 100644
--- a/yaksh/models.py
+++ b/yaksh/models.py
@@ -121,11 +121,13 @@ MOD_GROUP_NAME = 'moderator'
def get_assignment_dir(instance, filename):
- folder_name = instance.course.name.replace(" ", "_")
- sub_folder_name = instance.question_paper.quiz.description.replace(
- " ", "_")
- return os.sep.join((folder_name, sub_folder_name, instance.user.username,
- str(instance.assignmentQuestion.id),
+ course_id = instance.answer_paper.course_id
+ quiz_id = instance.answer_paper.question_paper.quiz_id
+ folder = f'Course_{course_id}'
+ sub_folder = f'Quiz_{quiz_id}'
+ user = instance.answer_paper.user.username
+ return os.sep.join((folder, sub_folder, user,
+ str(instance.assignmentQuestion_id),
filename
))
@@ -1380,7 +1382,9 @@ class Question(models.Model):
# Solution for the question.
solution = models.TextField(blank=True)
- content = GenericRelation("TableOfContents", related_query_name='questions')
+ content = GenericRelation(
+ "TableOfContents", related_query_name='questions'
+ )
tc_code_types = {
"python": [
@@ -1439,7 +1443,7 @@ class Question(models.Model):
for file in files]
if self.type == "upload":
assignment_files = AssignmentUpload.objects.filter(
- assignmentQuestion=self, user=user
+ assignmentQuestion=self
)
if assignment_files:
metadata['assign_files'] = [(file.assignmentFile.path, False)
@@ -1837,7 +1841,8 @@ class QuestionPaper(models.Model):
all_questions = questions
return all_questions
- def make_answerpaper(self, user, ip, attempt_num, course_id, special=False):
+ def make_answerpaper(self,
+ user, ip, attempt_num, course_id, special=False):
"""Creates an answer paper for the user to attempt the quiz"""
try:
ans_paper = AnswerPaper.objects.get(user=user,
@@ -2034,6 +2039,7 @@ class AnswerPaperManager(models.Manager):
).order_by("id").values(
"answerpaper__id", "question_id", "correct", "answer"
)
+
def _get_per_tc_data(answers, q_type):
tc = []
for answer in answers["answer"]:
@@ -2046,11 +2052,11 @@ class AnswerPaperManager(models.Manager):
df = pd.DataFrame(answers)
if not df.empty:
for question in all_questions:
- que = df[df["question_id"]==question.id].groupby(
+ que = df[df["question_id"] == question.id].groupby(
"answerpaper__id").tail(1)
if not que.empty:
total_attempts = que.shape[0]
- correct_attempts = que[que["correct"]==True].shape[0]
+ correct_attempts = que[que["correct"] == True].shape[0]
per_tc_ans = {}
if question.type in ["mcq", "mcc"]:
per_tc_ans = _get_per_tc_data(que, question.type)
@@ -2635,16 +2641,19 @@ class AssignmentUploadManager(models.Manager):
def get_assignments(self, qp, que_id=None, user_id=None, course_id=None):
if que_id and user_id:
assignment_files = AssignmentUpload.objects.filter(
- assignmentQuestion_id=que_id, user_id=user_id,
- question_paper=qp, course_id=course_id
+ assignmentQuestion_id=que_id,
+ answer_paper__user_id=user_id,
+ answer_paper__question_paper=qp,
+ answer_paper__course_id=course_id
)
file_name = User.objects.get(id=user_id).get_full_name()
else:
assignment_files = AssignmentUpload.objects.filter(
- question_paper=qp, course_id=course_id
+ answer_paper__question_paper=qp,
+ answer_paper__course_id=course_id
)
file_name = "{0}_Assignment_files".format(
- assignment_files[0].course.name
+ assignment_files[0].answer_paper.course.name
)
return assignment_files, file_name
@@ -2652,15 +2661,18 @@ class AssignmentUploadManager(models.Manager):
##############################################################################
class AssignmentUpload(models.Model):
- user = models.ForeignKey(User, on_delete=models.CASCADE)
assignmentQuestion = models.ForeignKey(Question, on_delete=models.CASCADE)
- assignmentFile = models.FileField(upload_to=get_assignment_dir, max_length=255)
- question_paper = models.ForeignKey(QuestionPaper, blank=True, null=True,
- on_delete=models.CASCADE)
- course = models.ForeignKey(Course, null=True, blank=True,
- on_delete=models.CASCADE)
+ assignmentFile = models.FileField(upload_to=get_assignment_dir,
+ max_length=255)
+ answer_paper = models.ForeignKey(AnswerPaper, blank=True, null=True,
+ on_delete=models.CASCADE)
+ upload_date = models.DateTimeField(auto_now=True)
+
objects = AssignmentUploadManager()
+ def __str__(self):
+ return f'Assignment File of the user {self.answer_paper.user}'
+
##############################################################################
class TestCase(models.Model):
@@ -2884,7 +2896,6 @@ class TOCManager(models.Manager):
toc.get_toc_as_yaml(file_path)
return file_path
-
def get_question_stats(self, toc_id):
answers = LessonQuizAnswer.objects.get_queryset().filter(
toc_id=toc_id).order_by('id')
@@ -2918,7 +2929,7 @@ class TOCManager(models.Manager):
if j not in mydata:
mydata[j] = 1
else:
- mydata[j] +=1
+ mydata[j] += 1
data = mydata.copy()
if is_percent:
for key, value in data.items():
@@ -2964,17 +2975,20 @@ class TOCManager(models.Manager):
if not is_valid_time_format(time):
messages.append(
(False,
- f"Invalid time format in {name}. "
+ f"Invalid time format in {name}. "
"Format should be 00:00:00")
- )
+ )
else:
if content_type == 1:
topic = Topic.objects.create(**content)
toc.append(TableOfContents(
- course_id=course_id, lesson_id=lesson_id, time=time,
- content_object=topic, content=content_type
+ course_id=course_id,
+ lesson_id=lesson_id, time=time,
+ content_object=topic, content=content_type
))
- messages.append((True, f"{topic.name} added successfully"))
+ messages.append(
+ (True, f"{topic.name} added successfully")
+ )
else:
content['user'] = user
test_cases = content.pop("testcase")
@@ -2991,10 +3005,13 @@ class TOCManager(models.Manager):
else:
que = Question.objects.create(**content)
for test_case in test_cases:
- test_case_type = test_case.pop('test_case_type')
+ test_case_type = test_case.pop(
+ 'test_case_type'
+ )
model_class = get_model_class(test_case_type)
model_class.objects.get_or_create(
- question=que, **test_case, type=test_case_type
+ question=que,
+ **test_case, type=test_case_type
)
toc.append(TableOfContents(
course_id=course_id, lesson_id=lesson_id,
@@ -3010,7 +3027,12 @@ class TOCManager(models.Manager):
class TableOfContents(models.Model):
- toc_types = ((1, "Topic"), (2, "Graded Quiz"), (3, "Exercise"), (4, "Poll"))
+ toc_types = (
+ (1, "Topic"),
+ (2, "Graded Quiz"),
+ (3, "Exercise"),
+ (4, "Poll")
+ )
course = models.ForeignKey(Course, on_delete=models.CASCADE,
related_name='course')
lesson = models.ForeignKey(Lesson, on_delete=models.CASCADE,
@@ -3028,7 +3050,7 @@ class TableOfContents(models.Model):
def get_toc_text(self):
if self.content == 1:
- content_name = self.content_object.name
+ content_name = self.content_object.name
else:
content_name = self.content_object.summary
return content_name