Add documentation and source
Added documentation, source and extra files.
This commit is contained in:
2
source/orm/library/views/__init__.py
Normal file
2
source/orm/library/views/__init__.py
Normal file
@ -0,0 +1,2 @@
|
||||
from .base import *
|
||||
from .person import *
|
13
source/orm/library/views/base.py
Normal file
13
source/orm/library/views/base.py
Normal file
@ -0,0 +1,13 @@
|
||||
from annoying.decorators import render_to
|
||||
from django.http import HttpRequest
|
||||
from django.views.decorators.cache import cache_page
|
||||
|
||||
from library.models import Person, Book
|
||||
|
||||
|
||||
@cache_page(30)
|
||||
@render_to("library/index-page.html")
|
||||
def view_index(request: HttpRequest) -> dict: # noqa
|
||||
people = Person.objects.all()
|
||||
books = Book.objects.all()
|
||||
return {"people": people, "books": books}
|
30
source/orm/library/views/person.py
Normal file
30
source/orm/library/views/person.py
Normal file
@ -0,0 +1,30 @@
|
||||
from typing import Union
|
||||
|
||||
from annoying.decorators import render_to
|
||||
from django.contrib import messages
|
||||
from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned
|
||||
from django.http import HttpRequest, Http404, HttpResponseRedirect, HttpResponse
|
||||
|
||||
from library.forms import PersonForm
|
||||
from library.models import Person
|
||||
|
||||
|
||||
@render_to("library/person-edit-page.html")
|
||||
def edit_person(request: HttpRequest, uuid: str) -> Union[dict, HttpResponse]: # noqa
|
||||
try:
|
||||
person = Person.objects.get(uuid=uuid)
|
||||
form = (
|
||||
PersonForm(instance=person)
|
||||
if request.method == "GET"
|
||||
else PersonForm(request.POST, files=request.FILES, instance=person)
|
||||
)
|
||||
if form.is_valid():
|
||||
# Save the form data in the database, automatically saves files
|
||||
recorded = form.save(commit=True)
|
||||
# Use the messages application to display a message on the next view.
|
||||
messages.success(request, f"{recorded} was successfully updated.")
|
||||
return HttpResponseRedirect("")
|
||||
return {"person": person, "form": form}
|
||||
except (ObjectDoesNotExist, MultipleObjectsReturned):
|
||||
# Ask Django view system to render the 404 page.
|
||||
raise Http404()
|
Reference in New Issue
Block a user