1
# -*- coding: utf-8 -*-
4
from django import forms
5
from django.forms import widgets
6
from django.contrib.contenttypes.models import ContentType
7
from django.utils.translation import ugettext_lazy as _
9
from wiki.models import Article
10
from wiki.templatetags.wiki import WIKI_WORD_RE
12
wikiword_pattern = re.compile('^' + WIKI_WORD_RE + '$')
15
class ArticleForm(forms.ModelForm):
17
summary = forms.CharField(widget=forms.Textarea)
19
comment = forms.CharField(required=False)
20
user_ip = forms.CharField(widget=forms.HiddenInput)
22
content_type = forms.ModelChoiceField(
23
queryset=ContentType.objects.all(),
25
widget=forms.HiddenInput)
26
object_id = forms.IntegerField(required=False,
27
widget=forms.HiddenInput)
29
action = forms.CharField(widget=forms.HiddenInput)
33
exclude = ('creator', 'creator_ip',
34
'group', 'created_at', 'last_update')
36
def clean_title(self):
37
""" Page title must be a WikiWord.
39
title = self.cleaned_data['title']
40
if not wikiword_pattern.match(title):
41
raise forms.ValidationError(_('Must be a WikiWord.'))
46
super(ArticleForm, self).clean()
49
if self.cleaned_data['action'] == 'create':
51
kw['title'] = self.cleaned_data['title']
52
kw['content_type'] = self.cleaned_data['content_type']
53
kw['object_id'] = self.cleaned_data['object_id']
55
pass # some error in this fields
57
if Article.objects.filter(**kw).count():
58
raise forms.ValidationError(
59
_("An article with this title already exists."))
61
return self.cleaned_data
65
editor_ip = self.cleaned_data['user_ip']
66
comment = self.cleaned_data['comment']
68
# 1 - Get the old stuff before saving
69
if self.instance.id is None:
70
old_title = old_content = old_markup = ''
73
old_title = self.instance.title
74
old_content = self.instance.content
75
old_markup = self.instance.markup
78
# 2 - Save the Article
79
article = super(ArticleForm, self).save()
81
# 3 - Set creator and group
82
editor = getattr(self, 'editor', None)
83
group = getattr(self, 'group', None)
85
article.creator_ip = editor_ip
86
if editor is not None:
87
article.creator = editor
91
# 4 - Create new revision
92
changeset = article.new_revision(
93
old_content, old_title, old_markup,
94
comment, editor_ip, editor)
96
return article, changeset
100
class SearchForm(forms.Form):
101
search_term = forms.CharField(required=True)