Minor cleanup.
[redakcja.git] / apps / wiki / forms.py
1 # -*- coding: utf-8 -*-
2 #
3 # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.  
5 #
6 from django import forms
7 from wiki.models import Document, getstorage
8
9
10 class DocumentForm(forms.Form):
11     name = forms.CharField(widget=forms.HiddenInput)
12     text = forms.CharField(widget=forms.Textarea)
13     revision = forms.IntegerField(widget=forms.HiddenInput)
14     comment = forms.CharField()
15     
16     def __init__(self, *args, **kwargs):
17         document = kwargs.pop('instance', None)
18         super(DocumentForm, self).__init__(*args, **kwargs)
19         if document:
20             self.fields['name'].initial = document.name
21             self.fields['text'].initial = document.text
22             self.fields['revision'].initial = document.revision()
23         
24     def save(self, document_author = 'anonymous'):
25         storage = getstorage()
26         
27         document = Document(storage, name=self.cleaned_data['name'], text=self.cleaned_data['text'])
28         
29         storage.put(document, 
30                 author = document_author, 
31                 comment = self.cleaned_data['comment'],
32                 parent =self.cleaned_data['revision'] )
33         
34         return storage.get(self.cleaned_data['name'])
35