def smtp_test_post(): data = request.form settings = Settings.query.first() if 'mail_to' not in data.keys(): return jsonify({'status': 'error', 'detail': 'Missing recipient'}), 400 if is_email(data['mail_to']) and check_length(data['mail_to'], 256): try: send_mail(receiver=data['mail_to']) settings.smtp_status = True db.session.commit() return jsonify({'status': 'OK', 'detail': 'SMTP configuration test successful'}), 200 except: settings.smtp_status = False db.session.commit() return jsonify({'status': 'error', 'detail': 'Could not send test email. Please review your SMTP configuration and don\'t forget to save it before testing it. '}), 400 else: return jsonify({'status': 'error', 'detail': 'Invalid recipient'}), 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