339bde8c9755ec107daba462ac6299b76f316820
[redakcja.git] / apps / wiki / views.py
1 from django.views.generic.simple import direct_to_template
2 from django.http import HttpResponseRedirect
3
4 from wiki.models import storage, Document, DocumentNotFound
5 from wiki.forms import DocumentForm
6
7
8 def document_list(request, template_name='wiki/document_list.html'):
9     return direct_to_template(request, template_name, extra_context={
10         'document_list': storage.all(),
11     })
12
13
14 def document_detail(request, name, template_name='wiki/document_details.html'):
15     try:
16         document = storage.get(name)
17     except DocumentNotFound:
18         document = Document(storage, name=name, text='')
19         
20     if request.method == 'POST':
21         form = DocumentForm(request.POST, instance=document)
22         if form.is_valid():
23             form.save()
24             return HttpResponseRedirect('/')
25     else:
26         form = DocumentForm(instance=document)
27     
28     return direct_to_template(request, template_name, extra_context={
29         'document': document,
30         'form': form,
31     })