def add_linkdata_doc(docdef, baseform, data, append_key, append_data): """ Add data to form on top of request data. Used for example data. If append_key points to a compound field, will add new field element with example data. If field is not compound field, append_data will replace it (in dictionary.update(append_data) fashion). """ form = baseform(data) append_form(form, append_key, append_data) field_name = get_field_definition(docdef['fields'], append_key)['label'] append_msg = _('The chosen values were added under %s.') % field_name return {"form" : form, "append" : append_data, 'append_msg' : append_msg, 'formdata' : data}
def autocomplete_doc(request, doctype, field): """ Autocomplete for basedocs that has query formulation parameters in basedoc JSON """ term = request.GET.get("term") if not term: raise Http404 term = pyes.clean_string(term) doc, baseform = get_base(doctype) field_def = get_field_definition(doc['fields'], field) if not field_def: raise Http404 return autocomplete(request, field_def["linkdocument"], field_def["query"], term)
def add_compoundfield_doc(docdef, baseform, data, append_key, eid): """ Add compound field to doc for given field's key as append_key. This relies on form_tools and wtforms. """ form = baseform(data) retval = append_form(form, append_key) field_name = get_field_definition(docdef['fields'], append_key)['label'] if not retval: append_msg = _("Field %s was not found.") % field_name else: append_msg = _("A new empty field was added to \"%s\".") % field_name return {"form" : form, 'append_msg' : append_msg, "append_key" : append_key, 'doc_id': eid, 'formdata' : data }
def remove_field_doc(docdef, baseform, data, remove_key): """ Removes given field from form. Works by removing fields from request data before form is actually built. Fields are encoded for HTTP key-value parameters, so remove_key needs to have the doc's whole structure incorporated. If removing field level3 from structure like level1: level2: level3 key needs to be like: level1-level2-level3. Normally wtforms takes care of creating keys from deep structure. """ field_name = '' msg = _('Failed to remove the field.') match = re.match('(.*-)?([^-]*)-\d+', remove_key) if match: parent_field_key = match.group(2) field_name = get_field_definition( docdef['fields'], parent_field_key)['label'] success = False valuename = 'ignore_' for key in data.keys(): if key.startswith(remove_key + '-'): if valuename == 'ignore_': valuename = data[key] del(data[key]) success = True if valuename == 'ignore_': valuename = None if success: msg = _('The field was removed') if field_name and len(field_name) > 0: msg += _(' from \"%s\".') % field_name else: msg += '.' context = update_doc(docdef, baseform, data) context['remove_msg'] = msg return context
def htmltree_conversion(data, basedoc): """ Converts document to html ul tree using labels from basedoc for field names """ ndata = [] for key, value in data.items(): try: field, lang = key.rsplit("_", 1) except ValueError: lang = None field = key fielddef = get_field_definition(basedoc["fields"], field) if fielddef: if lang and fielddef["type"] == "multilingualfield": label = fielddef["label"] + ' (%s)' % lang else: label = fielddef["label"] else: label = None if hasattr(value, "items"): ndata.append('<li>%s: <ul class="field_%s">%s</ul></li>' % (label or key, key, ''.join( htmltree_conversion(value, basedoc)))) elif type(value) == type([]): ul = [] for v in value: if hasattr(v, "items"): ul.append('<li><ul>%s</ul></li>' % (''.join( htmltree_conversion(v, basedoc)))) else: if value: ul.append('<li>%s</li>' % value) ndata.append('<li class="key_%s">%s: <ul>%s</ul></li>' % (key, label or key, ''.join(ul))) else: if value: ndata.append('<li>%s: %s</li>' % (label or key, value)) return '%s' % '\n'.join(ndata)