예제 #1
0
def add(request):
    add_params = request.POST
    add_type = add_params['type']
    Model = modelmap.get(add_type, None)
    if Model is None:
        raise KeyError('%s is not a recognized model type' % add_type)
    model_admin = get_admin(Model)
    if not model_admin.has_add_permission(request):
        raise PermissionDenied(
            'You do not have permission to add a model of the requested type'
        )
    good_fields = filter_fields(add_params.keys(), model_admin, request)
    bad_fields = set(good_fields) - set(add_params.keys())
    if bad_fields:
        raise PermissionDenied(
            'You do not have permission to set the following field(s) on new objects: %s'
            % ','.join(sorted(bad_fields))
        )
    newobj = Model()
    changed_fields = []
    m2m_collections = []
    for k, v in add_params.items():
        if k in ('type', 'id'):
            continue
        new_value = parse_value(Model, k, v, request.user)
        if type(new_value) == list:  # accounts for m2m relationships
            m2m_collections.append((k, new_value))
            new_value = [str(x) for x in new_value]
        else:
            setattr(newobj, k, new_value)
        changed_fields.append('Set %s to "%s".' % (k, new_value))
    newobj.full_clean()
    models = newobj.save() or [newobj]
    for k, v in m2m_collections:
        getattr(newobj, k).set(v)
    logutil.addition(request, newobj)
    logutil.change(request, newobj, ' '.join(changed_fields))
    resp = HttpResponse(
        serializers.serialize('json', models, ensure_ascii=False),
        content_type='application/json;charset=utf-8',
    )
    if 'queries' in request.GET and request.user.has_perm('tracker.view_queries'):
        return HttpResponse(
            json.dumps(connection.queries, ensure_ascii=False, indent=1),
            content_type='application/json;charset=utf-8',
        )
    return resp
예제 #2
0
def add(request):
    try:
        addParams = viewutil.request_params(request)
        addtype = addParams['type']
        if not request.user.has_perm('tracker.add_' + permmap.get(addtype,addtype)):
            return HttpResponse('Access denied',status=403,content_type='text/plain;charset=utf-8')
        Model = modelmap[addtype]
        newobj = Model()
        for k,v in addParams.items():
            if k in ('type','id'):
                continue
            setattr(newobj, k, parse_value(k, v))
        newobj.full_clean()
        models = newobj.save() or [newobj]
        logutil.addition(request, newobj)
        resp = HttpResponse(serializers.serialize('json', models, ensure_ascii=False),content_type='application/json;charset=utf-8')
        if 'queries' in request.GET and request.user.has_perm('tracker.view_queries'):
            return HttpResponse(json.dumps(connection.queries, ensure_ascii=False, indent=1),content_type='application/json;charset=utf-8')
        return resp
    except IntegrityError as e:
        return HttpResponse(json.dumps({'error': u'Integrity error: %s' % e}, ensure_ascii=False), status=400, content_type='application/json;charset=utf-8')
    except ValidationError as e:
        d = {'error': u'Validation Error'}
        if hasattr(e,'message_dict') and e.message_dict:
            d['fields'] = e.message_dict
        if hasattr(e,'messages') and e.messages:
            d['messages'] = e.messages
        return HttpResponse(json.dumps(d, ensure_ascii=False), status=400, content_type='application/json;charset=utf-8')
    except AttributeError as e:
        return HttpResponse(json.dumps({'error': 'Attribute Error, malformed add parameters', 'exception': unicode(e)}, ensure_ascii=False), status=400, content_type='application/json;charset=utf-8')
    except KeyError as e:
        return HttpResponse(json.dumps({'error': 'Key Error, malformed add parameters', 'exception': unicode(e)}, ensure_ascii=False), status=400, content_type='application/json;charset=utf-8')
    except FieldError as e:
        return HttpResponse(json.dumps({'error': 'Field Error, malformed add parameters', 'exception': unicode(e)}, ensure_ascii=False), status=400, content_type='application/json;charset=utf-8')
    except ValueError as e:
        return HttpResponse(json.dumps({'error': u'Value Error', 'exception': unicode(e)}, ensure_ascii=False), status=400, content_type='application/json;charset=utf-8')
    except ObjectDoesNotExist as e:
        return HttpResponse(json.dumps({'error': 'Foreign Key could not be found', 'exception': unicode(e)}, ensure_ascii=False), status=400, content_type='application/json;charset=utf-8')
예제 #3
0
def add(request):
    try:
        addParams = viewutil.request_params(request)
        addtype = addParams['type']
        if not request.user.has_perm('tracker.add_' + permmap.get(addtype,addtype)):
            return HttpResponse('Access denied',status=403,content_type='text/plain;charset=utf-8')
        Model = modelmap[addtype]
        newobj = Model()
        for k,v in addParams.items():
            if k in ('type','id'):
                continue
            setattr(newobj, k, parse_value(k, v))
        newobj.full_clean()
        models = newobj.save() or [newobj]
        logutil.addition(request, newobj)
        resp = HttpResponse(serializers.serialize('json', models, ensure_ascii=False),content_type='application/json;charset=utf-8')
        if 'queries' in request.GET and request.user.has_perm('tracker.view_queries'):
            return HttpResponse(json.dumps(connection.queries, ensure_ascii=False, indent=1),content_type='application/json;charset=utf-8')
        return resp
    except IntegrityError as e:
        return HttpResponse(json.dumps({'error': u'Integrity error: %s' % e}, ensure_ascii=False), status=400, content_type='application/json;charset=utf-8')
    except ValidationError as e:
        d = {'error': u'Validation Error'}
        if hasattr(e,'message_dict') and e.message_dict:
            d['fields'] = e.message_dict
        if hasattr(e,'messages') and e.messages:
            d['messages'] = e.messages
        return HttpResponse(json.dumps(d, ensure_ascii=False), status=400, content_type='application/json;charset=utf-8')
    except AttributeError as e:
        return HttpResponse(json.dumps({'error': 'Attribute Error, malformed add parameters', 'exception': unicode(e)}, ensure_ascii=False), status=400, content_type='application/json;charset=utf-8')
    except KeyError as e:
        return HttpResponse(json.dumps({'error': 'Key Error, malformed add parameters', 'exception': unicode(e)}, ensure_ascii=False), status=400, content_type='application/json;charset=utf-8')
    except FieldError as e:
        return HttpResponse(json.dumps({'error': 'Field Error, malformed add parameters', 'exception': unicode(e)}, ensure_ascii=False), status=400, content_type='application/json;charset=utf-8')
    except ValueError as e:
        return HttpResponse(json.dumps({'error': u'Value Error', 'exception': unicode(e)}, ensure_ascii=False), status=400, content_type='application/json;charset=utf-8')
    except ObjectDoesNotExist as e:
        return HttpResponse(json.dumps({'error': 'Foreign Key could not be found', 'exception': unicode(e)}, ensure_ascii=False), status=400, content_type='application/json;charset=utf-8')