def validate(fields): """ Validate a list of fields. Pass the json object from the request with a list of expected fields. :param fields: Tuple of (fieldName, validatorObj). :return: { 'validated': 'False', 'errors': ['Validation failed for this reason.', 'And this one.'] } """ #TODO: break this out into shared validation class and write unit tests. errors = [] for (name, validator) in fields: if type(validator) is not Validator: logger.logSystem('Bad validator specified!', enums.e_log_event_level.critical) raise (TypeError) if not fields.get(name): errors.append(name + ' not specified.') continue result = validator.test(fields[name]) if len(result) != 0: errors.append(name + ' failed validation: ' + result) continue return jsonify(validated=True if len(errors) == 0 else False, errors=errors)
def schemaCheckAndCreate(self): """ Checks the root object for the presence of the expected BTrees and creates them if they do not exist. """ if self.zdb is None: logger.logSystem("Schema creation invoked without a database.", enums.e_log_event_level.crash) server.hardStop() if self.root is None: logger.logSystem("Schema creation invoked without a root element.", enums.e_log_event_level.crash) server.hardstop() if not hasattr(self.root, 'globalSettings'): logger.logSystem('Creating default global settings...') self.root.globalSettings = GlobalSettings() if not hasattr(self.root, 'households'): logger.logSystem('Creating household collection...') self.root.households = BTrees.OOBTree.BTree() if not hasattr(self.root, 'users'): logger.logSystem('Creating user collection...') self.root.users = BTrees.OOBTree.BTree() transaction.commit()
def run_integrity_checks(): # TODO: We need to just force a logout here since the user probably doesn't exit # TODO: Don't populate these items, have a cleaner API for getting useful items. # Populate useful items if 'id' in session: g.dog.me = g.dog.zdb.getUser(session['id']) if g.dog.me is None: logger.logSystem( 'Integrity error - user object lookup failed for user id ' + str(session['id']), enums.e_log_event_level.critical) session.clear() flash('Please log in again.', 'info') return redirect(url_for('splash')) if 'householdId' in session: g.dog.hh = g.dog.zdb.getHousehold(session['householdId']) if g.dog.hh is None: logger.logSystem( "Integrity error - household object lookup failed for household id " + str(session['householdId']), enums.e_log_event_level.critical) session.clear() flash('Please log in again.', 'info') return redirect(url_for('splash'))
def hardstop(): """ Try to shut down as fast and minimally as possible, :return: """ logger.logSystem('--- Hard Stop Initiated ---', enums.e_log_event_level.critical) if g is not None: db = getattr(g, 'db', None) if db is not None: db.close() if request is not None: k = request.environ.get('werkzeug.server.shutdown') k() print('--- Hard Stop ---') raise SystemExit(0)
def softstop(): """ Initiate a regular shutdown through the Werkzeug shutdown hook. Existing requests will continue to completion. Locks and database will shut down gracefully according to the after_request handler. """ logger.logSystem('--- Soft Stop Initiated ---', enums.e_log_event_level.warning) if request is None: logger.logSystem('No request present, cannot soft stop.', enums.e_log_event_level.crash) hardstop() k = request.environ.get('werkzeug.server.shutdown') if k is None: logger.logSystem( 'Not running within the Werkzeug server, cannot soft stop.', enums.e_log_event_level.crash) hardstop() print('- Soft Stop -') k() abort(500, "CuteCasa - Critical Error, Soft Stop")