Ejemplo n.º 1
0
    def test_create_from_entity(self):
        title_entity = self.fixture_with_new_uuid('title')

        new_title = yield TitleStore().create_from_entity(title_entity)
        self.assertEquals(new_title.title_id, title_entity.title_id)

        title = yield TitleStore().get(new_title.uuid)
        self.assertEquals(title.title_id, new_title.title_id)

        title = yield TitleStore().get_by_id(new_title.title_id)
        self.assertEquals(title.uuid, new_title.uuid)
        self.assertEquals(title.published_date,
                          datetime.datetime(2007, 12, 5, 0, 0))
        self.assertEquals(title.tags, [
            'c736b780-11b6-4190-8529-4d89504b76a0',
            'efc5907c-7316-4a36-a910-044c18e39d10',
        ])
Ejemplo n.º 2
0
    def test_add_existed_tag(self):
        title = yield TitleService().create_with_entity(
            self.fixture_with_new_uuid('title'))
        tag_uuids = [
            'c736b780-11b6-4190-8529-4d89504b76a0',
            'efc5907c-7316-4a36-a910-044c18e39d10'
        ]
        add_tag_uuid = tag_uuids[1]
        self.assertEquals(len(title.tags), 2)
        self.assertEquals(title.tags[0], tag_uuids[0])
        self.assertEquals(title.tags[1], tag_uuids[1])

        yield TitleService().add_tag(
            title.uuid,
            add_tag_uuid,
            True,
        )

        title = yield TitleStore().get(title.uuid)
        self.assertEquals(len(title.tags), 2)
Ejemplo n.º 3
0
 def __init__(self):
     super(TitleService, self).__init__()
     self.store = TitleStore()
     self.tag_store = TagStore()
Ejemplo n.º 4
0
class TitleService(BaseService):
    def __init__(self):
        super(TitleService, self).__init__()
        self.store = TitleStore()
        self.tag_store = TagStore()

    @coroutine
    def get_by_id(self, title_id):
        title = yield self.store.get_by_id(title_id)

        logger.info(dict(
            title_id=title_id,
            method='get_by_id',
            result_uuid=None if not title else title.uuid,
        ))
        raise Return(title)

    @coroutine
    def check_duplicates(self, entity):
        title = yield self.get_by_id(entity.title_id)
        raise Return(title is not None)

    @coroutine
    def add_tag(self, title_uuid, tag_uuid, add=True):
        title_uuid = is_valid_uuid_string(title_uuid)
        tag_uuid = is_valid_uuid_string(tag_uuid)

        title = yield self.store.get(title_uuid)

        if not title:
            raise ResourceNotFoundError('Title not found')

        tag_list = list(title.tags)
        if add is True:
            if tag_uuid not in tag_list:
                tag_list.append(tag_uuid)
        else:
            if tag_uuid in tag_list:
                tag_list.remove(tag_uuid)

        update_dict = {'tags': tag_list}
        self.store.update(title_uuid, update_dict)

        logger.info(dict(
            title_uuid=title_uuid,
            tag_uuid=tag_uuid,
            add=add,
            method='add_tag',
        ))
        raise Return()

    @coroutine
    def get_tags_by_title(self, title_uuid):
        log_info = dict(
            uuid=title_uuid,
            method='get_tags_by_title',
        )

        title_uuid = is_valid_uuid_string(title_uuid)

        title = yield self.store.get(title_uuid)
        if not title:
            log_info.update({'error': 'title not found'})
            logger.exception(log_info)
            raise ResourceNotFoundError(log_info.get('error'))

        tag_list = list(getattr(title, 'tags'))

        tags = yield self.tag_store.get_all_by_uuids(tag_list)
        logger.info(log_info)
        raise Return(tags)

    @coroutine
    def get_all_by_tag(self, tag_uuid):
        tag_uuid = is_valid_uuid_string(tag_uuid)

        log_info = dict(
            tag_uuid=tag_uuid,
            method='get_all_by_tag',
        )

        titles = yield self.store.get_all_by_tag(tag_uuid)
        logger.info(log_info)
        raise Return(titles)

    @coroutine
    def get_recentlly_added_titles(self, n):
        titles = yield self.store.get_latest_created(n)
        logger.info(dict(count=n, method='get_recentlly_added_titles'))
        raise Return(titles)
