-
Notifications
You must be signed in to change notification settings - Fork 35
/
lib.py
117 lines (84 loc) · 3.12 KB
/
lib.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
"""
Collection of functions for general use
"""
import os
from functools import wraps
from urlparse import urlparse
from flask import redirect, url_for, session, request, flash
from . import PUBLISHED
from . import app
from . import models
def is_logged_in():
"""
Determine if user is logged in or not
"""
return 'github_token' in session and 'login' in session
def login_required(func):
"""
Decorator to require login and save URL for redirecting user after login
"""
@wraps(func)
def decorated_function(*args, **kwargs):
"""decorator args"""
if not is_logged_in():
# Save off the page so we can redirect them to what they were
# trying to view after logging in.
session['previously_requested_page'] = request.url
return redirect(url_for('login'))
return func(*args, **kwargs)
return decorated_function
def collaborator_required(func):
"""
Decorator to require login and logged in user to be collaborator
This should be used instead of @login_required when the URL endpoint should
be protected by login and the logged in user being a collaborator on the
repo. This will NOT redirect to login. It's meant to kick a user back to
the homepage if they tried something they do not have permissions for.
"""
@wraps(func)
def decorated_function(*args, **kwargs):
"""decorator args"""
if not is_logged_in():
flash('Must be logged in', category='error')
# Save off the page so we can redirect them to what they were
# trying to view after logging in.
session['previously_requested_page'] = request.url
return redirect(url_for('index'))
if 'collaborator' not in session or not session['collaborator']:
flash('Must be a repo collaborator for that functionality.',
category='error')
# Save off the page so we can redirect them to what they were
# trying to view after logging in.
session['previously_requested_page'] = request.url
return redirect(url_for('index'))
return func(*args, **kwargs)
return decorated_function
def lookup_url_redirect(requested_url):
"""
Lookup given URL for a 301 redirect
:param requested_url: URL to look for a redirect
:returns: URL to redirect to or None if no redirect found
"""
new_url = None
redirects = models.read_redirects()
# All our URLs should be ASCII!
try:
old_url = str(requested_url)
except UnicodeEncodeError:
return None
try:
new_url = redirects[old_url]
except KeyError:
# Maybe the url was referenced without the domain:
try:
old_url = urlparse(old_url).path
except Exception as err:
app.logger.error(u'Failed parsing URL "%s" for redirect: %s',
old_url, err)
return None
try:
new_url = redirects[old_url]
except KeyError:
# No worries, guess this really was a bad URL
pass
return new_url