-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Expand file tree
/
Copy pathviews.py
More file actions
46 lines (32 loc) · 1.32 KB
/
views.py
File metadata and controls
46 lines (32 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import random
from django.shortcuts import get_object_or_404, redirect
from django.urls import reverse_lazy
from django.views.generic import CreateView, ListView, UpdateView
from .forms import CardCheckForm
from .models import Card
class CardListView(ListView):
model = Card
queryset = Card.objects.all().order_by("box", "-date_created")
class CardCreateView(CreateView):
model = Card
fields = ["question", "answer", "box"]
success_url = reverse_lazy("card-create")
class CardUpdateView(CardCreateView, UpdateView):
success_url = reverse_lazy("card-list")
class BoxView(CardListView):
template_name = "cards/box.html"
form_class = CardCheckForm
def get_queryset(self):
return Card.objects.filter(box=self.kwargs["box_num"])
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["box_number"] = self.kwargs["box_num"]
if self.object_list:
context["check_card"] = random.choice(self.object_list)
return context
def post(self, request, *args, **kwargs):
form = self.form_class(request.POST)
if form.is_valid():
card = get_object_or_404(Card, id=form.cleaned_data["card_id"])
card.move(form.cleaned_data["solved"])
return redirect(request.META.get("HTTP_REFERER"))