Ejemplo n.º 5
0
 def __init__(self):
     super(UserService, self).__init__()
     self.store = UserStore()
     self.title_store = TitleStore()
     self.star_store = StarStore()
Ejemplo n.º 6
0
class UserService(BaseService):
    def __init__(self):
        super(UserService, self).__init__()
        self.store = UserStore()
        self.title_store = TitleStore()
        self.star_store = StarStore()

    @coroutine
    def get_by_name(self, user_name):
        user = yield self.store.get_by_name(user_name)

        logger.info(dict(
            user_name=user_name,
            method='get_by_name',
            result_uuid=None if not user else user.uuid,
        ))
        raise Return(user)

    @coroutine
    def check_duplicates(self, entity):
        user = yield self.get_by_name(entity.user_name)
        raise Return(user is not None)

    @coroutine
    def like_title(self, user_uuid, title_uuid, like):
        user_uuid = is_valid_uuid_string(user_uuid)
        title_uuid = is_valid_uuid_string(title_uuid)

        update_dict = {'liked_titles': {}}
        update_dict['liked_titles'][title_uuid] = like
        self.store.update(user_uuid, update_dict)

        logger.info(dict(
            user_uuid=user_uuid,
            title_uuid=title_uuid,
            like=like,
            method='like_title',
        ))
        raise Return()

    @coroutine
    def like_star(self, user_uuid, star_uuid, like):
        user_uuid = is_valid_uuid_string(user_uuid)
        star_uuid = is_valid_uuid_string(star_uuid)

        update_dict = {'liked_stars': {}}
        update_dict['liked_stars'][star_uuid] = like
        self.store.update(user_uuid, update_dict)

        logger.info(dict(
            user_uuid=user_uuid,
            star_uuid=star_uuid,
            like=like,
            method='like_star',
        ))
        raise Return()

    @coroutine
    def get_all_liked_titles(self, user_uuid):
        log_info = dict(
            user_uuid=user_uuid,
            method='get_all_liked_titles',
        )

        user_uuid = is_valid_uuid_string(user_uuid)

        user = yield self.store.get(user_uuid)
        if not user:
            log_info.update({'error': 'user not found'})
            logger.exception(log_info)
            raise ResourceNotFoundError(log_info.get('error'))

        if not user.liked_titles:
            title_uuids = []
        else:
            title_uuids = list(filter(lambda uuid: user.liked_titles[uuid], user.liked_titles))

        titles = yield self.title_store.get_all_by_uuids(title_uuids)

        logger.info(log_info)
        raise Return(titles)

    @coroutine
    def get_all_liked_stars(self, user_uuid):
        log_info = dict(
            user_uuid=user_uuid,
            method='get_all_liked_stars',
        )

        user_uuid = is_valid_uuid_string(user_uuid)

        user = yield self.store.get(user_uuid)
        if not user:
            log_info.update({'error': 'user not found'})
            logger.exception(log_info)
            raise ResourceNotFoundError(log_info.get('error'))

        if not user.liked_stars:
            star_uuids = []
        else:
            star_uuids = list(filter(lambda uuid: user.liked_stars[uuid], user.liked_stars))

        stars = yield self.star_store.get_all_by_uuids(star_uuids)

        logger.info(log_info)
        raise Return(stars)
Ejemplo n.º 7
0
 def test_get_all_by_tag(self):
     fake_uuid = uuid.uuid4()
     titles = yield TitleStore().get_all_by_tag(str(fake_uuid))
     self.assertEquals(titles, [])
Ejemplo n.º 8
0
 def test_get_all_by_uuids(self):
     titles = yield TitleStore().get_all_by_uuids([])
     self.assertEquals(titles, [])
Ejemplo n.º 9
0
 def test_get(self):
     fake_uuid = uuid.uuid4()
     title = yield TitleStore().get(str(fake_uuid))
     self.assertIsNone(title)
Ejemplo n.º 10
0
 def test_get_all(self):
     titles = yield TitleStore().get_all()
     self.assertEquals(titles, [])