This commit is contained in:
Holger Sielaff
2024-07-03 21:41:03 +02:00
commit 030d3c8059
45 changed files with 2025 additions and 0 deletions

0
quiz/__init__.py Normal file
View File

18
quiz/admin.py Normal file
View File

@@ -0,0 +1,18 @@
from django.contrib import admin
from quiz.models import Quiz, Question
class QuizQuestionInlineAdmin(admin.StackedInline):
model = Question
@admin.register(Quiz)
class QuizAdmin(admin.ModelAdmin):
inlines = (QuizQuestionInlineAdmin,)
list_display = ('name', 'download_urls')
search_fields = (
'name', 'question__question__name', 'question__question__question',
'question__question__awnser', 'question__question__level__value',
'question__question__label_name',
)

6
quiz/apps.py Normal file
View File

@@ -0,0 +1,6 @@
from django.apps import AppConfig
class QuizConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'quiz'

View File

@@ -0,0 +1,40 @@
# Generated by Django 4.2.13 on 2024-07-01 08:27
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
('content', '0005_question_buzzword'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='Quiz',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('author', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL)),
],
),
migrations.CreateModel(
name='Question',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('points', models.DecimalField(decimal_places=1, default=1.0, max_digits=10)),
('order', models.IntegerField(default=1)),
('description', models.TextField(blank=True, null=True)),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('question', models.ForeignKey(on_delete=django.db.models.deletion.RESTRICT, related_name='content_question', to='content.question')),
('quiz', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='quiz.quiz')),
],
),
]

View File

@@ -0,0 +1,19 @@
# Generated by Django 4.2.13 on 2024-07-01 09:08
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('quiz', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='quiz',
name='name',
field=models.CharField(default='Moepquiz', max_length=250),
preserve_default=False,
),
]

View File

@@ -0,0 +1,22 @@
# Generated by Django 4.2.13 on 2024-07-02 08:09
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import lib.middleware.current_user
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('quiz', '0002_quiz_name'),
]
operations = [
migrations.AlterField(
model_name='quiz',
name='author',
field=models.ForeignKey(default=lib.middleware.current_user.get_current_user, null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL),
),
]

View File

135
quiz/models.py Normal file
View File

