def test_slug(self): office = Office(state="MD", name="House of Delegates", district="35B", chamber="lower") self.assertEqual(office.slug, "house-of-delegates-35b") pres = Office(state="US", name="President") self.assertEqual(pres.slug, "president")
def _get_office(self, raw_result): office_query = { 'state': STATE, 'name': self._clean_office(raw_result.office) } if office_query['name'] is 'President': office_query['state'] = 'US' if office_query['name'] in self.district_offices: #if raw_result.district: office_query['district'] = raw_result.district or '' key = Office.make_key(**office_query) try: return self._office_cache[key] except KeyError: try: office = Office.objects.get(**office_query) assert key == office.key self._office_cache[key] = office return office except Office.DoesNotExist: logger.error( "\tNo office matching query {}".format(office_query)) raise
def _get_office(self, raw_result): office_query = { 'state': 'MD', 'name': self._clean_office(raw_result.office), } # Handle president, where state = "US" if office_query['name'] == "President": office_query['state'] = "US" if office_query['name'] in self.district_offices: office_query['district'] = self._strip_leading_zeros( raw_result.district) key = Office.make_key(**office_query) try: return self._office_cache[key] except KeyError: try: office = Office.objects.get(**office_query) # TODO: Remove this once I'm sure this always works. It should. assert key == office.key self._office_cache[key] = office return office except Office.DoesNotExist: print "No office matching query %s" % (office_query) raise
def _get_office(self, raw_resultDict): office_query = { 'state': 'VT', 'name': self._clean_office(raw_resultDict.get('office', "")), } # Handle president, where state = "US" if office_query['name'] == "President": office_query['state'] = "US" if office_query['name'] in self.district_offices: jurisdiction = self._cleanDistrict(raw_resultDict.get('district',"")) office_query['district'] = jurisdiction key = Office.make_key(**office_query) try: return self._office_cache[key] except KeyError: try: print (office_query) office = Office.objects.get(**office_query) # TODO: Remove this once I'm sure this always works. It should. assert key == office.key self._office_cache[key] = office return office except Office.DoesNotExist: print (raw_resultDict.get('office', "")) print (self._clean_office(raw_resultDict.get('office', ""))) print("No office matching query %s" % (office_query)) raise
def _get_office(self, raw_result): office_query = { 'state': 'MD', 'name': self._clean_office(raw_result.office), } # Handle president, where state = "US" if office_query['name'] == "President": office_query['state'] = "US" if office_query['name'] in self.district_offices: office_query['district'] = self._strip_leading_zeros(raw_result.district) key = Office.make_key(**office_query) try: return self._office_cache[key] except KeyError: try: office = Office.objects.get(**office_query) # TODO: Remove this once I'm sure this always works. It should. assert key == office.key self._office_cache[key] = office return office except Office.DoesNotExist: print "No office matching query %s" % (office_query) raise
def _get_office(self, raw_result): office_query = { 'state': STATE, 'name': self._clean_office(raw_result.office) } if office_query['name'] is 'President': office_query['state'] = 'US' if office_query['name'] in self.district_offices: #if raw_result.district: office_query['district'] = raw_result.district or '' key = Office.make_key(**office_query) try: return self._office_cache[key] except KeyError: try: office = Office.objects.get(**office_query) assert key == office.key self._office_cache[key] = office return office except Office.DoesNotExist: logger.error("\tNo office matching query {}".format(office_query)) raise
def test_auto_slug(self): office = Office(state="MD", name="House of Delegates", district="35B", chamber="lower") dem = Party(name="Democrat", abbrev="DEM") contest = Contest(office=office, primary_party=dem) self.assertEqual(contest.slug, "house-of-delegates-35b-dem")
def _get_or_make_office(self, raw_result): clean_name = self._clean_office_name(raw_result.office) if clean_name: office_query = self._make_office_query(clean_name, raw_result) key = Office.make_key(**office_query) try: return self._office_cache[key] except KeyError: try: office = Office.objects.get(**office_query) self._office_cache[key] = office return office except Office.DoesNotExist: office = Office(**office_query) office.save() self._office_cache[key] = office return office else: return None