Ejemplo n.º 1
0
def create_field_mapper(request):

    form = None
    if request.method == "POST":
        form = FieldMapperForm(request.POST)
        if form.is_valid():
            new_field_mapper = FieldMapper(**form.cleaned_data)
            new_field_mapper.save()
            return redirect(reverse('configuration'))
    if form is None:
        form = FieldMapperForm
    return render(request, 'core/new_configuration_object.html', {
        'form': form,
        'object_name': 'Field Mapper',
    })
Ejemplo n.º 2
0
def field_mapper_update(request):

    """
    Create and save JSON to FieldMapper instance, or update pre-existing
    """

    LOGGER.debug(request.POST)

    # get update type
    update_type = request.POST.get('update_type')

    # handle new FieldMapper creation
    if update_type == 'new':
        LOGGER.debug('creating new FieldMapper instance')

        field_mapper = FieldMapper(
            name=request.POST.get('fm_name'),
            config_json=request.POST.get('fm_config_json'),
            field_mapper_type='xml2kvp'
        )

        # validate fm_config before creating
        try:
            field_mapper.validate_config_json()
            field_mapper.save()
            return JsonResponse({'results': True,
                                 'msg': 'New Field Mapper configurations were <strong>saved</strong> as: <strong>%s</strong>' % request.POST.get(
                                     'fm_name')}, status=201)
        except jsonschema.ValidationError as err:
            return JsonResponse({'results': False,
                                 'msg': 'Could not <strong>create</strong> <strong>%s</strong>, the following error was had: %s' % (
                                     field_mapper.name, str(err))}, status=409)

    # handle update
    if update_type == 'update':
        LOGGER.debug('updating pre-existing FieldMapper instance')

        # get fm instance
        field_mapper = FieldMapper.objects.get(pk=int(request.POST.get('fm_id')))

        # update and save
        field_mapper.config_json = request.POST.get('fm_config_json')

        # validate fm_config before updating
        try:
            field_mapper.validate_config_json()
            field_mapper.save()
            return JsonResponse({'results': True,
                                 'msg': 'Field Mapper configurations for <strong>%s</strong> were <strong>updated</strong>' % field_mapper.name},
                                status=200)
        except jsonschema.ValidationError as err:
            return JsonResponse({'results': False,
                                 'msg': 'Could not <strong>update</strong> <strong>%s</strong>, the following error was had: %s' % (
                                     field_mapper.name, str(err))}, status=409)

    # handle delete
    if update_type == 'delete':
        LOGGER.debug('deleting pre-existing FieldMapper instance')

        # get fm instance
        field_mapper = FieldMapper.objects.get(pk=int(request.POST.get('fm_id')))

        # delete
        field_mapper.delete()
        return JsonResponse({'results': True,
                             'msg': 'Field Mapper configurations for <strong>%s</strong> were <strong>deleted</strong>' % field_mapper.name},
                            status=200)