def migrate(): documents = DocumentEntity.objects.using('default').all() log().info("There are %s documents in the database", len(documents)) for document in documents: log().info("Doc: '%s'", document)
def register_view(request): context = {} log().info("Register document") templates_list = docid_get_templates() # for now I only use first template TODO fix this template = templates_list[0] context['docid_template'] = template['long_form'] context['fields'] = template['fields'] params = {} for field_str in template['fields']: # context[field_str] = docid_get_field_info(field_str)['values'] model_dict = docid_get_field_info(field_str) params[field_str] = model_dict context['params'] = params return render(request, 'docweb/register_doc.html', context)
def get_next_running_number(template_model, field_name, params): """ Generates the next available number for the provided field_name in the provided template_model and with the provided parameters. The new number is also saved in the DB :param template_model: a template model :param field_name: the field nime of type DocIdTemplateTypes.RUNNING_ID :param params: a dict with parameters (necessary for the field_name) :return: the newly generated number as a string (with the required formatting) """ dbfieldname = DocIdTemplateField.objects.get(field_name=field_name) db_ob = TypeRunningId.objects.get(field_name=dbfieldname) fieldnames = get_tags(db_ob.fields_list, TAG_START, TAG_END) fields_value = '' docid_list = None for field_str in fieldnames: if field_str in params: kwargs = { 'field_id': get_field_by_field_name(field_str), 'value': params[field_str], } if docid_list is None: docid_list = DocIdFieldValue.objects.filter( template_id=template_model).filter(**kwargs).values_list( 'docid', flat=True) log().debug("1. fieldstr='%s' fieldvalue='%s' docid_list=%s", field_str, params[field_str], docid_list) else: docid_list = DocIdFieldValue.objects.filter( docid__in=docid_list).filter(**kwargs).values_list( 'docid', flat=True) log().debug("2. fieldstr='%s' fieldvalue='%s' docid_list=%s", field_str, params[field_str], docid_list) else: raise ValueError( "Parameter '{}' is missing. A new running_id cannot be built for '{}'" .format(field_str, field_name)) if len(docid_list): db_field = DocIdFieldValue.objects.filter(docid__in=docid_list).filter( field_id=get_field_by_field_name(field_name)).order_by('-value') for ob in db_field: log().debug("Value='%s'", ob.value) running_num = int(db_field[0].value) + 1 else: running_num = 1 # Formatting of the generated number new_value_str = str(running_num) while len(new_value_str) < db_ob.digits_number: new_value_str = "0{}".format(new_value_str) log().info("fields_value='%s'", fields_value) return new_value_str
def wip_view(request): # To decide if a homepage is necessary db_entities = DocumentEntity.objects.all() text = '' for entity in db_entities: docid = entity.docid elems = docid.split('-') elems.pop(0) last = elems.pop(-1) tmp = last.split('_') elems.append(str(int(tmp.pop(0)))) version = tmp.pop(0)[1:] elems = elems + version.split('.') log().info("Elements are: %s", elems) formatted_text = '<' + '><'.join(elems) + '>' log().info("Formatted text is : %s", formatted_text) text = '{}<br><code>{}</code>'.format(text, formatted_text) entity.docfields = formatted_text entity.save() return HttpResponse(text)
def build_id(template_str, params): """ Builds an id based on the provided template (as a string 'template_str') and the 'params' dict :param template_str: the docid template string :param params: the parameters dictionary :return: the docid corresponding to the provided params or throws ValueError """ if template_str is None: raise ValueError('The template cannot be None') field_names = get_tags(template_str, TAG_START, TAG_END) for field_str in field_names: if field_str in params: value = params[field_str] else: value = '' log().info("template '%s' current field: '%s'", template_str, field_str) template_str = template_str.replace( '{}{}{}'.format(TAG_START, field_str, TAG_END), str(value)) return template_str
def docid_get_field_info(field_name): log().debug("Loading Field with name '%s'", field_name) field_model = DocIdTemplateField.objects.get(field_name=field_name) fields_dict = field_model.to_dict() log().debug("Dict representation of this object is: '%s'", fields_dict) fields_dict['values'] = [] if field_model.field_type == DocIdTemplateTypes.CONSTANT: fieldtype_models = TypeConstant.objects.filter(field_name=field_model) elif field_model.field_type == DocIdTemplateTypes.RUNNING_ID: fieldtype_models = TypeRunningId.objects.filter(field_name=field_model) elif field_model.field_type == DocIdTemplateTypes.SEQUENCE: fieldtype_models = TypeSequence.objects.filter(field_name=field_model) elif field_model.field_type == DocIdTemplateTypes.MAP: fieldtype_models = TypeMap.objects.filter(field_name=field_model) elif field_model.field_type == DocIdTemplateTypes.TREE: fieldtype_models = TypeTree.objects.filter(field_name=field_model) else: fieldtype_models = [] for fieldtype_model in fieldtype_models: fields_dict['values'].append(fieldtype_model.to_dict()) return fields_dict
def search_view(request): context = {} # return docs_view(request) if (request.method == 'POST') and ('cmd' in request.POST): cmd = request.POST['cmd'] log().debug( 'You try to execute "{}" Good luck with that !'.format(cmd)) else: cmd = '' log().debug('Since no command was requested will use default') if cmd == 'Search': param_str = request.POST.get('search-str', '%') if len(param_str) == 0: param_str = '%' log().debug("Search string is '%s'", param_str) result = redirect(reverse('spacedoc_search_str', args=[param_str])) else: result = render(request, 'docweb/search.html', context) # return HttpResponse("Search page ...") return result
def search_str_view(request, param_str): log().info("Searching '%s'", param_str) # TODO implement return docs_view(request)