def _update_from_featured(): today = get_today() _update_featured_people(today) _update_featured_commons_divisions(today) _update_featured_lords_divisions(today) _update_featured_bills(today)
def test_get_current_post_for_person__any(self): person = Person.objects.first() GovernmentPostMember.objects.create(post_id=1234, person=person, start=get_today()) ParliamentaryPostMember.objects.create(post_id=1234, person=person, start=get_today()) OppositionPostMember.objects.create(post_id=1234, person=person, start=get_today()) post = get_current_post_for_person(person) self.assertIsNotNone(post) self.assertEqual(post.person, person) self.assertIn(post.post.name, ["governmental", "opposition", "parliamentary"])
def test_get_current_post_for_person__opposition(self): person = Person.objects.first() OppositionPostMember.objects.create(post_id=3456, person=person, start=get_today()) post = get_current_post_for_person(person) self.assertEqual(post.person, person) self.assertEqual(post.post.name, "opposition")
def test_get_current_post_for_person__parliamentary(self): person = Person.objects.first() ParliamentaryPostMember.objects.create(post_id=2345, person=person, start=get_today()) post = get_current_post_for_person(person) self.assertEqual(post.person, person) self.assertEqual(post.post.name, "parliamentary")
def test_get_current_post_for_person__governmental(self): person = Person.objects.first() GovernmentPostMember.objects.create(post_id=1234, person=person, start=get_today()) post = get_current_post_for_person(person) self.assertEqual(post.person, person) self.assertEqual(post.post.name, "governmental")
def get_constituency_for_date( name: str, date: Optional[_date], ) -> Optional[Constituency]: def _generalised_filter(n: str): """Remove punctuation, conjunctions, etc which may not be formatted the same way from different sources e.g. 'and' vs '&'.""" name_regex = (re.escape(n).replace(",", ",?").replace( "\&", "(&|and)").replace(" and\ ", " (&|and)\ ")) return {"name__iregex": name_regex} if name is None: return None if date is None: date = get_today() c = Constituency.objects.filter(**_generalised_filter(name)) count = c.count() # Simple cases if count == 0: # Name not found - try and resolve the constituency using ConstituencyAlsoKnownAs. try: return (ConstituencyAlsoKnownAs.objects.filter(name=name).filter( PeriodMixin.get_date_in_period_filter(date)).first().canonical) except Exception as e: log.info( f"No ConstituencyAKA found for name={name}, date={date}: {e}") return None elif count == 1: # Name found and only one result so no further filtering required return c.first() # There are multiple results so we have to try filtering by date with_valid_date = c.exclude(start=None).order_by("start") filtered_by_date = with_valid_date.filter( PeriodMixin.get_date_in_period_filter(date)) if filtered_by_date: # Result was found that matches the date requirement return filtered_by_date.first() earliest = with_valid_date.first() if earliest.start > date: # Date is before earliest result -> return earliest available result return earliest # All else fails, return the most recent available result. return with_valid_date.last()
def post(self, request, *args, **kwargs): parliamentdotuk = kwargs.get("id") try: self.model.objects.update_or_create( target_id=parliamentdotuk, defaults={ "start": get_today(), }, ) except Exception as e: log.warning(e) return HttpResponse(status=400) update_zeitgeist() return HttpResponse(status=204)
def setUp(self) -> None: Constituency.objects.create( parliamentdotuk=143474, name="Aberdeen North", ) Election.objects.create( parliamentdotuk=382037, name="2010 General Election", date=get_today(), ) Person.objects.create( parliamentdotuk=1423, name="Boris Johnson", active=True, ) ConstituencyResult.objects.create( election_id=382037, constituency_id=143474, mp_id=1423 )
def get_object(self): today = get_today() items = self.filter_queryset(self.get_queryset()) people = items.filter( target_type=ContentType.objects.get_for_model(Person)) commons_divisions = items.filter( target_type=ContentType.objects.get_for_model(CommonsDivision)) lords_divisions = items.filter( target_type=ContentType.objects.get_for_model(LordsDivision)) bills = items.filter( target_type=ContentType.objects.get_for_model(Bill)) motd = MessageOfTheDay.objects.filter( Q(display=True) & (Q(start__isnull=True) | Q(start__lte=today)) & (Q(end__isnull=True) | Q(end__gte=today))) return { "motd": motd, "people": people, "divisions": commons_divisions.union(lords_divisions), "bills": bills, }
def get_current_constituency(name: str) -> Optional[Constituency]: return get_constituency_for_date(name, get_today())