diff --git a/content/models.py b/content/models.py index 1edad1d..5eade18 100644 --- a/content/models.py +++ b/content/models.py @@ -1,5 +1,5 @@ +import csv import json -import logging import os from colorfield.fields import ColorField @@ -12,6 +12,7 @@ from filer.fields.file import FilerFileField from lib.core.db.models.base import SharedPermissionBase from lib.core.db.models.mixins import DateAware, AuthorAware, DescriptionAware, NameAware, PublishedAware +from tablequizwiki.settings import BASE_DIR class MediaFile(NameAware, DateAware, AuthorAware, DescriptionAware): @@ -88,37 +89,59 @@ class Question(DateAware, AuthorAware, PublishedAware, DescriptionAware): | models.Q(buzzword__icontains=term) \ | models.Q(labels__name__icontains=term) - @staticmethod def get_by_tearchterm(term, queryset=None): if not queryset: queryset = Question.objects return queryset.filter(Question.searchdomain(term)).annotate(cnt=models.Count('id')) + def to_view(self, for_players: bool = False): + ret = {} + ret['name'] = '{}{}'.format(self.name, f'({self.buzzword})' if self.buzzword else '') + ret['question'] = self.question + if self.description: + ret['question'] += ' \n\n------------------------------\n\n' + self.description + ret['awnser'] = self.awnser + ret['level'] = self.level.value + ret['medias'] = [] + ret['links'] = [] + for media in self.medias.all(): + ret.setdefault('medias', []).append(os.path.basename(media.file.path)) + for link in self.links.all(): + ret.setdefault('links', []).append(link.url) + ret['question'] = mark_safe(ret['question'].replace('\n', '\n
')) + return ret -def to_view(self, for_players: bool = False): - ret = {} - ret['name'] = '{}{}'.format(self.name, f'({self.buzzword})' if self.buzzword else '') - ret['question'] = self.question - if self.description: - ret['question'] += ' \n\n------------------------------\n\n' + self.description - ret['awnser'] = self.awnser - ret['level'] = self.level.value - ret['medias'] = [] - ret['links'] = [] - for media in self.medias.all(): - ret.setdefault('medias', []).append(os.path.basename(media.file.path)) - for link in self.links.all(): - ret.setdefault('links', []).append(link.url) - ret['question'] = mark_safe(ret['question'].replace('\n', '\n
')) - logging.error(ret) - return ret + @property + def zippath(self): + return str(BASE_DIR) + f'/filestore/zip/question.{self.id}.zip' + @property + def pdfpath(self): + return str(BASE_DIR) + f'/filestore/pdf/question.{self.id}.pdf' -def default_json(o): - if isinstance(o, (MediaFile,)): - return str(o) - return json.dumps(model_to_dict(o)) + @property + def csvpath(self): + return str(BASE_DIR) + f'/filestore/csv/question.{self.id}.csv' + + def default_json(o): + if isinstance(o, (MediaFile,)): + return str(o) + return json.dumps(model_to_dict(o)) + + def to_csv(self, fh=None): + if fh: + cf = fh + else: + cf = open(self.csvpath, 'w') + items = self.to_view() + keys = items.keys() + dw = csv.DictWriter(cf, fieldnames=list(keys)) + dw.writeheader() + dw.writerow(items) + if not fh: + cf.close() + return cf if fh else self.csvpath class QuestionVersion(DateAware, AuthorAware): diff --git a/content/templates/questions.html b/content/templates/questions.html index 752325a..612cbeb 100644 --- a/content/templates/questions.html +++ b/content/templates/questions.html @@ -1,5 +1,6 @@ {% extends 'base.html' %} {% block content %} +

Questions

@@ -23,6 +24,7 @@
Question
Files
Labels
+
{% for q in items %}
@@ -36,6 +38,9 @@ {% if not forloop.last %},{% endif %} {% endfor %}
+
+ CSV +
{% endfor %} {% endif %} diff --git a/content/urls.py b/content/urls.py index afa13d5..861c6f3 100644 --- a/content/urls.py +++ b/content/urls.py @@ -3,5 +3,6 @@ from content.views import public urlpatterns = [ path('questions/', public.search_question, name='search-questions'), + path('questions//csv/', public.as_csv, name='csv-question'), path('', public.search_question, name='search-questions'), ] diff --git a/content/views/public.py b/content/views/public.py index 18bfe29..386afa1 100644 --- a/content/views/public.py +++ b/content/views/public.py @@ -1,3 +1,4 @@ +from django.http import HttpResponseNotFound, HttpResponse, HttpResponseBadRequest from django.shortcuts import render from content.models import Question @@ -12,3 +13,14 @@ def search_question(request): else: items = published.all()[:10] return render(request, 'questions.html', {'items': items}) + + +def as_csv(request, **kwargs): + try: + q = Question.objects.get(id=kwargs['id']) + response = HttpResponse(content_type='text/csv') + return q.to_csv(fh=response) + except KeyError: + return HttpResponseBadRequest('No id given') + except Question.DoesNotExist: + return HttpResponseNotFound('Question not found') diff --git a/quiz/models.py b/quiz/models.py index 4b39135..2476dec 100644 --- a/quiz/models.py +++ b/quiz/models.py @@ -62,13 +62,18 @@ class Quiz(AuthorAware, DateAware, PublishedAware): 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 + if fh: + cf = fh + else: + cf = open(self.csvpath, 'w') + items = self.to_view()['items'] + keys = items[0].keys() + dw = csv.DictWriter(cf, fieldnames=list(keys)) + dw.writeheader() + dw.writerows(items) + if not fh: + cf.close() + return cf if fh else self.csvpath def to_zip(self, fh=None): with zipfile.ZipFile(self.zippath, 'w') as zf: diff --git a/templates/base.html b/templates/base.html index c846369..f6e17f7 100644 --- a/templates/base.html +++ b/templates/base.html @@ -11,31 +11,40 @@ -moz-border-radius: 4px; border-radius: 4px; } + .input-group-unstyled .input-group-addon { border-radius: 4px; border: 0px; background-color: transparent; } - - - - + + + + {# #} -
+
-

Tablequiz DB

- -
- {% block content %} - {% endblock %} +
+
+

Tablequiz DB

+
+
+ -
+
+
+ +
+ {% block content %} + {% endblock %} + +
\ No newline at end of file