~widelands-dev/widelands-website/django_staticfiles

11 by Holger Rapp
Imported wikiapp into our repository, because we did some local changes (users must be logged in to edit wiki pages)
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
446.1.16 by franku
added check if an artile name is reserved to not break old links
9
from wiki.models import ChangeSet
449.2.4 by franku
adjusted backlinks; cleanup
10
from settings import WIKI_WORD_RE
450.1.14 by franku
trys: excepts: ; some comments
11
try:
12
    from notification import models as notification
13
except:
14
    notification = None
450.1.19 by franku
forgotten one empty line with spaces
15
11 by Holger Rapp
Imported wikiapp into our repository, because we did some local changes (users must be logged in to edit wiki pages)
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
499.1.1 by franku
cleanup ip-address from wiki
37
        exclude = ('creator', 'group', 'created_at', 'last_update')
11 by Holger Rapp
Imported wikiapp into our repository, because we did some local changes (users must be logged in to edit wiki pages)
38
39
    def clean_title(self):
446.1.16 by franku
added check if an artile name is reserved to not break old links
40
        """Check for some errors regarding the title:
446.1.17 by franku
trying to edit a redirect opens now the actual article; reworked title check for the form
41
42
        1. Check for bad characters
43
        2. Check for already used titles
44
446.1.20 by franku
reworked the check for emmidiately changing the title for new articles, again
45
        Immediately trying to change the title of a new article to an existing title
446.1.22 by franku
wording
46
        is handled on the database level.
446.1.18 by franku
Added a hint for check on database level
47
446.1.17 by franku
trying to edit a redirect opens now the actual article; reworked title check for the form
48
        """
49
11 by Holger Rapp
Imported wikiapp into our repository, because we did some local changes (users must be logged in to edit wiki pages)
50
        title = self.cleaned_data['title']
51
        if not wikiword_pattern.match(title):
446.1.16 by franku
added check if an artile name is reserved to not break old links
52
            raise forms.ValidationError(
446.1.22 by franku
wording
53
                _('Only alphanumeric characters, blank spaces and the underscore are allowed in a title.'))
446.1.17 by franku
trying to edit a redirect opens now the actual article; reworked title check for the form
54
446.1.20 by franku
reworked the check for emmidiately changing the title for new articles, again
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:
446.1.22 by franku
wording
58
            # Check if the new name has been used already
446.1.20 by franku
reworked the check for emmidiately changing the title for new articles, again
59
            cs = ChangeSet.objects.filter(old_title=title)
60
            if cs:
446.1.17 by franku
trying to edit a redirect opens now the actual article; reworked title check for the form
61
                raise forms.ValidationError(
446.1.22 by franku
wording
62
                    _('The title %(title)s is already in use, maybe an other article used to have this name.'), params={'title': title},)
446.1.20 by franku
reworked the check for emmidiately changing the title for new articles, again
63
446.1.17 by franku
trying to edit a redirect opens now the actual article; reworked title check for the form
64
        # title not changed, no errors
11 by Holger Rapp
Imported wikiapp into our repository, because we did some local changes (users must be logged in to edit wiki pages)
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:
438.1.6 by franku
run the script
77
                pass  # some error in this fields
11 by Holger Rapp
Imported wikiapp into our repository, because we did some local changes (users must be logged in to edit wiki pages)
78
79
        return self.cleaned_data
80
262 by Holger Rapp
Whitespace fixes. Also, form is valid now changes the instance which broke history feature in wiki
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
247.1.1 by Holger Rapp
Model.save() should always take args and kwargs and pass them one. Whitespace fixes. Removed dependency on Site being configured on initial run of syncdb
91
    def save(self, *args, **kwargs):
11 by Holger Rapp
Imported wikiapp into our repository, because we did some local changes (users must be logged in to edit wiki pages)
92
        # 0 - Extra data
93
        comment = self.cleaned_data['comment']
94
95
        # 2 - Save the Article
247.1.1 by Holger Rapp
Model.save() should always take args and kwargs and pass them one. Whitespace fixes. Removed dependency on Site being configured on initial run of syncdb
96
        article = super(ArticleForm, self).save(*args, **kwargs)
11 by Holger Rapp
Imported wikiapp into our repository, because we did some local changes (users must be logged in to edit wiki pages)
97
98
        # 3 - Set creator and group
99
        editor = getattr(self, 'editor', None)
100
        group = getattr(self, 'group', None)
262 by Holger Rapp
Whitespace fixes. Also, form is valid now changes the instance which broke history feature in wiki
101
        if self.is_new:
11 by Holger Rapp
Imported wikiapp into our repository, because we did some local changes (users must be logged in to edit wiki pages)
102
            if editor is not None:
103
                article.creator = editor
104
                article.group = group
247.1.1 by Holger Rapp
Model.save() should always take args and kwargs and pass them one. Whitespace fixes. Removed dependency on Site being configured on initial run of syncdb
105
            article.save(*args, **kwargs)
450.1.14 by franku
trys: excepts: ; some comments
106
            if notification:
107
                notification.observe(article, editor, 'wiki_observed_article_changed')
11 by Holger Rapp
Imported wikiapp into our repository, because we did some local changes (users must be logged in to edit wiki pages)
108
109
        # 4 - Create new revision
110
        changeset = article.new_revision(
262 by Holger Rapp
Whitespace fixes. Also, form is valid now changes the instance which broke history feature in wiki
111
            self.old_content, self.old_title, self.old_markup,
499.1.1 by franku
cleanup ip-address from wiki
112
            comment, editor)
11 by Holger Rapp
Imported wikiapp into our repository, because we did some local changes (users must be logged in to edit wiki pages)
113
114
        return article, changeset