forked from allo-/ffprofile
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforms.py
More file actions
104 lines (95 loc) · 4.52 KB
/
forms.py
File metadata and controls
104 lines (95 loc) · 4.52 KB
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
from django import forms
from django.utils.translation import ugettext as _
import json, glob, os
# current structure:
# - FirefoxTracking: builtin features, which send data to Mozilla,
# google and other thirdparties
# - WebsiteTracking: Features, which are made for tracking (i.e. ping, beacons)
# or may used for it (i.e. battery api)
# - Privacy: General privacy related settings like referer, cookies, etc.
# which may be harmless or needed (i.e. cookies)
# - Security: No direct privacy problems, but maybe security issues
# (i.e. webgl may hang Firefox)
# - Bloatware: Settings, which disable unwanted features like hello or pocket
# - Annoyances: Settings, which disable first-run
# "did you know, here is our new tab page" popups.
#
# TODO: WebsiteTracking could be split into Tracking (ping, beacon, ...) and
# Fingerprinting (battery, canvas, ...), when there are more settings.
class ConfigForm(forms.Form):
def __init__(self, *args, **kwargs):
super(ConfigForm, self).__init__(*args, **kwargs)
self.fields['form_name'] = forms.CharField(initial=self.id, widget=forms.widgets.HiddenInput)
for option in self.options:
if option['type'] == "boolean":
self.fields[option['name']] = forms.BooleanField(
label=option['label'],
help_text=option['help_text'],
initial=option['initial'], required=False)
if option['type'] == "choice":
choices = option['choices']
self.fields[option['name']] = forms.ChoiceField(
label=option['label'],
help_text=option['help_text'],
choices = zip(range(len(choices)), choices),
initial=option['initial'], required=False)
elif option['type'] == "text":
self.fields[option['name']] = forms.CharField(
label=option['label'],
help_text=option['help_text'],
initial=option['initial'], required=False)
def get_config_and_addons(self):
config = {}
addons = []
files_inline = {}
if self.is_valid():
for option in self.options:
if option['type'] == "boolean":
if self.cleaned_data[option['name']]:
for key in option['config']:
config[key] = option['config'][key]
if "addons" in option:
addons += option['addons']
if 'files_inline' in option:
files_inline.update(option['files_inline'])
elif option['type'] == "choice":
choice = int(self.cleaned_data[option['name']])
for key in option['config'][choice]:
config[key] = option['config'][choice][key]
if "addons" in option:
addons += option['addons'][choice]
if 'files_inline' in option:
files.update(option['files_inline'][choice])
elif option['type'] == "text":
if option.get('blank_means_default', False) and self.cleaned_data[option['name']] == "":
continue
else:
config[option['setting']] = self.cleaned_data[option['name']]
return config, addons, files_inline
def create_configform(id, name, options):
class DynamicConfigForm(ConfigForm):
pass
DynamicConfigForm.id=id
DynamicConfigForm.name=name
DynamicConfigForm.options=options
return DynamicConfigForm
PROFILES = {}
settings_path = os.path.dirname(__file__) + "/settings"
profiles_path = os.path.dirname(__file__) + "/profiles"
profile_files = glob.glob(profiles_path + "/*.json")
for profile_file in profile_files:
profile_name, profile = json.load(open(profile_file, "r"))
items = {}
for category in profile:
options = []
for file in profile[category]:
data = json.load(open(settings_path + "/" + file, "r"))
for item in data:
item['label'] = _(item['label'] or "")
item['help_text'] = _(item['help_text'] or "")
options += data
items[category] = options
form_list = []
for idx, name in enumerate(items):
form_list.append(create_configform(id="form{0:d}".format(idx), name=name, options=items[name]))
PROFILES[os.path.basename(profile_file)] = [profile_name, form_list]