-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathviews.py
87 lines (65 loc) · 2.38 KB
/
views.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from django.contrib import messages
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse
from django.views.generic import (
CreateView,
DetailView,
FormView,
ListView,
TemplateView,
)
from django.views.generic.detail import SingleObjectMixin
from .forms import PublisherBooksWithImagesFormset
from .models import Publisher, Book, BookImage
class HomeView(TemplateView):
template_name = "books/home.html"
class PublisherListView(ListView):
model = Publisher
template_name = "books/publisher_list.html"
class PublisherDetailView(DetailView):
model = Publisher
template_name = "books/publisher_detail.html"
class PublisherCreateView(CreateView):
"""
Only for creating a new publisher. Adding books to it is done in the
PublisherBooksUpdateView().
"""
model = Publisher
template_name = "books/publisher_create.html"
fields = [
"name",
]
def form_valid(self, form):
messages.add_message(self.request, messages.SUCCESS, "The publisher was added.")
return super().form_valid(form)
class PublisherBooksUpdateView(SingleObjectMixin, FormView):
"""
For adding books to a Publisher, or editing them.
"""
model = Publisher
template_name = "books/publisher_books_update.html"
def get(self, request, *args, **kwargs):
# The Publisher we're editing:
self.object = self.get_object(queryset=Publisher.objects.all())
return super().get(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
# The Publisher we're uploading for:
self.object = self.get_object(queryset=Publisher.objects.all())
return super().post(request, *args, **kwargs)
def get_form(self, form_class=None):
"""
Use our big formset of formsets, and pass in the Publisher object.
"""
return PublisherBooksWithImagesFormset(
**self.get_form_kwargs(), instance=self.object
)
def form_valid(self, form):
"""
If the form is valid, redirect to the supplied URL.
"""
form.save()
messages.add_message(self.request, messages.SUCCESS, "Changes were saved.")
return HttpResponseRedirect(self.get_success_url())
def get_success_url(self):
return reverse("books:publisher_detail", kwargs={"pk": self.object.pk})