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
|
# Donate
|
||||||
|
|
||||||
**Bitcoin Address:** 1Mg55rPVuQ2P8zKsCcLdsmgqH24uLXfLbR
|
**Bitcoin Address:** 1Mg55rPVuQ2P8zKsCcLdsmgqH24uLXfLbR
|
||||||
|
|
||||||
**Patreon:** [patreon.com/xemeds](https://www.patreon.com/xemeds)
|
**Patreon:** [patreon.com/xemeds](https://www.patreon.com/xemeds)
|
||||||
|
|||||||
+24
-2
@@ -1,7 +1,9 @@
|
|||||||
from flask_wtf import FlaskForm
|
from flask_wtf import FlaskForm
|
||||||
from wtforms import StringField, SubmitField, ValidationError
|
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.config import WEBSITE_DOMAIN
|
||||||
|
from tiny0 import db
|
||||||
|
from tiny0.models import URL
|
||||||
|
|
||||||
# Validates a URL
|
# Validates a URL
|
||||||
def validate_URL(form, field):
|
def validate_URL(form, field):
|
||||||
@@ -39,9 +41,29 @@ def validate_URL(form, field):
|
|||||||
# Add http:// to the beginning of the URL
|
# Add http:// to the beginning of the URL
|
||||||
field.data = "http://" + field.data
|
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):
|
class URLForm(FlaskForm):
|
||||||
url = StringField(validators=[DataRequired(),
|
url = StringField(validators=[DataRequired(),
|
||||||
Length(min=4, max=2000, message="Invalid URL Length"),
|
Length(min=4, max=2000, message="Invalid URL length"),
|
||||||
validate_URL])
|
validate_URL])
|
||||||
|
|
||||||
|
token = StringField(validators=[Optional(), Length(min=6, max=16, message="Invalid token length"), validate_token])
|
||||||
|
|
||||||
submit = SubmitField("Shorten")
|
submit = SubmitField("Shorten")
|
||||||
|
|||||||
+1
-1
@@ -2,7 +2,7 @@ from tiny0 import db
|
|||||||
|
|
||||||
class URL(db.Model):
|
class URL(db.Model):
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
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)
|
url = db.Column(db.String(2000), nullable=False)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
|
|||||||
@@ -13,6 +13,18 @@ def index():
|
|||||||
|
|
||||||
# If the form was valid
|
# If the form was valid
|
||||||
if form.validate_on_submit():
|
if form.validate_on_submit():
|
||||||
|
|
||||||
|
# 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 + "/" + form.token.data)
|
||||||
|
|
||||||
|
# Else if a token was not given
|
||||||
|
else:
|
||||||
# Generate a valid token
|
# Generate a valid token
|
||||||
token = gen_valid_token()
|
token = gen_valid_token()
|
||||||
|
|
||||||
|
|||||||
+23
-1
@@ -67,6 +67,19 @@ body {
|
|||||||
border-color: #7b59a5;
|
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 {
|
.button {
|
||||||
width: 100px;
|
width: 100px;
|
||||||
background: #bb86fc;
|
background: #bb86fc;
|
||||||
@@ -82,12 +95,21 @@ body {
|
|||||||
border-color: #7b59a5;
|
border-color: #7b59a5;
|
||||||
}
|
}
|
||||||
|
|
||||||
.url-error-message {
|
.form-error-message {
|
||||||
color: #ffffff;
|
color: #ffffff;
|
||||||
|
list-style: none;
|
||||||
text-align: center;
|
text-align: center;
|
||||||
margin: -1vh 0 2vh 0;
|
margin: -1vh 0 2vh 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.token-explanation {
|
||||||
|
color: #ffffff;
|
||||||
|
font-size: 13px;
|
||||||
|
list-style: none;
|
||||||
|
text-align: center;
|
||||||
|
margin-bottom: 2vh;
|
||||||
|
}
|
||||||
|
|
||||||
.error-message {
|
.error-message {
|
||||||
text-align: center;
|
text-align: center;
|
||||||
color: #ffffff;
|
color: #ffffff;
|
||||||
|
|||||||
@@ -2,14 +2,24 @@
|
|||||||
|
|
||||||
{% block body %}
|
{% block body %}
|
||||||
<form method="POST" action="" class="url-form">
|
<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() }}
|
{{ form.hidden_tag() }}
|
||||||
<ul class='input-list'>
|
<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.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>
|
<li>{{ form.submit(class="button") }}</li>
|
||||||
</ul>
|
</ul>
|
||||||
</form>
|
</form>
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
<div id="page-container">
|
<div id="page-container">
|
||||||
<div id="content-wrap">
|
<div id="content-wrap">
|
||||||
{% if form %}
|
{% if form %}
|
||||||
{% if not form.url.errors %}
|
{% if not form.url.errors and not form.token.errors %}
|
||||||
<body onload="typeTitle()">
|
<body onload="typeTitle()">
|
||||||
<div class="headers">
|
<div class="headers">
|
||||||
<a class="title" id="title" href="{{ url_for('index') }}"></a>
|
<a class="title" id="title" href="{{ url_for('index') }}"></a>
|
||||||
@@ -38,7 +38,7 @@
|
|||||||
{% block body %}{% endblock %}
|
{% block body %}{% endblock %}
|
||||||
|
|
||||||
{% if form %}
|
{% if form %}
|
||||||
{% if not form.url.errors %}
|
{% if not form.url.errors and not form.token.errors %}
|
||||||
<script>
|
<script>
|
||||||
var i = 0;
|
var i = 0;
|
||||||
var title_text = "tiny0";
|
var title_text = "tiny0";
|
||||||
|
|||||||
Reference in New Issue
Block a user