2018-05-21 22:04:29 +02:00
|
|
|
from django.shortcuts import get_object_or_404
|
|
|
|
from django.views.generic import CreateView, FormView, TemplateView
|
2018-05-21 19:28:36 +02:00
|
|
|
|
2018-05-21 22:23:53 +02:00
|
|
|
from manuels.forms import AddBookForm
|
|
|
|
from manuels.models import Teacher, Book
|
2018-05-21 21:03:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
class HomePageView(CreateView):
|
|
|
|
model = Teacher
|
2018-05-21 22:04:29 +02:00
|
|
|
fields = ['first_name', 'last_name', 'phone_number']
|
2018-05-21 21:03:08 +02:00
|
|
|
template_name = 'manuels/home_page.html'
|
2018-05-21 22:04:29 +02:00
|
|
|
|
|
|
|
|
2018-05-21 22:23:53 +02:00
|
|
|
class AddBookView(CreateView):
|
|
|
|
model = Book
|
2018-05-21 22:04:29 +02:00
|
|
|
template_name = 'manuels/add_book.html'
|
2018-05-21 22:23:53 +02:00
|
|
|
form_class = AddBookForm
|
|
|
|
teacher = None
|
|
|
|
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
self.teacher = get_object_or_404(Teacher, pk=self.kwargs['id'])
|
|
|
|
return super().dispatch(request, *args, **kwargs)
|
2018-05-21 22:04:29 +02:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data()
|
2018-05-21 22:23:53 +02:00
|
|
|
context['teacher'] = self.teacher
|
2018-05-21 22:04:29 +02:00
|
|
|
|
|
|
|
return context
|
2018-05-21 22:23:53 +02:00
|
|
|
|
|
|
|
def get_initial(self):
|
|
|
|
return {
|
|
|
|
'teacher': self.teacher
|
|
|
|
}
|
|
|
|
|
|
|
|
def get_form(self, form_class=None):
|
|
|
|
form = super().get_form(form_class)
|
|
|
|
form.fields['teacher'].queryset = Teacher.objects.filter(pk=self.teacher.pk)
|
|
|
|
return form
|