diff --git a/tiny0/forms.py b/tiny0/forms.py index 042edfa..5c55626 100644 --- a/tiny0/forms.py +++ b/tiny0/forms.py @@ -1,9 +1,26 @@ from flask_wtf import FlaskForm -from wtforms import StringField, SubmitField -from wtforms.validators import DataRequired, Length, URL +from wtforms import StringField, SubmitField, ValidationError +from wtforms.validators import DataRequired, Length -''' +# Validates a URL +def validate_URL(form, field): + # Make sure the url is not too short or long + if len(field.data) < 4 or len(field.data) > 2000: + # Raise a ValidationError + raise ValidationError() -Declaration of forms + # If the url contains spaces or does not have any dots + if field.data.count(" ") > 0 or field.data.count(".") == 0: + # Raise a ValidationError + raise ValidationError() -''' + # If the URL does not start with http:// and https:// + if not(field.data.startswith("http://")) and not(field.data.startswith("https://")): + # Add https:// to the beginning of the URL + field.data = "https://" + field.data + + +class URLForm(FlaskForm): + url = StringField(validators=[DataRequired(), Length(min=4, max=2000), validate_URL]) + + submit = SubmitField("Shorten URL") diff --git a/tiny0/routes.py b/tiny0/routes.py index b1e7781..359dc7a 100644 --- a/tiny0/routes.py +++ b/tiny0/routes.py @@ -1,9 +1,27 @@ from flask import render_template, redirect, request, flash, url_for from tiny0 import app -#from tiny0.forms import URLForm +from tiny0.forms import URLForm #from tiny0.models import URLs #from token import gen_valid_token -@app.route("/") +# Index Page +@app.route("/", methods=['GET', 'POST']) def index(): - return "Hello, world!" + # Get request + if request.method == "GET": + # Create a instance of the form + form = URLForm() + + # Return the index page with the form + return render_template("index.html", form=form) + + # Post request + else: + # Create a instance of the form + form = URLForm() + + # If the form was valid + if form.validate_on_submit(): + return "Valid URL" + + return render_template("index.html", form=form) diff --git a/tiny0/static/style.css b/tiny0/static/style.css new file mode 100644 index 0000000..1820ddd --- /dev/null +++ b/tiny0/static/style.css @@ -0,0 +1,3 @@ +body { + background: #2c2c2c; +} diff --git a/tiny0/templates/index.html b/tiny0/templates/index.html new file mode 100644 index 0000000..f528d36 --- /dev/null +++ b/tiny0/templates/index.html @@ -0,0 +1,11 @@ +{% extends "layout.html" %} + +{% block body %} +
+
+ {{ form.hidden_tag() }} + {{ form.url(placeholder="Enter the URL here") }} + {{ form.submit }} +
+
+{% endblock %} diff --git a/tiny0/templates/layout.html b/tiny0/templates/layout.html new file mode 100644 index 0000000..86679aa --- /dev/null +++ b/tiny0/templates/layout.html @@ -0,0 +1,11 @@ + + + + + + tiny0 - Custom URL Shortener + + + {% block body %}{% endblock %} + +