Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Låsa terminer #82

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions data.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def _not_dirty(self):
class Semester(ndb.Model):
year = ndb.IntegerProperty(required=True)
ht = ndb.BooleanProperty(required=True)
locked = ndb.BooleanProperty(required=False)

@staticmethod
def getid(year, ht):
Expand Down Expand Up @@ -83,6 +84,9 @@ def getMaxDateStr(self):
else:
return "%04d-06-30" % (self.year)

def isOpen(self):
return not self.locked

# kår
class ScoutGroup(ndb.Model):
name = ndb.StringProperty(required=True)
Expand Down Expand Up @@ -412,6 +416,11 @@ def getFullTroopname(self):
semester = troop.semester_key.get()
return self.troop.get().getname() + ' - ' + semester.getname()

def isOpen(self):
troop = self.troop.get()
semester = troop.semester_key.get()
return semester.isOpen()

class UserPrefs(ndb.Model):
userid = ndb.StringProperty(required=True)
hasaccess = ndb.BooleanProperty(required=True)
Expand Down
18 changes: 15 additions & 3 deletions htmlform.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ class HtmlForm():
method="POST"
submittext=""
buttonType=""
disabled=False

def __init__(self, formname, submittext="Spara", method="POST", descriptionText="", buttonType="btn-primary"):
def __init__(self, formname, submittext="Spara", method="POST", descriptionText="", buttonType="btn-primary", disabled=False):
self.fields = []
self.name = formname
self.method=method
self.submittext = submittext
self.descriptionText = descriptionText
self.buttonType=buttonType
self.disabled=disabled

def AddField(self, name, value, description, type="text", required=True):
self.fields.append((name, value, description, type, required))
Expand All @@ -27,10 +29,20 @@ def __str__(self):
for field in self.fields:
s += '<div class="form-group">'
s += '<label for="' + field[0] + '">' + field[2] + '</label>'
s += '<input type="' + field[3] + '" class="form-control" size="50" {% if field[4] %}required=""{% endif %} name="' + field[0] + '" id="' + field[0] + '" value="' + str(field[1]) + '"/>'
s += '<input '
if self.disabled:
s += 'disabled="disabled '
s += 'type="' + field[3] + '" class="form-control" size="50" '

if field[4]:
s += 'required="required" '
s += 'name="' + field[0] + '" id="' + field[0] + '" value="' + str(field[1]) + '"/>'
s += '</div>'
s += '<div class="btn-toolbar">'
s += '<button type="submit" name="submit" class="btn btn-lg ' + self.buttonType +'">' + self.submittext + '</button>'
s += '<button '
if self.disabled:
s += 'disabled="disabled '
s += 'type="submit" name="submit" class="btn btn-lg ' + self.buttonType +'">' + self.submittext + '</button>'
s += '</div>'
s += '</form>'
return s
48 changes: 48 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@
reload(sys)
sys.setdefaultencoding('utf8')

def semester_sort(a, b):
a_name = a.getname()
b_name = b.getname()

a_year = a_name[:4]
b_year = b_name[:4]

if a_year == b_year:
if a_name[-2:] == "ht":
return 1
else:
return -1
elif a_year > b_year:
return 1
else:
return -1

@app.route('/')
def home():
Expand Down Expand Up @@ -182,6 +198,38 @@ def groupaccess(userprefs_url=None):
mygroupurl=user.groupaccess.urlsafe(),
mygroupname=user.groupaccess.get().getname())

@app.route('/admin/semester/', methods = ['POST', 'GET'])
def adminSemester():
user = UserPrefs.current()
if not user.isAdmin():
return "denied", 403

section_title = u'Hem'
baselink = '/'
breadcrumbs = [{'link':baselink, 'text':section_title}]

section_title = u'Admin'
baselink += 'admin/'
breadcrumbs.append({'link':baselink, 'text':section_title})

section_title = u'Terminer'
baselink += 'semester/'
breadcrumbs.append({'link':baselink, 'text':section_title})

semesters = sorted(Semester.query(), semester_sort, reverse=True)

if request.method == 'POST':
locked_semesters = request.form.getlist('locked')

for s in semesters:
s.locked = s.key.urlsafe() in locked_semesters
s.put()

return render_template('semesters.html',
heading="Terminer",
baselink=baselink,
breadcrumbs=breadcrumbs,
semesters=semesters)

