forked from ericls/niji
-
Notifications
You must be signed in to change notification settings - Fork 0
/
forms.py
139 lines (111 loc) · 3.81 KB
/
forms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# -*- coding: utf-8 -*-
from django.forms import ModelForm
from django.conf import settings
from crispy_forms.layout import Submit
from crispy_forms.helper import FormHelper
from .models import Topic, Appendix, ForumAvatar, Post
from django.utils.translation import ugettext as _
if 'pagedown' in settings.INSTALLED_APPS:
use_pagedown = True
from django import forms
from pagedown.widgets import PagedownWidget
else:
use_pagedown = False
class TopicForm(ModelForm):
if use_pagedown:
content_raw = forms.CharField(label=_('Content'), widget=PagedownWidget())
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user', None)
super(TopicForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.add_input(Submit('submit', _('Submit')))
class Meta:
model = Topic
fields = ['node', 'title', 'content_raw']
labels = {
'content_raw': _('Content'),
'node': _('Node'),
'title': _('Title'),
}
def save(self, commit=True):
inst = super(TopicForm, self).save(commit=False)
inst.user = self.user
if commit:
inst.save()
self.save_m2m()
return inst
class TopicEditForm(ModelForm):
if use_pagedown:
content_raw = forms.CharField(label=_('Content'), widget=PagedownWidget())
def __init__(self, *args, **kwargs):
super(TopicEditForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.add_input(Submit('submit', _('Submit')))
class Meta:
model = Topic
fields = ('content_raw', )
labels = {
'content_raw': _('Content'),
}
class AppendixForm(ModelForm):
def __init__(self, *args, **kwargs):
self.topic = kwargs.pop('topic', None)
super(AppendixForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.add_input(Submit('submit', _('Submit')))
class Meta:
model = Appendix
fields = ('content_raw', )
labels = {
'content_raw': _('Content'),
}
def save(self, commit=True):
inst = super(AppendixForm, self).save(commit=False)
inst.topic = self.topic
if commit:
inst.save()
self.save_m2m()
return inst
class ForumAvatarForm(ModelForm):
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user', None)
super(ForumAvatarForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.add_input(Submit('submit', _('Submit')))
class Meta:
model = ForumAvatar
fields = ('image', 'use_gravatar')
labels = {
'image': _('Avatar Image'),
'use_gravatar': _("Always Use Gravatar")
}
def save(self, commit=True):
inst = super(ForumAvatarForm, self).save(commit=False)
inst.user = self.user
if commit:
inst.save()
self.save_m2m()
return inst
class ReplyForm(ModelForm):
if use_pagedown:
content_raw = forms.CharField(label='', widget=PagedownWidget())
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user', None)
self.topic_id = kwargs.pop('topic_id', None)
super(ReplyForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.add_input(Submit('submit', _('Submit')))
class Meta:
model = Post
fields = ('content_raw',)
labels = {
'content_raw': '',
}
def save(self, commit=True):
inst = super(ReplyForm, self).save(commit=False)
inst.user = self.user
inst.topic_id = self.topic_id
if commit:
inst.save()
self.save_m2m()
return inst