예제 #1
0
        def handler(*args, **kwargs):
            user = UserPrefs.get_current()
            if user is None:
                return make_response(jsonify(code="Unauthorized"), 401, {})

            if user.authorized is False:
                return make_response(jsonify(code="Forbidden"), 403, {})

            if require_admin and not user.admin:
                return make_response(jsonify(code="Forbidden"), 403, {})

            return fn(*args, **kwargs)
예제 #2
0
파일: patient.py 프로젝트: iuridiniz/mejcrt
def create_or_update():
    if request.json is None:
        return make_response(jsonify(code="Bad Request"), 400, {})

    patient_key = request.json.get("key", None)
    code = onlynumbers(request.json.get("code", 0))
    patient = None
    is_new = False
    if patient_key and request.method == "PUT":
        try:
            key = ndb.Key(urlsafe=patient_key)
            patient = key.get()
            code = patient.code
        except ProtocolBufferDecodeError:
            pass
    elif patient_key is None and request.method == "POST" and int(code) > 0:
        is_new = True
        patient = Patient(id=code)
    else:
        logging.error("Invalid request %r for json %r" % (request.method, request.json))
        return make_response(jsonify(code="Bad Request"), 400, {})

    logs = patient.logs or []
    logs.append(LogEntry.from_user(UserPrefs.get_current(), is_new))

    name = request.json.get("name", None)

    type_ = request.json.get("type", "")
    blood_type = request.json.get("blood_type", None)
    try:
        patient.populate(name=name, type_=type_, blood_type=blood_type, logs=logs)
        key = patient.put(update=not is_new)
    except BadValueError as e:
        logging.error("Cannot create Patient from %r: %r" % (request.json, e))
        return make_response(jsonify(code="Bad Request"), 400, {})

    return make_response(jsonify(code="OK", data=dict(key=key.urlsafe())), 200, {})
예제 #3
0
def create_or_update():
    if not hasattr(request, 'json') or request.json is None:
        logging.error("Cannot create TR from %r: %r" % (request, 'no json'))
        return make_response(jsonify(code="Bad Request"), 400, {})

    tr_key = request.json.get('key', None)
    tr = None
    is_new = False
    if tr_key and request.method == "PUT":
        # update a transfusion
        try:
            key = ndb.Key(urlsafe=tr_key)
            tr = key.get()
        except ProtocolBufferDecodeError:
            logging.error("Cannot create TR from %r: %r" % (request.json, 'invalid transfusion key'))
            return make_response(jsonify(code="Not Found"), 404, {})
        patient_key = tr.patient
    elif tr_key is None and request.method == "POST":
        # create a new transfusion
        is_new = True
        tr_code = onlynumbers(request.json.get('code', '0'))
        if int(tr_code) == 0:
            logging.error("Cannot create TR from %r: %r" % (request.json, 'no transfusion code'))
            return make_response(jsonify(code="Bad Request"), 400, {})
        patient_key = None
        patient_key_url_safe = request.json.get('patient_key', None)
        patient_record_code = request.json.get('record', None)
        patient_dict = request.json.get('patient', dict())

        if patient_key_url_safe is None and isinstance(patient_dict, dict):
            patient_key_url_safe = patient_dict.get('key')

        if patient_key_url_safe is not None:
            try:
                patient_key = ndb.Key(urlsafe=patient_key_url_safe)
            except ProtocolBufferDecodeError:
                pass
        elif patient_record_code:
            patient_key = Patient.get_by_code(patient_record_code, onlykey=True)

        if patient_key is None:
            # no patient key
            logging.error("Cannot create TR from %r: %r" % (request.json, 'no patient key'))
            return make_response(jsonify(code="Bad Request"), 400, {})

        tr = Transfusion(id=tr_code)
    else:
        logging.error("Cannot create TR from %r: %r (%r)" % (request.json, 'incorrect method', request.method))
        return make_response(jsonify(code="Bad Request"), 400, {})

    transfusion_date = request.json.get('date', None)
    transfusion_local = request.json.get('local', None)
    bags = []
    for bag in request.json.get('bags', []):
        try:
            bags.append(BloodBag(type_=bag['type'], content=bag['content']))
        except BadValueError as e:
            logging.error("Cannot create TR from %r: %r" % (request.json, e))
            return make_response(jsonify(code="Bad Request"), 400, {})
    text = request.json.get('text', None) or None
    tags = request.json.get('tags', [])

    logs = tr.logs or []

    logs.append(LogEntry.from_user(UserPrefs.get_current(), is_new))
    try:
        tr.populate(patient=patient_key,
                    date=parse_date(transfusion_date),
                    local=transfusion_local,
                    bags=bags,
                    logs=logs,
                    tags=tags,
                    text=text)
        key = tr.put(update=not is_new)
    except (BadValueError, ValueError) as e:
        logging.error("Cannot create TR from %r: %r" % (request.json, e))
        return make_response(jsonify(code="Bad Request"), 400, {})

    return make_response(jsonify(code="OK", data=dict(key=key.urlsafe())), 200, {})