Added a way to create custom tokens
This commit is contained in:
@@ -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)
|
||||
|
||||
+25
-3
@@ -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")
|
||||
|
||||
+1
-1
@@ -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):
|
||||
|
||||
+19
-7
@@ -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:
|
||||
|
||||
+23
-1
@@ -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;
|
||||
|
||||
@@ -2,14 +2,24 @@
|
||||
|
||||
{% block body %}
|
||||
<form method="POST" action="" class="url-form">
|
||||
{% if form.url.errors or form.token.errors %}
|
||||
<ul class="form-error-message">
|
||||
{% for error in form.url.errors %}
|
||||
<li>{{ error }}</li>
|
||||
{% endfor %}
|
||||
{% for error in form.token.errors %}
|
||||
<li>{{ error }}</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% endif %}
|
||||
{{ form.hidden_tag() }}
|
||||
<ul class='input-list'>
|
||||
{% if form.url.errors %}
|
||||
{% for error in form.url.errors %}
|
||||
<p class="url-error-message">{{ error }}</p>
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
<li>{{ form.url(placeholder="Enter the URL here", autofocus=true, class="url") }}</li>
|
||||
<li>{{ form.token(placeholder="Enter the token here (optional)", autofocus=true, class="token") }}</li>
|
||||
<ul class="token-explanation">
|
||||
<li>Token must be between 6 and 16 characters long</li>
|
||||
<li>It can only contain letters, numbers, underscores(_) and dashes(-)</li>
|
||||
</ul>
|
||||
<li>{{ form.submit(class="button") }}</li>
|
||||
</ul>
|
||||
</form>
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
<div id="page-container">
|
||||
<div id="content-wrap">
|
||||
{% if form %}
|
||||
{% if not form.url.errors %}
|
||||
{% if not form.url.errors and not form.token.errors %}
|
||||
<body onload="typeTitle()">
|
||||
<div class="headers">
|
||||
<a class="title" id="title" href="{{ url_for('index') }}"></a>
|
||||
@@ -38,7 +38,7 @@
|
||||
{% block body %}{% endblock %}
|
||||
|
||||
{% if form %}
|
||||
{% if not form.url.errors %}
|
||||
{% if not form.url.errors and not form.token.errors %}
|
||||
<script>
|
||||
var i = 0;
|
||||
var title_text = "tiny0";
|
||||
|
||||
Reference in New Issue
Block a user