Ejemplo n.º 1
0
    def delete_from_json(cls, data):
        if isAllInData(['type', 'access_token'],
                       data) and data['type'] in cls.allowed_types:
            access_token = data['access_token']
            type = data['type']

            with DBConnection() as session:
                token = session.db.query(EntityToken).filter_by(
                    access_token=access_token, type=type).first()
                if token:
                    if type in cls.revoke_types:
                        with open("client_config.json") as client_config_file:
                            client_config = json.load(client_config_file)
                            client = client_config['clients'][type]
                        requests.post(client['revoke_token_url'],
                                      params={'token': access_token},
                                      headers={
                                          'content-type':
                                          'application/x-www-form-urlencoded'
                                      })

                    session.db.delete(token)
                    session.db.commit()

                    return {'token': access_token}, falcon.HTTP_200
                return {
                    'error': 'Invalid access token supplied'
                }, falcon.HTTP_400
        return {'error': 'Invalid parameters supplied'}, falcon.HTTP_400
Ejemplo n.º 2
0
    def add_from_json(cls, data, prop):

        PROPNAME_MAPPING = EntityProp.map_name_id()

        eid = None

        if isAllInData(['text', 'userid', 'id'], data):
            text = data['text']
            userid = data['userid']
            _id = data['id']

            from each.Prop.PropComment import PropComment
            comments = PropComment.get_comment_user_related(
                _id, PROPNAME_MAPPING[prop], userid)

            if not len(comments):
                new_entity = EntityComment(userid, text)
                eid = new_entity.add()

                PropComment(_id, PROPNAME_MAPPING[prop], eid).add()

            else:
                eid = comments[0]['eid']

        return eid
Ejemplo n.º 3
0
    def update_from_query(cls, data):
        if isAllInData(['type', 'access_token'],
                       data) and data['type'] in cls.allowed_types:
            type = data['type']
            access_token = data['access_token']

            token = EntityToken.get().filter_by(access_token=access_token,
                                                type=type).first()
            if token:
                user = EntityUser.get().filter_by(eid=token.user_id).first()
                if user:

                    res_data, res_status = cls.get_info_api(access_token, type)
                    if res_status != falcon.HTTP_200:
                        with DBConnection() as session:
                            session.db.delete(token)
                            session.db.commit()
                        return res_data, res_status

                    EntityUser.update_user(token.user_id, res_data)

                    return token, user, None, falcon.HTTP_200
            return None, None, {
                'error': 'Invalid access token supplied'
            }, falcon.HTTP_400
        return None, None, {
            'error': 'Invalid parameters supplied'
        }, falcon.HTTP_400
Ejemplo n.º 4
0
    def add_from_query(cls, data):
        if isAllInData(['type', 'redirect_uri', 'code'],
                       data) and data['type'] in cls.allowed_types:
            type = data['type']
            redirect_uri = data['redirect_uri']
            code = data['code']

            with open("client_config.json") as client_config_file:
                client_config = json.load(client_config_file)
                client = client_config['clients'][type]

            request_data = {
                'client_id': client['client_id'],
                'client_secret': client['client_secret'],
                'code': code,
                'redirect_uri': redirect_uri
            }
            if type in cls.granted_types:
                request_data['grant_type'] = 'authorization_code'

            r = requests.post(client['access_token_url'], data=request_data)
            if r.status_code != 200:
                return r.json(), falcon.__dict__['HTTP_%s' % r.status_code]

            res_data, res_status = cls.get_info_api(r.json()['access_token'],
                                                    type, client)
            if res_status != falcon.HTTP_200:
                return res_data, res_status

            if type == 'vkontakte':
                email = r.json()['email']
            else:
                email = res_data['email']

            user = EntityUser.get().filter_by(email=email, type=type).first()
            if user:
                user_id = user.eid
            else:
                user = EntityUser(type=type, email=email)
                for _ in EntityUser.required_fields:
                    if _ in res_data:
                        user[_] = res_data[_]
                user_id = user.add()

            new_entity = EntityToken(res_data['access_token'], type, user_id)
            new_entity.add()

            return new_entity, user, None, falcon.HTTP_200
        return None, None, {
            'error': 'Invalid parameters supplied'
        }, falcon.HTTP_400