@@ -0,0 +1,135 @@
import csv
import json
import os.path
import zipfile
import pdfkit
import requests
from django.contrib.auth.models import User
from django.db import models
from django.template.loader import render_to_string
from django.utils.safestring import mark_safe
from content.models import Question as QuestionContent
from lib import get_current_user
from tablequizwiki.settings import BASE_DIR
class Quiz(models.Model):
name = models.CharField(max_length=250)
author = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, default=get_current_user)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
@property
def download_urls(self):
return mark_safe(
f'<a href="/quiz/as_table/{self.id}/" target="_blank">Tabelle</a>'
' | '
f'<a href="/quiz/as_csv/{self.id}/" target="_blank">CSV</a>'
' | '
f'<a href="/quiz/as_pdf/{self.id}/" target="_blank">PDF</a>'
' | '
f'<a href="/quiz/as_zip/{self.id}/" target="_blank">ZIP</a>'
)
def to_view(self, for_players: bool = False):
data = {
'name': self.name,
'items': [],
'json': '',
}
for _q in self.question_set.prefetch_related('question').order_by('order').all():
data['items'].append(_q.to_view(for_players=for_players))
data['json'] = json.dumps(data['items'], indent=4, default=repr)
return data
def to_pdf(self, fh=None):
html = render_to_string('quiz/as_table.html', {'print': True, **self.to_view(), })
pdfkit.from_string(
html,
self.pdfpath,
options={
'orientation': 'landscape',
}
)
return self.pdfpath
def to_csv(self, fh=None):
with open(self.csvpath, 'w') as cf:
items = self.to_view()['items']
keys = items[0].keys()
dw = csv.DictWriter(cf, fieldnames=list(keys))
dw.writeheader()
dw.writerows(items)
return self.csvpath
def to_zip(self, fh=None):
with zipfile.ZipFile(self.zippath, 'w') as zf:
zf.write(self.to_pdf(), arcname=os.path.basename(self.pdfpath))
zf.write(self.to_csv(), arcname=os.path.basename(self.csvpath))
medias = set(list(self.medias))
links = set(list(self.links))
for media in medias:
zf.write(media, arcname='media/' + os.path.basename(media))
for link in links:
filename = link.replace('/', '-').split('?')[0].strip(' \n/') + '.html'
with open(f'/tmp/{filename}', 'w') as htmlfile:
txt = ''
try:
response = requests.get(link)
if response.ok:
htmlfile.write(response.text)
else:
htmlfile.write(f"Could not download {link} - {response}")
except Exception as e:
htmlfile.write(f"Could not download {link} - {e}")
zf.write(f'/tmp/{filename}', arcname='links/' + filename)
return self.zippath
@property
def links(self):
for q in self.question_set.all():
for link in q.question.links.all():
yield link.url
@property
def medias(self):
for q in self.question_set.all():
for media in q.question.medias.all():
yield media.file.path
@property
def zippath(self):
return str(BASE_DIR) + f'/filestore/zip/quiz.{self.id}.zip'
@property
def pdfpath(self):
return str(BASE_DIR) + f'/filestore/pdf/quiz.{self.id}.pdf'
@property
def csvpath(self):
return str(BASE_DIR) + f'/filestore/csv/quiz.{self.id}.csv'
class Question(models.Model):
question = models.ForeignKey(QuestionContent, on_delete=models.RESTRICT, related_name='content_question')
quiz = models.ForeignKey(Quiz, on_delete=models.CASCADE)
points = models.DecimalField(max_digits=10, decimal_places=1, default=1.0)
order = models.IntegerField(default=1)
description = models.TextField(null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return str(self.question)
def to_view(self, for_players: bool = False):
ret = self.question.to_view(for_players=for_players)
ret['points'] = self.points
return ret

View File

View File

@@ -0,0 +1,14 @@
import re
from django import template
from django.template.defaultfilters import stringfilter
register = template.Library()
@register.filter(name='replace')
@stringfilter
def replace(value: str, args: str):
args = re.sub('( +| *, *| *; *)', '§', args)
args = args.split('§')
return value.replace(args[0], args[1])

3
quiz/tests.py Normal file
View File

@@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

10
quiz/urls.py Normal file
View File

@@ -0,0 +1,10 @@
from django.urls import path
from quiz.views import quiz_as_table, quiz_as_pdf, quiz_as_csv, quiz_as_zip
urlpatterns = [
path('as_table/<int:id>/', quiz_as_table, name='quiz-as-table'),
path('as_pdf/<int:id>/', quiz_as_pdf, name='quiz-as-pdf'),
path('as_csv/<int:id>/', quiz_as_csv, name='quiz-as-csv'),
path('as_zip/<int:id>/', quiz_as_zip, name='quiz-as-zip'),
]

51
quiz/views.py Normal file
View File

@@ -0,0 +1,51 @@
from mimetypes import guess_type
from django.contrib.auth.decorators import login_required
from django.http.response import HttpResponseNotFound, HttpResponse
from django.shortcuts import render
from quiz.models import Quiz
def quiz_as_file(request, id: int, filetype: str):
try:
assert filetype in ('pdf', 'csv', 'zip')
quiz = Quiz.objects.get(id=int(id))
except AssertionError:
return HttpResponseNotFound('Wrong filetype')
except Quiz.DoesNotExist:
return HttpResponseNotFound('Not found')
mimetype, __ = guess_type(f'/tmp/a.{filetype}')
_path = getattr(quiz, f'to_{filetype}')()
with open(_path, 'rb') as _content:
response = HttpResponse(content_type=mimetype)
response['Content-Disposition'] = f'attachment; filename=quiz.{id}.{filetype}'
response.write(_content.read())
return response
@login_required(login_url='/admin/login/')
def quiz_as_table(request, id):
try:
quiz = Quiz.objects.get(id=int(id))
except Quiz.DoesNotExist:
return HttpResponseNotFound('Not found')
return render(request, 'quiz/as_table.html', quiz.to_view())
@login_required(login_url='/admin/login/')
def quiz_as_pdf(request, id):
return quiz_as_file(request, id, 'pdf')
@login_required(login_url='/admin/login/')
def quiz_as_csv(request, id):
return quiz_as_file(request, id, 'csv')
@login_required(login_url='/admin/login/')
def quiz_as_zip(request, id):
return quiz_as_file(request, id, 'zip')