def test_invalid_array(self):
     """
     Sparse array with 1,000 consecutive blank values
     """
     input_data = {
         'test[0]': 1,
         'test[1001]': 1,
     }
     with self.assertRaises(ParseException):
         parse_json_form(input_data)
Example #2
0
 def test_such_deep(self):
     """
     Example 6: Such Deep
     """
     input_data = {
         'wow[such][deep][3][much][power][!]': "Amaze",
     }
     expected_output = {
         'wow': {
             'such': {
                 'deep': [
                     None,
                     None,
                     None,
                     {
                         'much': {
                             'power': {
                                 '!': "Amaze",
                             }
                         }
                     }
                 ]
             }
         }
     }
     result = parse_json_form(input_data)
     assert result == expected_output
    def to_internal_value(self, data):
        """
        Extract 'values' JSON from relational and built-in form fields
        """

        # Save files and return paths
        for key, value in data.items():
             if isinstance(value, UploadedFile):
                 path = 'observations/' + value.name
                 data[key] = default_storage.save(path, value)

        # Convert nested keys to JSON structure
        data = parse_json_form(data)
        fixed_fields = list(self.get_fields().keys()) + [
            'csrfmiddlewaretoken', '_method',
        ]

        # On-the-fly site creation (New Site + Observation)
        if data.get('site_data', None):
            self.create_site(data)

        # Extract JSON data into 'values' field, leaving relational and
        # built-in fields in place.
        data.setdefault('values', {})
        for key in list(data.keys()):
            if key not in fixed_fields:
                data['values'][key] = data.pop(key)

        return super().to_internal_value(data)
Example #4
0
def import_row(run, i, row, instance_globals, matched):
    """
    Create actual report instance from parsed values.
    """

    # Copy global values to record hash
    record = {
        key: instance_globals[key]
        for key in instance_globals
    }

    for col in matched:
        if 'colnum' in col and 'meta_value' not in col:
            val = row[col['colnum']]
            save_value(col, val, record)

    seen = set()
    for col in matched:
        field_name = col['field_name']
        if col['type'] == 'meta' and field_name not in seen:
            seen.add(field_name)
            ident = Identifier.objects.filter(
                serializer=run.serializer,
                name__iexact=str(record[field_name]),
            ).first()
            if ident and ident.value:
                record[field_name] = ident.value

    record.pop('_attr_index', None)

    Serializer = run.get_serializer()
    try:
        serializer = Serializer(
            data=parse_json_form(record),
            context={
                'data_wizard': {
                    'run': run,
                }
            },
        )
        if serializer.is_valid():
            with transaction.atomic():
                obj = serializer.save()
            error = None
        else:
            obj = None
            error = json.dumps(serializer.errors)
    except Exception as e:
        logging.warning(
            "{run}: Error In Row {row}".format(
                run=run,
                row=i,
            )
        )
        logging.exception(e)
        obj = None
        error = repr(e)
    return obj, error
Example #5
0
 def test_multiple_values(self):
     """
     Example 2: Multiple Values
     """
     input_data = MultiValueDict()
     input_data.setlist('bottle-on-wall', [1, 2, 3])
     expected_output = {
         'bottle-on-wall': [1, 2, 3]
     }
     result = parse_json_form(input_data)
     assert result == expected_output
 def test_sparse_array(self):
     """
     Sparse array
     """
     input_data = {
         'test[0]': 1,
         'test[100]': 1,
     }
     result = parse_json_form(input_data)
     assert 'test' in result
     assert len(result['test']) == 101
 def test_sparser_array(self):
     """
     Sparser array
     """
     input_data = OrderedDict((
         ('test[0]', 1),
         ('test[1000]', 1),
         ('test[2000]', 1),
     ))
     result = parse_json_form(input_data)
     assert 'test' in result
     assert len(result['test']) == 2001
Example #8
0
 def test_append(self):
     """
     Example 8: Append
     """
     input_data = {
         'highlander[]': "one",
     }
     expected_output = {
         'highlander': ["one"],
     }
     result = parse_json_form(input_data)
     assert result == expected_output
Example #9
0
 def test_basic_keys(self):
     """
     Example 1: Basic Keys
     """
     input_data = {
         'name': "Bender",
         'hind': "Bitable",
         'shiny': True
     }
     expected_output = input_data
     result = parse_json_form(input_data)
     assert result == expected_output
Example #10
0
 def test_sparse_arrays(self):
     """
     Example 4: Sparse Arrays
     """
     input_data = {
         "hearbeat[0]": "thunk",
         "hearbeat[2]": "thunk",
     }
     expected_output = {
         "hearbeat": ["thunk", None, "thunk"]
     }
     result = parse_json_form(input_data)
     assert result == expected_output
