~widelands-dev/widelands-website/trunk

« back to all changes in this revision

Viewing changes to wiki/forms.py

  • Committer: Holger Rapp
  • Date: 2009-02-20 12:25:18 UTC
  • Revision ID: holgerrapp@gmx.net-20090220122518-feaq34ta973snnct
Imported wikiapp into our repository, because we did some local changes (users must be logged in to edit wiki pages)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
import re
 
3
 
 
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 _
 
8
 
 
9
from wiki.models import Article
 
10
from wiki.templatetags.wiki import WIKI_WORD_RE
 
11
 
 
12
wikiword_pattern = re.compile('^' + WIKI_WORD_RE + '$')
 
13
 
 
14
 
 
15
class ArticleForm(forms.ModelForm):
 
16
 
 
17
    summary = forms.CharField(widget=forms.Textarea)
 
18
 
 
19
    comment = forms.CharField(required=False)
 
20
    user_ip = forms.CharField(widget=forms.HiddenInput)
 
21
 
 
22
    content_type = forms.ModelChoiceField(
 
23
        queryset=ContentType.objects.all(),
 
24
        required=False,
 
25
        widget=forms.HiddenInput)
 
26
    object_id = forms.IntegerField(required=False,
 
27
                                   widget=forms.HiddenInput)
 
28
 
 
29
    action = forms.CharField(widget=forms.HiddenInput)
 
30
 
 
31
    class Meta:
 
32
        model = Article
 
33
        exclude = ('creator', 'creator_ip',
 
34
                   'group', 'created_at', 'last_update')
 
35
 
 
36
    def clean_title(self):
 
37
        """ Page title must be a WikiWord.
 
38
        """
 
39
        title = self.cleaned_data['title']
 
40
        if not wikiword_pattern.match(title):
 
41
            raise forms.ValidationError(_('Must be a WikiWord.'))
 
42
 
 
43
        return title
 
44
 
 
45
    def clean(self):
 
46
        super(ArticleForm, self).clean()
 
47
        kw = {}
 
48
 
 
49
        if self.cleaned_data['action'] == 'create':
 
50
            try:
 
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']
 
54
            except KeyError:
 
55
                pass # some error in this fields
 
56
            else:
 
57
                if Article.objects.filter(**kw).count():
 
58
                    raise forms.ValidationError(
 
59
                        _("An article with this title already exists."))
 
60
 
 
61
        return self.cleaned_data
 
62
 
 
63
    def save(self):
 
64
        # 0 - Extra data
 
65
        editor_ip = self.cleaned_data['user_ip']
 
66
        comment = self.cleaned_data['comment']
 
67
 
 
68
        # 1 - Get the old stuff before saving
 
69
        if self.instance.id is None:
 
70
            old_title = old_content = old_markup = ''
 
71
            new = True
 
72
        else:
 
73
            old_title = self.instance.title
 
74
            old_content = self.instance.content
 
75
            old_markup = self.instance.markup
 
76
            new = False
 
77
 
 
78
        # 2 - Save the Article
 
79
        article = super(ArticleForm, self).save()
 
80
 
 
81
        # 3 - Set creator and group
 
82
        editor = getattr(self, 'editor', None)
 
83
        group = getattr(self, 'group', None)
 
84
        if new:
 
85
            article.creator_ip = editor_ip
 
86
            if editor is not None:
 
87
                article.creator = editor
 
88
                article.group = group
 
89
            article.save()
 
90
 
 
91
        # 4 - Create new revision
 
92
        changeset = article.new_revision(
 
93
            old_content, old_title, old_markup,
 
94
            comment, editor_ip, editor)
 
95
 
 
96
        return article, changeset
 
97
 
 
98
 
 
99
 
 
100
class SearchForm(forms.Form):
 
101
    search_term = forms.CharField(required=True)