def test_old_granular_dates(self): """Can we parse dates older than 1900 without strftime barfing?""" obj = DummyObject() d = datetime.date(year=1899, month=1, day=23) obj.date_start = d obj.date_granularity_start = GRANULARITY_DAY try: granular_date(obj, "date_start") except ValueError: self.fail("Granular date failed while parsing date prior to 1900.")
def test_old_granular_dates(self): """Can we parse dates older than 1900 without strftime barfing?""" obj = DummyObject() d = datetime.date(year=1899, month=1, day=23) obj.date_start = d obj.date_granularity_start = GRANULARITY_DAY try: granular_date(obj, 'date_start') except ValueError: self.fail("Granular date failed while parsing date prior to 1900.")
def make_slugs(person): slug_name = ( f"{slugify(f'{person.name_last} {person.name_first}')}.jpeg") slug_name_dob = "{slug}-{date}.jpeg".format( slug=slug_name.rsplit(".")[0], date=granular_date(person, "date_dob", iso=True).lower(), ) return slug_name, slug_name_dob
def test_granularity_missing_date(self): """Does granularity code work with missing data?""" obj = DummyObject() d = "" obj.date_start = d obj.date_granularity_start = GRANULARITY_DAY # Missing date value self.assertEqual(granular_date(obj, "date_start"), "Unknown")
def map_judges_to_photos(self): """Identify which of the judges in the DB have photos. We iterate over the entire collection of judges, identifying which have photos. We could instead iterate over the photos, but that increases the risk of duplicate issues. """ # Create a dict of judge paths, mapping paths to empty lists. judge_paths = os.listdir(os.path.join(judge_root, "orig")) judge_map = {} for path in judge_paths: judge_map[path] = [] # Iterate over the people, attempting to look them up in the list people = Person.objects.filter(is_alias_of=None) for person in people: for name in self.make_slugs(person): if name in judge_map: # If there's a hit, add the path to the dict of judge paths. judge_map[name].append(person) break # After iterating, set all people to not have photos. if not self.debug: people.update(has_photo=False) found = 0 missed = 0 multi = 0 for path, people in judge_map.items(): if len(people) == 0: logger.warn("Did not find a judge for %s" % path) missed += 1 if len(people) == 1: person = people[0] found += 1 if not self.debug: logger.info("Updating judge %s" % person) person.has_photo = True person.save() if len(people) > 1: logger.warn("Found more than one match for %s:" % path) for person in people: logger.warn("Found: %s - %s" % ( person, granular_date( person, "date_dob", iso=True, ), )) multi += 1 logger.info("\n\n%s Matches\n%s Missed\n%s Multiple results" % (found, missed, multi))
def test_granularity_missing_date(self): """Does granularity code work with missing data?""" obj = DummyObject() d = '' obj.date_start = d obj.date_granularity_start = GRANULARITY_DAY # Missing date value self.assertEqual( granular_date(obj, 'date_start'), "Unknown" )
def make_slugs(person): slug_name = slugify("%s %s" % (person.name_last, person.name_first)) + ".jpeg" slug_name_dob = "{slug}-{date}.jpeg".format( slug=slug_name.rsplit('.')[0], date=granular_date( person, 'date_dob', iso=True, ).lower()) return slug_name, slug_name_dob
def test_granularity_dict_or_obj(self) -> None: """Can you pass a dict or an object and will both work? This is important because some objects in Django templates are dicts... others are objects. """ obj = {} d = "" obj["date_start"] = d obj["date_granularity_start"] = GRANULARITY_DAY self.assertEqual(granular_date(obj, "date_start"), "Unknown")
def map_judges_to_photos(self): """Identify which of the judges in the DB have photos. We iterate over the entire collection of judges, identifying which have photos. We could instead iterate over the photos, but that increases the risk of duplicate issues. """ # Create a dict of judge paths, mapping paths to empty lists. judge_paths = os.listdir(os.path.join(judge_root, 'orig')) judge_map = {} for path in judge_paths: judge_map[path] = [] # Iterate over the people, attempting to look them up in the list people = Person.objects.filter(is_alias_of=None) for person in people: for name in self.make_slugs(person): if name in judge_map: # If there's a hit, add the path to the dict of judge paths. judge_map[name].append(person) break # After iterating, set all people to not have photos. if not self.debug: people.update(has_photo=False) found = 0 missed = 0 multi = 0 for path, people in judge_map.items(): if len(people) == 0: print "WARNING: Did not find a judge for %s" % path missed += 1 if len(people) == 1: person = people[0] found += 1 if not self.debug: print "INFO: Updating judge %s" % person person.has_photo = True person.save() if len(people) > 1: print "WARNING: Found more than one match for %s\nFound:" % path for person in people: print " %s - %s" % (person, granular_date( person, 'date_dob', iso=True, )) multi += 1 print "\n\n%s Matches\n%s Missed\n%s Multiple results" % (found, missed, multi)
def make_img_path(person): """Make the path for a person's img tag.""" img_path = None if person.has_photo: p = slugify(('%s-%s' % (person.name_last, person.name_first)).lower()) if person.date_dob: img_path = static('judge_pics/%s-%s.jpeg' % (p, granular_date(person, 'date_dob', iso=True, default=''))) else: img_path = static('judge_pics/%s.jpeg' % p) return img_path
def make_slugs(person): slug_name = slugify("%s %s" % (person.name_last, person.name_first)) + ".jpeg" slug_name_dob = "{slug}-{date}.jpeg".format( slug=slug_name.rsplit('.')[0], date=granular_date( person, 'date_dob', iso=True, ).lower() ) return slug_name, slug_name_dob
def make_img_path(person): """Make the path for a person's img tag.""" img_path = None if person.has_photo: p = slugify(f"{person.name_last}-{person.name_first}".lower()) if person.date_dob: img_path = static( "judge_pics/%s-%s.jpeg" % (p, granular_date(person, "date_dob", iso=True, default=""))) else: img_path = static(f"judge_pics/{p}.jpeg") return img_path
def html_title(self): """Display the position as a title.""" s = '' # Title if self.get_position_type_display(): s += self.get_position_type_display() else: s += self.job_title # Where if self.school or self.organization_name or self.court: s += ' <span class="alt text-lowercase">at</span> ' if self.court: s += self.court.full_name elif self.school: s += self.school elif self.organization_name: s += self.organization_name # When if self.date_start or self.date_termination: if self.date_termination: end_date = granular_date(self, 'date_termination') else: # If we don't know when the position ended, we use a ? if the # person has died, or say Present if they're alive. if self.person.date_dod: end_date = "?" else: end_date = "Present" s += ' <span class="text-capitalize">(%s – %s)' % ( granular_date(self, 'date_start', default="Unknown Date"), end_date, ) return s
def html_title(self): """Display the position as a title.""" s = '' # Title if self.get_position_type_display(): s += self.get_position_type_display() else: s += self.job_title # Where if self.school or self.organization_name or self.court: s += ' <span class="alt text-lowercase">at</span> ' if self.court: s += self.court.full_name elif self.school: s += self.school elif self.organization_name: s += self.organization_name # When if self.date_start or self.date_termination: if self.date_termination: end_date = granular_date(self, 'date_termination') else: # If we don't know when the position ended, we use a ? if the # person has died, or say Present if they're alive. if self.person.date_dod: end_date = "?" else: end_date = "Present" s += ' <span class="text-capitalize">(%s – %s)' % ( granular_date(self, 'date_start', default="Unknown Date"), end_date, ) return s
def test_granularity_dict_or_obj(self): """Can you pass a dict or an object and will both work? This is important because some objects in Django templates are dicts... others are objects. """ obj = {} d = '' obj['date_start'] = d obj['date_granularity_start'] = GRANULARITY_DAY self.assertEqual( granular_date(obj, 'date_start'), "Unknown", )
def test_granular_dates(self): """Can we get the correct values for granular dates?""" q_a = ( ((GRANULARITY_DAY, True), "1982-06-09"), ((GRANULARITY_MONTH, True), "1982-06"), ((GRANULARITY_YEAR, True), "1982"), ((GRANULARITY_DAY, False), "June 9, 1982"), ((GRANULARITY_MONTH, False), "June, 1982"), ((GRANULARITY_YEAR, False), "1982"), ) d = datetime.date(year=1982, month=6, day=9) obj = DummyObject() for q, a in q_a: setattr(obj, 'date_start', d) setattr(obj, 'date_granularity_start', q[0]) result = granular_date(obj, 'date_start', *q) self.assertEqual( result, a, msg=("Incorrect granular date conversion. Got: %s instead of " "%s" % (result, a)))
def test_granular_dates(self): """Can we get the correct values for granular dates?""" q_a = ( ((GRANULARITY_DAY, True), "1982-06-09"), ((GRANULARITY_MONTH, True), "1982-06"), ((GRANULARITY_YEAR, True), "1982"), ((GRANULARITY_DAY, False), "June 9, 1982"), ((GRANULARITY_MONTH, False), "June, 1982"), ((GRANULARITY_YEAR, False), "1982"), ) d = datetime.date(year=1982, month=6, day=9) obj = DummyObject() for q, a in q_a: setattr(obj, 'date_start', d) setattr(obj, 'date_granularity_start', q[0]) result = granular_date(obj, 'date_start', *q) self.assertEqual( result, a, msg=("Incorrect granular date conversion. Got: %s instead of " "%s" % (result, a)) )