-
Notifications
You must be signed in to change notification settings - Fork 5
/
helpers.py
33 lines (26 loc) · 969 Bytes
/
helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import random
import string
from functools import wraps
from flask import request, redirect, url_for, session
def login_required(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if session.get("user_id") is None:
return redirect(url_for('login')) # , next=request.url
return f(*args, **kwargs)
return decorated_function
def random_str(digit=7):
chars = ""
for i in range(digit):
chars += random.choice(string.ascii_letters + string.digits)
return chars
def validate_url(url):
import re
regex = re.compile(
r'^(?:http|ftp)s?://' # http:// or https://
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' #domain...
r'localhost|' #localhost...
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
r'(?::\d+)?' # optional port
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
return (re.match(regex, url) is not None) # True