def load_event(self, eventId): ''' Loads an event object by a given id. ''' eventRows = {} try: self._cur.execute( 'SELECT id, name, creator_id, created_date from event WHERE id={0}' .format(eventId)) eventData = self._cur.fetchone() except Exception as e: print(e, sys.exc_info()) raise DaoException('Unknown error when loading event') if (len(eventData) > 0): # ID is primary key. Should only ever get 1 or 0 data = {} data['id'] = eventData[0] data['name'] = eventData[1] data['creator_id'] = eventData[2] data['created_date'] = eventData[3] return data else: # not found print('Given ID was not found', eventId) raise DaoException('Unknown error when loading event')
def save_event(self, eventObject): ''' Generates a unique ID for the event and creates a new instance in the database. An attendee can be in multiple events. Creator ID is stored as a cookie. When joining an event, a creator ID is either generated and saved, or it is retrieved from the cookies. ''' try: # generate an initial id based on event name generatedId = HashUtils.generate_code(eventObject['name']) # if the id is taken, append characters and re-generate count = 0 newSeed = eventObject['name'] + 'a' while count < 5: if (self.event_exists(generatedId)): break generatedId = HashUtils.generate_code(newSeed) newSeed = newSeed + 'a' count += 1 # after 5 tries, check if id is still taken... if not self.event_exists(generatedId): # ... save if id is unique self._cur.execute( 'INSERT INTO event (id, name, creator_id, created_date) ' 'VALUES (\'{0}\', \'{1}\', {2}, \'{3}\')'.format( generatedId, eventObject['name'], eventObject['creator_id'], datetime.datetime.now().strftime('%Y%m%d'))) # ... and update the event_attendee join table self._cur.execute( 'INSERT INTO event_attendee (event_id, creator_id) ' 'VALUES (\'{0}\', {1})'.format(generatedId, eventObject['creator_id'])) return self.load_event(generatedId) # ... raise an exception if not else: raise DaoException( 'Unable to generate a unique ID. Please choose a new name.' ) # Throw any DaoExceptions. Catch anything else. except DaoException as e: raise e except Exception as e: print(e, sys.exc_info()) raise DaoException( 'An error occurred saving this event. Please try again later.') print(eventObject)
def load_event(self, eventId): print('Calling load') if os.environ['TEST_DB_FAIL'] == 'True': raise DaoException('General exception') if eventId in const.MOCK_EVENT_IDS: print('id found in list') return { 'id': eventId, 'name': 'A mock event', 'creator_id': const.GOOD_USER_ID } else: print('id not found') raise DaoException('Event wasn\'t in there')
def delete_availability(self, availability_id='', attendee_id='', event_id=''): if os.environ['TEST_DB_FAIL'] == 'True': raise DaoException('General exception') pass
def load_attendee_events(self, attendee_id): ''' Loads all events that the attendee is attending. ''' eventRows = {} try: self._cur.execute( 'SELECT events.id, events.name, events.creator_id, ' + 'events.created_date FROM events INNER JOIN event_attendees ' + 'ON event_attendees.event_id = events.id AND ' + 'event_attendees.attendee_id = {0}'.format(attendee_id)) eventRows = self._cur.fetchall() except Exception as e: print(e, sys.exc_info()) raise DaoException('Unknown error when loading event') data = [] for event in eventRows: tempData = {} tempData['id'] = event[0] tempData['name'] = event[1] tempData['creator_id'] = event[2] tempData['created_date'] = event[3] data.append(tempData) return data
def save_event(self, eventObject): print('Calling save') if os.environ['TEST_DB_FAIL'] == 'True': raise DaoException('General exception') generatedId = HashUtils.generate_code(eventObject['name']) const.MOCK_EVENT_IDS.append(generatedId) return self.load_event(generatedId)
def load_event_attendees (self, event_id): ''' loads all attendees that are attending this event. Uses an inner join on the attendee and event_attendee table to retrieve the attendee data. ''' try: # retrieve all event attendees from the DB self._cur.execute( 'SELECT attendee.id, attendee.name, attendee.email FROM attendee ' 'INNER JOIN event_attendee ON attendee.id=event_attendee.attendee_id ' 'WHERE event_attendee.event_id={0}'.format( event_id ) ) atts = self._cur.fetchall() # build a list of attendee objects att_list = [] for att in atts: temp_att = { 'id': att[0], 'name': att[1], 'email': att[2] } att_list.append(temp_att) # return the built list return att_list except Exception as e: print(e, sys.exc_info()) raise DaoException('Unknown error when loading attendee')
def event_exists(eventId): print('Calling exists??') if os.environ['TEST_DB_FAIL'] == 'True': raise DaoException('General exception') if eventId in const.MOCK_EVENT_IDS: return True else: return False
def event_exists(self, eventId): try: self._cur.execute( 'SELECT name FROM event WHERE id = {0}'.format(eventId)) val = self._cur.fetchone() return val is not None except Exception as e: print(e, sys.exc_info()) raise DaoException('Unknown error when searching for event')
def update_event(self, eventObject): try: self._cur.execute('INSERT INTO event (name) VALUES (\'{0}\') ' 'WHERE id={1}'.format(eventObject['name'], eventObject['id'])) return self._cur.fetchone() except Exception as e: print(e, sys.exc_info()) raise DaoException('Unknown error while updating event')
def attendee_exists (self, attendee_id): try: self._cur.execute( 'SELECT id name email FROM attendee WHERE id = {0}'.format( attendee_id ) ) return self._cur.fetchone() is not None except Exception as e: print(e, sys.exc_info()) raise DaoException('Unknown error when searching for attendee')
def load_event_attendees(self, event_id): if os.environ['TEST_DB_FAIL'] == 'True': raise DaoException('test exception') data = [{ 'id': 'asd', 'name': 'idkidkidk', 'email': '*****@*****.**' }, { 'id': 'asd', 'name': 'idkidkidk', 'email': '*****@*****.**' }] return data
def join_event (self, attendee_id, event_id): try: self._cur.execute( 'INSERT INTO event_attendee (event_id, creator_id) ' 'VALUES (\'{0}\', {1})'.format( event_id, attendee_id ) ) except Exception as e: print(e, sys.exc_info(), attendee, event_id) raise DaoException( 'Unknown error when adding attendee to event' )
def update_attendee (self, attendee): try: self._cur.execute( 'INSERT INTO attendee (name, email) VALUES (\'{0}\', \'{1}\') ' 'WHERE id={2}'.format( attendee['name'], attendee['email'], attendee['id'] ) ) return self._cur.fetchone() except Exception as e: print(e, sys.exc_info()) raise DaoException('Unknown error when loading attendee')
def load_attendee (self, attendee_id): attendeeRows = {} try: self._cur.execute( 'SELECT id, name, email from attendee WHERE id={0}' .format(attendee_id) ) attendeeRows = self._cur.fetchone() except Exception as e: print(e, sys.exc_info()) raise DaoException('Unknown error when loading attendee') if (attendeeRows is not None): # ID is primary key. Should only ever get 1 or 0 data = {} data['id'] = attendeeRows[0] data['name'] = attendeeRows[1] data['email'] = attendeeRows[2] return data else: # not found print('Given id was not found', attendee['id']) raise DaoException('Attendee with this ID not found.')
def delete_attendee (self, attendee_id, event_id): try: # delete the attenddee self._cur.execute( 'DELETE FROM attendee WHERE id={0}'.format(attendee_id) ) # delete event_attendee entries self._cur.execute( 'DELETE FROM event_attendee WHERE attendee_id={0} AND event_id={1}'.format( attendee_id, event_id ) ) except Exception as e: print(e, sys.exc_info()) raise DaoException( 'An error occurred deleting attendee. Please try again later.' )
def delete_event(self, event_id): ''' Deletes the event with the given id. If successful, this function returns nothing. THrows exception otherise. ''' try: # delete the event self._cur.execute( 'DELETE FROM event WHERE id={0}'.format(event_id)) # delete event_attendee entries self._cur.execute( 'DELETE FROM event_attendee WHERE event_id={0}'.format( event_id)) except Exception as e: print(e, sys.exc_info()) raise DaoException( 'An error occurred deleting this event. Please try again later.' )
def save_attendee (self, attendee): try: self._cur.execute( 'INSERT INTO attendee (name) VALUES ({0})'.format( attendee['name'] ) ) at = self._cur.fetchone() data = { 'id': at[0], 'name': at[1], 'email': at[2] } except Exception as e: print(e, sys.exc_info()) raise DaoException( 'Unknown error while saving attendee' ) return data
def delete_attendee(self, attendee_id, event_id): if os.environ['TEST_DB_FAIL'] == 'True': raise DaoException('General exception') pass
def delete_event(self, event_id): print('Calling delete') if os.environ['TEST_DB_FAIL'] == 'True': raise DaoException('General exception') if event_id not in const.MOCK_EVENT_IDS: raise DaoException('Event wasn\'t in there')
def update_event(self, eventObject): print('Calling update') if os.environ['TEST_DB_FAIL'] == 'True': raise DaoException('General exception') return self.load_event(eventObject['id'])
def load_attendee(self, attendee_id): if os.environ['TEST_DB_FAIL'] == 'True': raise DaoException('General exception') data = {'id': 'asd', 'name': 'idkidkidk', 'email': '*****@*****.**'} return data
def availability_exists(self, event_id, attendee_id, year): if os.environ['TEST_DB_FAIL'] == 'True': raise DaoException('General exception') return True
def create_availability(self, availability): if os.environ['TEST_DB_FAIL'] == 'True': raise DaoException('General exception') return self.__simple_obj
def leave_event(self, attendee_id, event_id): if os.environ['TEST_DB_FAIL'] == 'True': raise DaoException('General exception') print('leaving this event...')
def get_availability(self, attendee_id='', event_id=''): if os.environ['TEST_DB_FAIL'] == 'True': raise DaoException('General exception') return self.__simple_obj
def join_event(self, attendee, event_id): if os.environ['TEST_DB_FAIL'] == 'True': raise DaoException('General exception') pass
def attendee_exists(self, attendee_id): if os.environ['TEST_DB_FAIL'] == 'True': raise DaoException('General exception') return False