This repository has been archived by the owner on Jan 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 209
/
Copy pathforms.py
106 lines (88 loc) · 3.84 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
from common.forms import ModelFormWithHelper
from common.helpers import SubmitCancelFormHelper
from blog.models import News, Resource, Tag, ResourceType
from users.models import SystersUser
class AddNewsForm(ModelFormWithHelper):
"""Form to add new Community News. The author and the community of the
news should be provided by the view:
* author - currently logged in user
* community - defined by the community slug from the URL
"""
class Meta:
model = News
fields = ['slug', 'title', 'content', 'is_public', 'is_monitored',
'tags']
helper_class = SubmitCancelFormHelper
helper_cancel_href = "{% url 'view_community_news_list' " \
"community.slug %}"
def __init__(self, *args, **kwargs):
self.author = kwargs.pop('author')
self.community = kwargs.pop('community')
super(AddNewsForm, self).__init__(*args, **kwargs)
def save(self, commit=True):
"""Override save to add author and community to the instance."""
instance = super(AddNewsForm, self).save(commit=False)
instance.author = SystersUser.objects.get(user=self.author)
instance.community = self.community
if commit:
instance.save()
return instance
class EditNewsForm(ModelFormWithHelper):
"""Form to edit Community News."""
class Meta:
model = News
fields = ['slug', 'title', 'content', 'is_public', 'is_monitored',
'tags']
helper_class = SubmitCancelFormHelper
helper_cancel_href = "{% url 'view_community_news' community.slug " \
"object.slug %}"
class AddResourceForm(ModelFormWithHelper):
"""Form to add new Community Resource. The author and the community of the
resource should be provided by the view:
* author - currently logged in user
* community - defined by the community slug from the URL
"""
class Meta:
model = Resource
fields = ['slug', 'title', 'content', 'is_public', 'is_monitored',
'tags', 'resource_type']
helper_class = SubmitCancelFormHelper
helper_cancel_href = "{% url 'view_community_resource_list' " \
"community.slug %}"
def __init__(self, *args, **kwargs):
self.author = kwargs.pop('author')
self.community = kwargs.pop('community')
super(AddResourceForm, self).__init__(*args, **kwargs)
def save(self, commit=True):
"""Override save to add author and community to the instance."""
instance = super(AddResourceForm, self).save(commit=False)
instance.author = SystersUser.objects.get(user=self.author)
instance.community = self.community
if commit:
instance.save()
return instance
class EditResourceForm(ModelFormWithHelper):
"""Form to edit Community Resource."""
class Meta:
model = Resource
fields = ['slug', 'title', 'content', 'is_public', 'is_monitored',
'tags', 'resource_type']
helper_class = SubmitCancelFormHelper
helper_cancel_href = "{% url 'view_community_resource' " \
"community.slug object.slug %}"
class TagForm(ModelFormWithHelper):
"""Form to create or edit a tag"""
class Meta:
model = Tag
fields = ['name']
helper_class = SubmitCancelFormHelper
helper_cancel_href = "{% url 'view_community_news_list' " \
"community.slug %}"
class ResourceTypeForm(ModelFormWithHelper):
"""Form to create or edit a ResourceType object"""
class Meta:
model = ResourceType
fields = ['name']
helper_class = SubmitCancelFormHelper
helper_cancel_href = "{% url 'view_community_resource_list' " \
"community.slug %}"