def add_booth(self, payloads): response = ResponseBuilder() booth_exists = db.session.query( db.exists().where(Booth.user_id == payloads['user_id'])).scalar() if booth_exists: return response.set_data(payloads).build() self.booth = Booth() self.booth.name = payloads['booth_name'] self.booth.user_id = payloads['user_id'] self.booth.stage_id = payloads['stage_id'] self.booth.points = payloads['points'] self.booth.summary = payloads['summary'] db.session.add(self.booth) try: db.session.commit() data = self.booth.as_dict() user_booth = UserBooth() user_booth.user_id = data['user_id'] user_booth.booth_id = data['id'] db.session.add(user_booth) db.session.commit() return response.set_data(data).build() except SQLAlchemyError as e: data = e.orig.args return response.set_data(data).set_error(True).build()
def create(self, payloads): response = ResponseBuilder() self.model_booth = Booth() self.model_booth.name = payloads['name'] self.model_booth.user_id = payloads['user_id'] self.model_booth.stage_id = payloads['stage_id'] self.model_booth.points = payloads['points'] self.model_booth.summary = payloads['summary'] self.model_booth.logo_url = payloads['logo_url'] db.session.add(self.model_booth) try: db.session.commit() data = self.model_booth.as_dict() return response.set_data(data).build() except SQLAlchemyError as e: data = e.orig.args return response.set_data(data).set_error(True).build()
def purchase(self, payloads): response = ResponseBuilder() partner = db.session.query(Partner).filter_by( id=payloads['partner_id']).first() self.model_booth = Booth() self.model_booth.name = partner.name self.model_booth.user_id = None self.model_booth.stage_id = None self.model_booth.points = 0 self.model_booth.summary = '' self.model_booth.logo_url = partner.photo db.session.add(self.model_booth) try: db.session.commit() data = self.model_booth.as_dict() return response.set_data(partner.as_dict()).build() except SQLAlchemyError as e: data = e.orig.args return response.set_data(data).set_error(True).build()
def create_booth(self, user, type): booth = Booth() booth.name = 'Your booth name here' booth.user_id = user.id booth.points = 0 booth.summary = '' booth.type = type booth.logo_url = None booth.stage_id = None db.session.add(booth) db.session.commit() userbooth = UserBooth() userbooth.user_id = user.id userbooth.booth_id = booth.id db.session.add(userbooth) db.session.commit()
def run(): """ Create 4 Users seeds """ # create admin admin = User() admin.first_name = 'Admin' admin.last_name = 'Herman' admin.email = '*****@*****.**' admin.username = '******' admin.hash_password('supersecret') admin.role_id = 1 db.session.add(admin) # create ganesh ganesh = User() ganesh.first_name = 'Ganesh' ganesh.last_name = 'Cauda' ganesh.email = '*****@*****.**' ganesh.username = '******' ganesh.hash_password('supersecret') ganesh.role_id = 7 ganesh.points = 0 db.session.add(ganesh) # create erdi erdi = User() erdi.first_name = 'Erdi' erdi.last_name = 'yansah' erdi.email = '*****@*****.**' erdi.username = '******' erdi.hash_password('supersecret') erdi.role_id = 3 db.session.add(erdi) # create mgufrone guffy = User() guffy.first_name = 'Mochamad' guffy.last_name = 'Gufrone' guffy.email = '*****@*****.**' guffy.username = '******' guffy.hash_password('supersecret') guffy.role_id = 4 db.session.add(guffy) db.session.commit() # create booth for erdi erdi_booth = Booth() erdi_booth.user_id = 3 erdi_booth.points = 10000 erdi_booth.stage = None erdi_booth.summary = 'Promoting Mytrhil.js to Indonesia' db.session.add(erdi_booth) # create speaker for guffy guffy_speaker = Speaker() guffy_speaker.user_id = 4 guffy_speaker.information = '-' guffy_speaker.summary = 'code 24.1 hours' guffy_speaker.job = 'COO & Senior software developer at Refactory' db.session.add(guffy_speaker) db.session.commit()
class BoothService(BaseService): def __init__(self, perpage): self.perpage = perpage self.headers = { 'Accept': 'application/json', 'Content-Type': 'application/json', 'qiscus_sdk_secret': QISCUS['SDK_SECRET'] } def get(self, request): self.total_items = Booth.query.count() if request.args.get('page'): self.page = request.args.get('page') else: self.perpage = self.total_items self.page = 1 self.base_url = request.base_url paginate = super().paginate(db.session.query(Booth)) paginate = super().include(['user', 'stage']) for row in paginate['data']: row['logo_url'] = Helper().url_helper( row['logo_url'], current_app.config['GET_DEST'] ) if row[ 'logo_url'] else "https://museum.wales/media/40374/thumb_480/empty-profile-grey.jpg" response = ResponseBuilder() result = response.set_data(paginate['data']).set_links( paginate['links']).build() return result def show(self, id): # get the booth id response = ResponseBuilder() booth = db.session.query(Booth).filter_by(id=id).first() if booth is None: data = {'user_exist': True} return response.set_data(data).set_error(True).set_message( 'booth not found').build() data = booth.as_dict() data['user'] = booth.user.include_photos().as_dict() if data['logo_url']: data['logo_url'] = Helper().url_helper( data['logo_url'], current_app.config['GET_DEST']) user_booth = db.session.query(UserBooth).filter_by(booth_id=id).all() data['members'] = [] for user in user_booth: user_id = user.as_dict()['user_id'] user = db.session.query(User).filter_by(id=user_id).first() data['members'].append(user.include_photos().as_dict()) data['stage'] = booth.stage.as_dict() if booth.stage else None return response.set_data(data).build() def update(self, payloads, booth_id): response = ResponseBuilder() try: self.model_booth = db.session.query(Booth).filter_by(id=booth_id) self.model_booth.update({ 'name': payloads['name'], 'stage_id': payloads['stage_id'], 'points': payloads['points'], 'summary': payloads['summary'], 'url': payloads['url'] }) if payloads['logo']: photo = self.save_file(payloads['logo'], booth_id) self.model_booth.update({'logo_url': photo}) db.session.commit() data = self.model_booth.first().as_dict() data['user'] = self.model_booth.first().user.include_photos( ).as_dict() data['stage'] = self.model_booth.first().stage.as_dict( ) if payloads['stage_id'] is not None else None return response.set_data(data).build() except SQLAlchemyError as e: data = e.orig.args return response.set_error(True).set_data(data).set_message( 'sql error').build() def save_file(self, file, id=None): if file and Helper().allowed_file( file.filename, current_app.config['ALLOWED_EXTENSIONS']): filename = secure_filename(file.filename) filename = Helper().time_string() + "_" + file.filename.replace( " ", "_") file.save( os.path.join(current_app.config['POST_BOOTH_PHOTO_DEST'], filename)) if id: temp_booth = db.session.query(Booth).filter_by(id=id).first() booth_photo = temp_booth.as_dict() if temp_booth else None if booth_photo is not None and booth_photo[ 'logo_url'] is not None: Helper().silent_remove(current_app.config['STATIC_DEST'] + booth_photo['logo_url']) return current_app.config['SAVE_BOOTH_PHOTO_DEST'] + filename else: return None def update_logo(self, payloads, booth_id): response = ResponseBuilder() file = request.files['image_data'] if file and Helper().allowed_file( file.filename, current_app.config['ALLOWED_EXTENSIONS']): try: if not os.path.exists( current_app.config['POST_BOOTH_PHOTO_DEST']): os.makedirs(current_app.config['POST_BOOTH_PHOTO_DEST']) filename = Helper().time_string( ) + "_" + file.filename.replace(" ", "_") file.save( os.path.join(current_app.config['POST_BOOTH_PHOTO_DEST'], filename)) newUrl = current_app.config['SAVE_BOOTH_PHOTO_DEST'] + filename self.model_booth = db.session.query(Booth).filter_by( id=booth_id) self.booth_logo = db.session.query(Booth).filter_by( id=booth_id).first() # check in case current profile don't have any logo picture, so need to remove if self.booth_logo is not None and self.booth_logo.as_dict( )['logo_url'] is not None: Helper().silent_remove( current_app.config['STATIC_DEST'] + self.booth_logo.as_dict()['logo_url']) self.model_booth.update({ 'logo_url': newUrl, 'updated_at': datetime.datetime.now() }) db.session.commit() data = self.model_booth.first().as_dict() data['logo_url'] = Helper().url_helper( data['logo_url'], current_app.config['GET_DEST']) return response.set_data(data).set_message( 'Booth logo updated successfully').build() except SQLAlchemyError as e: data = e.orig.args return response.set_error(True).set_data(None).set_message( data).build() def create(self, payloads): response = ResponseBuilder() self.model_booth = Booth() self.model_booth.name = payloads['name'] self.model_booth.user_id = payloads['user_id'] self.model_booth.stage_id = payloads['stage_id'] self.model_booth.points = payloads['points'] self.model_booth.summary = payloads['summary'] self.model_booth.type = payloads['type'] self.model_booth.logo_url = payloads['logo_url'] self.model_booth.url = payloads['url'] db.session.add(self.model_booth) try: db.session.commit() data = self.model_booth.as_dict() return response.set_data(data).build() except SQLAlchemyError as e: data = e.orig.args return response.set_data(data).set_error(True).build() def purchase(self, payloads): response = ResponseBuilder() partner = db.session.query(Partner).filter_by( id=payloads['partner_id']).first() self.model_booth = Booth() self.model_booth.name = partner.name self.model_booth.user_id = None self.model_booth.stage_id = None self.model_booth.points = 0 self.model_booth.summary = '' self.model_booth.logo_url = partner.photo db.session.add(self.model_booth) try: db.session.commit() data = self.model_booth.as_dict() return response.set_data(partner.as_dict()).build() except SQLAlchemyError as e: data = e.orig.args return response.set_data(data).set_error(True).build() def generate_room(self, id): response = ResponseBuilder() channel_id = None booth, user = db.session.query( Booth, User).join(User).filter(Booth.id == id).first() name = booth.name if booth.name is not None else 'Booth Chatroom' logo = booth.logo_url email = self.qiscus_register(user, booth) endpoint = QISCUS['BASE_URL'] + 'create_room' payloads = { 'name': name, 'participants': [email], 'creator': email, 'avatar_url': logo } result = requests.post(endpoint, headers=self.headers, json=payloads) payload = result.json() if 'error' not in payload: channel_id = payload['results']['room_id_str'] else: return response.set_error(True).set_data( payload['error']).set_message('qiscus service error').build() try: self.model_booth = db.session.query(Booth).filter_by(id=id) self.model_booth.update({ 'channel_id': channel_id, }) db.session.commit() data = self.model_booth.first().as_dict() return response.set_data(data).build() except SQLAlchemyError as e: data = e.orig.args return response.set_error(True).set_data(data).set_message( 'sql error').build() def qiscus_register(self, user, booth): endpoint = QISCUS['BASE_URL'] + 'login_or_register' payloads = { 'email': user.email, 'password': user.email, 'username': user.username, 'avatar_url': booth.logo_url } result = requests.post(endpoint, headers=self.headers, json=payloads) payload = result.json() return payload['results']['user']['email']
class GrantroleService(): def update(self, payloads, id): response = ResponseBuilder() try: self.model_grant_role = db.session.query(User).filter_by(id=id) self.model_grant_role.update({'role_id': payloads['role_id']}) db.session.commit() data = self.model_grant_role.first().as_dict() return response.set_data(data).set_message( 'User updated successfully').set_error(False).build() except SQLAlchemyError as e: return response.set_data( e.orig.args).set_message('SQL error').set_error(True).build() def add_attendee(self, payloads): response = ResponseBuilder() self.attendee = Attendee() self.attendee.user_id = payloads['user_id'] self.attendee.points = payloads['points'] db.session.add(self.attendee) try: db.session.commit() data = self.attendee.as_dict() return response.set_data(data).build() except SQLAlchemyError as e: data = e.orig.args return response.set_data(data).set_error(True).build() def add_booth(self, payloads): response = ResponseBuilder() booth_exists = db.session.query( db.exists().where(Booth.user_id == payloads['user_id'])).scalar() if booth_exists: return response.set_data(payloads).build() self.booth = Booth() self.booth.name = payloads['booth_name'] self.booth.user_id = payloads['user_id'] self.booth.stage_id = payloads['stage_id'] self.booth.points = payloads['points'] self.booth.summary = payloads['summary'] db.session.add(self.booth) try: db.session.commit() data = self.booth.as_dict() user_booth = UserBooth() user_booth.user_id = data['user_id'] user_booth.booth_id = data['id'] db.session.add(user_booth) db.session.commit() return response.set_data(data).build() except SQLAlchemyError as e: data = e.orig.args return response.set_data(data).set_error(True).build() def add_speaker(self, payloads): response = ResponseBuilder() self.speaker = Speaker() self.speaker.user_id = payloads['user_id'] self.speaker.job = payloads['job'] self.speaker.summary = payloads['summary'] self.speaker.information = payloads['information'] self.speaker.type = payloads['type'] db.session.add(self.speaker) try: db.session.commit() data = self.speaker.as_dict() return response.set_data(data).build() except SQLAlchemyError as e: data = e.orig.args return response.set_data(data).set_error(True).build() def add_ambassador(self, payloads): response = ResponseBuilder() self.ambassador = Ambassador() self.ambassador.user_id = payloads['user_id'] self.ambassador.informations = payloads['infrormation'] self.ambassador.institution = payloads['institution'] db.session.add(self.ambassador) try: db.session.commit() data = self.ambassador.as_dict() return response.set_data(data).build() except SQLAlchemyError as e: data = e.orig.args return response.set_data(data).set_error(True).build()
class BoothService(BaseService): def __init__(self, perpage): self.perpage = perpage def get(self, request): self.total_items = Booth.query.count() if request.args.get('page'): self.page = request.args.get('page') else: self.perpage = self.total_items self.page = 1 self.base_url = request.base_url paginate = super().paginate(db.session.query(Booth)) paginate = super().include(['user', 'stage']) for row in paginate['data']: row['logo_url'] = Helper().url_helper( row['logo_url'], current_app.config['GET_DEST'] ) if row[ 'logo_url'] else "https://museum.wales/media/40374/thumb_480/empty-profile-grey.jpg" response = ResponseBuilder() result = response.set_data(paginate['data']).set_links( paginate['links']).build() return result def show(self, id): # get the booth id response = ResponseBuilder() booth = db.session.query(Booth).filter_by(id=id).first() if booth is None: data = {'user_exist': True} return response.set_data(data).set_error(True).set_message( 'booth not found').build() data = booth.as_dict() data['user'] = booth.user.include_photos().as_dict() data['logo_url'] = Helper().url_helper(data['logo_url'], current_app.config['GET_DEST']) user_booth = db.session.query(UserBooth).filter_by(booth_id=id).all() data['members'] = [] for user in user_booth: user_id = user.as_dict()['user_id'] user = db.session.query(User).filter_by(id=user_id).first() data['members'].append(user.include_photos().as_dict()) data['stage'] = booth.stage.as_dict() if booth.stage else None return response.set_data(data).build() def update(self, payloads, booth_id): response = ResponseBuilder() try: self.model_booth = db.session.query(Booth).filter_by(id=booth_id) self.model_booth.update({ 'name': payloads['name'], 'stage_id': payloads['stage_id'], 'points': payloads['points'], 'summary': payloads['summary'] }) db.session.commit() data = self.model_booth.first().as_dict() data['user'] = self.model_booth.first().user.include_photos( ).as_dict() data['stage'] = self.model_booth.first().stage.as_dict( ) if payloads['stage_id'] is not None else None return response.set_data(data).build() except SQLAlchemyError as e: data = e.orig.args return response.set_error(True).set_data(data).set_message( 'sql error').build() def update_logo(self, payloads, booth_id): response = ResponseBuilder() file = request.files['image_data'] if file and Helper().allowed_file( file.filename, current_app.config['ALLOWED_EXTENSIONS']): try: if not os.path.exists( current_app.config['POST_BOOTH_PHOTO_DEST']): os.makedirs(current_app.config['POST_BOOTH_PHOTO_DEST']) filename = Helper().time_string( ) + "_" + file.filename.replace(" ", "_") file.save( os.path.join(current_app.config['POST_BOOTH_PHOTO_DEST'], filename)) newUrl = current_app.config['SAVE_BOOTH_PHOTO_DEST'] + filename self.model_booth = db.session.query(Booth).filter_by( id=booth_id) self.booth_logo = db.session.query(Booth).filter_by( id=booth_id).first() # check in case current profile don't have any logo picture, so need to remove if self.booth_logo is not None and self.booth_logo.as_dict( )['logo_url'] is not None: Helper().silent_remove( current_app.config['STATIC_DEST'] + self.booth_logo.as_dict()['logo_url']) self.model_booth.update({ 'logo_url': newUrl, 'updated_at': datetime.datetime.now() }) db.session.commit() data = self.model_booth.first().as_dict() data['logo_url'] = Helper().url_helper( data['logo_url'], current_app.config['GET_DEST']) return response.set_data(data).set_message( 'Booth logo updated successfully').build() except SQLAlchemyError as e: data = e.orig.args return response.set_error(True).set_data(None).set_message( data).build() def create(self, payloads): response = ResponseBuilder() self.model_booth = Booth() self.model_booth.name = payloads['name'] self.model_booth.user_id = payloads['user_id'] self.model_booth.stage_id = payloads['stage_id'] self.model_booth.points = payloads['points'] self.model_booth.summary = payloads['summary'] self.model_booth.logo_url = payloads['logo_url'] db.session.add(self.model_booth) try: db.session.commit() data = self.model_booth.as_dict() return response.set_data(data).build() except SQLAlchemyError as e: data = e.orig.args return response.set_data(data).set_error(True).build() def purchase(self, payloads): response = ResponseBuilder() partner = db.session.query(Partner).filter_by( id=payloads['partner_id']).first() self.model_booth = Booth() self.model_booth.name = partner.name self.model_booth.user_id = None self.model_booth.stage_id = None self.model_booth.points = 0 self.model_booth.summary = '' self.model_booth.logo_url = partner.photo db.session.add(self.model_booth) try: db.session.commit() data = self.model_booth.as_dict() return response.set_data(partner.as_dict()).build() except SQLAlchemyError as e: data = e.orig.args return response.set_data(data).set_error(True).build()