Пример #1
0
def get_json_from_request(request):
    # special request:
    special_request = ['name', 'source', 'long', 'lat', 'csrfmiddlewaretoken', 'uuid']

    mstring = []
    json = {}
    for key in request.POST.iterkeys():  # 'for key in request.GET' works too.
        # Add filtering logic here.
        valuelist = request.POST.getlist(key)
        mstring.extend(['%s=%s' % (key, val) for val in valuelist])

    for str in mstring:
        req = str.split('=', 1)
        json[req[0].lower()] = req[1]
        try:
            Attribute.objects.get(key=req[0].lower())
        except Attribute.DoesNotExist:
            if req[0] not in special_request:
                tmp_changeset = Changeset.objects.create(
                    social_user=request.user
                )
                attribute = Attribute()
                attribute.key = req[0]
                attribute.changeset = tmp_changeset
                attribute.save()
                domain = Domain.objects.get(name='Health')
                specification = Specification()
                specification.domain = domain
                specification.attribute = attribute
                specification.changeset = tmp_changeset
                specification.save()

    # check mandatory
    is_valid = True
    if not json['name'] or json['name'] == '':
        is_valid = False
        json['invalid_key'] = 'name'

    if not json['lat'] or json['lat'] == '':
        is_valid = False
        json['invalid_key'] = 'latitude'

    if not json['long'] or json['long'] == '':
        is_valid = False
        json['invalid_key'] = 'longitude'

    if is_valid:
        domain = Domain.objects.get(name='Health')
        attributes = Specification.objects.filter(domain=domain).filter(required=True)
        for attribute in attributes:
            try:
                if len(json[attribute.attribute.key]) == 0:
                    is_valid = False
                    json['invalid_key'] = attribute.attribute.key
                    break
            except Exception:
                pass

    json['is_valid'] = is_valid
    return json
 def check_what3words_attribute(self, user):
     try:
         Attribute.objects.get(key='what3words')
     except Attribute.DoesNotExist:
         tmp_changeset = Changeset.objects.create(social_user=user)
         attribute = Attribute()
         attribute.key = 'what3words'
         attribute.changeset = tmp_changeset
         attribute.save()
         domain = Domain.objects.get(name="Health")
         specification = Specification()
         specification.domain = domain
         specification.attribute = attribute
         specification.changeset = tmp_changeset
         specification.save()
Пример #3
0
 def check_what3words_attribute(self, user):
     try:
         Attribute.objects.get(key='what3words')
     except Attribute.DoesNotExist:
         tmp_changeset = Changeset.objects.create(
             social_user=user
         )
         attribute = Attribute()
         attribute.key = 'what3words'
         attribute.changeset = tmp_changeset
         attribute.save()
         domain = Domain.objects.get(name='Health')
         specification = Specification()
         specification.domain = domain
         specification.attribute = attribute
         specification.changeset = tmp_changeset
         specification.save()
Пример #4
0
def get_json_from_request(request):
    # special request:
    special_request = [
        "name", "source", "long", "lat", "csrfmiddlewaretoken", "uuid"
    ]

    mstring = []
    json = {}
    for key in request.POST.iterkeys():  # "for key in request.GET" works too.
        # Add filtering logic here.
        valuelist = request.POST.getlist(key)
        mstring.extend(['%s=%s' % (key, val) for val in valuelist])

    for str in mstring:
        req = str.split('=', 1)
        json[req[0].lower()] = req[1]
        try:
            Attribute.objects.get(key=req[0].lower())
        except Attribute.DoesNotExist:
            if req[0] not in special_request:
                tmp_changeset = Changeset.objects.create(
                    social_user=request.user)
                attribute = Attribute()
                attribute.key = req[0]
                attribute.changeset = tmp_changeset
                attribute.save()
                domain = Domain.objects.get(name="Health")
                specification = Specification()
                specification.domain = domain
                specification.attribute = attribute
                specification.changeset = tmp_changeset
                specification.save()

    # check mandatory
    is_valid = True
    if not json['name'] or json['name'] == "":
        is_valid = False
        json['invalid_key'] = "name"

    if not json['lat'] or json['lat'] == "":
        is_valid = False
        json['invalid_key'] = "latitude"

    if not json['long'] or json['long'] == "":
        is_valid = False
        json['invalid_key'] = "longitude"

    if is_valid:
        domain = Domain.objects.get(name="Health")
        attributes = Specification.objects.filter(domain=domain).filter(
            required=True)
        for attribute in attributes:
            try:
                if len(json[attribute.attribute.key]) == 0:
                    is_valid = False
                    json['invalid_key'] = attribute.attribute.key
                    break
            except:
                pass

    json['is_valid'] = is_valid
    return json