~widelands-dev/widelands-website/django_staticfiles

« back to all changes in this revision

Viewing changes to pybb/forms.py

  • Committer: franku
  • Date: 2016-12-13 18:28:51 UTC
  • mto: This revision was merged to the branch mainline in revision 443.
  • Revision ID: somal@arcor.de-20161213182851-bo5ebf8pdvw5beua
run the script

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
from django.core.mail import send_mail
15
15
from django.contrib.sites.models import Site
16
16
 
 
17
 
17
18
class AddPostForm(forms.ModelForm):
18
19
    name = forms.CharField(label=_('Subject'))
19
20
    attachment = forms.FileField(label=_('Attachment'), required=False)
21
22
    class Meta:
22
23
        model = Post
23
24
        # Listing fields again to get the the right order; See also the NOCOMM
24
 
        fields = ['name','body', 'markup', 'attachment',]
 
25
        fields = ['name', 'body', 'markup', 'attachment', ]
25
26
 
26
27
    def __init__(self, *args, **kwargs):
27
28
        self.user = kwargs.pop('user', None)
29
30
        self.forum = kwargs.pop('forum', None)
30
31
        self.ip = kwargs.pop('ip', None)
31
32
        super(AddPostForm, self).__init__(*args, **kwargs)
32
 
        
33
 
        # NOCOMM: This doesn't work anymore with django 1.8 Use 'field_order' with django 1.9
34
 
        self.fields.keyOrder = ['name', 
35
 
                                'body', 
36
 
                                'markup', 
 
33
 
 
34
        # NOCOMM: This doesn't work anymore with django 1.8 Use 'field_order'
 
35
        # with django 1.9
 
36
        self.fields.keyOrder = ['name',
 
37
                                'body',
 
38
                                'markup',
37
39
                                'attachment']
38
40
 
39
41
        if self.topic:
43
45
        if not pybb_settings.ATTACHMENT_ENABLE:
44
46
            self.fields['attachment'].widget = forms.HiddenInput()
45
47
            self.fields['attachment'].required = False
46
 
 
47
48
 
48
49
    def clean_attachment(self):
49
50
        if self.cleaned_data['attachment']:
52
53
                raise forms.ValidationError(_('Attachment is too big'))
53
54
        return self.cleaned_data['attachment']
54
55
 
55
 
 
56
 
 
57
56
    def save(self, *args, **kwargs):
58
57
        if self.forum:
59
58
            topic_is_new = True
77
76
 
78
77
        if re.search(settings.ANTI_SPAM_PHONE_NR, text):
79
78
            hidden = True
80
 
        
 
79
 
81
80
        if topic_is_new:
82
81
            text = self.cleaned_data['name']
83
82
            if any(x in text.lower() for x in settings.ANTI_SPAM_TOPIC):
95
94
 
96
95
        if not hidden:
97
96
            if topic_is_new:
98
 
                send(User.objects.all(), "forum_new_topic",
99
 
                    {'topic': topic, 'post':post, 'user':topic.user})
 
97
                send(User.objects.all(), 'forum_new_topic',
 
98
                     {'topic': topic, 'post': post, 'user': topic.user})
100
99
            else:
101
 
                send(self.topic.subscribers.all(), "forum_new_post",
102
 
                    {'post':post, 'topic':topic, 'user':post.user})
 
100
                send(self.topic.subscribers.all(), 'forum_new_post',
 
101
                     {'post': post, 'topic': topic, 'user': post.user})
103
102
 
104
103
        return post
105
104
 
106
 
 
107
105
    def save_attachment(self, post, memfile):
108
106
        if memfile:
109
107
            obj = Attachment(size=memfile.size, content_type=memfile.content_type,
110
108
                             name=memfile.name, post=post)
111
 
            dir = os.path.join(settings.MEDIA_ROOT, pybb_settings.ATTACHMENT_UPLOAD_TO)
 
109
            dir = os.path.join(settings.MEDIA_ROOT,
 
110
                               pybb_settings.ATTACHMENT_UPLOAD_TO)
112
111
            fname = '%d.0' % post.id
113
112
            path = os.path.join(dir, fname)
114
113
            file(path, 'w').write(memfile.read())
116
115
            obj.save()
117
116
 
118
117
 
119
 
 
120
118
class EditPostForm(forms.ModelForm):
 
119
 
121
120
    class Meta:
122
121
        model = Post
123
122
        fields = ['body', 'markup']