~widelands-dev/widelands-website/trunk

« back to all changes in this revision

Viewing changes to wiki/forms.py

  • Committer: Holger Rapp
  • Date: 2019-06-21 18:34:42 UTC
  • mfrom: (540.1.3 update_ops_script)
  • Revision ID: sirver@gmx.de-20190621183442-y2ulybzr0rdvfefd
Adapt the update script for the new server.

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