Ejemplo n.º 5
0
    def add_from_json(cls, data):
        def mediaPropMapping(s, _eid, _id, _val, _uid):
            cls.process_media(s, 'image', _uid, _eid, _id, _val)

        PROPNAME_MAPPING = EntityProp.map_name_id()

        eid = None

        PROP_MAPPING = {
            'logo':
            mediaPropMapping,
            'image':
            mediaPropMapping,
            'location':
            lambda s, _eid, _id, _val, _uid: [
                PropLocation(_eid, _id, _).add(session=s, no_commit=True)
                for _ in _val['add']
            ]
        }

        if isAllInData(['name', 'desc', 'ownerid', 'prop'], data):
            create_args = {'ownerid': data['ownerid']}
            for _ in cls.locales:
                create_args['name_%s' %
                            _] = data['name'][_] if _ in data['name'] else ''
                create_args['desc_%s' %
                            _] = data['desc'][_] if _ in data['desc'] else ''
            new_entity = EntityMuseum(**create_args)
            eid = new_entity.add()

            with DBConnection() as session:
                for prop_name, prop_val in data['prop'].items():
                    if prop_name in PROPNAME_MAPPING and prop_name in PROP_MAPPING:
                        PROP_MAPPING[prop_name](session, eid,
                                                PROPNAME_MAPPING[prop_name],
                                                prop_val, eid)
                    else:
                        new_entity.delete(eid)
                        raise Exception(
                            '{%s} not existed property\nPlease use one of:\n%s'
                            % (prop_name, str(PROPNAME_MAPPING)))

                session.db.commit()

        return eid
Ejemplo n.º 6
0
    def add_from_json(cls, data):
        eid = None

        PROPNAME_MAPPING = EntityProp.map_name_id()

        PROP_MAPPING = {
            'image':
                lambda s, _eid, _id, _val, _uid: cls.process_media(s, 'image', _uid, _eid, _id, _val),
            'priority':
                lambda s, _eid, _id, _val, _uid: PropInt(eid, _id, _val).add_or_update(session=s, no_commit=False)
        }
        if isAllInData(['title', 'desc', 'text', 'prop'], data):
            create_args = {}
            if 'prop' in data:
                for _ in cls.locales:
                    create_args['title_%s' % _] = data['title'][_] if _ in data['title'] else ''
                    create_args['desc_%s' % _] = data['desc'][_] if _ in data['desc'] else ''
                    create_args['text_%s' % _] = data['text'][_] if _ in data['text'] else ''
            new_entity = EntityNews(**create_args)

            eid = new_entity.add()

            try:
                with DBConnection() as session:
                    for prop_name, prop_val in data['prop'].items():
                        if prop_name in PROPNAME_MAPPING and prop_name in PROP_MAPPING:
                            PROP_MAPPING[prop_name](session, eid, PROPNAME_MAPPING[prop_name], prop_val, eid)
                        else:
                            EntityNews.delete(eid)
                            raise Exception('{%s} not existed property\nPlease use one of:\n%s' %
                                        (prop_name, str(PROPNAME_MAPPING)))

                    session.db.commit()
            except Exception as e:
                EntityNews.delete(eid)
                raise Exception('Internal error')

        return eid
