~widelands-dev/widelands-website/django_staticfiles

« back to all changes in this revision

Viewing changes to wiki/forms.py

  • Committer: Holger Rapp
  • Date: 2009-02-19 15:31:42 UTC
  • Revision ID: sirver@h566336-20090219153142-dc8xuabldnw5t395
Initial commit of new widelands homepage

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.contrib.contenttypes.models import ContentType
6
 
from django.utils.translation import ugettext_lazy as _
7
 
 
8
 
from wiki.models import Article
9
 
from wiki.models import ChangeSet
10
 
from settings import WIKI_WORD_RE
11
 
try:
12
 
    from notification import models as notification
13
 
except:
14
 
    notification = None
15
 
 
16
 
 
17
 
wikiword_pattern = re.compile('^' + WIKI_WORD_RE + '$')
18
 
 
19
 
 
20
 
class ArticleForm(forms.ModelForm):
21
 
 
22
 
    summary = forms.CharField(widget=forms.Textarea)
23
 
 
24
 
    comment = forms.CharField(required=False)
25
 
 
26
 
    content_type = forms.ModelChoiceField(
27
 
        queryset=ContentType.objects.all(),
28
 
        required=False,
29
 
        widget=forms.HiddenInput)
30
 
    object_id = forms.IntegerField(required=False,
31
 
                                   widget=forms.HiddenInput)
32
 
 
33
 
    action = forms.CharField(widget=forms.HiddenInput)
34
 
 
35
 
    class Meta:
36
 
        model = Article
37
 
        exclude = ('creator', 'group', 'created_at', 'last_update')
38
 
 
39
 
    def clean_title(self):
40
 
        """Check for some errors regarding the title:
41
 
 
42
 
        1. Check for bad characters
43
 
        2. Check for already used titles
44
 
 
45
 
        Immediately trying to change the title of a new article to an existing title
46
 
        is handled on the database level.
47
 
 
48
 
        """
49
 
 
50
 
        title = self.cleaned_data['title']
51
 
        if not wikiword_pattern.match(title):
52
 
            raise forms.ValidationError(
53
 
                _('Only alphanumeric characters, blank spaces and the underscore are allowed in a title.'))
54
 
 
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)
60
 
            if cs:
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},)
63
 
 
64
 
        # title not changed, no errors
65
 
        return title
66
 
 
67
 
    def clean(self):
68
 
        super(ArticleForm, self).clean()
69
 
        kw = {}
70
 
 
71
 
        if self.cleaned_data['action'] == 'create':
72
 
            try:
73
 
                kw['title'] = self.cleaned_data['title']
74
 
                kw['content_type'] = self.cleaned_data['content_type']
75
 
                kw['object_id'] = self.cleaned_data['object_id']
76
 
            except KeyError:
77
 
                pass  # some error in this fields
78
 
 
79
 
        return self.cleaned_data
80
 
 
81
 
    def cache_old_content(self):
82
 
        if self.instance.id is None:
83
 
            self.old_title = self.old_content = self.old_markup = ''
84
 
            self.is_new = True
85
 
        else:
86
 
            self.old_title = self.instance.title
87
 
            self.old_content = self.instance.content
88
 
            self.old_markup = self.instance.markup
89
 
            self.is_new = False
90
 
 
91
 
    def save(self, *args, **kwargs):
92
 
        # 0 - Extra data
93
 
        comment = self.cleaned_data['comment']
94
 
 
95
 
        # 2 - Save the Article
96
 
        article = super(ArticleForm, self).save(*args, **kwargs)
97
 
 
98
 
        # 3 - Set creator and group
99
 
        editor = getattr(self, 'editor', None)
100
 
        group = getattr(self, 'group', None)
101
 
        if self.is_new:
102
 
            if editor is not None:
103
 
                article.creator = editor
104
 
                article.group = group
105
 
            article.save(*args, **kwargs)
106
 
            if notification:
107
 
                notification.observe(article, editor, 'wiki_observed_article_changed')
108
 
 
109
 
        # 4 - Create new revision
110
 
        changeset = article.new_revision(
111
 
            self.old_content, self.old_title, self.old_markup,
112
 
            comment, editor)
113
 
 
114
 
        return article, changeset