def __init__(self, course_name, start_time, end_time, day, room_code): tmp = db_connection.DBConnection() self.course_name = course_name self.start_time = start_time self.end_time = end_time self.day = day.upper() self.room_code = room_code self.building_code = room_code[0] self.building_location = tmp.get_location(self.building_code)
def __init__(self): self.dict_keys = ('status_id', 'user_id', 'status_text') self.db_conn = sql.DBConnection() with self.db_conn as db_conn: self.table = db_conn.social[sql.STATUS_TABLE] self.table.create_index('status_id', unique=True) self.table.create_index([('status_text', 'text')]) self.logger = logging.getLogger('main.userstatuscollection') self.logger.info('New UserStatusCollection instance created')
def __init__(self): self.dict_keys = ('user_id', 'user_name', 'user_last_name', 'user_email') self.db_conn = sql.DBConnection() with self.db_conn as db_conn: self.table = db_conn.social[sql.USER_TABLE] self.table.create_index(self.dict_keys[0], unique=True) self.logger = logging.getLogger('main.userscollection') self.logger.info('New UserCollection instance created')
def take_me(code): tmp = db_connection.DBConnection() room_code = code[1:] for c in room_code: if c.isdigit(): floor = c break building = code[0] location = tmp.get_location(building) return 'Go to building {} ({})'.format( building.upper(), location), 'Go to floor {}'.format( floor), 'Find room {}'.format(room_code)
def bootstrap(): """ Sets up the database. Args: None Returns: None """ db = db_connection.DBConnection() #pylint: disable=C0103 db.create_tables() print("Created database tables") db.clear_data() db.insert_heart_beat(first=True) print("Bootstrap procedure complete")
def connection_stats(): """ Returns the connection stats for database connection Args: None Returns: Response 200 and JSON String of connection stats - see DBConnection class for more info. Response 504 in case of connection error """ try: db = db_connection.DBConnection() except IOError: return "Could not connect to DB", 504, {'ContentType': 'text/plain'} return db.get_connection_stats(), 200, {'ContentType': 'application/json'}
def database_info(): """ Returns the all data stored in the database. Args: None Returns: Response 200 and JSON String where values are list of records indexed by table name as key. Response 504 in case of connection error """ try: db = db_connection.DBConnection() except IOError: return "Could not connect to DB", 504, {'ContentType': 'text/plain'} return db.get_database_info(), 200, {'ContentType': 'application/json'}
def insert_files_data(): """ Takes a post request with a JSON object of form { "Files" : ["file_name_1", "file_name_2", ...] } Args: None """ try: db = db_connection.DBConnection() except IOError: return "Database connection not possible", 504, { 'ContentType': 'text/plain' } files = request.get_json() files = roll_dice_for_corrupted_files(files) db.insert_files_names(files['Files']) return "Successful Insertion", 200, {'ContentType': 'application/json'}
def connection_stats(): """ Returns the connection stats for database connection (used for debugging). Args: None Returns: Response 200 and JSON String of connection stats - see DBConnection class for more info. Response 504 if database connection not possible """ try: db = db_connection.DBConnection() except IOError: return "Database connection not possible", 504, { 'ContentType': 'text/plain' } return db.get_connection_stats(), 200, {'ContentType': 'application/json'}
def query_air_pollution_data(): """ Returns the air pollution data Args: None Returns: Response 200 and JSON String - List of records (Lat, Long, PM10, PM2.5). Again see DBConnection class for more info. Response 504 if database connection not possible """ try: db = db_connection.DBConnection() except IOError: return "Database connection not possible", 504, { 'ContentType': 'text/plain' } return db.query_air_pollution_data(), 200, { 'ContentType': 'application/json' }
def bootstrap(): """ Sets up the database with a clean consistent state. It creates the tables, clears any existing data and restores data from backup csvs. Args: None Returns: None """ print("Beginning bootstrap procedure...") #for debugging db = db_connection.DBConnection() #pylint: disable=C0103 print("Established connection to db...") #for debugging db.create_tables() print("Created database tables") #for debugging #clear any lingering data and start afresh db.clear_data() # iterate through backup csv folder and add them to database for csv_file in glob.glob('/usr/backups/*.csv'): db.insert_backup_data(csv_file) print("Bootstrap procedure complete") #for debugging
def job_function(): try: db = db_connection.DBConnection() except IOError: return "Could not connect to DB", 504, {'ContentType': 'text/plain'} db.insert_heart_beat()
def insert_sensor_data(): """ Takes a post request with a JSON object of form { "app_id": "cleancycle-application", "dev_id": "", "hardware_serial": "", "port": 1, "counter": 0, "payload_raw": "", "payload_fields": { "0": 52.19398498535156, "1": 0.1360626220703125, "2": 38, "3": 29, "4": 52.19419860839844, .... }, "metadata": { "time": "2019-02-25T16:00:46.840341614Z" }, "downlink_url": "" } Payload fields: JSON object where keys 0,1,2,3 correspond to (Lat, Long, PM10, PM2.5) for first measurement. Subsequent measurements can be obtained by grouping subsequent keys by 4 Args: None Returns: Response 201 if data inserted successfully Response 400 iif data has no payload. Response 400 if data payload_fields are malformed - i.e. can't be grouped as records Response 504 if database connection not possible """ try: db = db_connection.DBConnection() except IOError: return "Database connection not possible", 504, { 'ContentType': 'text/plain' } #this contains the air pollution data if "payload_fields" in request.get_json( ): #i.e. we have the payload fields sensor_data = request.get_json().get("payload_fields") try: db.insert_sensor_data(sensor_data.values()) return "Successful Insertion", 201, {'ContentType': 'text/plain'} except IOError: return "Error: malformed payload fields data", 400, { 'ContentType': 'text/plain' } else: return "Error no payload fields in JSON", 400, { 'ContentType': 'text/plain' }
global course_start_time global course_end_time global taking_to_class adding_course = False removing_course = False taking_to_class = False course_name = None course_day = None course_start_time = None course_end_time = None course_room = None db = db_connection.DBConnection() def handle(msg): global adding_course global removing_course global course_day global course_name global course_room global course_start_time global course_end_time global taking_to_class global db content_type, chat_type, chat_id = telepot.glance(msg) if adding_course:
def test_menu(mocked_print, mocked_input): ''' Integration test script for menu.py ''' # Drop tables for clean testing db_conn = sql.DBConnection() with db_conn as conn: conn.social.UserTable.drop() conn.social.StatusTable.drop() # Setup menu for testing mocked_input.side_effect = selections menu.user_collection = menu.main.init_user_collection() menu.status_collection = menu.main.init_status_collection() # Test Users related functions menu.load_users() menu.add_user() mocked_print.assert_called_with(responces[0]) # add_user() menu.add_user() mocked_print.assert_called_with(responces[1]) # add_user() error menu.update_user() mocked_print.assert_called_with(responces[2]) # update_user() menu.update_user() mocked_print.assert_called_with(responces[3]) # update_user() error menu.search_user() mocked_print.assert_called_with(responces[4]) # search_user() menu.search_user() mocked_print.assert_called_with(responces[5]) # search_user() error menu.delete_user() mocked_print.assert_called_with(responces[6]) # delete_user() menu.delete_user() mocked_print.assert_called_with(responces[7]) # delete_user() error # Test UserStatus related functions menu.add_user() mocked_print.assert_called_with(responces[8]) # add_user() for status menu.load_status_updates() menu.add_status() mocked_print.assert_called_with(responces[9]) # add_status() menu.add_status() mocked_print.assert_called_with(responces[10]) # add_status() error menu.update_status() mocked_print.assert_called_with(responces[11]) # update_status() menu.update_status() mocked_print.assert_called_with(responces[12]) # update_status() error menu.search_status() mocked_print.assert_called_with(responces[13]) # search_status() menu.search_status() mocked_print.assert_called_with(responces[14]) # search_status() error menu.delete_status() mocked_print.assert_called_with(responces[15]) # delete_status() menu.delete_status() mocked_print.assert_called_with(responces[16]) # delete_status() error # Test cascading deletes menu.add_status() mocked_print.assert_called_with(responces[17]) # add_status() menu.delete_user() mocked_print.assert_called_with(responces[18]) # delete_user() menu.search_status() mocked_print.assert_called_with(responces[19]) # search_status() error # Test table restrictions menu.add_user() mocked_print.assert_called_with(responces[20]) # add_user() error menu.add_user() mocked_print.assert_called_with(responces[21]) # add_user() error menu.add_user() mocked_print.assert_called_with(responces[22]) # add_user() error