~widelands-dev/widelands-website/trunk

« back to all changes in this revision

Viewing changes to pybb/forms.py

  • Committer: kaputtnik
  • Date: 2019-05-30 18:20:02 UTC
  • mto: This revision was merged to the branch mainline in revision 540.
  • Revision ID: kaputtnik-20190530182002-g7l91m1xo28clghv
adjusted README; first commit on the new server

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
from django.utils.translation import ugettext as _
8
8
from django.contrib.auth.models import User
9
9
 
10
 
from pybb.models import Topic, Post, PrivateMessage, Attachment
 
10
from pybb.models import Topic, Post, Attachment
11
11
from pybb import settings as pybb_settings
 
12
from django.conf import settings
12
13
 
13
 
from notification import models as notification
14
14
 
15
15
class AddPostForm(forms.ModelForm):
16
16
    name = forms.CharField(label=_('Subject'))
18
18
 
19
19
    class Meta:
20
20
        model = Post
21
 
        fields = ['body', 'markup',]
 
21
        # Listing fields again to get the the right order; See also the TODO
 
22
        fields = ['name', 'body', 'markup', 'attachment', ]
22
23
 
23
24
    def __init__(self, *args, **kwargs):
24
25
        self.user = kwargs.pop('user', None)
25
26
        self.topic = kwargs.pop('topic', None)
26
27
        self.forum = kwargs.pop('forum', None)
27
 
        self.ip = kwargs.pop('ip', None)
28
28
        super(AddPostForm, self).__init__(*args, **kwargs)
29
29
 
30
 
        self.fields.keyOrder = ['name', 'body', 'markup', 'attachment']
31
 
 
32
30
        if self.topic:
33
31
            self.fields['name'].widget = forms.HiddenInput()
34
32
            self.fields['name'].required = False
36
34
        if not pybb_settings.ATTACHMENT_ENABLE:
37
35
            self.fields['attachment'].widget = forms.HiddenInput()
38
36
            self.fields['attachment'].required = False
39
 
 
40
37
 
41
38
    def clean_attachment(self):
42
39
        if self.cleaned_data['attachment']:
45
42
                raise forms.ValidationError(_('Attachment is too big'))
46
43
        return self.cleaned_data['attachment']
47
44
 
48
 
 
49
 
 
50
45
    def save(self, *args, **kwargs):
51
46
        if self.forum:
52
 
            topic_is_new = True
 
47
            topic_is_new = True
53
48
            topic = Topic(forum=self.forum,
54
49
                          user=self.user,
55
50
                          name=self.cleaned_data['name'])
58
53
            topic_is_new = False
59
54
            topic = self.topic
60
55
 
61
 
        post = Post(topic=topic, user=self.user, user_ip=self.ip,
 
56
        post = Post(topic=topic, user=self.user,
62
57
                    markup=self.cleaned_data['markup'],
63
58
                    body=self.cleaned_data['body'])
64
59
        post.save(*args, **kwargs)
66
61
        if pybb_settings.ATTACHMENT_ENABLE:
67
62
            self.save_attachment(post, self.cleaned_data['attachment'])
68
63
 
69
 
        if topic_is_new:
70
 
            notification.send(User.objects.all(), "forum_new_topic",
71
 
                {'topic': topic, 'post':post, 'user':topic.user})
72
 
        else:
73
 
            notification.send(self.topic.subscribers.all(), "forum_new_post",
74
 
                {'post':post, 'topic':topic, 'user':post.user})
75
64
        return post
76
65
 
77
 
 
78
66
    def save_attachment(self, post, memfile):
79
67
        if memfile:
80
68
            obj = Attachment(size=memfile.size, content_type=memfile.content_type,
81
69
                             name=memfile.name, post=post)
82
 
            dir = os.path.join(settings.MEDIA_ROOT, pybb_settings.ATTACHMENT_UPLOAD_TO)
 
70
            dir = os.path.join(settings.MEDIA_ROOT,
 
71
                               pybb_settings.ATTACHMENT_UPLOAD_TO)
83
72
            fname = '%d.0' % post.id
84
73
            path = os.path.join(dir, fname)
85
74
            file(path, 'w').write(memfile.read())
87
76
            obj.save()
88
77
 
89
78
 
90
 
 
91
79
class EditPostForm(forms.ModelForm):
 
80
 
92
81
    class Meta:
93
82
        model = Post
94
 
        fields = ['body']
 
83
        fields = ['body', 'markup']
95
84
 
96
85
    def save(self, *args, **kwargs):
97
86
        post = super(EditPostForm, self).save(commit=False)
100
89
        return post
101
90
 
102
91
 
103
 
class UserSearchForm(forms.Form):
104
 
    query = forms.CharField(required=False, label='')
105
 
 
106
 
    def filter(self, qs):
107
 
        if self.is_valid():
108
 
            query = self.cleaned_data['query']
109
 
            return qs.filter(username__contains=query)
110
 
        else:
111
 
            return qs
 
92
class LastPostsDayForm(forms.Form):
 
93
    days = forms.IntegerField(
 
94
        max_value = 1000,
 
95
        min_value = 5,
 
96
        )
 
97
    
 
98
    sort_by = forms.ChoiceField(
 
99
        choices = [('forum','Forum'),('topic', 'Topic'),],
 
100
        label = 'Group by:',
 
101
        )