def selectReadings(sessionID=None): respData = MultiDict(url=url, title='Blood Glucose', sessionID=sessionID) if sessionID is None or not Sessions.ifSessionExistsInDB(sessionID): return jinja2_template('Home.jinja2', respData, template_lookup=['templates']) Sessions.putSessionValueInDB(sessionID, 'sessionIDtoSelect', sessionID) if not Sessions.getSessionValueFromDB(sessionID, 'isSignedOn'): return jinja2_template('Signon.jinja2', respData, template_lookup=['templates']) with db_session: heldReadings = Readings.select(lambda c: c.am is not None and c.pm is None).order_by(1) heldReadingsList = list(heldReadings) numberOfHeldReadings = len(heldReadingsList) respData.update(MultiDict(numberOfHeldReadings=numberOfHeldReadings)) if numberOfHeldReadings > 0: heldReadingDates = [] index = 1 for heldReading in heldReadingsList: heldReadingDates.append((f'D{index}', heldReading.date)) index += 1 respData.update(MultiDict(heldReadingDates=heldReadingDates)) respData.update(MultiDict(heldData=None)) return jinja2_template('SelectReading.jinja2', respData, template_lookup=['templates']) else: return jinja2_template('NoneHeld.jinja2', respData, template_lookup=['templates'])
def signon(sessionID): numberOfHeldReadings=numberOfPartials() Sessions.putSessionValueInDB(sessionID, "numberOfHeldReadings", numberOfHeldReadings) respData = MultiDict(url=url, title='Blood Glucose', sessionID=sessionID) respData.update(MultiDict(numberOfHeldReadings=numberOfHeldReadings)) if Sessions.getSessionValueFromDB(sessionID, 'isSignedOn'): return jinja2_template('Admin.jinja2', respData, template_lookup=['templates']) else: return jinja2_template('Signon.jinja2', respData, template_lookup=['templates'])
def adminCommon(request, response, sessionID=None): Sessions.purgeOldSessions() sessionID = Sessions.initializeSession(sessionID, request, response) Sessions.putSessionValueInDB(sessionID, 'currentDB', str(dbPath)) respData = MultiDict(url=url, title='Blood Glucose', sessionID=sessionID) respData.update(MultiDict(numberOfHeldReadings=numberOfPartials())) isSignedOn = Sessions.getSessionValueFromDB(sessionID, 'isSignedOn') if isSignedOn: template = jinja2_template('Admin.jinja2', respData, template_lookup=['templates']) else: template = jinja2_template('Signon.jinja2', respData, template_lookup=['templates']) log('adminCommon', 'made new session', sessionID=sessionID) return template
def enter(sessionID): sessionID = Sessions.initializeSession(sessionID, request, response) respData = MultiDict(url=url, title='Blood Glucose') isSignedOn = Sessions.getSessionValueFromDB(sessionID, 'isSignedOn') respData.update(MultiDict(sessionID=sessionID, numberOfHeldReadings=numberOfPartials())) respData.update(MultiDict(dbFileName=dbFileName)) if isSignedOn: with db_session: maxReadingDate = max(r.date for r in Readings) respData.update(MultiDict(maxDate=maxReadingDate)) return jinja2_template('EnterReading.jinja2', respData, template_lookup=['templates']) else: return jinja2_template('Signon.jinja2', respData, template_lookup=['templates'])
def selectPost(): rf = request.forms sessionID = rf.sessionID selectedDate = rf.selectedDate respData = MultiDict(url=url, title='Blood Glucose', sessionID=sessionID) with db_session: reading = Readings[selectedDate] form = MultiDict() form.date = reading.date form.am = reading.am form.pm = reading.pm if reading.pm is not None else '' form.comment = reading.comment if reading.comment is not None else '' respData.update(MultiDict(form=form, dbFileName=dbFileName)) return jinja2_template('EditReading.jinja2', respData, template_lookup=['templates'])
def signonPost(): sessionID = request.forms.sessionID if isNone(sessionID): sessionID = HTTPCookie.getSessionCookie(request) syscode = System.getCode() formcode = request.forms.get('code') isSignedOn = verify_password(syscode, formcode) Sessions.putSessionValueInDB(sessionID, 'isSignedOn', isSignedOn) respData = MultiDict(url=url, title='Blood Glucose', sessionID=sessionID) respData.update(MultiDict(numberOfHeldReadings=numberOfPartials())) if isSignedOn: return jinja2_template('Admin.jinja2', respData, template_lookup=['templates']) else: respData.update(MultiDict(tryAgain=True)) return jinja2_template('Signon.jinja2', respData, template_lookup=['templates'])
def test_ismulti(self): """ MultiDict has some special features """ m = MultiDict(a=5) m['a'] = 6 self.assertEqual([5, 6], m.getall('a')) self.assertEqual([], m.getall('b')) self.assertEqual([('a', 5), ('a', 6)], list(m.iterallitems()))
def edit(): respData = MultiDict(url=url, title='Blood Glucose') rf = request.forms updated = False isFloatOK = False with db_session: reading = Readings[rf.date] if rf.pm != '': reading.pm = rf.pm isFloatOK = isFloat(rf.pm) updated = True if rf.comment != '': reading.comment = rf.comment updated = True if not isFloatOK: respData.update(MultiDict(PMisNotOK=True, errorValue=rf.pm)) return jinja2_template('EditReading.jinja2', respData, template_lookup=['templates']) if updated: System.putLastReadingDateStr(dateTimeStr(datetime.now(), 'America/Vancouver',ampm=True, month=True)) return jinja2_template('UpdateDone.jinja2', respData, template_lookup=['templates'])
def post_validate(self, parent_form, validation_stopped): if validation_stopped: return # FIXME: a bit hairy validation, find something more idiomatic url_validator = validators.URL(require_tld=True) class TmpForm(form.Form): url = fields.StringField(validators=[url_validator]) for url in self.data: tmp_form = TmpForm(MultiDict(url=url)) if not tmp_form.validate(): raise validators.ValidationError("Invalid URL(s).")
def test_isadict(self): """ MultiDict should behaves like a normal dict """ d, m = dict(a=5), MultiDict(a=5) d['key'], m['key'] = 'value', 'value' d['k2'], m['k2'] = 'v1', 'v1' d['k2'], m['k2'] = 'v2', 'v2' self.assertEqual(d.keys(), m.keys()) self.assertEqual(d.values(), m.values()) self.assertEqual(d.get('key'), m.get('key')) self.assertEqual(d.get('cay'), m.get('cay')) self.assertEqual(list(iter(d)), list(iter(m))) self.assertEqual([k for k in d], [k for k in m]) self.assertEqual(len(d), len(m)) self.assertEqual('key' in d, 'key' in m) self.assertEqual('cay' in d, 'cay' in m) self.assertRaises(KeyError, lambda: m['cay'])
def enterPost(): rf = request.forms formDate = rf.date formAM = rf.am formAM = None if formAM == '' else formAM formPM = rf.pm formPM = None if formPM == '' else formPM formNote = rf.comment sessionID = HTTPCookie.getSessionCookie(request) respData = MultiDict(url=url, title='Blood Glucose', sessionID=sessionID) respData.update(MultiDict(numberOfHeldReadings=numberOfPartials())) respData.update(MultiDict(editDate=formDate)) with db_session: maxReadingDate = max(r.date for r in Readings) respData.update(MultiDict(maxDate=maxReadingDate)) valueError = False if not isFloat(formAM): valueError = True respData.update(MultiDict(AMisNotOK=True, errorValue=formAM)) if formPM is not None: if not isFloat(formPM): valueError = True respData.update(MultiDict(PMisNotOK=True, errorValue=formPM)) if valueError: return jinja2_template('EnterReading.jinja2', respData, template_lookup=['templates']) alreadyEntered = False try: with db_session: Readings(date=formDate, am=formAM, pm=formPM, comment=formNote) except (IntegrityError, Exception): alreadyEntered = True if alreadyEntered: respData.update(MultiDict(alreadyEntered=True)) return jinja2_template('EnterReading.jinja2', respData, template_lookup=['templates']) respData.update(MultiDict(numberOfHeldReadings=numberOfPartials())) System.putLastReadingDateStr(dateTimeStr(datetime.now(), 'America/Vancouver', ampm=True, month=True)) return jinja2_template('Admin.jinja2', respData, template_lookup=['templates'])
def cleanup(): forms = request.forms action = forms.get('action', 'check') if action not in ['check', 'delete']: # Translators, used as response to innvalid HTTP request abort(400, _('Invalid request')) selected = forms.getall('selection') metadata = list(archive.cleanup_list()) selected = [z for z in metadata if z['md5'] in selected] if action == 'check': if not selected: # Translators, used as message to user when clean-up is started # without selecting any content message = _('No content selected') else: tot = hsize(sum([s['size'] for s in selected])) message = str( # Translators, used when user is previewing clean-up, %s is # replaced by amount of content that can be freed in bytes, # KB, MB, etc _('%s can be freed by removing selected content')) % tot return {'vals': forms, 'metadata': metadata, 'message': message, 'needed': archive.needed_space()} else: success, errors = archive.remove_from_archive([z['md5'] for z in selected]) if selected and not errors: redirect(i18n_path('/')) if errors: # Translators, error message shown on clean-up page when some of # the content could not be removed for unknown reasons message = _('Some files could not be removed') elif not selected: # Translators, error message shown on clean-up page when there was # no deletable content message = _('Nothing to delete') metadata = archive.cleanup_list() return {'vals': MultiDict(), 'metadata': metadata, 'message': message, 'needed': archive.needed_space()}
def error405handler(error): f = request.fullpath respData = MultiDict(dict(f=f)) return jinja2_template('405.jinja2', respData, template_lookup=['templates'])
def home(): respData = MultiDict(url=url, title='Blood Glucose') return jinja2_template('Home.jinja2', respData, template_lookup=['templates'])
def home(): # sessionID = Sessions.makeNewSessionID() # Sessions.initializeSession(sessionID, request, response) # respData = dict(sessionID=sessionID) respData = MultiDict(url=url, title='Blood Glucose') return jinja2_template('Home.jinja2', respData, template_lookup=['templates'])
last_updated = archive.last_update() needed = archive.needed_space() zipballs = downloads.get_zipballs() zipballs = list(reversed(downloads.order_zipballs(zipballs))) if zipballs: last_zip = datetime.fromtimestamp(zipballs[0][1]) zipballs = len(zipballs) logging.debug('Found %s updates' % zipballs) else: logging.debug('No updates found') with open(request.app.config['logging.output'], 'rt') as log: logs = ''.join(reversed(list(log))) return locals() @view('cleanup', message=None, vals=MultiDict()) def cleanup_list(): """ Render a list of items that can be deleted """ return {'metadata': archive.cleanup_list(), 'needed': archive.needed_space()} @view('cleanup', message=None, vals=MultiDict()) def cleanup(): forms = request.forms action = forms.get('action', 'check') if action not in ['check', 'delete']: # Translators, used as response to innvalid HTTP request abort(400, _('Invalid request')) selected = forms.getall('selection') metadata = list(archive.cleanup_list())