This repository was archived by the owner on Oct 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathforms.py
151 lines (115 loc) · 5.68 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
140
141
142
143
144
145
146
147
148
149
from django import forms
from util import constants
import haystack.forms
class ListFormField(forms.MultipleChoiceField):
"""
A form field for a list of values that are unchecked.
The Django MultipleChoiceField does *almost* what we want, except
it validates that each choice is in a supplied list of choices,
even when that list is empty. We simply override the validation.
"""
def valid_value(self, value):
return True
class EnzymeForm(forms.Form):
ec = forms.CharField(max_length=50)
# Convenience accessors for clean data with defaults.
cleaned_ec = property(lambda self: self.cleaned_data['ec'])
class BaseSearchForm(haystack.forms.SearchForm):
def _GetWithDefault(self, key, default):
if (key not in self.cleaned_data or self.cleaned_data[key] is None):
return default
return self.cleaned_data[key]
class SuggestForm(BaseSearchForm):
query = forms.CharField(max_length=2048, required=False)
cleaned_query = property(lambda self: self._GetWithDefault('query', ''))
class SearchForm(BaseSearchForm):
query = forms.CharField(max_length=2048, required=False)
ph = forms.FloatField(required=False)
pmg = forms.FloatField(required=False)
ionic_strength = forms.FloatField(required=False)
electronReductionPotential = forms.FloatField(required=False)
max_priority = forms.IntegerField(required=False)
mode = forms.ChoiceField(required=False,
choices=[('BA', 'basic'), ('AD', 'advanced')])
# Convenience accessors for clean data with defaults.
cleaned_query = property(lambda self: self._GetWithDefault('query', ''))
cleaned_ph = property(lambda self: self._GetWithDefault('ph', None))
cleaned_pmg = property(lambda self: self._GetWithDefault('pmg', None))
cleaned_ionic_strength = property(
lambda self: self._GetWithDefault('ionic_strength', None))
cleaned_e_reduction_potential = property(
lambda self: self._GetWithDefault('electronReductionPotential', None))
cleaned_max_priority = property(
lambda self: self._GetWithDefault('max_priority', 0))
cleaned_mode = property(
lambda self: self._GetWithDefault('mode', ''))
class BaseReactionForm(SearchForm):
def GetReactantConcentrations(self):
prefactors = map(float,
self.cleaned_data['reactantsConcentrationPrefactor'])
for f, c in zip(prefactors,
self.cleaned_data['reactantsConcentration']):
try:
conc = f * float(c)
if conc <= 0:
yield 1e-9
else:
yield conc
except ValueError:
yield 1e-9
reactantsPhase = forms.MultipleChoiceField(required=False,
choices=constants.PHASE_CHOICES)
reactantsConcentration = ListFormField(required=False)
reactantsConcentrationPrefactor = ListFormField(required=False)
# Convenience accessors for clean data with defaults.
cleaned_reactantsPhase = property(
lambda self: self.cleaned_data['reactantsPhase'])
cleaned_reactantsConcentration = property(GetReactantConcentrations)
class ReactionForm(BaseReactionForm):
reactionId = forms.CharField(required=False)
reactantsId = ListFormField(required=False)
reactantsCoeff = ListFormField(required=False)
reactantsName = ListFormField(required=False)
submit = forms.ChoiceField(required=False,
choices=[('Update', 'update'),
('Save', 'save'),
('Reverse', 'reverse'),
('Reset', 'reset')])
# Convenience accessors for clean data with defaults.
cleaned_reactionId = property(
lambda self: self.cleaned_data['reactionId'])
cleaned_reactantsId = property(
lambda self: self.cleaned_data['reactantsId'])
cleaned_reactantsCoeff = property(
lambda self: [float(c) for c in self.cleaned_data['reactantsCoeff']])
cleaned_reactantsName = property(
lambda self: self.cleaned_data['reactantsName'])
cleaned_submit = property(
lambda self: self._GetWithDefault('submit', 'Update'))
class ReactionGraphForm(ReactionForm):
vary_ph = forms.BooleanField(required=False)
vary_is = forms.BooleanField(required=False)
vary_pmg = forms.BooleanField(required=False)
# Convenience accessors for clean data with defaults.
cleaned_vary_ph = property(
lambda self: self._GetWithDefault('vary_ph', False))
cleaned_vary_pmg = property(
lambda self: self._GetWithDefault('vary_pmg', False))
cleaned_vary_is = property(
lambda self: self._GetWithDefault('vary_is', False))
class CompoundForm(BaseReactionForm):
compoundId = forms.CharField(max_length=50)
submit = forms.ChoiceField(required=False,
choices=[('Update', 'update'),
('Reset', 'reset')])
# Convenience accessors for clean data with defaults.
cleaned_compoundId = property(lambda self: self.cleaned_data['compoundId'])
# we need to create the following properties in order for this form
# to impersonate a reaction_form (something we need to do for creating
# a Reaction object using .FromForm(form))
cleaned_reactionId = property(lambda self: None)
cleaned_reactantsId = property(lambda self: [self.cleaned_compoundId])
cleaned_reactantsCoeff = property(lambda self: [1])
cleaned_reactantsName = property(lambda self: [None])
cleaned_submit = property(
lambda self: self._GetWithDefault('submit', 'Update'))