Added URL validation form
This commit is contained in:
+22
-5
@@ -1,9 +1,26 @@
|
|||||||
from flask_wtf import FlaskForm
|
from flask_wtf import FlaskForm
|
||||||
from wtforms import StringField, SubmitField
|
from wtforms import StringField, SubmitField, ValidationError
|
||||||
from wtforms.validators import DataRequired, Length, URL
|
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")
|
||||||
|
|||||||
+21
-3
@@ -1,9 +1,27 @@
|
|||||||
from flask import render_template, redirect, request, flash, url_for
|
from flask import render_template, redirect, request, flash, url_for
|
||||||
from tiny0 import app
|
from tiny0 import app
|
||||||
#from tiny0.forms import URLForm
|
from tiny0.forms import URLForm
|
||||||
#from tiny0.models import URLs
|
#from tiny0.models import URLs
|
||||||
#from token import gen_valid_token
|
#from token import gen_valid_token
|
||||||
|
|
||||||
@app.route("/")
|
# Index Page
|
||||||
|
@app.route("/", methods=['GET', 'POST'])
|
||||||
def index():
|
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)
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
body {
|
||||||
|
background: #2c2c2c;
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
{% extends "layout.html" %}
|
||||||
|
|
||||||
|
{% block body %}
|
||||||
|
<div>
|
||||||
|
<form method="POST" action="">
|
||||||
|
{{ form.hidden_tag() }}
|
||||||
|
{{ form.url(placeholder="Enter the URL here") }}
|
||||||
|
{{ form.submit }}
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
{% endblock %}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<link href="/static/style.css" rel="stylesheet" type="text/css"/>
|
||||||
|
|
||||||
|
<title>tiny0 - Custom URL Shortener</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
{% block body %}{% endblock %}
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user