~widelands-dev/widelands-website/trunk

« back to all changes in this revision

Viewing changes to pybb/forms.py

  • Committer: kaputtnik
  • Date: 2019-09-03 06:16:23 UTC
  • mfrom: (544.2.25 pybb_attachments)
  • Revision ID: kaputtnik-20190903061623-xu4kvqpabnzmuskw
Allow file uploads in the forum; added some basic file checks to validate files

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
import re
2
2
from datetime import datetime
3
 
import os.path
 
3
import os
4
4
 
5
5
from django import forms
6
6
from django.conf import settings
10
10
from pybb.models import Topic, Post, Attachment
11
11
from pybb import settings as pybb_settings
12
12
from django.conf import settings
 
13
from .util import validate_file
 
14
from mainpage.validators import virus_scan
13
15
 
14
16
 
15
17
class AddPostForm(forms.ModelForm):
16
18
    name = forms.CharField(label=_('Subject'))
17
 
    attachment = forms.FileField(label=_('Attachment'), required=False)
 
19
    attachment = forms.FileField(
 
20
        label=_('Attachment'),
 
21
        required=False,
 
22
        validators=[virus_scan, validate_file, ])
18
23
 
19
24
    class Meta:
20
25
        model = Post
21
 
        # Listing fields again to get the the right order; See also the TODO
 
26
        # Listing fields again to get the the right order
22
27
        fields = ['name', 'body', 'markup', 'attachment', ]
 
28
        widgets = {
 
29
            'body': forms.Textarea(attrs={'cols': 80, 'rows': 15}),
 
30
        }
23
31
 
24
32
    def __init__(self, *args, **kwargs):
25
33
        self.user = kwargs.pop('user', None)
31
39
            self.fields['name'].widget = forms.HiddenInput()
32
40
            self.fields['name'].required = False
33
41
 
34
 
        if not pybb_settings.ATTACHMENT_ENABLE:
 
42
        if not pybb_settings.ATTACHMENT_ENABLE or self.user.wlprofile.post_count() < settings.ALLOW_ATTACHMENTS_AFTER:
35
43
            self.fields['attachment'].widget = forms.HiddenInput()
36
44
            self.fields['attachment'].required = False
37
45
 
38
 
    def clean_attachment(self):
39
 
        if self.cleaned_data['attachment']:
40
 
            memfile = self.cleaned_data['attachment']
41
 
            if memfile.size > pybb_settings.ATTACHMENT_SIZE_LIMIT:
42
 
                raise forms.ValidationError(_('Attachment is too big'))
43
 
        return self.cleaned_data['attachment']
44
 
 
45
46
    def save(self, *args, **kwargs):
46
47
        if self.forum:
47
48
            topic_is_new = True
69
70
                             name=memfile.name, post=post)
70
71
            dir = os.path.join(settings.MEDIA_ROOT,
71
72
                               pybb_settings.ATTACHMENT_UPLOAD_TO)
72
 
            fname = '%d.0' % post.id
 
73
            if not os.path.exists(dir):
 
74
                os.makedirs(dir)
 
75
 
 
76
            fname = '{}.0'.format(post.id)
73
77
            path = os.path.join(dir, fname)
74
 
            file(path, 'w').write(memfile.read())
 
78
 
 
79
            with open(path, 'wb') as f:
 
80
                f.write(memfile.read())
 
81
 
75
82
            obj.path = fname
76
83
            obj.save()
77
84