This commit is contained in:
Holger Sielaff
2024-09-10 12:32:53 +02:00
parent 1531b0f6d3
commit be447f4862
9 changed files with 179 additions and 7 deletions

View File

@@ -1,11 +1,11 @@
import csv
from datetime import datetime
import json
import os
from colorfield.fields import ColorField
from django.contrib.auth.models import User
from django.db import models
from django.dispatch import receiver
from django.forms.models import model_to_dict
from django.utils.safestring import mark_safe
from filer.fields.file import FilerFileField
@@ -63,10 +63,55 @@ class SharedQuestion(SharedPermissionBase):
abstract = False
class SubmittedQuestion(DateAware):
name = models.CharField(max_length=500, unique=True)
buzzword = models.CharField(max_length=25, null=True, blank=True)
question = models.TextField()
awnser = models.TextField()
level = models.ForeignKey(Level, on_delete=models.SET_NULL, null=True, blank=True)
labels = models.ManyToManyField(Label, blank=True)
medias = models.ManyToManyField(MediaFile, blank=True)
links = models.ManyToManyField(Link, blank=True)
shares = models.ManyToManyField(SharedQuestion, blank=True)
release = models.BooleanField(default=False)
released = models.BooleanField(default=False)
released_at = models.DateTimeField(null=True,blank=True)
def __str__(self):
return f"{self.name} (#{self.id})"
from django.db.models.signals import post_save
from django.dispatch import receiver
@receiver(post_save, sender=SubmittedQuestion)
def release_submitted(sender, instance: SubmittedQuestion, **kwargs):
if instance.release:
n = Question.objects.create(
name=instance.name,
buzzword=instance.buzzword,
question=instance.question,
awnser=instance.awnser,
)
try:
n.labels.set(instance.labels.all())
n.level_id = instance.level_id
n.save()
except:
n.delete()
raise
instance.released = True
instance.release = False
instance.released_at = datetime.now()
instance.save()
class Question(DateAware, AuthorAware, PublishedAware, DescriptionAware):
name = models.CharField(max_length=500, unique=True, db_index=True)
question = models.TextField(db_index=True)
buzzword = models.CharField(max_length=25, null=True, blank=True)
question = models.TextField(db_index=True)
awnser = models.TextField(db_index=True)
level = models.ForeignKey(Level, on_delete=models.SET_NULL, null=True)
labels = models.ManyToManyField(Label, blank=True)