Added click tracking
This commit is contained in:
+38
-4
@@ -5,7 +5,7 @@ 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):
|
||||
# Make sure the url is not too short or long
|
||||
if len(field.data) < 4 or len(field.data) > 2000:
|
||||
@@ -64,11 +64,45 @@ def validate_token(form, field):
|
||||
# Raise a ValidationError
|
||||
raise ValidationError("Token already exists")
|
||||
|
||||
# Validates a short url
|
||||
def validate_short_URL(form, field):
|
||||
# Make sure the short url is not too short or long
|
||||
if len(field.data) < (len(WEBSITE_DOMAIN) + 7) or len(field.data) > (len(WEBSITE_DOMAIN) + 25):
|
||||
return
|
||||
|
||||
# If the start of the short url is not valid
|
||||
if (not(field.data.lower().startswith(WEBSITE_DOMAIN + "/"))
|
||||
and not(field.data.lower().startswith("http://" + WEBSITE_DOMAIN + "/"))
|
||||
and not(field.data.lower().startswith("https://" + WEBSITE_DOMAIN + "/"))):
|
||||
# Raise a ValidationError
|
||||
raise ValidationError("Invalid short URL")
|
||||
|
||||
# Get the token of the short url
|
||||
if field.data.lower().startswith(WEBSITE_DOMAIN + "/"):
|
||||
token = field.data[len(WEBSITE_DOMAIN) + 1:]
|
||||
|
||||
elif field.data.lower().startswith("http://" + WEBSITE_DOMAIN + "/"):
|
||||
token = field.data[len(WEBSITE_DOMAIN) + 8:]
|
||||
|
||||
elif field.data.lower().startswith("https://" + WEBSITE_DOMAIN + "/"):
|
||||
token = field.data[len(WEBSITE_DOMAIN) + 9:]
|
||||
|
||||
# If the token of the short url does not exist in the database
|
||||
if not db.session.query(db.session.query(URL).filter_by(token=token).exists()).scalar():
|
||||
# Raise a ValidationError
|
||||
raise ValidationError("That short URL does not exists")
|
||||
|
||||
# After all the validation is done set the forms url value as the token
|
||||
field.data = token
|
||||
|
||||
class URLForm(FlaskForm):
|
||||
url = StringField(validators=[DataRequired(),
|
||||
Length(min=4, max=2000, message="Invalid URL length"),
|
||||
validate_URL])
|
||||
url = StringField(validators=[DataRequired(), 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")
|
||||
|
||||
class TrackerForm(FlaskForm):
|
||||
url = StringField(validators=[DataRequired(), Length(min=len(WEBSITE_DOMAIN) + 7, max=len(WEBSITE_DOMAIN) + 25, message="Invalid short URL"), validate_short_URL])
|
||||
|
||||
submit = SubmitField("Track")
|
||||
|
||||
Reference in New Issue
Block a user