4
4
from django import forms
5
from django.forms import widgets
5
6
from django.contrib.contenttypes.models import ContentType
6
7
from django.utils.translation import ugettext_lazy as _
8
9
from wiki.models import Article
9
from wiki.models import ChangeSet
10
from settings import WIKI_WORD_RE
12
from notification import models as notification
10
from wiki.templatetags.wiki_extras import WIKI_WORD_RE
17
12
wikiword_pattern = re.compile('^' + WIKI_WORD_RE + '$')
22
17
summary = forms.CharField(widget=forms.Textarea)
24
19
comment = forms.CharField(required=False)
20
user_ip = forms.CharField(widget=forms.HiddenInput)
26
22
content_type = forms.ModelChoiceField(
27
23
queryset=ContentType.objects.all(),
37
exclude = ('creator', 'group', 'created_at', 'last_update')
33
exclude = ('creator', 'creator_ip',
34
'group', 'created_at', 'last_update')
39
36
def clean_title(self):
40
"""Check for some errors regarding the title:
42
1. Check for bad characters
43
2. Check for already used titles
45
Immediately trying to change the title of a new article to an existing title
46
is handled on the database level.
37
""" Page title must be a WikiWord.
50
39
title = self.cleaned_data['title']
51
40
if not wikiword_pattern.match(title):
52
raise forms.ValidationError(
53
_('Only alphanumeric characters, blank spaces and the underscore are allowed in a title.'))
55
# 'self.initial' contains the prefilled values of the form
56
pre_title = self.initial.get('title', None)
57
if pre_title != title or not pre_title:
58
# Check if the new name has been used already
59
cs = ChangeSet.objects.filter(old_title=title)
61
raise forms.ValidationError(
62
_('The title %(title)s is already in use, maybe an other article used to have this name.'), params={'title': title},)
64
# title not changed, no errors
41
raise forms.ValidationError(_('Must be a WikiWord.'))
74
52
kw['content_type'] = self.cleaned_data['content_type']
75
53
kw['object_id'] = self.cleaned_data['object_id']
77
pass # some error in this fields
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."))
79
61
return self.cleaned_data
99
82
editor = getattr(self, 'editor', None)
100
83
group = getattr(self, 'group', None)
85
article.creator_ip = editor_ip
102
86
if editor is not None:
103
87
article.creator = editor
104
88
article.group = group
105
89
article.save(*args, **kwargs)
107
notification.observe(article, editor, 'wiki_observed_article_changed')
109
91
# 4 - Create new revision
110
92
changeset = article.new_revision(
111
93
self.old_content, self.old_title, self.old_markup,
94
comment, editor_ip, editor)
114
96
return article, changeset