Exemple #1
0
def index(account_id=-1):
    #TODO check if the user has permissions to do these operations
    #these are used for testing right now
    if request.method == "GET":
        try:
            success, message = Accounts.get(account_id)
            if not success:
                return json.dumps({"success": 0, "message": message})
            else:
                return json.dumps({"success": 1, "message": message})

        except FieldsException as e:
            return json.dumps({"success": 0, "message": str(e)})
        except Exception as e:
            logging.error(str(e))
            return json.dumps({
                "success": 0,
                "message": "Something went wrong"
            })

    if request.method == "PUT":
        try:
            data = json.loads(request.data)
            check_fields(data, [],
                         ["name", "email", "password", "permissions", "orgs"])
            success, message = Accounts.update(account_id, data)
            if not success:
                return json.dumps({"success": 0, "message": message})
            else:
                return json.dumps({"success": 1, "message": message})

        except FieldsException as e:
            return json.dumps({"success": 0, "message": str(e)})
        except Exception as e:
            logging.error(str(e))
            return json.dumps({
                "success": 0,
                "message": "Something went wrong"
            })

    if request.method == "DELETE":
        try:
            success, message = Accounts.delete(account_id)
            if not success:
                return json.dumps({"success": 0, "message": message})
            else:
                return json.dumps({"success": 1, "message": message})

        except FieldsException as e:
            return json.dumps({"success": 0, "message": str(e)})
        except Exception as e:
            logging.error(str(e))
            return json.dumps({
                "success": 0,
                "message": "Something went wrong"
            })
Exemple #2
0
def index():
    #TODO refactor these methods because some of them already have the account
    #  they do not need to retrieve it from an id.
    if request.method == "GET":
        try:
            account = check_hash(request.cookies)
            success, message = Accounts.get(account['_id'])
            if not success:
                return json.dumps({"success": 0, "message": message})
            else:
                return json.dumps({"success": 1, "message": message})

        except AuthException as e:
            return json.dumps({"success": 0, "message": str(e)})
        except FieldsException as e:
            return json.dumps({"success": 0, "message": str(e)})
        except Exception as e:
            logging.error(str(e))
            return json.dumps({
                "success": 0,
                "message": "Something went wrong"
            })

    if request.method == "POST":
        try:
            data = json.loads(request.data)
            check_fields(data,
                         ["name", "email", "password", "permissions", "orgs"],
                         [])
            success, message = Accounts.create(data)
            if not success:
                return json.dumps({"success": 0, "message": message})
            else:
                return json.dumps({"success": 1, "message": message})

        except FieldsException as e:
            return json.dumps({"success": 0, "message": str(e)})
        except Exception as e:
            logging.error(str(e))
            return json.dumps({
                "success": 0,
                "message": "Something went wrong"
            })

    if request.method == "PUT":
        try:
            account = check_hash(request.cookies)
            data = json.loads(request.data)
            check_fields(data, [],
                         ["name", "email", "password", "permissions", "orgs"])
            success, message = Accounts.update(account, data)
            if not success:
                return json.dumps({"success": 0, "message": message})
            else:
                return json.dumps({"success": 1, "message": message})

        except AuthException as e:
            return json.dumps({"success": 0, "message": str(e)})
        except FieldsException as e:
            return json.dumps({"success": 0, "message": str(e)})
        except Exception as e:
            logging.error(str(e))
            return json.dumps({
                "success": 0,
                "message": "Something went wrong"
            })

    if request.method == "DELETE":
        try:
            account = check_hash(request.cookies)
            success, message = Accounts.delete(account['_id'])
            if not success:
                return json.dumps({"success": 0, "message": message})
            else:
                return json.dumps({"success": 1, "message": message})

        except AuthException as e:
            return json.dumps({"success": 0, "message": str(e)})
        except FieldsException as e:
            return json.dumps({"success": 0, "message": str(e)})
        except Exception as e:
            logging.error(str(e))
            return json.dumps({
                "success": 0,
                "message": "Something went wrong"
            })