-
Notifications
You must be signed in to change notification settings - Fork 15
/
persons.py
189 lines (165 loc) · 7.17 KB
/
persons.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
# -*- coding: utf-8 -*-
from data import Person, ScoutGroup, TroopPerson, UserPrefs, Semester
from flask import abort, Blueprint, redirect, render_template, request, make_response
from google.appengine.ext import ndb
import logging
import scoutnet
import urllib
persons = Blueprint('persons_page', __name__, template_folder='templates')
@persons.route('/')
@persons.route('/<sgroup_url>')
@persons.route('/<sgroup_url>/')
@persons.route('/<sgroup_url>/<person_url>')
@persons.route('/<sgroup_url>/<person_url>/')
@persons.route('/<sgroup_url>/<person_url>/<action>')
def show(sgroup_url=None, person_url=None, action=None):
# logging.info("persons.py: sgroup_url=%s, person_url=%s, action=%s", sgroup_url, person_url, action)
user = UserPrefs.current()
if not user.hasAccess():
return "denied", 403
breadcrumbs = [{'link':'/', 'text':'Hem'}]
section_title = u'Personer'
breadcrumbs.append({'link': '/persons', 'text': section_title})
baselink = '/persons/'
sgroup_key = None # type: ndb.Key
scoutgroup = None # type: ScoutGroup
if sgroup_url is not None:
sgroup_key = ndb.Key(urlsafe=sgroup_url)
if not user.hasGroupKeyAccess(sgroup_key):
return "denied", 403
scoutgroup = sgroup_key.get()
baselink += sgroup_url+"/"
breadcrumbs.append({'link': baselink, 'text': scoutgroup.getname()})
if "gbg_csv" in request.args:
return get_gbg_csv(sgroup_url)
if scoutgroup is None:
return render_template(
'index.html',
heading=section_title,
baselink=baselink,
items=ScoutGroup.getgroupsforuser(user),
breadcrumbs=breadcrumbs,
username=user.getname())
person_key = None # type: ndb.Key
person = None # type: Person
if person_url is not None:
person_key = ndb.Key(urlsafe=person_url)
person = person_key.get()
baselink += person_url+"/"
section_title = person.getname()
breadcrumbs.append({'link': baselink, 'text': section_title})
if person is None:
section_title = 'Personer'
return render_template(
'persons.html',
heading=section_title,
baselink=baselink,
persons=Person.query(Person.scoutgroup == sgroup_key).order(Person.firstname, Person.lastname).fetch(),
breadcrumbs=breadcrumbs,
username=user.getname())
if person.scoutgroup != sgroup_key:
return "denied", 403
if not user.hasPersonAccess(person):
return "denied", 403
if action is not None:
if action == "deleteperson" or action == "addbackperson":
person.removed = action == "deleteperson"
person.put() # we only mark the person as removed
if person.removed:
tps = TroopPerson.query(TroopPerson.person == person.key).fetch()
for tp in tps:
tp.key.delete()
return redirect(breadcrumbs[-1]['link'])
elif action == "removefromtroop" or action == "setasleader" or action == "removeasleader":
troop_key = ndb.Key(urlsafe=request.args["troop"])
tps = TroopPerson.query(TroopPerson.person == person.key, TroopPerson.troop == troop_key).fetch(1)
if len(tps) == 1:
tp = tps[0]
if action == "removefromtroop":
tp.key.delete()
else:
tp.leader = (action == "setasleader")
tp.put()
elif action == "addtowaitinglist":
scoutgroup = person.scoutgroup.get()
troop = None
tps = TroopPerson.query(TroopPerson.person == person.key).fetch(1)
if len(tps) == 1:
troop = tps[0].troop.get()
scoutgroup = person.scoutgroup.get()
if scoutgroup.canAddToWaitinglist():
try:
member_no = scoutnet.AddPersonToWaitinglist(
scoutgroup,
person.firstname,
person.lastname,
person.personnr,
person.email,
person.street,
person.zip_code,
person.zip_name,
person.phone,
person.mobile,
troop,
"",
"",
"",
"",
"",
"",
"",
"")
person.notInScoutnet = False
person.put()
except scoutnet.ScoutnetException as e:
return render_template('error.html', error=str(e))
else:
logging.error('unknown action=' + action)
abort(404)
return ""
# render main pages
scoutgroup_url = scoutgroup.key.urlsafe()
person_url = person.key.urlsafe()
return render_template(
'person.html',
heading=section_title,
baselink='/persons/' + scoutgroup_url + '/',
# TODO: memcache
trooppersons=TroopPerson.query(TroopPerson.person == person.key).fetch(),
ep=person,
scoutgroup=scoutgroup,
breadcrumbs=breadcrumbs,
badge_url='/badges/' + scoutgroup_url + '/person/' + person_url + '/')
"""
Generate a members CSV file data for Gothenburg municipality. See documentation at:
https://goteborg.se/wps/portal/start/foretag-och-organisationer/foreningar/kulturstod-och-bidrag-till-foreningar/ansok-om-bidrag-till-foreningar-inom-idrott-och-fritid/aktivitetsbidrag
"""
def get_gbg_csv(sgroup_url=None):
user = UserPrefs.current()
if not user.hasAccess():
return "denied", 403
sgroup_key = None # type: ndb.Key
scoutgroup = None # type: ScoutGroup
if sgroup_url is not None:
sgroup_key = ndb.Key(urlsafe=sgroup_url)
scoutgroup = sgroup_key.get()
if user.activeSemester is None:
semester = Semester.getOrCreateCurrent()
else:
semester = user.activeSemester.get()
persons=Person.query(Person.scoutgroup == sgroup_key).order(Person.firstname, Person.lastname).fetch()
rows = '\ufeff' # BOM for Excel
rows += u"Förnamn;Efternamn;Personnummer;Har funktionsnedsättning\n"
for person in persons:
if semester.year not in person.member_years:
continue
if len(person.personnr) != 12:
continue # skip persons with invalid person number
# add a dash to the person number
formatted_personnr = person.personnr[:-4] + '-' + person.personnr[-4:]
rows += person.firstname + u';' + person.lastname + u';' + formatted_personnr + u';' + 'Nej' + '\n'
response = make_response(rows)
response.headers['Content-Type'] = 'text/csv; charset=utf-8'
response.headers['Content-Disposition'] = ('attachment; filename=' + urllib.parse.quote(str(scoutgroup.name), safe='') +
'-' + str(semester.year) + '.csv;')
return response