def register(): """Creates a new user""" data = request.form if 'username' not in data.keys(): return jsonify({'status': 'error', 'detail': 'Missing username'}), 400 if not (not_empty(data['username']) and check_length(data['username'], 128)): return jsonify({ 'status': 'error', 'detail': 'Invalid username (too long or empty)' }), 400 if User.query.filter_by(username=data['username']).first() != None: return jsonify({ 'status': 'error', 'detail': 'This user already exists' }), 400 user = User(username=data['username']) password = user.generate_password() user.set_password(password) db.session.add(user) db.session.commit() return jsonify({'status': 'OK', 'detail': password}), 200
def register(): """Creates a new user""" data = request.get_json() if "username" not in data.keys(): return jsonify({"status": "error", "detail": "Missing username"}), 400 if not (not_empty(data["username"]) and check_length(data["username"], 128)): return jsonify({ "status": "error", "detail": "Invalid username (too long or empty)" }), 400 if User.query.filter_by(username=data["username"]).first() != None: return jsonify({ "status": "error", "detail": "This user already exists" }), 400 user = User(username=data["username"]) password = user.generate_password() user.set_password(password) db.session.add(user) db.session.commit() return jsonify({"status": "OK", "detail": password}), 200
def client_put(): """Creates a new client""" current_user = get_current_user() data = request.get_json() if "name" not in data.keys() or "description" not in data.keys(): return jsonify({ "status": "error", "detail": "Missing name or description" }), 400 if Client.query.filter_by(name=data["name"]).first() != None: return jsonify({ "status": "error", "detail": "Client already exists" }), 400 if not_empty(data["name"]) and check_length( data["name"], 32) and check_length(data["description"], 128): new_client = Client(name=data["name"], description=data["description"], owner_id=current_user.id) new_client.gen_uid() db.session.add(new_client) db.session.commit() return jsonify({ "status": "OK", "detail": "New client {} created successfuly".format(new_client.name) }), 201 else: return jsonify({ "status": "error", "detail": "Invalid data (name empty or too long or description too long)" }), 400
def client_put(): """Creates a new client""" data = request.form if 'name' not in data.keys() or\ 'description' not in data.keys(): return jsonify({ 'status': 'error', 'detail': 'Missing name or description' }), 400 if Client.query.filter_by(name=data['name']).first() != None: return jsonify({ 'status': 'error', 'detail': 'Client already exists' }), 400 if not_empty(data['name']) and check_length( data['name'], 32) and check_length(data['description'], 128): new_client = Client(name=data['name'], description=data['description'], owner_id=current_user.id) new_client.gen_uid() db.session.add(new_client) db.session.commit() return jsonify({ 'status': 'OK', 'detail': 'New client {} created successfuly'.format(new_client.name) }), 201 else: return jsonify({ 'status': 'error', 'detail': 'Invalid data (name empty or too long or description too long)' }), 400
def client_post(client_id): """Edits a client""" data = request.get_json() client = Client.query.filter_by(id=client_id).first_or_404() if "name" in data.keys(): if client.name != data["name"]: if Client.query.filter_by(name=data["name"]).first() != None: return jsonify({ "status": "error", "detail": "Another client already uses this name" }), 400 if not_empty(data["name"]) and check_length(data["name"], 32): client.name = data["name"] else: return jsonify({ "status": "error", "detail": "Invalid name (too long or empty)" }), 400 if "description" in data.keys(): if check_length(data["description"], 128): client.description = data["description"] else: return jsonify({ "status": "error", "detail": "Invalid description (too long)" }), 400 if "owner" in data.keys(): user = User.query.filter_by(id=data["owner"]).first() if user == None: return jsonify({ "status": "error", "detail": "This user does not exist" }), 400 client.owner_id = data["owner"] if "mail_to" in data.keys(): if data["mail_to"] == "": client.mail_to = None else: if is_email(data["mail_to"]) and check_length( data["mail_to"], 256): client.mail_to = data["mail_to"] else: return jsonify({ "status": "error", "detail": "Invalid mail recipient" }), 400 else: client.mail_to = None if "webhook_url" in data.keys(): if is_url(data["webhook_url"]): client.webhook_url = data["webhook_url"] else: return jsonify({ "status": "error", "detail": "Webhook URL format is invalid" }), 400 else: client.webhook_url = None db.session.commit() return jsonify({ "status": "OK", "detail": "Client {} edited successfuly".format(client.name) }), 200
def client_post(client_id): """Edits a client""" data = request.form client = Client.query.filter_by(id=client_id).first_or_404() if 'name' in data.keys(): if client.name != data['name']: if Client.query.filter_by(name=data['name']).first() != None: return jsonify({ 'status': 'error', 'detail': 'Another client already uses this name' }), 400 if not_empty(data['name']) and check_length(data['name'], 32): client.name = data['name'] else: return jsonify({ 'status': 'error', 'detail': 'Invalid name (too long or empty)' }), 400 if 'description' in data.keys(): if check_length(data['description'], 128): client.description = data['description'] else: return jsonify({ 'status': 'error', 'detail': 'Invalid description (too long)' }), 400 if 'owner' in data.keys(): user = User.query.filter_by(id=data['owner']).first() if user == None: return jsonify({ 'status': 'error', 'detail': 'This user does not exist' }), 400 client.owner_id = data['owner'] if 'mail_to' in data.keys(): if data['mail_to'] == '': client.mail_to = None else: if is_email(data['mail_to']) and check_length( data['mail_to'], 256): client.mail_to = data['mail_to'] else: return jsonify({ 'status': 'error', 'detail': 'Invalid mail recipient' }), 400 db.session.commit() return jsonify({ 'status': 'OK', 'detail': 'Client {} edited successfuly'.format(client.name) }), 200