Ejemplo n.º 7
0
    def add_from_json(cls, data):
        from each.Prop.PropGame import PropGame
        PROPNAME_MAPPING = EntityProp.map_name_id()

        eid = None

        PROP_MAPPING = {
            'image':
                lambda s, _eid, _id, _val, _uid: cls.process_media(s, 'image', _uid, _eid, _id, _val),
            # in table - eid is museum id, and value is eid of game in each_game table
            'game':
                lambda s, _eid, _id, _val, _uid: PropGame(_val, _id, _eid).add(session=s, no_commit=True),
            'scenario':
                lambda s, _eid, _id, _val, _uid: PropScenario(_eid, _id, EntityScenario.add_by_game_id(_eid))
                                                    .add(session=s, no_commit=True)
        }

        if isAllInData(['name', 'desc', 'ownerid', 'active', 'prop'], data):
            create_args = {'ownerid': data['ownerid'], 'active': data['active']}
            for _ in cls.locales:
                create_args['name_%s' % _] = data['name'][_] if _ in data['name'] else ''
                create_args['desc_%s' % _] = data['desc'][_] if _ in data['desc'] else ''
            new_entity = EntityGame(**create_args)
            eid = new_entity.add()

            with DBConnection() as session:
                data['prop']['scenario'] = ''
                for prop_name, prop_val in data['prop'].items():
                    if prop_name in PROPNAME_MAPPING and prop_name in PROP_MAPPING:
                        PROP_MAPPING[prop_name](session, eid, PROPNAME_MAPPING[prop_name], prop_val, eid)
                    else:
                        new_entity.delete(eid)
                        raise Exception('{%s} not existed property\nPlease use one of:\n%s' %
                                        (prop_name, str(PROPNAME_MAPPING)))

                session.db.commit()

        return eid
Ejemplo n.º 8
0
    def update_from_json(cls, data, prop):
        PROPNAME_MAPPING = EntityProp.map_name_id()

        eid = None

        if isAllInData(['userid', 'id'], data):
            userid = data['userid']
            _id = data['id']

            from each.Prop.PropLike import PropLike
            with DBConnection() as session:
                likes = session.db.query(PropLike, EntityLike).filter(PropLike.eid == _id).\
                    filter(PropLike.propid == PROPNAME_MAPPING[prop]).\
                    filter(PropLike.value == EntityLike.eid).filter(EntityLike.userid == userid).all()

                if len(likes):
                    eid = likes[0][0].value
                    if 'weight' in data:
                        for _ in likes:
                            setattr(_[1], 'weight', data['weight'])
                        session.db.commit()

        return eid
Ejemplo n.º 9
0
    def add_from_json(cls, data, prop):
        PROPNAME_MAPPING = EntityProp.map_name_id()

        eid = None

        if isAllInData(['weight', 'userid', 'id'], data):
            weight = data['weight']
            userid = data['userid']
            _id = data['id']

            from each.Prop.PropLike import PropLike
            likes = PropLike.get_like_user_related(_id, PROPNAME_MAPPING[prop], userid)

            if not len(likes):
                new_entity = EntityLike(userid, weight)
                eid = new_entity.add()

                PropLike(_id, PROPNAME_MAPPING[prop], eid).add()

            else:
                eid = likes[0]['eid']

        return eid
Ejemplo n.º 10
0
    def update_from_json(cls, data, prop):
        PROPNAME_MAPPING = EntityProp.map_name_id()

        eid = None

        if isAllInData(['userid', 'id'], data):
            userid = data['userid']
            _id = data['id']

            from each.Prop.PropComment import PropComment
            with DBConnection() as session:
                comments = session.db.query(PropComment, EntityComment).filter(PropComment.eid == _id). \
                    filter(PropComment.propid == PROPNAME_MAPPING[prop]). \
                    filter(PropComment.value == EntityComment.eid).filter(EntityComment.userid == userid).all()

                if len(comments):
                    eid = comments[0][0].value
                    if 'text' in data:
                        for _ in comments:
                            setattr(_[1], 'text', data['text'])
                        session.db.commit()

        return eid
