def layout(item): """ Language menu layout options for each entry: - lang_code: the language code - lang_name: the language name option for the menu - current_language: code of the current language """ if item.enabled: if item.components: # The language menu itself current_language = current.T.accepted_language items = item.render_components() select = SELECT( items, value=current_language, _name="_language", # @ToDo T: _title="Language Selection", _onchange= "S3.reloadWithQueryStringVars({'_language':$(this).val()});" ) form = FORM(select, _class="language-selector", _name="_language", _action="", _method="get") return form else: # A language entry return OPTION(item.opts.lang_name, _value=item.opts.lang_code) else: return None
def widget(cls, field, value, collation=None, **attributes): """ Generates a SELECT tag, including OPTIONs (only 1 option allowed) see also: `FormWidget.widget` """ default = dict(value=value) attr = cls._attributes(field, default, **attributes) requires = field.requires if not isinstance(requires, (list, tuple)): requires = [requires] if requires: if hasattr(requires[0], 'options'): options = requires[0].options() else: raise SyntaxError('widget cannot determine options of %s' % field) if collation: myloc = Locale(collation) coll = Collator.createInstance(myloc) options = sorted(options, key=itemgetter(1), cmp=coll.compare) opts = [OPTION(v, _value=k) for (k, v) in options] return SELECT(*opts, **attr)
def informe_mes_empleado(): empleados = db(db.empleado.is_active is True).select(db.empleado.ALL) fempl = ([" "] + [f"{p.user_code} {p.nombre} {p.apellido}" for p in empleados]) form = FORM( CENTER( H4('Marcadas del personal'), TABLE( TR( TAG('<label class "control-label">Persona</label>'), SELECT(fempl, _name='fempleado', _type='text', _id="persona", _class="form-control string")), TR( TAG('<label class "control-label">Periodo desde</label>'), INPUT(_name='fdesde', _type='date', _id="mesanio", _class="form-control string", requires=IS_NOT_EMPTY())), TR( TAG('<label class "control-label">Periodo hasta</label>'), INPUT( _name='fhasta', _type='date', _id="mesanio", _class="form-control string", ))), BR(), INPUT(_type="submit", _class="btn btn-primary btn-medium", _value='Continuar'))) if form.accepts(request, session): session.empleado = request.vars['fempleado'] session.user_code = request.vars['fempleado'].split()[0] session.fdesde = request.vars['fdesde'] session.fhasta = request.vars['fhasta'] log(f"seleccionado {session.empleado}") log(f"desde: {session.fdesde} hasta {session.fhasta}") # selector = (db.empleado.user_code == user_code) # usuario = db(selector).select().first().as_dict() session.tdesde = datetime.datetime.strptime(session.fdesde, '%Y-%m-%d') session.thasta = datetime.datetime.strptime(session.fhasta, '%Y-%m-%d') lista = aplico_politica(session.user_code, session.fdesde, session.fhasta) nombre_archivo = f'''{session.empleado} -{session.fdesde}-{session.fhasta}''' session.table = list_dict_to_table_sortable(lista, nombre_archivo) redirect(URL('informe')) else: log(f'acceso {request.function}') return dict(form=form)
def event_rheader(r): rheader = None record = r.record if record and r.representation == "html": from gluon import A, DIV, TABLE, TR, TH from s3 import s3_rheader_tabs name = r.name if name == "incident": tabs = [ (T("Incident Details"), None), #(T("Tasks"), "task"), #(T("Human Resources"), "human_resource"), #(T("Equipment"), "asset"), (T("Action Plan"), "plan"), (T("Incident Reports"), "incident_report"), (T("Logs"), "log"), (T("Expenses"), "expense"), (T("Situation Reports"), "sitrep"), ] rheader_tabs = s3_rheader_tabs(r, tabs) record_id = r.id incident_type_id = record.incident_type_id # Dropdown of Scenarios to select stable = current.s3db.event_scenario query = (stable.incident_type_id == incident_type_id) & \ (stable.deleted == False) scenarios = current.db(query).select( stable.id, stable.name, ) if len(scenarios) and r.method != "event": from gluon import SELECT, OPTION dropdown = SELECT(_id="scenarios") dropdown["_data-incident_id"] = record_id dappend = dropdown.append dappend(OPTION(T("Select Scenario"))) for s in scenarios: dappend(OPTION(s.name, _value=s.id)) scenarios = TR( TH("%s: " % T("Scenario")), dropdown, ) s3 = current.response.s3 script = "/%s/static/themes/SAFIRE/js/incident_profile.js" % r.application if script not in s3.scripts: s3.scripts.append(script) s3.js_global.append('''i18n.scenarioConfirm="%s"''' % T( "Populate Incident with Tasks, Organizations, Positions and Equipment from the Scenario?" )) else: scenarios = "" if record.exercise: exercise = TH(T("EXERCISE")) else: exercise = TH() if record.closed: closed = TH(T("CLOSED")) else: closed = TH() if record.event_id or r.method == "event": event = "" else: event = A(T("Assign to Event"), _href=URL( c="event", f="incident", args=[record_id, "event"], ), _class="action-btn") table = r.table rheader = DIV( TABLE( TR(exercise), TR( TH("%s: " % table.name.label), record.name, ), TR( TH("%s: " % table.incident_type_id.label), table.incident_type_id.represent(incident_type_id), ), TR( TH("%s: " % table.location_id.label), table.location_id.represent(record.location_id), ), # @ToDo: Add Zone TR( TH("%s: " % table.severity.label), table.severity.represent(record.severity), ), TR( TH("%s: " % table.level.label), table.level.represent(record.level), ), TR( TH("%s: " % table.organisation_id.label), table.organisation_id.represent( record.organisation_id), ), TR( TH("%s: " % table.person_id.label), table.person_id.represent(record.person_id), ), scenarios, TR( TH("%s: " % table.comments.label), record.comments, ), TR( TH("%s: " % table.date.label), table.date.represent(record.date), ), TR(closed), event, ), rheader_tabs) elif name == "incident_report": record_id = r.id ltable = current.s3db.event_incident_report_incident query = (ltable.incident_report_id == record_id) link = current.db(query).select(ltable.incident_id, limitby=(0, 1)).first() if link: from s3 import S3Represent represent = S3Represent(lookup="event_incident", show_link=True) rheader = DIV( TABLE( TR( TH("%s: " % ltable.incident_id.label), represent(link.incident_id), ), )) else: rheader = DIV( A(T("Assign to Incident"), _href=URL( c="event", f="incident_report", args=[record_id, "assign"], ), _class="action-btn")) elif name == "event": tabs = [ (T("Event Details"), None), (T("Incidents"), "incident"), (T("Documents"), "document"), (T("Photos"), "image"), ] rheader_tabs = s3_rheader_tabs(r, tabs) table = r.table rheader = DIV( TABLE( TR( TH("%s: " % table.event_type_id.label), table.event_type_id.represent( record.event_type_id), ), TR( TH("%s: " % table.name.label), record.name, ), TR( TH("%s: " % table.start_date.label), table.start_date.represent(record.start_date), ), TR( TH("%s: " % table.comments.label), record.comments, ), ), rheader_tabs) elif name == "scenario": tabs = [ (T("Scenario Details"), None), #(T("Tasks"), "task"), #(T("Human Resources"), "human_resource"), #(T("Equipment"), "asset"), (T("Action Plan"), "plan"), (T("Incident Reports"), "incident_report"), ] rheader_tabs = s3_rheader_tabs(r, tabs) table = r.table rheader = DIV( TABLE( TR( TH("%s: " % table.incident_type_id.label), table.incident_type_id.represent( record.incident_type_id), ), TR( TH("%s: " % table.organisation_id.label), table.organisation_id.represent( record.organisation_id), ), TR( TH("%s: " % table.location_id.label), table.location_id.represent(record.location_id), ), TR( TH("%s: " % table.name.label), record.name, ), TR( TH("%s: " % table.comments.label), record.comments, ), ), rheader_tabs) return rheader
def materialize_form(form, fields): """ Change layout of SQLFORM forms """ form.add_class("form-horizontal center") main_div = DIV(_class="center") for field_id, label, controls, field_help in fields: curr_div = DIV(_class="row") input_field = None _controls = controls try: _name = controls.attributes["_name"] except: _name = "" try: _type = controls.attributes["_type"] except: _type = "string" try: _id = controls.attributes["_id"] except: _id = "" if isinstance(controls, INPUT): if _type == "file": # Layout for file type inputs input_field = DIV( DIV(SPAN("Upload"), INPUT(_type=_type, _id=_id), _class="btn"), DIV(INPUT(_type="text", _class="file-path", _placeholder=label.components[0]), _class="file-path-wrapper"), _class="col input-field file-field offset-s3 s6") if isinstance(controls, SPAN): # Mostly for ids which cannot be edited by user _controls = INPUT(_value=controls.components[0], _id=_id, _name=_name, _disabled="disabled") elif isinstance(controls, TEXTAREA): # Textarea inputs try: _controls = TEXTAREA(controls.components[0], _name=_name, _id=_id, _class="materialize-textarea text") except IndexError: _controls = TEXTAREA(_name=_name, _id=_id, _class="materialize-textarea text") elif isinstance(controls, SELECT): # Select inputs _controls = SELECT(OPTION(label, _value=""), _name=_name, _class="browser-default", *controls.components[1:]) # Note now label will be the first element # of Select input whose value would be "" label = "" elif isinstance(controls, A): # For the links in the bottom while updating tables like auth_user label = "" elif isinstance(controls, INPUT) is False: # If the values are readonly _controls = INPUT(_value=controls, _name=_name, _disabled="") if input_field is None: input_field = DIV(_controls, label, _class="input-field col offset-s3 s6") curr_div.append(input_field) main_div.append(curr_div) return main_div
def materialize_form(form, fields): """ Change layout of SQLFORM forms @params form (FORM): FORM object representing the form DOM @params fields (List): List of fields in the form @return (DIV): Materialized form wrapped with a DIV """ form.add_class("form-horizontal center") main_div = DIV(_class="center") for _, label, controls, field_tooltip in fields: curr_div = DIV(_class="row center valign-wrapper") input_field = None _controls = controls try: _name = controls.attributes["_name"] except: _name = "" try: _type = controls.attributes["_type"] except: _type = "string" try: _id = controls.attributes["_id"] except: _id = "" if isinstance(controls, INPUT): if _type == "file": # Layout for file type inputs input_field = DIV( DIV(SPAN("Upload"), INPUT(_type=_type, _id=_id), _class="btn"), DIV(INPUT(_type="text", _class="file-path", _placeholder=label.components[0]), _class="file-path-wrapper"), _class="col input-field file-field offset-s3 s6") elif _type == "checkbox": # Checkbox input field does not require input-field class input_field = DIV(_controls, label, _class="col offset-s3 s6") if isinstance(controls, SPAN): # Mostly for ids which cannot be edited by user _controls = INPUT(_value=controls.components[0], _id=_id, _name=_name, _disabled="disabled") elif isinstance(controls, TEXTAREA): # Textarea inputs try: _controls = TEXTAREA(controls.components[0], _name=_name, _id=_id, _class="materialize-textarea text") except IndexError: _controls = TEXTAREA(_name=_name, _id=_id, _class="materialize-textarea text") elif isinstance(controls, SELECT): # Select inputs _controls = SELECT(OPTION(label, _value=""), _name=_name, _class="browser-default", *controls.components[1:]) # Note now label will be the first element # of Select input whose value would be "" input_field = DIV(_controls, _class="col offset-s3 s6") elif isinstance(controls, A): # For the links in the bottom while updating tables like auth_user label = "" elif isinstance(controls, INPUT) is False: # If the values are readonly _controls = INPUT(_value=controls, _name=_name, _disabled="") if input_field is None: input_field = DIV(_controls, label, _class="input-field col offset-s3 s6") curr_div.append(input_field) if field_tooltip: curr_div.append( DIV(I(_class="fa fa-info-circle tooltipped", data={ "position": "top", "delay": "30", "tooltip": field_tooltip }, _style="cursor: pointer;"), _class="col s1 valign")) main_div.append(curr_div) return main_div
def widget(cls, field, value, restricted=None, rval=None, orderby=None, multiple=True, **attributes): """ generates a SELECT tag, including OPTIONs (only 1 option allowed) This method takes one argument more than MultipleOptionsWidget.widget. The restricted argument identifies the form field whose value constrains the values to be included as available options for this widget. see also: :meth:`FormWidget.widget` :meth:`OptionsWidget.widget` :meth:`MultipleOptionsWidget.widget` """ db = current.db default = {'value': value} attributes.update({ '_restricted': restricted, '_orderby': orderby, '_multiple': multiple }) attr = cls._attributes(field, default, **attributes) # get raw list of options for this widget requires = field.requires if not isinstance(requires, (list, tuple)): requires = [requires] if requires: try: options = requires[0].options() except SyntaxError: print('widget cannot get options of %s' % field) # get the table referenced by this field linktable = get_linktable(field) # get the value of the restricting field if restricted not in [None, 'None', 'none']: table = field.table filter_field = table[restricted] if rval: filter_val = rval else: filter_row = db(field == value).select().first() filter_val = filter_row[filter_field] # get the table referenced by the restricting field filter_linktable = get_linktable(filter_field) # find the linktable field that references filter_linktable reffields = db[linktable].fields ref = 'reference {}'.format(filter_linktable) cf = [f for f in reffields if db[linktable][f].type == ref][0] # filter and order raw list of options myorder = orderby if ( orderby and orderby.replace('~', '') in reffields) else 'id' rows = db(db[linktable][cf] == filter_val).select(orderby=myorder) else: reffields = db[linktable].fields myorder = orderby if ( orderby and orderby.replace('~', '') in reffields) else 'id' rows = db(db[linktable].id > 0).select(orderby=myorder) # build widget with filtered and ordered options print(options) f_options = [ o for r in rows for o in options if o[0] and r.id == int(o[0]) ] opts = [OPTION(v, _value=k) for (k, v) in f_options] widget = SELECT(*opts, **attr) return widget
def event_rheader(r): rheader = None record = r.record if record and r.representation == "html": from gluon import DIV, TABLE, TR, TH from s3 import s3_rheader_tabs name = r.name if name == "incident": # Incident Controller tabs = [ (T("Incident Details"), None), #(T("Tasks"), "task"), #(T("Human Resources"), "human_resource"), #(T("Equipment"), "asset"), (T("Action Plan"), "plan"), (T("Incident Reports"), "incident_report"), ] rheader_tabs = s3_rheader_tabs(r, tabs) incident_type_id = record.incident_type_id # Dropdown of Scenarios to select stable = current.s3db.event_scenario query = (stable.incident_type_id == incident_type_id) & \ (stable.deleted == False) scenarios = current.db(query).select( stable.id, stable.name, ) if len(scenarios): from gluon import SELECT, OPTION dropdown = SELECT(_id="scenarios") dropdown["_data-incident_id"] = r.id dappend = dropdown.append dappend(OPTION(T("Select Scenario"))) for s in scenarios: dappend(OPTION(s.name, _value=s.id)) scenarios = TR( TH("%s: " % T("Scenario")), dropdown, ) s3 = current.response.s3 script = "/%s/static/themes/SAFIRE/js/incident_profile.js" % r.application if script not in s3.scripts: s3.scripts.append(script) s3.js_global.append('''i18n.scenarioConfirm="%s"''' % T( "Populate Incident with Tasks, Positions and Equipment from the Scenario?" )) else: scenarios = "" if record.exercise: exercise = TH(T("EXERCISE")) else: exercise = TH() if record.closed: closed = TH(T("CLOSED")) else: closed = TH() table = r.table rheader = DIV( TABLE( TR(exercise), TR( TH("%s: " % table.name.label), record.name, ), TR( TH("%s: " % table.incident_type_id.label), table.incident_type_id.represent(incident_type_id), ), TR( TH("%s: " % table.person_id.label), table.person_id.represent(record.person_id), ), scenarios, TR( TH("%s: " % table.location_id.label), table.location_id.represent(record.location_id), ), TR( TH("%s: " % table.comments.label), record.comments, ), TR( TH("%s: " % table.date.label), table.date.represent(record.date), ), TR(closed), ), rheader_tabs) elif name == "scenario": # Scenarios Controller tabs = [ (T("Scenario Details"), None), #(T("Tasks"), "task"), #(T("Human Resources"), "human_resource"), #(T("Equipment"), "asset"), (T("Action Plan"), "plan"), (T("Incident Reports"), "incident_report"), ] rheader_tabs = s3_rheader_tabs(r, tabs) table = r.table rheader = DIV( TABLE( TR( TH("%s: " % table.incident_type_id.label), table.incident_type_id.represent( record.incident_type_id), ), TR( TH("%s: " % table.name.label), record.name, ), TR( TH("%s: " % table.comments.label), record.comments, ), ), rheader_tabs) return rheader
def event_rheader(r): rheader = None record = r.record if record and r.representation == "html": from gluon import A, DIV, TABLE, TR, TH from s3 import s3_rheader_tabs name = r.name if name == "incident": if settings.get_incident_label(): # == "Ticket" label = T("Ticket Details") else: label = T("Incident Details") tabs = [ (label, None), #(T("Tasks"), "task"), #(T("Human Resources"), "human_resource"), #(T("Equipment"), "asset"), (T("Action Plan"), "plan"), (T("Incident Reports"), "incident_report"), (T("Logs"), "log"), (T("Expenses"), "expense"), (T("Situation Reports"), "sitrep"), ] rheader_tabs = s3_rheader_tabs(r, tabs) record_id = r.id incident_type_id = record.incident_type_id editable = current.auth.s3_has_permission( "UPDATE", "event_incident", record_id) if editable and r.method == "plan": # Dropdown of Scenarios to select # @ToDo: Move this to a Popup behind an Action Button, to make it clearer that this isn't a maintained link # @ToDo: Also add 'Clear' button to clear all elements & start from a blank slate stable = current.s3db.event_scenario query = (stable.incident_type_id == incident_type_id) & \ (stable.deleted == False) scenarios = current.db(query).select( stable.id, stable.name, ) if len(scenarios) and r.method != "event": from gluon import SELECT, OPTION dropdown = SELECT(_id="scenarios") dropdown["_data-incident_id"] = record_id dappend = dropdown.append dappend(OPTION(T("Select Scenario"))) for s in scenarios: dappend(OPTION(s.name, _value=s.id)) scenarios = TR( TH("%s: " % T("Apply Scenario")), dropdown, ) s3 = current.response.s3 script = "/%s/static/themes/SAFIRE/js/incident_profile.js" % r.application if script not in s3.scripts: s3.scripts.append(script) s3.js_global.append( '''i18n.scenarioConfirm="%s"''' % T("Populate Incident with Tasks, Organizations, Positions and Equipment from the Scenario?" )) else: scenarios = "" else: scenarios = "" if record.exercise: exercise = TH(T("EXERCISE")) else: exercise = TH() if record.closed: closed = TH(T("CLOSED")) else: closed = TH() if record.event_id or r.method == "event" or not editable: event = "" else: if settings.get_event_label(): # == "Disaster" label = T("Assign to Disaster") else: label = T("Assign to Event") event = A(label, _href=URL( c="event", f="incident", args=[record_id, "event"], ), _class="action-btn") table = r.table rheader = DIV( TABLE( TR(exercise), TR( TH("%s: " % table.name.label), record.name, ), TR( TH("%s: " % table.incident_type_id.label), table.incident_type_id.represent(incident_type_id), ), TR( TH("%s: " % table.location_id.label), table.location_id.represent(record.location_id), ), # @ToDo: Add Zone TR( TH("%s: " % table.severity.label), table.severity.represent(record.severity), ), TR( TH("%s: " % table.level.label), table.level.represent(record.level), ), TR( TH("%s: " % table.organisation_id.label), table.organisation_id.represent( record.organisation_id), ), TR( TH("%s: " % table.person_id.label), table.person_id.represent(record.person_id), ), scenarios, TR( TH("%s: " % table.comments.label), record.comments, ), TR( TH("%s: " % table.date.label), table.date.represent(record.date), ), TR(closed), event, ), rheader_tabs) return rheader