-
Notifications
You must be signed in to change notification settings - Fork 155
/
helper_functions.py
51 lines (37 loc) · 1.22 KB
/
helper_functions.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
44
45
46
47
48
49
50
51
import re
import string
import random
from urlparse import urljoin
from flask import request, url_for, session, flash, redirect
from functools import wraps
def url_for_other_page(page):
args = request.view_args.copy()
args['page'] = page
return url_for(request.endpoint, **args)
def extract_tags(tags):
whitespace = re.compile('\s')
nowhite = whitespace.sub("", tags)
tags_array = nowhite.split(',')
cleaned = []
for tag in tags_array:
if tag not in cleaned and tag != "":
cleaned.append(tag)
return cleaned
def random_string(size=6, chars=string.ascii_uppercase + string.digits):
return ''.join(random.choice(chars) for x in range(size))
def generate_csrf_token():
if '_csrf_token' not in session:
session['_csrf_token'] = random_string()
return session['_csrf_token']
def make_external(url):
return urljoin(request.url_root, url)
def login_required():
def wrapper(f):
@wraps(f)
def wrapped(*args, **kwargs):
if not session.get('user'):
flash('You must be logged in..', 'error')
return redirect(url_for('login'))
return f(*args, **kwargs)
return wrapped
return wrapper