Ejemplo n.º 1
0
def media_updated(affected_refs):
    reference_keys = _filter(affected_refs['references'])
    years = _filter(affected_refs['year'])
    media_tags = _filter(affected_refs['media_tag_enum'])

    team_keys = filter(lambda x: x.kind() == 'Team', reference_keys)
    event_team_keys_future = EventTeam.query(EventTeam.team.IN(team_keys)).fetch_async(None, keys_only=True) if team_keys else None

    queries_and_keys = []
    for reference_key in reference_keys:
        if reference_key.kind() == 'Team':
            for year in years:
                queries_and_keys.append((TeamYearMediaQuery(reference_key.id(), year)))
            for media_tag in media_tags:
                queries_and_keys.append((TeamTagMediasQuery(reference_key.id(), media_tag)))
            queries_and_keys.append((TeamSocialMediaQuery(reference_key.id())))
        if reference_key.kind() == 'Event':
            queries_and_keys.append((EventMediasQuery(reference_key.id())))

    if event_team_keys_future:
        for event_team_key in event_team_keys_future.get_result():
            event_key = event_team_key.id().split('_')[0]
            year = int(event_key[:4])
            if year in years:
                queries_and_keys.append(EventTeamsMediasQuery(event_key))
                queries_and_keys.append(EventTeamsPreferredMediasQuery(event_key))

    return queries_and_keys
    def _render(self, team_key, tag, year=None):
        media_tag_enum = MediaTag.get_enum_from_url(tag)
        if year:
            medias, self._last_modified = TeamYearTagMediasQuery(team_key, int(year), media_tag_enum).fetch(dict_version=3, return_updated=True)
        else:
            medias, self._last_modified = TeamTagMediasQuery(team_key, media_tag_enum).fetch(dict_version=3, return_updated=True)

        return json.dumps(medias, ensure_ascii=True, indent=2, sort_keys=True)
    def test_media_updated_team(self):
        affected_refs = {
            'references': {ndb.Key(Team, 'frc254'),
                           ndb.Key(Team, 'frc604')},
            'year': {2014, 2015},
            'media_tag_enum':
            {MediaTag.CHAIRMANS_ESSAY, MediaTag.CHAIRMANS_VIDEO},
        }
        cache_keys = [
            q.cache_key
            for q in get_affected_queries.media_updated(affected_refs)
        ]

        self.assertEqual(len(cache_keys), 22)
        self.assertTrue(
            TeamYearMediaQuery('frc254', 2014).cache_key in cache_keys)
        self.assertTrue(
            TeamYearMediaQuery('frc254', 2015).cache_key in cache_keys)
        self.assertTrue(TeamSocialMediaQuery('frc254').cache_key in cache_keys)
        self.assertTrue(
            TeamYearMediaQuery('frc604', 2014).cache_key in cache_keys)
        self.assertTrue(
            TeamYearMediaQuery('frc604', 2015).cache_key in cache_keys)
        self.assertTrue(TeamSocialMediaQuery('frc604').cache_key in cache_keys)
        self.assertTrue(
            EventTeamsMediasQuery('2015cama').cache_key in cache_keys)
        self.assertTrue(
            EventTeamsMediasQuery('2015casj').cache_key in cache_keys)
        self.assertTrue(
            EventTeamsPreferredMediasQuery('2015cama').cache_key in cache_keys)
        self.assertTrue(
            EventTeamsPreferredMediasQuery('2015casj').cache_key in cache_keys)
        self.assertTrue(
            TeamTagMediasQuery('frc254', MediaTag.CHAIRMANS_ESSAY).cache_key in
            cache_keys)
        self.assertTrue(
            TeamTagMediasQuery('frc604', MediaTag.CHAIRMANS_VIDEO).cache_key in
            cache_keys)
        self.assertTrue(
            TeamYearTagMediasQuery('frc254', MediaTag.CHAIRMANS_ESSAY,
                                   2014).cache_key in cache_keys)
        self.assertTrue(
            TeamYearTagMediasQuery('frc604', MediaTag.CHAIRMANS_VIDEO,
                                   2015).cache_key in cache_keys)