django-flashdeck 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. django_flashdeck-0.1.0.dist-info/METADATA +215 -0
  2. django_flashdeck-0.1.0.dist-info/RECORD +53 -0
  3. django_flashdeck-0.1.0.dist-info/WHEEL +4 -0
  4. django_flashdeck-0.1.0.dist-info/licenses/LICENSE +28 -0
  5. flashdeck/__init__.py +1 -0
  6. flashdeck/admin.py +68 -0
  7. flashdeck/apps.py +8 -0
  8. flashdeck/bundles.py +346 -0
  9. flashdeck/conf.py +19 -0
  10. flashdeck/images.py +85 -0
  11. flashdeck/management/__init__.py +0 -0
  12. flashdeck/management/commands/__init__.py +0 -0
  13. flashdeck/management/commands/flashdeck_export.py +23 -0
  14. flashdeck/management/commands/flashdeck_import.py +29 -0
  15. flashdeck/manifest.py +33 -0
  16. flashdeck/marking.py +107 -0
  17. flashdeck/migrations/0001_initial.py +146 -0
  18. flashdeck/migrations/0002_card_options.py +44 -0
  19. flashdeck/migrations/0003_reference.py +28 -0
  20. flashdeck/migrations/__init__.py +0 -0
  21. flashdeck/models.py +325 -0
  22. flashdeck/practice.py +232 -0
  23. flashdeck/reference.py +108 -0
  24. flashdeck/scheduling.py +75 -0
  25. flashdeck/static/flashdeck/flashdeck.css +241 -0
  26. flashdeck/static/flashdeck/flashdeck.js +137 -0
  27. flashdeck/stats.py +248 -0
  28. flashdeck/templates/flashdeck/_account.html +1 -0
  29. flashdeck/templates/flashdeck/_assets.html +2 -0
  30. flashdeck/templates/flashdeck/_crumbs.html +1 -0
  31. flashdeck/templates/flashdeck/_crumbs_root.html +1 -0
  32. flashdeck/templates/flashdeck/_forms.html +3 -0
  33. flashdeck/templates/flashdeck/_prompt.html +5 -0
  34. flashdeck/templates/flashdeck/_reference_dialog.html +8 -0
  35. flashdeck/templates/flashdeck/_search.html +5 -0
  36. flashdeck/templates/flashdeck/admin/bundles.html +24 -0
  37. flashdeck/templates/flashdeck/fragment.html +1 -0
  38. flashdeck/templates/flashdeck/index.html +29 -0
  39. flashdeck/templates/flashdeck/nothing_due.html +21 -0
  40. flashdeck/templates/flashdeck/page.html +23 -0
  41. flashdeck/templates/flashdeck/progress.html +80 -0
  42. flashdeck/templates/flashdeck/question.html +46 -0
  43. flashdeck/templates/flashdeck/reference_deck.html +28 -0
  44. flashdeck/templates/flashdeck/reference_item.html +18 -0
  45. flashdeck/templates/flashdeck/reference_subject.html +19 -0
  46. flashdeck/templates/flashdeck/result.html +56 -0
  47. flashdeck/templates/flashdeck/settings.html +44 -0
  48. flashdeck/templatetags/__init__.py +0 -0
  49. flashdeck/templatetags/flashdeck_tags.py +52 -0
  50. flashdeck/urls.py +31 -0
  51. flashdeck/views.py +392 -0
  52. flashdeck/wagtail_hooks.py +109 -0
  53. flashdeck/wagtail_views.py +106 -0
