def PUT(self): logging.info("PUT request to users.") cherrypy.response.headers['Content-Type'] = 'application/json' try: data = json.loads(cherrypy.request.body.read()) except ValueError: logging.error('Json data could not be read.') return {"error": "Data could not be read."} if "firstName" not in data: logging.error('user firstName not found.') return {"error": "You must provide a user firstName."} # NOTE: We actually don't care if all of the fields are here # because maybe we just want to update ex: meeting time? with sessionScope() as session: # We try to find the user to see if it already exists. If it does, we continue # but quit early, if it doesn't, then we 'throw an error', so that we can create # a new user. try: user = session.query(User).filter_by( first_name=data['firstName']).one() logging.info("user found.") updatedUser = UpdateUser(user, data, session) data = {"olduser": user, "updateduser": updatedUser} except Exception, e: logging.error("user doesn't exist.") data = {"error": e, "note": "user already exists."}
def PUT(self): logging.info("PUT request to groups.") cherrypy.response.headers['Content-Type'] = 'application/json' try: data = json.loads(cherrypy.request.body.read()) except ValueError: logging.error('Json data could not be read.') return {"error": "Data could not be read."} if "groupDescription" not in data: logging.error('group description not found.') return {"error": "You must provide a group description."} # NOTE: We actually don't care if all of the fields are here # because maybe we just want to update ex: meeting time? with sessionScope() as session: # We try to find the group to see if it already exists. If it does, we continue # but quit early, if it doesn't, then we 'throw an error', so that we can create # a new group. try: group = session.query(Group).filter_by( group_description=data['groupDescription']).one() logging.info("Group found.") updatedGroup = UpdateGroup(group, data, session) data = {"oldGroup": group, "updatedGroup": updatedGroup} except Exception, e: logging.error("Group doesn't exist.") data = {"error": e, "note": "Group already exists."}
def POST(self): logging.info("POST request to courses.") cherrypy.response.headers['Content-Type'] = 'application/json' try: data = json.loads(cherrypy.request.body.read()) except ValueError: logging.error('Json data could not be read.') return {"error": "Data could not be read."} if "courseDescription" not in data: logging.error('course description not found.') return {"error": "You must provide a course description."} # TODO: Ensure all other fields exist with sessionScope() as session: # We try to find the course to see if it already exists. If it does, we continue # but quit early, if it doesn't, then we 'throw an error', so that we can create # a new course. try: course = session.query(course).filter_by( course_description=data['courseDescription']).one() logging.error("course already exists!.") data = { "error": "course already exists!", "course": course.toDict() } except Exception: logging.info("course doesn't yet exist. Creating new one.") data = CreateCourse(data, session).toDict() return json.dumps(data)
def main(): with sessionScope() as s: loadAdmin(s) # loadScalingFunctions(s) loadSensors(s) loadCalibrations(s) loadDatastreams(s) loadReadings(s)
def login(self, username=None, password=None, from_page="/"): if username is None or password is None: return self.loginForm("", from_page=from_page) with sessionScope() as s: error_msg = check_credentials(username, password, s) if error_msg: return self.loginForm(username, error_msg, from_page) else: cherrypy.session[SESSION_KEY] = cherrypy.request.login = username self.on_login(username) raise cherrypy.HTTPRedirect(from_page or "/")
def GET(self, email=None): logging.info('GET request to users.') cherrypy.response.headers['Content-Type'] = 'application/json' with sessionScope() as session: data = {"user_list": []} if email is not None: objs = session.query(User).filter_by(email=email) else: objs = session.query(User) for i in objs: data['user_list'].append(i.toDict()) return json.dumps(data)
def POST(self): logging.info("POST request to users.") cherrypy.response.headers['Content-Type'] = 'application/json' try: data = json.loads(cherrypy.request.body.read()) except ValueError: logging.error('Json data could not be read.') return {"error": "Data could not be read."} if "firstName" not in data: logging.error('user first name not found.') return {"error": "You must provide a user firstName."} if "lastName" not in data: logging.error('user last name not found.') return {"error": "You must provide a user lastName."} if "email" not in data: logging.error('user email not found.') return {"error": "You must provide a user email."} if "password" not in data: logging.error('user password not found.') return {"error": "You must provide a user password."} if "university" not in data: logging.error('user university not found.') return {"error": "You must provide a user university."} if "major" not in data: logging.error('user major not found.') return {"error": "You must provide a user major."} with sessionScope() as session: # We try to find the user to see if it already exists. If it does, we continue # but quit early, if it doesn't, then we 'throw an error', so that we can create # a new user. try: user = session.query(User).filter_by( first_name=data['firstName']).one() logging.error("user already exists!.") data = {"error": "user already exists!", "user": user.toDict()} except Exception: logging.info("user doesn't yet exist. Creating new one.") data = CreateUser(data, session).toDict() return json.dumps(data)
def GET(self, userId=None): logging.info('GET request to groups.') cherrypy.response.headers['Content-Type'] = 'application/json' with sessionScope() as session: data = {"group_list": []} if userId is not None: print "doing queries" userQuery = session.query(User).filter_by(id=userId).one() print userQuery.toDict() groupQuery = session.query(Group).filter( Group.myMembers.any(id=userId)).all() print groupQuery try: print groupQuery.toDict() except Exception, e: print "cant print" else:
def GET(self, course_name=None): logging.info('GET request to courses.') cherrypy.response.headers['Content-Type'] = 'application/json' with sessionScope() as session: if course_name is None: data = {"course_list": []} try: objs = session.query(Course) for i in objs: print 'itter' data['course_list'].append(i.toDict()) except Exception, e: data = { "error": e, "note": "No courses currently exist in database." } logging.error('No courses exist.') else:
volt = SensorReading(**voltReading) amp = SensorReading(**ampReading) readings.append(volt) readings.append(amp) # session.add_all(readings) def main(): with sessionScope() as s: loadAdmin(s) # loadScalingFunctions(s) loadSensors(s) loadCalibrations(s) loadDatastreams(s) loadReadings(s) if __name__ == "__main__": cmds = { "loadreadings": loadReadings } args = sys.argv if (len(args) > 2 and args[1] in cmds): interval = None if len(args) <= 3 else int(args[3]) with sessionScope() as s: cmds[args[1]](s, args[2], interval) else: main()