Example #11
0
 def echo(self, **update):
     length = int(self.headers.get('content-length'))
     qs = self.rfile.read(length).decode('utf-8')
     data = parse_json_form({
         key: val[0]
         for key, val in parse_qs(qs).items()
     })
     for value in data.values():
         if isinstance(value, list):
             for i, val in enumerate(value):
                 val['@index'] = i
     data.update(**update)
     self.send_response(200)
     self.send_header("Content-Type", "application/json")
     self.end_headers()
     self.wfile.write(json.dumps(data).encode('utf-8'))
Example #12
0
 def test_invalid(self):
     """
     Example 10: Invalid
     """
     input_data = {
         "error[good]": "BOOM!",
         "error[bad": "BOOM BOOM!",
     }
     expected_output = {
         'error': {
             'good': "BOOM!",
         },
         'error[bad': "BOOM BOOM!",
     }
     result = parse_json_form(input_data)
     assert result == expected_output
Example #13
0
 def test_single_element_array(self):
     """
     Test array with single item and no digit in key
     """
     input_data = {
         "simple_key": "simple",
         "nested[][nested_key_1]": "nested_value_1",
         "nested[][nested_key_2]": "nested_value_2",
     }
     expected_output = {
         "simple_key": "simple",
         "nested": [{
             "nested_key_1": "nested_value_1",
             "nested_key_2": "nested_value_2",
         }],
     }
     result = parse_json_form(input_data)
     assert result == expected_output
Example #14
0
File: server.py Project: wq/wq.app
 def echo(self, status=200, **update):
     ctype, pdict = cgi.parse_header(self.headers.get('content-type'))
     pdict = {key: val.encode('utf-8') for key, val in pdict.items()}
     form = cgi.parse_multipart(self.rfile, pdict)
     data = parse_json_form({
         key: val[0].decode('utf-8')
         for key, val in form.items()
     })
     for value in data.values():
         if isinstance(value, list):
             for i, val in enumerate(value):
                 val['@index'] = i
     data.update(**update)
     self.send_response(status)
     self.send_header("Content-Type", "application/json")
     self.end_headers()
     # FIXME: This is to avoid a race condition in wq/outbox
     time.sleep(random.random())
     self.wfile.write(json.dumps(data).encode('utf-8'))
Example #15
0
 def test_deeper_structure(self):
     """
     Example 3: Deeper Structure
     """
     input_data = {
         'pet[species]': "Dahut",
         'pet[name]': "Hypatia",
         'kids[1]': "Thelma",
         'kids[0]': "Ashley",
     }
     expected_output = {
         'pet': {
             'species': "Dahut",
             'name': "Hypatia",
         },
         'kids': ["Ashley", "Thelma"],
     }
     result = parse_json_form(input_data)
     assert result == expected_output
Example #16
0
def import_row(run, row, instance_globals, matched):
    """
    Create actual report instance from parsed values.
    """

    # Copy global values to record hash
    record = {key: instance_globals[key] for key in instance_globals}

    for col in matched:
        if 'colnum' in col:
            val = row[col['colnum']]
            save_value(col, val, record)

    seen = set()
    for col in matched:
        field_name = col['field_name']
        if col['type'] == 'meta' and field_name not in seen:
            seen.add(field_name)
            ident = Identifier.objects.filter(
                serializer=run.serializer,
                name__iexact=str(record[field_name]),
            ).first()
            if ident and ident.value:
                record[field_name] = ident.value

    record.pop('_attr_index', None)
    record.pop('_attr_field', None)

    Serializer = run.get_serializer()
    try:
        serializer = Serializer(data=parse_json_form(record))
        if serializer.is_valid():
            with transaction.atomic():
                obj = serializer.save()
            error = None
        else:
            obj = None
            error = json.dumps(serializer.errors)
    except Exception as e:
        obj = None
        error = repr(e)
    return obj, error
def admin_mongo_edit(request, collection_name, object_id):
    if not check_user_group('staff',
                            request.user) and not request.user.is_superuser:
        raise PermissionDenied
    if request.method == 'POST':
        tmp = dict(request.POST)
        for key in tmp:
            tmp[key] = tmp[key][0]
        del tmp['csrfmiddlewaretoken']
        # return JsonResponse(parse_json_form(tmp))
        data = parse_json_form(tmp)
        status, fields = api.get_collection_pattern(collection_name)
        fill_field(fields, data)
        status, result = api.admin_update_document(collection_name, object_id,
                                                   data)
        if status:
            return redirect('..')
        else:
            return redirect('.')
    status, fields = api.get_collection_pattern(collection_name)
    status, data = api.admin_get_detail(collection_name, object_id)
    clean_datatype(data)
    found_id = False
    for i in range(len(fields)):
        if fields[i]['field_name'] == '_id':
            found_id = True
            index = i
            break
    if found_id:
        del fields[index]
    return render(
        request, 'app/admin_mongo-add.html', {
            'title': 'mongoDB Admin',
            'header_title': 'mongoDB Admin',
            'collection_name': collection_name,
            'fields': json.dumps(fields).replace('"', '\\"').replace(
                "'", "\\'"),
            'data': json.dumps(data).replace('"', '\\"').replace("'", "\\'"),
            'logo_link': '/staff',
            'only_logout': True
        })
