Coverage for khufu/modals/modalForm.py : 23%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# !/usr/bin/env python
2# encoding: utf-8
3"""
4*The modal form class*
6:Author:
7 David Young
8"""
9from builtins import object
10import sys
11import os
12import datetime
13import numpy as np
14import khufu
15from khufu.forms import *
16from khufu.scaffolding import *
17from khufu.buttons import *
18from . import modal
20class modalForm(object):
21 """
22 *The worker class for the modalForm module*
24 **Key Arguments**
26 - ``log`` -- logger
27 - ``title`` -- title
28 - ``postToScriptUrl`` -- postToScriptUrl
29 - ``reloadToUrl`` -- reloadToUrl
30 - ``formClassName`` -- give a class name to form (if required by CSS or JS)
32 """
34 def __init__(
35 self,
36 log,
37 title,
38 postToScriptUrl,
39 reloadToUrl,
40 formClassName=False
41 ):
42 self.log = log
43 self.title = title
44 self.postToScriptUrl = postToScriptUrl
45 self.reloadToUrl = reloadToUrl
46 self.formClass = formClassName
47 # xt-self-arg-tmpx
49 # VARIABLE DATA ATRRIBUTES
50 self.formContent = u""
51 self.randNum = int(np.random.rand() * 10000)
52 self.hiddenParameterList = []
53 # ADD REQUIRED ICON IF NEEDED
54 self.requredIcon = khufu.coloredText(
55 text="*",
56 color="red",
57 size=5, # 1-10
58 pull=False, # "left" | "right",
59 addBackgroundColor=False
60 )
61 self.submitButtonText = "submit"
63 return None
65 def close(self):
66 del self
67 return None
69 def get(self):
70 """
71 *get the modalForm object*
73 **Return**
75 - ``modalForm``
77 """
78 self.log.debug('starting the ``get`` method')
80 randNum = self.randNum
81 modalTrigger = "modalTrigger%(randNum)s" % locals()
82 formId = "form%(randNum)s" % locals()
84 self.set_hidden_parameters()
86 modalForm = form(
87 content=self.formContent, # list of control groups
88 # [ "inline" | "horizontal" | "search" | "navbar-form" | "navbar-search" ]
89 formType='horizontal',
90 navBarPull=False, # [ false | right | left ],
91 postToScript=self.postToScriptUrl,
92 htmlId=formId,
93 htmlClass=self.formClass,
94 postInBackground=True,
95 redirectUrl=self.reloadToUrl,
96 span=12,
97 offset=False
98 )
100 modalForm = grid_row(
101 responsive=True,
102 columns=modalForm,
103 htmlId=False,
104 htmlClass=False,
105 onPhone=True,
106 onTablet=True,
107 onDesktop=True
108 )
110 # THE REQUIRED INPUT FOOTNOTE
111 requredIcon = self.requredIcon
112 requredAlert = '%(requredIcon)s required input         ' % locals(
113 )
115 modalForm = modal(
116 modalHeaderContent=self.title,
117 modalBodyContent=modalForm,
118 modalFooterContent=requredAlert +
119 self.get_form_action_buttons(formId),
120 htmlId=modalTrigger,
121 centerContent=True
122 )
124 modalTrigger = "#%(modalTrigger)s" % locals()
126 self.log.debug('completed the ``get`` method')
127 return modalForm, modalTrigger
129 def add_form_object(
130 self,
131 formObject,
132 label="",
133 hidden=False):
134 """
135 *add a form objec to the modal form*
137 **Key Arguments**
139 - ``formObject`` -- the object to add to the form
140 - ``label`` -- label to assign to the object
141 - ``hidden`` -- is the form object hidden initially?
143 """
144 self.log.debug('starting the ``addFormObject`` method')
146 randNum = self.randNum
147 randId = "randId%(randNum)s" % locals()
148 formObject = formObject.replace(
149 "class=", """id="%(randId)s" class=""" % locals())
151 if " required " in formObject:
152 label = label + self.requredIcon
154 thisControlRow = controlRow(
155 inputList=[formObject, ]
156 )
157 thisContentLabel = horizontalFormControlLabel(
158 labelText=label,
159 forId=randId,
160 location="left"
161 )
162 thisContentCG = horizontalFormControlGroup(
163 content=thisContentLabel + thisControlRow,
164 validationLevel=False
165 )
166 self.formContent = self.formContent + thisContentCG
168 self.log.debug('completed the ``addFormObject`` method')
169 return None
171 def get_form_action_buttons(
172 self,
173 formId):
174 """
175 *get form action buttons*
177 **Key Arguments**
179 - ``formId`` -- the HTML id of the form
182 **Return**
184 - ``actionButtons`` -- the action buttos for the form (cancel, submit)
186 """
187 self.log.debug('starting the ``get_form_action_buttons`` method')
189 cancel = button(
190 buttonText='cancel',
191 # [ default | primary | info | success | warning | danger | inverse | link ]
192 buttonStyle='danger',
193 buttonSize='small', # [ large | default | small | mini ]
194 htmlId=False,
195 href=False,
196 pull=False, # right, left, center
197 submit=False,
198 block=False,
199 disable=False,
200 postInBackground=False,
201 dataToggle=False, # [ modal ]
202 popover=False,
203 close=True
204 )
206 submit = button(
207 buttonText=self.submitButtonText,
208 # [ default | primary | info | success | warning | danger | inverse | link ]
209 buttonStyle='info',
210 buttonSize='small', # [ large | default | small | mini ]
211 htmlId=False,
212 href=False,
213 pull=False, # right, left, center
214 submit=True,
215 block=False,
216 disable=False,
217 postInBackground=False,
218 dataToggle=False, # [ modal ]
219 popover=False,
220 formId=formId,
221 close=True
222 )
224 actionButtons = buttonGroup(
225 buttonList=[cancel, submit],
226 format='default' # [ default | toolbar | vertical ]
227 )
229 self.log.debug('completed the ``get_form_action_buttons`` method')
230 return actionButtons
232 def add_hidden_parameter_value(
233 self,
234 key,
235 value):
236 """
237 *add hidden parameter value to the form (to be submitted with the form but does not need user input)*
239 **Key Arguments**
241 - ``key`` -- the key for the hidden value (will be appended to query string when form submitted)
242 - ``value`` -- the value of the hidden parameter
244 """
245 self.log.debug('starting the ``add_hidden_parameter_value`` method')
247 if isinstance(value, ("".__class__, u"".__class__)):
248 ttype = "text"
249 if isinstance(value, str):
250 ttype = "text"
251 elif isinstance(value, datetime.date):
252 ttype = "datetime"
253 else:
254 ttype = "number"
256 thisInput = formInput(
257 # [ text | password | datetime | datetime-local | date | month | time | week | number | email | url | search | tel | color ]
258 ttype=ttype,
259 htmlId=key,
260 hidden=True,
261 defaultValue=value
262 )
263 self.hiddenParameterList.append(thisInput)
265 self.log.debug('completed the ``add_hidden_parameter_value`` method')
266 return None
268 def set_hidden_parameters(
269 self):
270 """
271 *get hidden parameters*
272 """
273 self.log.debug('starting the ``set_hidden_parameters`` method')
275 thisControlRow = controlRow(
276 inputList=self.hiddenParameterList
277 )
278 thisContentCG = horizontalFormControlGroup(
279 content=thisControlRow,
280 validationLevel=False,
281 hidden=True
282 )
284 self.formContent = self.formContent + thisContentCG
286 self.log.debug('completed the ``set_hidden_parameters`` method')
287 return
289 # xt-class-method