-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.py
198 lines (163 loc) · 7.09 KB
/
main.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# -*- coding: utf-8 -*-
import logging
import sys
from data import ScoutGroup, UserPrefs
from flask import Flask, redirect, render_template, request
from google.appengine.api import app_identity
from google.appengine.api import users
from google.appengine.api import mail
from google.appengine.api import memcache
from google.appengine.ext import ndb
from google.appengine.api import wrap_wsgi_app
from badges import badges
logging.getLogger().setLevel(logging.INFO) # make sure info logs are displayed on the console
app = Flask(__name__, static_url_path='')
app.debug = True
# this is to get the app engine services back
app.wsgi_app = wrap_wsgi_app(app.wsgi_app, use_deferred=False)
memcache.set("memcache_test", "123")
assert(memcache.get("memcache_test") == "123")
#reload(sys)
#sys.setdefaultencoding('utf8')
# page routes:
from groupsummary import groupsummary
from persons import persons
from scoutgroupinfo import scoutgroupinfo
from start import start
from imports import import_page
from progress import progress
from admin import admin
from tasks import tasks
from terminsprogram import terminsprogram
app.register_blueprint(start, url_prefix='/start')
app.register_blueprint(persons, url_prefix='/persons')
app.register_blueprint(scoutgroupinfo, url_prefix='/scoutgroupinfo')
app.register_blueprint(groupsummary, url_prefix='/groupsummary')
app.register_blueprint(import_page, url_prefix='/import')
app.register_blueprint(progress, url_prefix='/progress')
app.register_blueprint(admin, url_prefix='/admin')
app.register_blueprint(tasks, url_prefix='/tasks')
app.register_blueprint(badges, url_prefix='/badges')
app.register_blueprint(terminsprogram, url_prefix='/terminsprogram')
@app.route('/')
def home():
breadcrumbs = [{'link':'/', 'text':'Hem'}]
user = UserPrefs.current()
user.attemptAutoGroupAccess()
starturl = '/start/'
personsurl = '/persons/'
badgesurl = '/badges/'
if user.groupaccess != None:
starturl += user.groupaccess.urlsafe() + '/'
personsurl += user.groupaccess.urlsafe() + '/'
badgesurl += user.groupaccess.urlsafe() + '/'
return render_template('start.html',
heading='Hem',
items=[],
breadcrumbs=breadcrumbs,
user=user,
starturl=starturl,
personsurl=personsurl,
badgesurl=badgesurl,
logouturl=users.create_logout_url('/')
)
@app.route('/getaccess/', methods = ['POST', 'GET'])
def getaccess():
user = UserPrefs.current()
breadcrumbs = [{'link':'/', 'text':'Hem'}]
baselink = "/getaccess/"
section_title = "Access"
breadcrumbs.append({'link':baselink, 'text':section_title})
if request.method == "POST":
# anti-spam protection, the user can only ask once.
user_request_access_key = "request_access_" + user.getemail()
if memcache.get(user_request_access_key) is not None:
logging.warning("User is spamming req-access:" + user.getemail())
return "denied", 403
memcache.add(user_request_access_key, True)
sgroup = None
if len(request.form.get('scoutgroup')) != 0:
sgroup = ndb.Key(urlsafe=request.form.get('scoutgroup')).get()
if sgroup is not None:
groupAdminEmails = UserPrefs.getAllGroupAdminEmails(sgroup.key)
if len(groupAdminEmails) > 0:
app_id = app_identity.get_application_id()
mail.send_mail(
sender="noreply@" + app_id + ".appspotmail.com",
to=','.join(groupAdminEmails),
subject=u"""Användaren: {} vill ha access till närvaroregistrering i Skojjt
för scoutkåren {}""".format(user.getemail(), sgroup.getname()),
body=u"""Gå till {} för att lägga till {}""".format(request.host_url + "groupaccess/?name=" + user.getname(), user.getname()))
return redirect('/')
else:
return render_template('getaccess.html',
baselink=baselink,
breadcrumbs=breadcrumbs,
scoutgroups=ScoutGroup.query().fetch())
@app.route('/groupaccess')
@app.route('/groupaccess/')
@app.route('/groupaccess/<user_name>')
def groupaccess(user_name=None):
user = UserPrefs.current()
if not user.isGroupAdmin():
return "denied", 403
section_title = u'Hem'
baselink = '/'
breadcrumbs = [{'link':baselink, 'text':section_title}]
section_title = u'Kåraccess'
baselink += 'groupaccess/'
breadcrumbs.append({'link':baselink, 'text':section_title})
users = []
if user_name != None:
candidate = UserPrefs().query(UserPrefs.name == user_name).fetch(1)[0]
groupaccessurl = request.args["accept_user"]
if groupaccessurl == '1':
candidate.groupaccess = user.groupaccess
candidate.hasaccess = True
else:
candidate.groupaccess = None
candidate.put()
users = [candidate]
else:
if 'name' in request.args:
users = UserPrefs().query(UserPrefs.name == request.args['name']).fetch(1)
else:
users = UserPrefs().query(UserPrefs.groupaccess == None).fetch()
users.extend(UserPrefs().query(UserPrefs.groupaccess == user.groupaccess).fetch())
return render_template('groupaccess.html',
heading=section_title,
baselink=baselink,
users=users,
breadcrumbs=breadcrumbs,
mygroupurl=user.groupaccess.urlsafe(),
mygroupname=user.groupaccess.get().getname())
@app.errorhandler(404)
def page_not_found(e):
return render_template('notfound.html'), 404
@app.errorhandler(403)
def access_denied(e):
return render_template('403.html'), 403
@app.errorhandler(500)
def serverError(e):
logging.error("Error 500:%s", str(e))
return render_template('error.html', error=str(e)), 500
#@app.errorhandler(Exception)
#def defaultHandler(e):
# logging.error("Error:%s", str(e))
# return render_template('error.html', error=str(e)), 500
##############################################################################
if __name__ == '__main__':
# This is used when running locally only. When deploying to Google App
# Engine, a webserver process such as Gunicorn will serve the app. This
# can be configured by adding an `entrypoint` to app.yaml.
# Flask's development server will automatically serve static files in
# the "static" directory. See:
# http://flask.pocoo.org/docs/1.0/quickstart/#static-files. Once deployed,
# App Engine itself will serve those files as configured in app.yaml.
logging.info(f"*** starting main ***")
logging.info(f"python version {sys.version}")
py3_11_or_greater = sys.version_info.major > 3 or (sys.version_info.major == 3 and sys.version_info.minor >= 11)
assert(py3_11_or_greater) # if this fails check your environment (source env/bin/activate)
# main app:
app.run(host='localhost', port=8080, debug=True)
##############################################################################