diff --git a/content/admin.py b/content/admin.py index 1d459b6..61995d2 100644 --- a/content/admin.py +++ b/content/admin.py @@ -1,11 +1,32 @@ from django.contrib import admin from django.utils.safestring import mark_safe -from content.models import Link, MediaFile, Question, QuestionVersion, Level, Label, SharedQuestion +from content.models import Link, MediaFile, Question, QuestionVersion, Level, Label, SharedQuestion, SubmittedQuestion from lib.mixins import PermissionsAdminMixin from lib.utils import color_label +class ActiveFilter(admin.SimpleListFilter): + title = "Aktiv/Inaktiv" + parameter_name = 'released' + + def lookups(self, request, model_admin): + return [ + (1, 'Inaktiv'), + (0, 'Aktiv') + ] + + def queryset(self, request, queryset): + v = int(self.value() or -1) + if v == 1: + return queryset.filter(**{self.parameter_name: True}) + elif v == 0: + return queryset.filter(**{self.parameter_name: False}) + else: + return queryset.filter(**{self.parameter_name: False}) + # return queryset + + @admin.register(SharedQuestion) class SharedQuestionAdmin(admin.ModelAdmin): search_fields = ('user__username',) @@ -25,6 +46,11 @@ class LabelAdmin(PermissionsAdminMixin, admin.ModelAdmin): list_editable = ('color',) +@admin.register(SubmittedQuestion) +class SubmittedQuestionAdmin(admin.ModelAdmin): + list_filter = [ActiveFilter,] + + @admin.register(Question) class QuestionAdmin(PermissionsAdminMixin, admin.ModelAdmin): autocomplete_fields = ('medias', 'links', 'labels', 'shares') diff --git a/content/models.py b/content/models.py index 5eade18..f53871a 100644 --- a/content/models.py +++ b/content/models.py @@ -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) diff --git a/content/templates/questions.html b/content/templates/questions.html index 612cbeb..5957eff 100644 --- a/content/templates/questions.html +++ b/content/templates/questions.html @@ -12,6 +12,7 @@ + {# {{ form.captcha }} #} diff --git a/content/templates/submit-question.html b/content/templates/submit-question.html new file mode 100644 index 0000000..e0a22d6 --- /dev/null +++ b/content/templates/submit-question.html @@ -0,0 +1,50 @@ +{% extends 'base.html' %} +{% block content %} + + {% if messages %} +
+ {% endif %} +