# merge scoutgroups with different names (renamed in scoutnet):
@app.route('/admin/merge_sg/', methods = ['POST', 'GET'])
Expand Down
27 changes: 20 additions & 7 deletions start.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,19 @@ def show(sgroup_url=None, troop_url=None, key_url=None):
breadcrumbs.append({'link':baselink, 'text':troop.getname()})
semester = troop.semester_key.get()

lockedTroop = semester.locked

if key_url == "settings":
section_title = u'Inställningar'
baselink += "settings/"
breadcrumbs.append({'link':baselink, 'text':section_title})
if request.method == "POST":
if not lockedTroop and request.method == "POST":
troop.defaultstarttime = request.form['defaultstarttime']
troop.defaultduration = int(request.form['defaultduration'])
troop.rapportID = int(request.form['rapportID'])
troop.put()

form = htmlform.HtmlForm('troopsettings')
form = htmlform.HtmlForm('troopsettings', disabled=lockedTroop)
form.AddField('defaultstarttime', troop.defaultstarttime, 'Avdelningens vanliga starttid')
form.AddField('defaultduration', troop.defaultduration, u'Avdelningens vanliga mötestid i minuter', 'number')
form.AddField('rapportID', troop.rapportID, 'Unik rapport ID för kommunens närvarorapport', 'number')
Expand All @@ -93,7 +95,7 @@ def show(sgroup_url=None, troop_url=None, key_url=None):
if key_url == "delete":
if troop == None:
return "", 404
if len(request.form) > 0 and "confirm" in request.form:
if not lockedTroop and len(request.form) > 0 and "confirm" in request.form:
if not user.isGroupAdmin():
return "", 403
troop.delete()
Expand Down Expand Up @@ -121,7 +123,7 @@ def show(sgroup_url=None, troop_url=None, key_url=None):
breadcrumbs=breadcrumbs,
trooppersons=[],
scoutgroup=scoutgroup)
elif request.method == "POST":
elif not lockedTroop and request.method == "POST":
pnr = request.form['personnummer'].replace('-','')
person = Person.createlocal(
request.form['firstname'],
Expand Down Expand Up @@ -192,7 +194,7 @@ def show(sgroup_url=None, troop_url=None, key_url=None):
jsonstr+=']'
return jsonstr
elif action == "addperson":
if troop == None or key_url == None:
if troop == None or key_url == None or lockedTroop:
raise ValueError('Missing troop or person')
person_key = ndb.Key(urlsafe=key_url)
person = person_key.get()
Expand All @@ -207,7 +209,7 @@ def show(sgroup_url=None, troop_url=None, key_url=None):
user.activeSemester = ndb.Key(urlsafe=semester_url)
user.put()
elif action == "removefromtroop" or action == "setasleader" or action == "removeasleader":
if troop == None or key_url == None:
if troop == None or key_url == None or lockedTroop:
raise ValueError('Missing troop or person')
person_key = ndb.Key(urlsafe=key_url)
tps = TroopPerson.query(TroopPerson.person == person_key, TroopPerson.troop == troop_key).fetch(1)
Expand All @@ -226,7 +228,7 @@ def show(sgroup_url=None, troop_url=None, key_url=None):
if request.method == "POST" and len(request.form) > 0 and "action" in request.form:
action=request.form["action"]
if action == "saveattendance":
if troop == None or scoutgroup == None or key_url == None:
if troop == None or scoutgroup == None or key_url == None or lockedTroop:
raise ValueError('Missing troop or group')

meeting = ndb.Key(urlsafe=key_url).get()
Expand All @@ -239,6 +241,9 @@ def show(sgroup_url=None, troop_url=None, key_url=None):
meeting.put()
return "ok"
elif action == "addmeeting" or action == "updatemeeting":
if troop == None or scoutgroup == None or lockedTroop:
raise ValueError('Missing troop or group')

mname = request.form['name']
mdate = request.form['date']
mtime = request.form['starttime'].replace('.', ':')
Expand All @@ -259,16 +264,24 @@ def show(sgroup_url=None, troop_url=None, key_url=None):
meeting.commit()
return redirect(breadcrumbs[-1]['link'])
elif action == "deletemeeting":
if troop == None or scoutgroup == None or key_url == None or lockedTroop:
raise ValueError('Missing troop or group')

meeting = ndb.Key(urlsafe=key_url).get()
logging.debug("deleting meeting=%s", meeting.getname())
meeting.delete()
return redirect(breadcrumbs[-1]['link'])
elif action == "savepatrol":
if troop == None or scoutgroup == None or lockedTroop:
raise ValueError('Missing troop or group')

patrolperson = ndb.Key(urlsafe=request.form['person']).get()
patrolperson.setpatrol(request.form['patrolName'])
patrolperson.put()
return "ok"
elif action == "newtroop":
if lockedTroop:
raise ValueError('Termin låst')
troopname = request.form['troopname']
troop_id = hash(troopname)
conflict = Troop.get_by_id(Troop.getid(troop_id, scoutgroup.key, user.activeSemester), use_memcache=True)
Expand Down
5 changes: 4 additions & 1 deletion templates/admin.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% include "header.html" %}
<body>
{% include "navigation.html" %}
<div class="main">
<div class="main container">
<div class="jumbotron">
<div class="btn-toolbar">
<a href="/admin/access/" class="btn btn-lg btn-default">Access</a>
Expand All @@ -12,6 +12,9 @@
<div class="btn-toolbar">
<a href="/admin/setcurrentsemester" class="btn btn-lg btn-default">Byt till ny termin för alla användare</a>
</div>
<div class="btn-toolbar">
<a href="/admin/semester/" class="btn btn-lg btn-default">Hantera terminer</a>
</div>
<div class="btn-toolbar">
<a href="/admin/backup/" class="btn btn-lg btn-default">Backup xml</a>
</div>
Expand Down
4 changes: 3 additions & 1 deletion templates/meetingform.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{% set hasMeeting = True if existingmeeting is defined else False %}
<form role="form" name="meeting" method="POST">
<fieldset {% if semester.locked %}disabled="disabled"{% endif %}>
{% if hasMeeting %}
<input type="hidden" name="action" value="updatemeeting"/>
{% else %}
Expand Down Expand Up @@ -31,8 +32,9 @@
<div class="btn-toolbar">
<button type="submit" name="submit" class="btn btn-lg btn-primary">{% if hasMeeting %}Spara{% else %}Lägg till{% endif %}</button>
</div>
</fieldset>
</form>
{% if hasMeeting %}
{% if hasMeeting and semester.isOpen() %}
<hr/>
<form role="form" name="deletemeeting" method="POST">
<input type="hidden" name="action" value="deletemeeting"/>
Expand Down
4 changes: 2 additions & 2 deletions templates/person.html
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,10 @@ <h3>Anhörig 2</h3>
<a href="/start/{{group_key.urlsafe()}}/{{troop.key.urlsafe()}}/">{{tp.getFullTroopname()}}</a>{% if tp.leader %} - Ledare{% endif %}
</td>
<td>
<a href="removefromtroop?troop={{troop.key.urlsafe()}}" class="btn btn-sm btn-danger">Ta bort från avdelning</a>
{% if tp.isOpen() %}<a href="removefromtroop?troop={{troop.key.urlsafe()}}" class="btn btn-sm btn-danger">Ta bort från avdelning</a>{% endif %}
</td>
<td>
{% if tp.leader %}<a href="removeasleader?troop={{troop.key.urlsafe()}}" class="btn btn-sm btn-danger">Ta bort som ledare</a>{% else %}<a href="setasleader?troop={{troop.key.urlsafe()}}" class="btn btn-sm btn-danger">Gör till ledare</a>{% endif %}
{% if tp.isOpen() %}{% if tp.leader %}<a href="removeasleader?troop={{troop.key.urlsafe()}}" class="btn btn-sm btn-danger">Ta bort som ledare</a>{% else %}<a href="setasleader?troop={{troop.key.urlsafe()}}" class="btn btn-sm btn-danger">Gör till ledare</a>{% endif %}{% endif %}
</td>
</tr>
{% endfor %}
Expand Down
25 changes: 25 additions & 0 deletions templates/semesters.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{% extends "layout.html" %}
{% block main %}
<h1 class="page-header">{{heading}}</h1>
<form name="semesters" method="POST">
<div class="table-responsive">
<table id="maintable" class="table table-striped">
<thead>
<tr>
<th>Namn</th>
<th>Låst?</th>
</tr>
</thead>
<tbody>
{% for semester in semesters %}
<tr>
<td>{{semester.getname()}}</td>
<td><input name="locked" value="{{semester.key.urlsafe()}}" type="checkbox" {% if semester.locked %}checked="checked"{% endif %} /></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<button type="submit" class="btn btn-lg btn-primary">Spara</button>
</form>
{% endblock %}
Loading