SHARE
TWEET

Untitled

a guest Oct 19th, 2019 114 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from django.db import models
  2. from django.contrib.auth.models import AbstractUser
  3. from django.db import models
  4. from django.utils.html import escape,mark_safe
  5.  
  6. class User(AbstractUser):
  7.     is_student = models.BooleanField(default= False)
  8.     is_teacher = models.BooleanField(default= False)
  9.  
  10. class Subject(models.Model):
  11.     name = models.CharField(max_length=30)
  12.     color = models.CharField(max_length=7,default='#007bff')
  13.  
  14.     def __str__(self):
  15.         return self.name
  16.  
  17.     def get_html_badge(self):
  18.         name = escape(self.name)
  19.         color = escape(self.color)
  20.         html = '<span class="badge badge-primary" style="background-color: %s">%s</span>'%(color,name)
  21.         return mark_safe(html)
  22.  
  23. class Quiz(models.Model):
  24.     owner = models.ForeignKey(User,related_name='quizzes',on_delete=models.CASCADE)
  25.     name = models.CharField(max_length=255)
  26.     subject = models.ForeignKey(Subject,related_name='quizzes',on_delete=models.CASCADE)
  27.  
  28.     def __str__(self):
  29.         return self.name
  30.  
  31. class Question(models.Model):
  32.     quiz = models.ForeignKey(Quiz,related_name='questions',on_delete=models.CASCADE)
  33.     text = models.CharField('Question',max_length=255)
  34.  
  35.     def __str__(self):
  36.         return self.text
  37.  
  38. class Answer(models.Model):
  39.     question = models.ForeignKey(Question,on_delete=models.CASCADE,related_name='answers')
  40.     text = models.CharField('Answer',max_length=255)
  41.     is_correct = models.BooleanField('Correct answer',default=False)
  42.  
  43.     def __str__(self):
  44.         self.text
  45.  
  46. class Student(models.Model):
  47.     user = models.OneToOneField(User,on_delete=models.CASCADE, primary_key=True)
  48.     quizzes = models.ManyToManyField(Subject,through='TakenQuiz')
  49.     interests = models.ManyToManyField(Subject,related_name='interested_students')
  50.  
  51.     def get_unanswered_questions(self,quiz):
  52.         answered_questions = self.quiz_answers.filter(answer__question__quiz = quiz).values_list('answer__question__pk',flat=True)
  53.         questions = quiz.questions.exclude(pk=answered_questions).order_by('text')
  54.         return questions
  55.  
  56.     def __str__(self):
  57.         return self.user.username
  58.  
  59. class TakenQuiz(models.Model):
  60.     student = models.ForeignKey(Student,related_name='taken_quizzes')
  61.     quiz = models.ForeignKey(Quiz,related_name='taken_quizes',on_delete=models.CASCADE)
  62.     score = models.FloatField()
  63.     date = models.DateTimeField(auto_now_add=True)
  64.  
  65. class StudentAnswer(models.Model):
  66.     student = models.ForeignKey(Student,related_name='quiz_answers')
  67.     answer = models.ForeignKey(Answer,on_delete=models.CASCADE,related_name="+")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top