Ejemplo n.º 1
0
    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()
Ejemplo n.º 2
0
    def postIncludes(self, includes):
        response = ResponseBuilder()
        user_id = self.model_user.as_dict()['id']

        entityModel = eval(includes['name'])()
        entityModel.user_id = user_id
        for key in includes:
            if key is not 'name':
                setattr(entityModel, key, includes[key])
        db.session.add(entityModel)
        try:
            db.session.commit()

            if includes['name'] == 'Booth':
                user_booth = UserBooth()
                user_booth.user_id = user_id
                user_booth.booth_id = entityModel.as_dict()['id']
                db.session.add(user_booth)
                db.session.commit()

            return response.set_data(None).build()
        except SQLAlchemyError as e:
            data = e.args
            return response.set_message(data).set_error(True).set_data(
                None).build()
Ejemplo n.º 3
0
    def update(self, code, user):
        response = ResponseBuilder()
        _result = {}
        _result['user'] = user
        token = db.session.query(AccessToken).filter_by(
            user_id=user['id']).first().as_dict()
        raw_user = db.session.query(User).filter_by(id=user['id'])
        raw_redeem_code = db.session.query(RedeemCode).filter_by(code=code)
        if raw_redeem_code.first() is None:
            return response.set_data(None).set_error(True).set_message(
                'code not found').build()
        redeem_code = raw_redeem_code.first().as_dict()
        if raw_user.first() is None:
            return response.set_data(None).set_error(True).set_message(
                'user not found').build()
        if redeem_code['used'] == 1:
            return response.set_data(None).set_error(True).set_message(
                'code already used').build()

        try:
            if redeem_code['codeable_type'] in ['partner', 'user']:
                # become attendee
                userticket = UserTicket()
                userticket.user_id = user['id']
                userticket.ticket_id = 1
                db.session.add(userticket)
                db.session.commit()
            elif redeem_code['codeable_type'] == 'booth':
                # get booth of the code
                booth = db.session.query(Booth).filter_by(
                    id=redeem_code['codeable_id']).first().as_dict()
                # become member of booth
                user_booth = UserBooth()
                user_booth.user_id = user['id']
                user_booth.booth_id = booth['id']
                db.session.add(user_booth)
                user['role_id'] = 3
                db.session.commit()
                _result['user']['booth'] = booth
            raw_redeem_code.update({'used': True})
            raw_user.update({'role_id': user['role_id']})
            db.session.commit()
            token_payload = {
                'access_token': token['access_token'],
                'refresh_token': token['refresh_token']
            }
            return response.set_data(token_payload).set_included(
                _result['user']).set_message('Redeem code 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()
Ejemplo n.º 4
0
 def create_booth(self, user):
     booth = Booth()
     booth.name = 'Your booth name here'
     booth.user_id = user.id
     booth.points = 0
     booth.summary = ''
     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()