~widelands-dev/widelands-website/django_staticfiles

« back to all changes in this revision

Viewing changes to wiki/forms.py

  • Committer: Holger Rapp
  • Date: 2016-08-08 10:06:42 UTC
  • mto: This revision was merged to the branch mainline in revision 419.
  • Revision ID: sirver@gmx.de-20160808100642-z62vwqitxoyl5fh4
Added the apt-get update script I run every 30 days.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
import re
3
3
 
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 _
7
8
 
8
9
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
 
 
 
10
from wiki.templatetags.wiki_extras import WIKI_WORD_RE
16
11
 
17
12
wikiword_pattern = re.compile('^' + WIKI_WORD_RE + '$')
18
13
 
22
17
    summary = forms.CharField(widget=forms.Textarea)
23
18
 
24
19
    comment = forms.CharField(required=False)
 
20
    user_ip = forms.CharField(widget=forms.HiddenInput)
25
21
 
26
22
    content_type = forms.ModelChoiceField(
27
23
        queryset=ContentType.objects.all(),
34
30
 
35
31
    class Meta:
36
32
        model = Article
37
 
        exclude = ('creator', 'group', 'created_at', 'last_update')
 
33
        exclude = ('creator', 'creator_ip',
 
34
                   'group', 'created_at', 'last_update')
38
35
 
39
36
    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
 
 
 
37
        """ Page title must be a WikiWord.
48
38
        """
49
 
 
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.'))
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
 
41
            raise forms.ValidationError(_('Must be a WikiWord.'))
 
42
 
65
43
        return title
66
44
 
67
45
    def clean(self):
74
52
                kw['content_type'] = self.cleaned_data['content_type']
75
53
                kw['object_id'] = self.cleaned_data['object_id']
76
54
            except KeyError:
77
 
                pass  # some error in this fields
 
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."))
78
60
 
79
61
        return self.cleaned_data
80
62
 
90
72
 
91
73
    def save(self, *args, **kwargs):
92
74
        # 0 - Extra data
 
75
        editor_ip = self.cleaned_data['user_ip']
93
76
        comment = self.cleaned_data['comment']
94
77
 
95
78
        # 2 - Save the Article
99
82
        editor = getattr(self, 'editor', None)
100
83
        group = getattr(self, 'group', None)
101
84
        if self.is_new:
 
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)
106
 
            if notification:
107
 
                notification.observe(article, editor, 'wiki_observed_article_changed')
108
90
 
109
91
        # 4 - Create new revision
110
92
        changeset = article.new_revision(
111
93
            self.old_content, self.old_title, self.old_markup,
112
 
            comment, editor)
 
94
            comment, editor_ip, editor)
113
95
 
114
96
        return article, changeset
 
97