@@ -0,0 +1,109 @@
1
+ """Editing in Wagtail's admin, under a Flashcards menu. Wagtail loads this module
2
+ itself; on a site without Wagtail it is never imported."""
3
+
4
+ from django import forms
5
+ from wagtail.admin.panels import FieldPanel, InlinePanel, MultiFieldPanel
6
+ from wagtail.snippets.models import register_snippet
7
+ from wagtail.snippets.views.snippets import SnippetViewSet, SnippetViewSetGroup
8
+
9
+ from .models import Card, ClaimedAnswer, Deck, Subject
10
+ from .wagtail_views import BundlesViewSet
11
+
12
+
13
+ class SubjectViewSet(SnippetViewSet):
14
+ model = Subject
15
+ icon = "folder-open-inverse"
16
+ list_display = ["title", "slug", "language", "position"]
17
+ panels = [
18
+ FieldPanel("title"),
19
+ FieldPanel("slug"),
20
+ FieldPanel("description"),
21
+ FieldPanel("language"),
22
+ FieldPanel("characters"),
23
+ FieldPanel("case_sensitive"),
24
+ FieldPanel("position"),
25
+ ]
26
+
27
+
28
+ class DeckViewSet(SnippetViewSet):
29
+ model = Deck
30
+ icon = "folder-inverse"
31
+ list_display = ["title", "subject", "is_public", "position"]
32
+ list_filter = ["subject", "is_public"]
33
+ panels = [
34
+ FieldPanel("subject"),
35
+ FieldPanel("title"),
36
+ FieldPanel("slug"),
37
+ FieldPanel("description"),
38
+ FieldPanel("is_public"),
39
+ FieldPanel("position"),
40
+ ]
41
+
42
+
43
+ class CardViewSet(SnippetViewSet):
44
+ model = Card
45
+ icon = "doc-full"
46
+ list_display = ["__str__", "deck", "key"]
47
+ list_filter = ["deck"]
48
+ search_fields = ["prompt", "key"]
49
+ search_backend_name = None # search with the database, not Wagtail's index
50
+ panels = [
51
+ FieldPanel("deck"),
52
+ FieldPanel("key"),
53
+ FieldPanel("prompt"),
54
+ MultiFieldPanel(
55
+ [
56
+ FieldPanel("image"),
57
+ FieldPanel("image_alt"),
58
+ FieldPanel("image_credit"),
59
+ FieldPanel("image_licence"),
60
+ ],
61
+ heading="Image",
62
+ ),
63
+ InlinePanel(
64
+ "answers",
65
+ label="Answer",
66
+ min_num=1,
67
+ panels=[
68
+ FieldPanel("label"),
69
+ FieldPanel("text"),
70
+ FieldPanel("alternatives", widget=forms.Textarea(attrs={"rows": 2})),
71
+ ],
72
+ ),
73
+ FieldPanel("notes"),
74
+ FieldPanel("position"),
75
+ ]
76
+
77
+
78
+ class ClaimedAnswerViewSet(SnippetViewSet):
79
+ model = ClaimedAnswer
80
+ icon = "help"
81
+ menu_label = "Claimed answers"
82
+ list_display = ["text", "answer", "user", "status", "created"]
83
+ list_filter = ["status"]
84
+ panels = [
85
+ FieldPanel("answer", read_only=True),
86
+ FieldPanel("text", read_only=True),
87
+ FieldPanel("user", read_only=True),
88
+ FieldPanel(
89
+ "status",
90
+ help_text="Accepting an answer marks it right for everyone, by adding it "
91
+ "to the card's other answers.",
92
+ ),
93
+ ]
94
+
95
+
96
+ class FlashcardsGroup(SnippetViewSetGroup):
97
+ menu_label = "Flashcards"
98
+ menu_icon = "tasks"
99
+ menu_name = "flashdeck"
100
+ items = (
101
+ SubjectViewSet,
102
+ DeckViewSet,
103
+ CardViewSet,
104
+ ClaimedAnswerViewSet,
105
+ BundlesViewSet,
106
+ )
107
+
108
+
109
+ register_snippet(FlashcardsGroup)
@@ -0,0 +1,106 @@
1
+ """Importing and exporting bundles in Wagtail's admin (Flashcards → Import and
2
+ export), so that a bundle need not be on the server to be imported."""
3
+
4
+ import tempfile
5
+ from pathlib import Path
6
+
7
+ from django import forms
8
+ from django.contrib import messages
9
+ from django.core.exceptions import PermissionDenied
10
+ from django.http import HttpResponse
11
+ from django.shortcuts import redirect
12
+ from django.urls import path
13
+ from django.views.generic import TemplateView
14
+ from wagtail.admin.views.generic.base import WagtailAdminTemplateMixin
15
+ from wagtail.admin.viewsets.base import ViewSet
16
+
17
+ from .bundles import BundleError, export_bundle, import_bundle
18
+ from .models import Subject
19
+
20
+ MAX_UPLOAD = 100 * 1024 * 1024
21
+ PERMISSIONS = ["flashdeck.add_card", "flashdeck.change_card"]
22
+
23
+
24
+ class ImportForm(forms.Form):
25
+ bundle = forms.FileField(
26
+ help_text="A zip file holding cards.yaml and the images it names."
27
+ )
28
+ update = forms.BooleanField(
29
+ required=False,
30
+ label="Update what is already on the site",
31
+ help_text="Subjects, decks and cards already here are updated from the "
32
+ "bundle; answers accepted here are kept, images are replaced only if they "
33
+ "have changed, and whether each deck is public is kept. Otherwise they are left alone and only new "
34
+ "ones are added.",
35
+ )
36
+
37
+ def clean_bundle(self):
38
+ bundle = self.cleaned_data["bundle"]
39
+ if bundle.size > MAX_UPLOAD:
40
+ raise forms.ValidationError("The file is larger than 100 MB.")
41
+ return bundle
42
+
43
+
44
+ class ExportForm(forms.Form):
45
+ subject = forms.ModelChoiceField(queryset=Subject.objects.all())
46
+
47
+
48
+ class BundlesView(WagtailAdminTemplateMixin, TemplateView):
49
+ page_title = "Import and export"
50
+ header_icon = "upload"
51
+ template_name = "flashdeck/admin/bundles.html"
52
+
53
+ def dispatch(self, request, *args, **kwargs):
54
+ if not request.user.has_perms(PERMISSIONS):
55
+ raise PermissionDenied
56
+ return super().dispatch(request, *args, **kwargs)
57
+
58
+ def get_context_data(self, **kwargs):
59
+ kwargs.setdefault("import_form", ImportForm())
60
+ kwargs.setdefault("export_form", ExportForm())
61
+ return super().get_context_data(**kwargs)
62
+
63
+ def post(self, request, *args, **kwargs):
64
+ if request.POST.get("action") == "export":
65
+ return self.export(request)
66
+ form = ImportForm(request.POST, request.FILES)
67
+ if form.is_valid():
68
+ try:
69
+ counts = self.import_upload(form.cleaned_data)
70
+ except BundleError as error:
71
+ form.add_error("bundle", str(error))
72
+ else:
73
+ summary = ", ".join(f"{n} {what}" for what, n in counts.items())
74
+ messages.success(request, f"Imported: {summary or 'nothing new'}.")
75
+ return redirect(request.path)
76
+ return self.render_to_response(self.get_context_data(import_form=form))
77
+
78
+ def import_upload(self, data):
79
+ with tempfile.NamedTemporaryFile(suffix=".zip") as file:
80
+ for chunk in data["bundle"].chunks():
81
+ file.write(chunk)
82
+ file.flush()
83
+ return import_bundle(file.name, update=data["update"])
84
+
85
+ def export(self, request):
86
+ form = ExportForm(request.POST)
87
+ if not form.is_valid():
88
+ return self.render_to_response(self.get_context_data(export_form=form))
89
+ subject = form.cleaned_data["subject"]
90
+ with tempfile.TemporaryDirectory() as directory:
91
+ path = Path(directory) / f"{subject.slug}.zip"
92
+ export_bundle(subject, path)
93
+ content = path.read_bytes()
94
+ response = HttpResponse(content, content_type="application/zip")
95
+ response["Content-Disposition"] = f'attachment; filename="{subject.slug}.zip"'
96
+ return response
97
+
98
+
99
+ class BundlesViewSet(ViewSet):
100
+ name = "flashdeck_bundles"
101
+ url_prefix = "flashcards/bundles"
102
+ icon = "upload"
103
+ menu_label = "Import and export"
104
+
105
+ def get_urlpatterns(self):
106
+ return [path("", BundlesView.as_view(), name="index")]