Example #18
0
 def test_merge_behaviour(self):
     """
     Example 7: Merge Behaviour
     """
     # FIXME: Shouldn't this work regardless of key order?
     input_data = OrderedDict([
         ('mix', "scalar"),
         ('mix[0]', "array 1"),
         ('mix[2]', "array 2"),
         ('mix[key]', "key key"),
         ('mix[car]', "car key"),
     ])
     expected_output = {
         'mix': {
             '': "scalar",
             '0': "array 1",
             '2': "array 2",
             'key': "key key",
             'car': "car key",
         }
     }
     result = parse_json_form(input_data)
     assert result == expected_output
def admin_mongo_add(request, collection_name):
    if not check_user_group('staff',
                            request.user) and not request.user.is_superuser:
        raise PermissionDenied
    if request.method == 'POST':
        tmp = dict(request.POST)
        for key in tmp:
            tmp[key] = tmp[key][0]
        del tmp['csrfmiddlewaretoken']
        # return JsonResponse(parse_json_form(tmp))
        # print(parse_json_form(tmp))
        status, result = api.admin_insert_document(collection_name,
                                                   parse_json_form(tmp))
        if status:
            return redirect('..')
        else:
            return redirect('.')
    status, fields = api.get_collection_pattern(collection_name)
    found_id = False
    for i in range(len(fields)):
        if fields[i]['field_name'] == '_id':
            found_id = True
            index = i
            break
    if found_id:
        del fields[index]
    # print(fields)
    return render(
        request, 'app/admin_mongo-add.html', {
            'title': 'mongoDB Admin',
            'header_title': 'mongoDB Admin',
            'collection_name': collection_name,
            'fields': json.dumps(fields),
            'logo_link': '/staff',
            'only_logout': True
        })
Example #20
0
 def test_even_deeper(self):
     """
     Example 5: Even Deeper
     """
     input_data = {
         'pet[0][species]': "Dahut",
         'pet[0][name]': "Hypatia",
         'pet[1][species]': "Felis Stultus",
         'pet[1][name]': "Billie",
     }
     expected_output = {
         'pet': [
             {
                 'species': "Dahut",
                 'name': "Hypatia",
             },
             {
                 'species': "Felis Stultus",
                 'name': "Billie",
             }
         ]
     }
     result = parse_json_form(input_data)
     assert result == expected_output
Example #21
0
def new_order(request):
    errors = {}
    status = 'error'
    object = None
    if request.GET:
        data = parse_json_form(request.GET)
        print(data)
        entity = data.get('entity', None)
        action = data.get('action', 'create')
        pk = data.get('pk', None)
        form = None
        if entity == 'order':
            if (action == 'edit'):
                instance = DeliveryOrder.objects.filter(pk=pk).first()
                if instance:
                    form = DeliveryOrderForm(data, instance=instance)
            else:
                form = DeliveryOrderForm(data)
        elif entity == 'driver':
            name = data.get("name", None)
            driver = DeliveryDriver.objects.filter(name=name).first()
            if driver:
                phones = data.get('phones', None)
                if phones:
                    phones = [format_phone(phone) for phone in phones]
                    DriverPhone.objects.filter(driver=driver,
                                               phone__in=phones).delete()
                    for phone in phones:
                        DriverPhone.objects.create(driver=driver, phone=phone)
                status = 'ok'
            else:
                form = DeliveryDriverForm(data)
        else:
            raise AttributeError
        if form:
            if form.is_valid():
                object = form.save(commit=True)
                if entity == 'order':
                    loader_ids = data.get('new_workers', [])
                    turnouts_new = data.get('turnouts_new', [])
                    DeliveryTurnout.objects.filter(order=object.pk).exclude(
                        pk__in=turnouts_new).delete()
                    status = 'ok'
                    #loader_ids = [str(item) for item in loader_list]#.split(",")
                    errors['turnouts'] = ''
                    for loader in loader_ids:
                        turnoutForm = DeliveryTurnoutForm({
                            'order': pk,
                            'worker': loader
                        })
                        if turnoutForm.is_valid():
                            turnoutForm.save(commit=True)
                        else:
                            errors[
                                'turnouts'] = "Ограничение на добавление грузчика"
                            status = 'error'

                object.refresh_from_db()
                object = object.get_dict()
            else:
                for field in form:
                    errors[field.name] = " ".join(field.errors)
                print(errors)
    print(status)
    print(object)
    return JsonResponse({'status': status, 'errors': errors, 'entity': object})