Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
def smtp_test_post():
    data = request.get_json()

    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
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
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
Ejemplo n.º 5
0
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
Ejemplo n.º 6
0
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
Ejemplo n.º 7
0
def settings_post():

    data = request.get_json()

    settings = Settings.query.first()

    if "smtp_host" in data.keys():

        if data["smtp_host"] != "":

            if check_length(data["smtp_host"], 256):
                settings.smtp_host = data["smtp_host"]
            else:
                return jsonify({
                    "status": "error",
                    "detail": "Server address too long"
                }), 400

            if "smtp_port" in data.keys():

                try:
                    smtp_port = int(data["smtp_port"])
                except ValueError:
                    return jsonify({
                        "status": "error",
                        "detail": "Port is invalid"
                    }), 400

                if smtp_port <= 65535 and smtp_port >= 0:
                    settings.smtp_port = int(data["smtp_port"])
                else:
                    return jsonify({
                        "status": "error",
                        "detail": "Port is invalid"
                    }), 400

            else:
                return jsonify({
                    "status": "error",
                    "detail": "Missing SMTP port"
                }), 400

            if "starttls" in data.keys() and "ssl_tls" in data.keys():
                return jsonify({
                    "status":
                    "error",
                    "detail":
                    "Cannot use STARTTLS and SSL/TLS at the same time"
                }), 400

            if "starttls" in data.keys():
                settings.starttls = True

            if "ssl_tls" in data.keys():
                settings.ssl_tls = True

            if "starttls" not in data.keys() and "ssl_tls" not in data.keys():
                settings.starttls = False
                settings.ssl_tls = False

            if "mail_from" in data.keys():
                if is_email(data["mail_from"]) and check_length(
                        data["mail_from"], 256):
                    settings.mail_from = data["mail_from"]
                else:
                    return jsonify({
                        "status": "error",
                        "detail": "Email address format is invalid"
                    }), 400

            else:
                return jsonify({
                    "status": "error",
                    "detail": "Missing sender address"
                }), 400

            if "smtp_user" in data.keys():

                if check_length(data["smtp_user"], 128):
                    settings.smtp_user = data["smtp_user"]
                else:
                    return jsonify({
                        "status": "error",
                        "detail": "SMTP username too long"
                    }), 400

                if "smtp_pass" in data.keys():
                    if check_length(data["smtp_pass"], 128):
                        settings.smtp_pass = data["smtp_pass"]
                    else:
                        return jsonify({
                            "status": "error",
                            "detail": "SMTP password too long"
                        }), 400

            else:
                settings.smtp_user = None
                settings.smtp_pass = None

        else:
            settings.smtp_host = None
            settings.smtp_port = None
            settings.starttls = False
            settings.ssl_tls = False
            settings.mail_from = None
            settings.smtp_user = None
            settings.smtp_pass = None
            settings.smtp_status = None

    else:
        settings.smtp_host = None
        settings.smtp_port = None
        settings.starttls = False
        settings.ssl_tls = False
        settings.mail_from = None
        settings.smtp_user = None
        settings.smtp_pass = None
        settings.smtp_status = None

    db.session.commit()

    return jsonify({
        "status": "OK",
        "detail": "Configuration saved successfuly"
    }), 200
Ejemplo n.º 8
0
def settings_post():

    data = request.form

    settings = Settings.query.first()

    if 'smtp_host' in data.keys():

        if data['smtp_host'] != '':

            if check_length(data['smtp_host'], 256):
                settings.smtp_host = data['smtp_host']
            else:
                return jsonify({'status': 'error', 'detail': 'Server address too long'}), 400

            if 'smtp_port' in data.keys():

                try:
                    smtp_port = int(data['smtp_port'])
                except ValueError:
                    return jsonify({'status': 'error', 'detail': 'Port is invalid'}), 400

                if smtp_port <= 65535 and smtp_port >= 0:
                    settings.smtp_port = int(data['smtp_port'])
                else:
                    return jsonify({'status': 'error', 'detail': 'Port is invalid'}), 400

            else:
                return jsonify({'status': 'error', 'detail': 'Missing SMTP port'}), 400

            if 'starttls' in data.keys() and 'ssl_tls' in data.keys():
                return jsonify({'status': 'error', 'detail': 'Cannot use STARTTLS and SSL/TLS at the same time'}), 400

            if 'starttls' in data.keys():
                settings.starttls = True

            if 'ssl_tls' in data.keys():
                settings.ssl_tls = True

            if 'starttls' not in data.keys() and 'ssl_tls' not in data.keys():
                settings.starttls = False
                settings.ssl_tls = False

            if 'mail_from' in data.keys():
                if is_email(data['mail_from']) and check_length(data['mail_from'], 256):
                    settings.mail_from = data['mail_from']
                else:
                    return jsonify({'status': 'error', 'detail': 'Email address format is invalid'}), 400

            else:
                return jsonify({'status': 'error', 'detail': 'Missing sender address'}), 400

            if 'smtp_user' in data.keys():

                if check_length(data['smtp_user'], 128):
                    settings.smtp_user = data['smtp_user']
                else:
                    return jsonify({'status': 'error', 'detail': 'SMTP username too long'}), 400

                if 'smtp_pass' in data.keys():
                    if check_length(data['smtp_pass'], 128):
                        settings.smtp_pass = data['smtp_pass']
                    else:
                        return jsonify({'status': 'error', 'detail': 'SMTP password too long'}), 400

            else:
                settings.smtp_user = None
                settings.smtp_pass = None

        else:
            settings.smtp_host = None
            settings.smtp_port = None
            settings.starttls = False
            settings.ssl_tls = False
            settings.mail_from = None
            settings.smtp_user = None
            settings.smtp_pass = None
            settings.smtp_status = None

    else:
        settings.smtp_host = None
        settings.smtp_port = None
        settings.starttls = False
        settings.ssl_tls = False
        settings.mail_from = None
        settings.smtp_user = None
        settings.smtp_pass = None
        settings.smtp_status = None

    db.session.commit()

    return jsonify({'status': 'OK', 'detail': 'Configuration saved successfuly'}), 200
Ejemplo n.º 9
0
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
Ejemplo n.º 10
0
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