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