Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
2.9 kB
6
Indexable
Never
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from ckeditor.fields import RichTextField
from django.utils import timezone


# Create a user profile model
class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    score = models.IntegerField(default=0)
    bio = models.TextField(max_length=500, blank=True)  
    date_modified = models.DateTimeField(User, auto_now=True)
    profile_image = models.ImageField(null=True, blank=True, upload_to="images/")
    

    def __str__(self):
        return self.user.username

# create profile when new user signs up
def create_profile(sender, instance, created, **kwargs):
    if created:
        user_profile = Profile(user=instance)
        user_profile.save()

post_save.connect(create_profile, sender=User)



class Lesson(models.Model):
    title = models.CharField(max_length=100)
    details = models.CharField(max_length=100)
    content = RichTextField(blank=True, null=True)
    lesson_image = models.ImageField(null=True, blank=True, upload_to="images/")
    
    def __str__(self):
        return self.title


class Quiz(models.Model):
    lesson = models.ForeignKey(Lesson, on_delete=models.CASCADE)
    type = models.CharField(max_length=100, default="quiz", null=False, blank=False)
    question = RichTextField(null=False, blank=False)
    answer = models.CharField(max_length=100, null=False, blank=False)
    created_at = models.DateTimeField( default=timezone.now)

    
    def __str__(self):
        return self.type



class Assessment(models.Model):
    lesson = models.ForeignKey(Lesson, on_delete=models.CASCADE)
    type = models.CharField(max_length=100, default="assessment", null=False, blank=False)
    question = RichTextField(null=False, blank=False)
    answer = models.CharField(max_length=100, null=False, blank=False)
    created_at = models.DateTimeField( default=timezone.now)

    
    def __str__(self):
        return self.type



class QuizScore(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    lesson = models.ForeignKey(Lesson, on_delete=models.CASCADE)
    achievements = models.CharField(max_length=100, null=True)
    score = models.IntegerField(default=0)
    created_at = models.DateTimeField( default=timezone.now)

    def __str__(self):
        return f"{self.user.username} - {self.lesson.title}"



class AssessmentScore(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    lesson = models.ForeignKey(Lesson, on_delete=models.CASCADE)
    achievements = models.CharField(max_length=100, null=True)
    score = models.IntegerField(default=0)
    created_at = models.DateTimeField( default=timezone.now)

    def __str__(self):
        return f"{self.user.username} - {self.lesson.title}"
Leave a Comment