-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathforms.py
93 lines (83 loc) · 4.24 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
from django import forms
from .models import Media
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Field, Fieldset, Div, HTML, ButtonHolder, Submit, Hidden
from workflow.views import getStatesCommunity
from workflow.models import States
class MediaCreateForm(forms.ModelForm):
upload_or_link = (
('Upload', 'Upload'),
('Link', 'Link'),
)
media_type = forms.ChoiceField(choices=upload_or_link, widget=forms.RadioSelect)
class Meta:
model = Media
fields = ['title', 'mediafile', 'medialink', 'references']
def __init__(self, *args, **kwargs):
mtype = kwargs.pop('mediatype', None)
super().__init__(*args, **kwargs)
self.fields['title'].widget.attrs.update({'class': 'form-control'})
self.fields['references'].widget.attrs.update({'class': 'form-control'})
self.fields['references'].required = False
self.fields['mediafile'].required = False
if mtype == 'IMAGE':
self.fields['mediafile'].widget.attrs.update({'class': 'file', 'data-allowed-file-extensions':'["jpeg", "jpg","png"]', 'data-show-upload':'false', 'data-show-preview':'false', 'data-msg-placeholder':'Select image for upload...'})
if mtype == 'AUDIO':
self.fields['mediafile'].widget.attrs.update({'class': 'file', 'data-allowed-file-extensions':'["mp3"]', 'data-show-upload':'false', 'data-show-preview':'false', 'data-msg-placeholder':'Select audio for upload...'})
if mtype == 'VIDEO':
self.fields['mediafile'].widget.attrs.update({'class': 'file', 'data-allowed-file-extensions':'["mp4"]', 'data-show-upload':'false', 'data-show-preview':'false', 'data-msg-placeholder':'Select video for upload...'})
self.fields['medialink'].widget.attrs.update({'class': 'form-control'})
self.fields['medialink'].hidden = True
self.fields['medialink'].required = False
self.fields['media_type'].initial = 'Upload'
self.helper = FormHelper()
self.helper.layout = Layout(
Div(
Field('title'),
Field('media_type'),
Field('medialink'),
# Field('medialink','value'),
Field('mediafile'),
Field('references'),
HTML("<br>"),
ButtonHolder(Submit('submit', 'Create')),
)
)
class MediaUpdateForm(forms.ModelForm):
class Meta:
model = Media
# fields = ['title', 'mediafile', 'medialink', 'state']
fields = ['title', 'mediafile', 'medialink', 'references']
def __init__(self, *args, **kwargs):
role = kwargs.pop('role', None)
mtype = kwargs.pop('mediatype', None)
super().__init__(*args, **kwargs)
self.fields['title'].widget.attrs.update({'class': 'form-control'})
self.fields['references'].widget.attrs.update({'class': 'form-control'})
self.fields['references'].required = False
# self.fields['state'].widget.attrs.update({'class': 'form-control'})
# self.fields['state'].required = False
# states = getStatesCommunity(self.instance.state.id, role)
# self.fields['state'].queryset = States.objects.filter(name__in=states)
self.fields['mediafile'].required = False
if mtype == 'IMAGE':
self.fields['mediafile'].widget.attrs.update({'class': 'file', 'data-allowed-file-extensions':'["jpeg", "jpg","png"]', 'data-show-upload':'false', 'data-show-preview':'false', 'data-msg-placeholder':'Select image for upload...'})
if mtype == 'AUDIO':
self.fields['mediafile'].widget.attrs.update({'class': 'file', 'data-allowed-file-extensions':'["mp3"]', 'data-show-upload':'false', 'data-show-preview':'false', 'data-msg-placeholder':'Select audio for upload...'})
if mtype == 'VIDEO':
self.fields['mediafile'].widget.attrs.update({'class': 'file', 'data-allowed-file-extensions':'["mp4"]', 'data-show-upload':'false', 'data-show-preview':'false', 'data-msg-placeholder':'Select video for upload...'})
self.fields['medialink'].widget.attrs.update({'class': 'form-control'})
self.fields['medialink'].required = False
self.helper = FormHelper()
self.helper.layout = Layout(
Div(
Field('title'),
Field('media_type'),
Field('medialink'),
Field('mediafile'),
Field('references'),
# Field('state'),
HTML("<br>"),
ButtonHolder(Submit('submit', 'Update')),
)
)