diff --git a/README.md b/README.md index 317ccbd..c93c0f1 100644 --- a/README.md +++ b/README.md @@ -53,4 +53,5 @@ This project is under the [MIT](https://github.com/xemeds/tiny0/blob/master/LICE # Donate **Bitcoin Address:** 1Mg55rPVuQ2P8zKsCcLdsmgqH24uLXfLbR + **Patreon:** [patreon.com/xemeds](https://www.patreon.com/xemeds) diff --git a/tiny0/forms.py b/tiny0/forms.py index 914cbd6..1a07e98 100644 --- a/tiny0/forms.py +++ b/tiny0/forms.py @@ -1,7 +1,9 @@ from flask_wtf import FlaskForm from wtforms import StringField, SubmitField, ValidationError -from wtforms.validators import DataRequired, Length +from wtforms.validators import DataRequired, Length, Optional from tiny0.config import WEBSITE_DOMAIN +from tiny0 import db +from tiny0.models import URL # Validates a URL def validate_URL(form, field): @@ -31,7 +33,7 @@ def validate_URL(form, field): # If the url contains the websites domain if WEBSITE_DOMAIN in field.data.lower(): - # Raise a ValidationError + # Raise a ValidationError raise ValidationError("Invalid URL") # If the URL does not start with http:// and https:// @@ -39,9 +41,29 @@ def validate_URL(form, field): # Add http:// to the beginning of the URL field.data = "http://" + field.data +# Validates a token +def validate_token(form, field): + # Make sure the token is not too short or long + if len(field.data) < 6 or len(field.data) > 16: + return + + # For each character in the token + for char in field.data: + # If it is not a valid character + if not(char.isalpha()) and not(char.isdigit()) and not(char == "_") and not(char == '-'): + # Raise a ValidationError + raise ValidationError("Token contains invalid characters") + + # If the token exists in the database + if db.session.query(db.session.query(URL).filter_by(token=field.data).exists()).scalar(): + # Raise a ValidationError + raise ValidationError("Token already exists") + class URLForm(FlaskForm): url = StringField(validators=[DataRequired(), - Length(min=4, max=2000, message="Invalid URL Length"), + Length(min=4, max=2000, message="Invalid URL length"), validate_URL]) + token = StringField(validators=[Optional(), Length(min=6, max=16, message="Invalid token length"), validate_token]) + submit = SubmitField("Shorten") diff --git a/tiny0/models.py b/tiny0/models.py index c52436f..55d397d 100644 --- a/tiny0/models.py +++ b/tiny0/models.py @@ -2,7 +2,7 @@ from tiny0 import db class URL(db.Model): id = db.Column(db.Integer, primary_key=True) - token = db.Column(db.String(6), index=True, unique=True, nullable=False) + token = db.Column(db.String(16), index=True, unique=True, nullable=False) url = db.Column(db.String(2000), nullable=False) def __repr__(self): diff --git a/tiny0/routes.py b/tiny0/routes.py index 700de53..aa180b7 100644 --- a/tiny0/routes.py +++ b/tiny0/routes.py @@ -13,15 +13,27 @@ def index(): # If the form was valid if form.validate_on_submit(): - # Generate a valid token - token = gen_valid_token() - # Add the token and the given url to the database - db.session.add(URL(token=token, url=form.url.data)) - db.session.commit() + # If a token was given + if form.token.data: + # Add the token and the given url to the database + db.session.add(URL(token=form.token.data, url=form.url.data)) + db.session.commit() - # Return the url page with the shortened url - return render_template("url.html", url=WEBSITE_DOMAIN + "/" + token) + # Return the url page with the shortened url + return render_template("url.html", url=WEBSITE_DOMAIN + "/" + form.token.data) + + # Else if a token was not given + else: + # Generate a valid token + token = gen_valid_token() + + # Add the token and the given url to the database + db.session.add(URL(token=token, url=form.url.data)) + db.session.commit() + + # Return the url page with the shortened url + return render_template("url.html", url=WEBSITE_DOMAIN + "/" + token) # If the form was invalid or not submitted else: diff --git a/tiny0/static/style.css b/tiny0/static/style.css index b768505..621d3e8 100644 --- a/tiny0/static/style.css +++ b/tiny0/static/style.css @@ -67,6 +67,19 @@ body { border-color: #7b59a5; } +.token { + width: 80%; + padding: 10px; + background: #121212; + border: 2px solid #bb86fc; + color: #ffffff; + margin-bottom: 2vh; +} + +.token:focus { + border-color: #7b59a5; +} + .button { width: 100px; background: #bb86fc; @@ -82,12 +95,21 @@ body { border-color: #7b59a5; } -.url-error-message { +.form-error-message { color: #ffffff; + list-style: none; text-align: center; margin: -1vh 0 2vh 0; } +.token-explanation { + color: #ffffff; + font-size: 13px; + list-style: none; + text-align: center; + margin-bottom: 2vh; +} + .error-message { text-align: center; color: #ffffff; diff --git a/tiny0/templates/index.html b/tiny0/templates/index.html index a138205..934aedf 100644 --- a/tiny0/templates/index.html +++ b/tiny0/templates/index.html @@ -2,14 +2,24 @@ {% block body %}
diff --git a/tiny0/templates/layout.html b/tiny0/templates/layout.html index d33049e..397d906 100644 --- a/tiny0/templates/layout.html +++ b/tiny0/templates/layout.html @@ -14,7 +14,7 @@