def insert(cls, source, syndication, original): """Insert a new (non-blank) syndication -> original relationship. This method does a check-and-set within transaction to avoid including duplicate relationships. If blank entries exists for the syndication or original URL (i.e. syndication -> None or original -> None), they will first be removed. If non-blank relationships exist, they will be retained. Args: source: :class:`Source` subclass syndication: string (not None) original: string (not None) Returns: SyndicatedPost: newly created or preexisting entity """ # check for an exact match duplicate = cls.query(cls.syndication == syndication, cls.original == original, ancestor=source.key).get() if duplicate: return duplicate # delete blanks (expect at most 1 of each) for filter in (ndb.AND(cls.syndication == syndication, cls.original == None), ndb.AND(cls.original == original, cls.syndication == None)): for synd in cls.query(filter, ancestor=source.key).fetch(keys_only=True): synd.delete() r = cls(parent=source.key, original=original, syndication=syndication) r.put() return r
def regional(): with client.context(): year = datetime.date.today().year championships = Championship.query( ndb.AND(Championship.year == year, Championship.region != None)).fetch() competitions = ndb.get_multi([c.competition for c in championships]) states = State.query().fetch() regions = Region.query().order(Region.name).fetch() championships.sort( key=lambda championship: championship.competition.get().start_date) championship_regions = [ championship.region for championship in championships ] regions_missing_championships = [ region for region in regions if region.key not in championship_regions ] return render_template( 'regional.html', c=common.Common(wca_disclaimer=True), year=year, championships=championships, regions_missing_championships=regions_missing_championships)
def all_of(*nodes: ndb.Node) -> ndb.Node: """Returns a query node which performs a boolean AND on their conditions. Args: *nodes: datastore_services.Node. The nodes to combine. Returns: datastore_services.Node. A node combining the conditions using boolean AND. """ return ndb.AND(*nodes)
def champions_table(event_id, championship_type, championship_region='', year=0): with client.context(): is_national = championship_type == 'national' is_regional = championship_type == 'regional' is_state = championship_type == 'state' all_champions = [] filters = [] if is_national: filters.append(Champion.national_champion == True) elif year: filters.append(Champion.year == int(year)) if is_regional: filters.append(Champion.region != None) elif is_state: filters.append(Champion.state != None) elif is_regional: filters.append( Champion.region == ndb.Key(Region, championship_region)) elif is_state: filters.append( Champion.state == ndb.Key(State, championship_region)) filters.append(Champion.event == ndb.Key(Event, str(event_id))) all_champions = Champion.query(ndb.AND(*filters)).fetch() if year and is_regional: all_champions.sort(key=lambda c: c.region.id()) championship_formatter = lambda c: c.region.get().name all_regions = Region.query().fetch() elif year and is_state: all_champions.sort(key=lambda c: c.state.id()) championship_formatter = lambda c: c.state.get().name all_states = State.query().fetch() else: all_champions.sort(key=lambda c: c.championship.id(), reverse=True) championship_formatter = lambda c: c.year return render_template('champions_table.html', c=common.Common(), champions=all_champions, championship_formatter=championship_formatter)
def state_rankings_table(event_id, state_id, use_average): with client.context(): ranking_class = RankAverage if use_average == '1' else RankSingle state = State.get_by_id(state_id) if not state: self.response.write('Unrecognized state %s' % state_id) return event = Event.get_by_id(event_id) if not event: self.response.write('Unrecognized event %s' % event_id) return rankings = (ranking_class.query( ndb.AND(ranking_class.event == event.key, ranking_class.state == state.key)).order( ranking_class.best).fetch(100)) people = ndb.get_multi([ranking.person for ranking in rankings]) people_by_id = {person.key.id(): person for person in people} return render_template('state_rankings_table.html', c=common.Common(), is_average=(use_average == '1'), rankings=rankings, people_by_id=people_by_id)