-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
43 lines (27 loc) · 1015 Bytes
/
util.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
34
35
36
37
38
39
40
41
42
43
import datetime
import random
import re
from functools import wraps
from flask import abort
from flask_login import current_user, login_required
from passlib.hash import bcrypt
def isoformat(seconds):
return datetime.datetime.fromtimestamp(seconds).isoformat() + "Z"
def generate_string(length=32, alpha='0123456789abcdef'):
return "".join([random.choice(alpha) for x in range(length)])
def hash_password(password, rounds=10):
return bcrypt.encrypt(password, rounds=rounds)
def verify_password(to_check, password_hash):
return bcrypt.verify(to_check, password_hash)
def validate_email_format(email):
return re.match('[^@]+@[^@]+\.[^@]+', email) is not None
def validate_username_format(username):
return re.match('^[a-zA-Z0-9_.-\[\]\(\)]+$', username) is not None
def admin_required(func):
@wraps(func)
@login_required
def wrapper(*args, **kwargs):
if not current_user.admin:
abort(403)
return func(*args, **kwargs)
return wrapper