def read_session(sessionUUID): db_instance = Db() session = db_instance.session t_session = db_instance.model.Session t_session_tag = db_instance.model.SessionTag t_category = db_instance.model.Categories try: query = session.query(t_session).join(t_session_tag).join(t_category) results = dict() sessions = query.filter(t_session.UUID == sessionUUID).one() hashtags = sessions.SessionTag category = sessions.Categories results['name'] = sessions.Name results['category'] = {'value': category.Value, 'UUID': category.UUID} results['hashtags'] = hashtags[0].Hashtag.split(',') if len( hashtags[0].Hashtag) > 0 else '' results['description'] = sessions.Description results['language_iso'] = sessions.LanguageISO results['creator_uuid'] = sessions.CreatorUUID results['tokens'] = sessions.Tokens results['uuid'] = sessions.UUID results['created_at'] = sessions.CreationDateTime.strftime( "%Y-%m-%dT%H:%M:%SZ") return results except exc.NoResultFound as ex: print(str(ex)) raise NotFoundException('Session Id ' + sessionUUID + " not found") except Exception as ex: print(str(ex)) raise ServerException(str(ex))
def read_live(liveUUID): db_instance = Db() session = db_instance.session t_live = db_instance.model.Live t_live_tag = db_instance.model.LiveTag try: query = session.query(t_live).join(t_live_tag) lives = query.filter(t_live.UUID == liveUUID).one() hashtags = lives.LiveTag except exc.NoResultFound as ex: print(str(ex)) raise NotFoundException('Live Id ' + liveUUID + " not found") except Exception as ex: print(str(ex)) raise ServerException(str(ex)) results = dict() results['from'] = lives.StartAtGMT.strftime("%Y-%m-%dT%H:%M:%S.%fZ") results['to'] = lives.EndsAtGMT.strftime("%Y-%m-%dT%H:%M:%S.%fZ") results['hashtags'] = hashtags[0].Hashtag.split( ',') if hashtags[0].Hashtag != '' else [] results['description'] = lives.Description results['uuid'] = lives.UUID results['language'] = lives.LanguageISO results['session_uuid'] = lives.SessionUUID results['presenter_uuid'] = lives.PresenterUUID return results
def decreaseBookSlot(liveUUID): db_instance = Db() availability = db_instance.session t_availability = db_instance.model.Availability try: getData = read_availability(liveUUID) if getData['max_slots'] > getData['booked_slots']: lives = availability.query(t_availability).filter( t_availability.LiveUUID == liveUUID).update({ 'BookedSlots': getData['booked_slots'] + 1, 'LastUpdateDatetime': datetime.datetime.now(), }) if lives: availability.commit() return True else: raise ConflictException( 'There are conflicts with the requested Id ' + liveUUID + " in slot booking") else: return False except ConflictException as ex: print(str(ex)) availability.rollback() raise ConflictException(str(ex)) except exc.NoResultFound as ex: print(str(ex)) raise NotFoundException(str(ex)) except Exception as ex: print(str(ex)) availability.rollback() raise ServerException(str(ex))
def delete(image_uuid): db_instance = Db() session = db_instance.session t_image = db_instance.model.Image try: query = session.query(t_image).filter( t_image.RefUUID == image_uuid).one() os.remove(query.Uri) # Removing from local directory sessions = session.query(t_image).filter( t_image.RefUUID == image_uuid).delete() # Removing from database if sessions: session.commit() return 'Successfully deleted' except NotFoundException as ex: print(str(ex)) raise NotFoundException('Image UUID' + image_uuid + " not found") except FileNotFoundError as ex: sessions = session.query(t_image).filter( t_image.RefUUID == image_uuid).delete() if sessions: session.commit() raise FileNotFound('Image not found on server') except Exception as ex: print(str(ex)) raise ServerException(str(ex))
def write(image_file, session_uuid): db_instance = Db() image_table = db_instance.model.Image sql_image_table = _populate_image_model(image_table, session_uuid) session = db_instance.session try: session.add(sql_image_table) session.commit() url = os.path.join(basedir, 'static/' + session_uuid) if not os.path.exists(url): os.makedirs(os.path.join(basedir, 'static/' + session_uuid)) image_file.save(url + '/' + sql_image_table.RefUUID + '.jpg') else: image_file.save(url + '/' + sql_image_table.RefUUID + '.jpg') return {'image_uuid': sql_image_table.RefUUID} except IntegrityError as ex: print(str(ex)) session.rollback() raise ServerException(str(ex)) except Exception as ex: print(str(ex)) session.rollback() raise ServerException(str(ex))
def edit(liveUUID, dt_from, dt_to, presenter_uuid, description, session_uuid, hashtags): db_instance = Db() session = db_instance.session t_live = db_instance.model.Live t_liveTag = db_instance.model.LiveTag session_table = db_instance.model.Session try: language = session.query(session_table).filter( session_table.UUID == session_uuid).one().LanguageISO except exc.NoResultFound as ex: print(ex) raise NotFoundException('No such session found with UUID: ' + session_uuid) try: lives = session.query(t_live).filter(t_live.UUID == liveUUID).update({ 'StartAtGMT': dt_from, 'EndsAtGMT': dt_to, 'PresenterUUID': presenter_uuid, 'SessionUUID': session_uuid, 'LanguageISO': language, 'Description': description, 'LastUpdateDatetime': datetime.datetime.now(), }) hashLives = session.query(t_liveTag).filter( t_liveTag.LiveUUID == liveUUID).update({ 'Hashtag': ','.join(hashtags) if hashtags is not None else '', 'LanguageISO': language }) if lives and hashLives: session.commit() return 'Live updated' else: raise ConflictException( 'There are conflicts with the requested Id ' + liveUUID) except ConflictException as ex: print(str(ex)) session.rollback() raise ConflictException(str(ex)) except exc.NoResultFound as ex: print(str(ex)) raise NotFoundException('No such live found with UUID: ' + liveUUID) except Exception as ex: print(str(ex)) session.rollback() raise ServerException(str(ex))
def read_lives(session_uuid, search): db_instance = Db() session = db_instance.session t_live = db_instance.model.Live t_live_tag = db_instance.model.LiveTag t_session = db_instance.model.Session try: results = [] query = session.query(t_live).join(t_live_tag) if search is None: lives = query.filter(t_live.SessionUUID == session_uuid) else: lives = query.filter( t_live.SessionUUID == session_uuid, or_( t_live.Description.contains(search), t_live_tag.Hashtag.contains(search), and_( or_(t_session.Name.contains(search), t_session.Description.contains(search)), t_session.UUID == t_live.SessionUUID))) if lives: for live in lives: hashtags = live.LiveTag result = { 'from': live.StartAtGMT.strftime("%Y-%m-%dT%H:%M:%S.%fZ"), 'to': live.EndsAtGMT.strftime("%Y-%m-%dT%H:%M:%S.%fZ"), 'hashtags': hashtags[0].Hashtag.split(',') if hashtags[0].Hashtag != '' else [], 'description': live.Description, 'presenter_uuid': live.PresenterUUID, 'uuid': live.UUID, 'language': live.LanguageISO, 'session_uuid': live.SessionUUID, } results.append(result) return results except Exception as ex: print(str(ex)) raise ServerException(str(ex))
def read_sessions(search): db_instance = Db() session = db_instance.session t_session = db_instance.model.Session t_session_tag = db_instance.model.SessionTag t_category = db_instance.model.Categories try: results = [] query = session.query(t_session).join(t_session_tag).join(t_category) if search is None: sessions = query.all() else: sessions = query.filter( or_(t_session.Name.contains(search), t_session.Description.contains(search), t_session_tag.Hashtag.contains(search))) if sessions: for each_session in sessions: hashtags = each_session.SessionTag category = each_session.Categories result = { 'name': each_session.Name, 'category': { "value": category.Value, "UUID": category.UUID }, 'hashtags': hashtags[0].Hashtag.split(',') if len(hashtags[0].Hashtag) > 0 else '', 'description': each_session.Description, 'language_is': each_session.LanguageISO, 'creator_uuid': each_session.CreatorUUID, 'tokens': each_session.Tokens, 'session_uuid': each_session.UUID, 'created_at': each_session.CreationDateTime.strftime( "%Y-%m-%dT%H:%M:%SZ") } results.append(result) return results except Exception as ex: print(str(ex)) raise ServerException(str(ex))
def read_categories(category_languageISO): db_instance = Db() session = db_instance.session t_categories = db_instance.model.Categories try: result = session.query(t_categories).filter( t_categories.LanguageISO == category_languageISO).all() return result except exc.NoResultFound as ex: print(str(ex)) raise NotFoundException('No categories found with ' + category_languageISO) except Exception as ex: print(str(ex)) raise ServerException(str(ex))
def read_availability(liveUUID): db_instance = Db() availability = db_instance.session t_availability = db_instance.model.Availability try: results = dict() availabilities = availability.query(t_availability).filter(t_availability.LiveUUID == liveUUID).one() results['live_uuid'] = availabilities.LiveUUID results['max_slots'] = availabilities.MaxSlots results['booked_slots'] = availabilities.BookedSlots return results except exc.NoResultFound as ex: print(str(ex)) raise NotFoundException(liveUUID + ' Live ID not found') except Exception as ex: print(str(ex)) raise ServerException(str(ex))
def delete(liveUUID): db_instance = Db() session = db_instance.session t_live = db_instance.model.Live try: live = session.query(t_live).filter(t_live.UUID == liveUUID).delete() if live: session.commit() return 'Live deleted' else: raise NotFoundException('Live UUID ' + liveUUID + " not found") except NotFoundException as ex: print(str(ex)) raise NotFoundException('Live UUID ' + liveUUID + " not found") except Exception as ex: print(str(ex)) raise ServerException(str(ex))
def writedt_to_live_tag(live_UUID, hashtags, languageISO): db_instance = Db() liveTag_table = db_instance.model.LiveTag sql_liveTag_table = _populate_liveTag_model(liveTag_table, live_UUID, hashtags, languageISO) liveTag = db_instance.session try: liveTag.add(sql_liveTag_table) liveTag.commit() return True except IntegrityError as ex: print(str(ex)) liveTag.rollback() raise IntegrityException(str(ex)) except Exception as ex: print(str(ex)) liveTag.rollback() raise ServerException(str(ex))
def write_to_session_tag(session_UUID, hashtags, languageISO): db_instance = Db() sessionTag_table = db_instance.model.SessionTag sql_sessionTag_table = _populate_session_tag_model(sessionTag_table, session_UUID, hashtags, languageISO) sessionTag = db_instance.session try: sessionTag.add(sql_sessionTag_table) sessionTag.commit() except IntegrityError as ex: print(str(ex)) sessionTag.rollback() raise ServerException(str(ex)) except Exception as ex: print(str(ex)) sessionTag.rollback() raise ServerException(str(ex))
def delete(sessionUUID): db_instance = Db() session = db_instance.session t_session = db_instance.model.Session try: sessions = session.query(t_session).filter( t_session.UUID == sessionUUID).delete() if sessions: session.commit() return 'Successfully deleted' else: raise NotFoundException('Session UUID ' + sessionUUID + " not found") except NotFoundException as ex: print(str(ex)) raise NotFoundException('Session UUID ' + sessionUUID + " not found") except Exception as ex: print(str(ex)) raise ServerException(str(ex))
def write(dt_from, dt_to, presenter_uuid, description, session_uuid, hashtags): db_instance = Db() session = db_instance.session live_table = db_instance.model.Live session_table = db_instance.model.Session try: language = session.query(session_table).filter( session_table.UUID == session_uuid).one() except exc.NoResultFound as ex: print(str(ex)) session.rollback() raise NotFoundException('No such session found with UUID: ' + session_uuid) sql_live_table = _populate_live_model(live_table, dt_from, dt_to, presenter_uuid, description, language.LanguageISO, session_uuid) try: session.add(sql_live_table) if writedt_to_live_tag(sql_live_table.UUID, hashtags, language.LanguageISO): session.commit() return {'live_uuid': sql_live_table.UUID} else: raise ConflictException( 'There are conflicts, please contact support') except ConflictException as ex: print(str(ex)) session.rollback() raise ConflictException(str(ex)) except IntegrityError as ex: print(str(ex)) session.rollback() raise IntegrityException('Request data not in proper format') except Exception as ex: print(str(ex)) session.rollback() raise ServerException(str(ex))
def write(name, category, hashtags, tokens, description, language_iso, creator_uuid): db_instance = Db() session_table = db_instance.model.Session sql_session_table = _populate_session_model(session_table, name, tokens, category['uuid'], creator_uuid, description, language_iso) session = db_instance.session try: session.add(sql_session_table) session.commit() write_to_session_tag(sql_session_table.UUID, hashtags, language_iso) return {'session_uuid': sql_session_table.UUID} except IntegrityError as ex: print(str(ex)) session.rollback() raise ServerException(str(ex)) except Exception as ex: print(str(ex)) session.rollback() raise ServerException(str(ex))
def write(liveUUID, max_slots): db_instance = Db() availability_table = db_instance.model.Availability sql_availability_table = _populate_availability_model( availability_table, liveUUID, max_slots, 0) availability = db_instance.session try: availability.add(sql_availability_table) availability.commit() return { 'ref_uuid': sql_availability_table.LiveUUID, "max_slots": max_slots } except IntegrityError as ex: print(str(ex)) availability.rollback() raise IntegrityException(str(ex)) except Exception as ex: print(str(ex)) availability.rollback() raise ServerException(str(ex))
def edit(sessionUUID, name, tokens, category, hashtags, description, LanguageISO, creatorUUID): db_instance = Db() session = db_instance.session t_session = db_instance.model.Session t_sessionTag = db_instance.model.SessionTag try: sessions = session.query(t_session).filter(t_session.UUID == sessionUUID, t_session.CreatorUUID == creatorUUID).update( { 'Name': name, 'Tokens': tokens, 'Category': category['uuid'], 'LastUpdateDatetime': datetime.datetime.now(), 'LanguageISO': LanguageISO, 'Description': description, }) hashSessions = session.query(t_sessionTag).filter(t_sessionTag.SessionUUID == sessionUUID).update({ 'Hashtag': ','.join(hashtags) if len(hashtags) > 0 else '', 'LanguageISO': LanguageISO if LanguageISO != '' else 'en' }) if sessions and hashSessions: session.commit() return 'Session updated' else: raise ConflictException('There are conflicts with the requested Id ' + sessionUUID) except ConflictException as ex: print(str(ex)) session.rollback() raise ConflictException(str(ex)) except exc.NoResultFound as ex: print(str(ex)) raise NotFoundException(str(ex)) except Exception as ex: print(str(ex)) session.rollback() raise ServerException(str(ex))
def read_images(sessionUUID): db_instance = Db() session = db_instance.session t_image = db_instance.model.Image try: result = [] query = session.query(t_image).filter( t_image.SessionUUID == sessionUUID).all() for image in query: result.append({ "uuid": image.RefUUID, "uri": image.Uri, "session_uuid": image.SessionUUID }) return result except exc.NoResultFound as ex: print(str(ex)) raise NotFoundException('Session Id ' + sessionUUID + " not found") except Exception as ex: print(str(ex)) raise ServerException(str(ex))
def init_app(flask_app): flask_app.config.from_object(config.DEVConfig) db_instance = Db(flask_app) print('DB Connection: ' + str(db_instance)) print(db_instance.engine.table_names()) print(dir(db_instance.model))