Esempio n. 1
0
 def test_validate_config_json_provided(self):
     invalid_config_mapper = FieldMapper(
         config_json=json_string({"add_literals": "invalid value"}))
     self.assertIsNone(
         invalid_config_mapper.validate_config_json(
             json_string({"add_literals": {
                 "foo": "bar"
             }})))
Esempio 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)