def setPreferences(request): lecture = request.context.lecture times = lecture.prepareTimePreferences(user=request.user) row = 1 tps = [] while 'time-%i' % row in request.POST: time = types.TutorialTime(request.POST['time-%i' % row]) tp = models.getOrCreate(models.TimePreference, request.db, (lecture.id, request.user.id, time)) tp.penalty = int(request.POST['pref-%i' % row]) tps.append(tp) row += 1 if lecture.minimum_preferences: valid = len(filter(lambda tp: tp.penalty < 100, tps)) >= lecture.minimum_preferences else: #TODO: Works not for just one tutorial! min_number_of_times = len(tps) / 100.0 + 1 penalty_count = sum([1.0 / tp.penalty for tp in tps]) valid = penalty_count > min_number_of_times if not valid: request.db.rollback() request.session.flash(u'Fehler: Sie haben zu wenige Zeiten ausgewählt', queue='errors') else: request.db.commit() request.session.flash(u'Präferenzen gespeichert.', queue='messages') return HTTPFound( location=request.route_url('lecture_view', lecture_id=lecture.id))
def __init__(self, request): MatplotlibView.__init__(self) self.request = request lecture = self.request.context.lecture time = self.request.matchdict['time'] preferences = self.request.db.query(sa.func.count(TimePreference.penalty),TimePreference.penalty).filter(TimePreference.lecture_id==lecture.id)\ .filter(TimePreference.time==time).group_by(TimePreference.penalty).order_by(TimePreference.penalty).all() prefdict = {} for count, penalty in preferences: prefdict[penalty] = count self.bars = [prefdict.get(p['penalty'], 0) for p in utils.preferences] self.inds = range(len(utils.preferences)) self.xticks = [p['name'] for p in utils.preferences] self.label = types.TutorialTime(time).__html__()