-
Notifications
You must be signed in to change notification settings - Fork 0
/
forms.py
125 lines (105 loc) · 3.53 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
import os
from django import forms
from django.contrib.admin.widgets import FilteredSelectMultiple
from plugins.customstyling import plugin_settings, widgets, models
class StylingForm(forms.Form):
css = forms.CharField(
widget=widgets.CodeEditor,
)
def __init__(self, *args, **kwargs):
self.journal = kwargs.pop('journal', None)
self.repository = kwargs.pop('repository', None)
super(StylingForm, self).__init__(*args, **kwargs)
if self.journal:
open_path = os.path.join(
plugin_settings.BASE_CSS_PATH,
str(self.journal.pk),
'custom.css',
)
elif self.repository:
open_path = os.path.join(
plugin_settings.BASE_CSS_PATH,
'repositories',
str(self.repository.pk),
'custom.css',
)
else:
open_path = os.path.join(
plugin_settings.BASE_CSS_PATH,
'press',
'custom.css'
)
if os.path.isfile(open_path):
with open(open_path) as css_file:
css = css_file.read()
self.fields['css'].initial = css
else:
self.fields['css'].initial = '/* Insert CSS Below */'
def save(self):
css = self.cleaned_data['css']
if self.journal:
path = os.path.join(
plugin_settings.BASE_CSS_PATH,
str(self.journal.pk),
)
elif self.repository:
path = os.path.join(
plugin_settings.BASE_CSS_PATH,
'repositories',
str(self.repository.pk),
)
else:
path = os.path.join(
plugin_settings.BASE_CSS_PATH,
'press',
)
file = os.path.join(path, 'custom.css')
if not os.path.exists(path):
os.makedirs(path)
with open(file, 'w') as css_file:
css_file.write(css)
css_file.close()
class CrossJournalStylingForm(forms.ModelForm):
css = forms.CharField(
widget=widgets.CodeEditor,
)
class Meta:
model = models.CrossJournalStylesheet
fields = ('journals',)
widgets = {
'journals': FilteredSelectMultiple(
"Journals",
False,
attrs={'rows': '2'},
)
}
field_order = ['css', 'journals']
def __init__(self, *args, **kwargs):
super(CrossJournalStylingForm, self).__init__(*args, **kwargs)
open_path = os.path.join(
plugin_settings.BASE_CSS_PATH,
'press',
self.instance.stylesheet_name
)
if self.instance.pk and os.path.isfile(open_path):
with open(open_path) as css_file:
css = css_file.read()
self.fields['css'].initial = css
else:
self.fields['css'].initial = '/* Insert CSS Below */'
def save(self, commit=True):
stylesheet = super(CrossJournalStylingForm, self).save(commit=False)
css = self.cleaned_data['css']
path = os.path.join(
plugin_settings.BASE_CSS_PATH,
'press',
)
file = os.path.join(path, stylesheet.stylesheet_name)
if not os.path.exists(path):
os.makedirs(path)
with open(file, 'w') as css_file:
css_file.write(css)
css_file.close()
if commit:
stylesheet.save()
return stylesheet