Ejemplo n.º 11
0
    def drop_from_json(cls, data):
        from each.Prop.PropRun import PropRun

        PROPNAME_MAPPING = EntityProp.map_name_id()

        eid = []

        if isAllInData(['user_id', 'game_ids'], data):

            user = data['user_id']
            games = data['game_ids']

            with DBConnection() as session:
                for g in games:
                    scenario = [_[1] for _ in session.db.query(PropScenario, EntityScenario).
                                filter(PropScenario.eid == g).
                                filter(PropScenario.propid == PROPNAME_MAPPING['scenario']).
                                filter(PropScenario.value == EntityScenario.eid).all()]
                    if len(scenario):
                        ts = time.time()
                        entity = [_[1] for _ in session.db.query(PropRun, EntityRun).filter(PropRun.eid == user).
                                  filter(PropRun.propid == PROPNAME_MAPPING['run']).
                                  filter(PropRun.value == EntityRun.eid).filter(EntityRun.game_id == g).all()]
                        if len(entity):
                            for s in scenario:
                                sc = json.loads(s.json)
                                for _ in entity:
                                    eid.append(_.eid)
                                    if _.bonus == 0:
                                        PropRun.delete_value(_.eid, False)
                                    else:
                                        _.status = 'pass'
                                        _.step_passed = sc['step_count']
                                        _.updated = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M')
                                    session.db.commit()

        return eid
Ejemplo n.º 12
0
    def update_from_json(cls, data):
        from each.Prop.PropRun import PropRun

        def create_new_entity(session):
            new_entity = EntityRun(game)
            n_eid = new_entity.add()
            PropRun(user, PROPNAME_MAPPING['run'], n_eid).add(session=session)
            return [n_eid]

        PROPNAME_MAPPING = EntityProp.map_name_id()

        eid = None

        if isAllInData(['user_id', 'game_id', 'step_passed'], data):

            user = data['user_id']
            game = data['game_id']
            steps = data['step_passed']

            with DBConnection() as session:
                scenario = [_[1] for _ in session.db.query(PropScenario, EntityScenario).
                            filter(PropScenario.eid == game).
                            filter(PropScenario.propid == PROPNAME_MAPPING['scenario']).
                            filter(PropScenario.value == EntityScenario.eid).all()]
                if len(scenario):
                    entity = [_[1] for _ in session.db.query(PropRun, EntityRun).filter(PropRun.eid == user).
                              filter(PropRun.propid == PROPNAME_MAPPING['run']).
                              filter(PropRun.value == EntityRun.eid).filter(EntityRun.game_id == game).all()]
                    if not len(entity):
                        _eid = create_new_entity(session)
                        entity = session.db.query(EntityRun).filter(EntityRun.eid.in_(_eid)).all()
                    times = session.db.query(PropInterval).filter_by(eid=user).all()
                    if not len(times):
                        PropInterval(user, PROPNAME_MAPPING['time_in_game'], datetime.timedelta(seconds=0)). \
                            add(session=session)
                        times = session.db.query(PropInterval).filter_by(eid=user).all()
                    ts = time.time()
                    for s in scenario:
                        sc = json.loads(s.json)
                        for _ in entity:
                            if steps == 0:
                                _.start_time = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
                                d_time = datetime.timedelta(seconds=0)
                            else:
                                d_time = datetime.datetime.fromtimestamp(ts) - \
                                    datetime.datetime.strptime(_.start_time.strftime('%Y-%m-%d %H:%M:%S'),
                                                               '%Y-%m-%d %H:%M:%S')
                            one_week = datetime.timedelta(weeks=1)
                            if d_time >= one_week:
                                if _.bonus == 0:
                                    eid = 0
                                    PropRun.delete_value(_.eid, False)
                                else:
                                    eid = _.eid
                                    _.status = 'pass'
                                    _.step_passed = sc['step_count']
                                    _.updated = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M')
                            else:
                                eid = _.eid
                                _.step_passed = steps
                                if sc['step_count'] == steps:
                                    _.status = 'pass'
                                    if _.bonus == 0:
                                        _.bonus = int(sc['difficulty_bounty'])
                                    if _.best_time == datetime.timedelta(seconds=0) or _.best_time > d_time:
                                        _.best_time = d_time
                                        for t in times:
                                            t.value += d_time
                                else:
                                    _.status = 'process'
                                _.updated = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M')
                            session.db.commit()

        return eid