836d8f0d143226e66c4e5227c6a1968f37b22367
[redakcja.git] / apps / wiki / forms.py
1 from django import forms
2 from wiki.models import Document, storage
3
4
5 class DocumentForm(forms.Form):
6     name = forms.CharField(widget=forms.HiddenInput)
7     text = forms.CharField(widget=forms.Textarea)
8     revision = forms.IntegerField(widget=forms.HiddenInput)
9     author = forms.CharField()
10     comment = forms.CharField()
11     
12     def __init__(self, *args, **kwargs):
13         document = kwargs.pop('instance', None)
14         super(DocumentForm, self).__init__(*args, **kwargs)
15         if document:
16             self.fields['name'].initial = document.name
17             self.fields['text'].initial = document.text
18             self.fields['revision'].initial = document.revision()
19     
20     def get_storage(self):
21         return storage
22     
23     def save(self):
24         document = Document(self.get_storage(), name=self.cleaned_data['name'], text=self.cleaned_data['text'])
25         storage.put(document, self.cleaned_data['author'], self.cleaned_data['comment'],
26             self.cleaned_data['revision'])
27         return storage.get(self.cleaned_data['name'])
28