From 27491c933a3b676960179448c7c28ba1b788e3e7 Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Thu, 15 Apr 2021 17:51:01 +0100 Subject: enabled mugshots & blurb in people pages --- parsers/people.py | 75 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 52 insertions(+), 23 deletions(-) (limited to 'parsers/people.py') diff --git a/parsers/people.py b/parsers/people.py index 345210c..3f7c02a 100644 --- a/parsers/people.py +++ b/parsers/people.py @@ -1,11 +1,13 @@ import csv, re, datetime, os, shutil from html.parser import HTMLParser from unidecode import unidecode +from pathlib import Path from django.conf import settings from troggle.core.models.troggle import Expedition, Person, PersonExpedition -from troggle.core.utils import save_carefully +from troggle.core.models.troggle import DataIssue +from troggle.core.utils import save_carefully, TROG '''These functions do not match how the stand-alone folk script works. So the script produces an HTML file which has href links to pages in troggle which troggle does not think are right. @@ -13,32 +15,59 @@ The standalone script needs to be renedred defucnt, and all the parsing needs to or they should use the same code by importing a module. ''' -def parseMugShotAndBlurb(personline, header, person): +def parse_blurb(personline, header, person): """create mugshot Photo instance""" - mugShotFilename=personline[header["Mugshot"]] - mugShotPath = os.path.join(settings.EXPOWEB, "folk", mugShotFilename) - if mugShotPath[-3:]=='jpg': #if person just has an image, add it - #saveMugShot(mugShotPath=mugShotPath, mugShotFilename=mugShotFilename, person=person) + ms_filename = personline[header["Mugshot"]] + ms_path = Path(settings.EXPOWEB, "folk", ms_filename) + + if ms_filename: + if not ms_path.is_file(): + message = f"! INVALID mug_shot field '{ms_filename}' for {person.fullname}" + print(message) + DataIssue.objects.create(parser='people', message=message, url=f"/person/{person.fullname}") + return + + if ms_filename.startswith('i/'): + #if person just has an image, add it. It has format 'i/adama2018.jpg' + person.mug_shot = str(Path("/folk", ms_filename)) + person.blurb = None + + elif ms_filename.startswith('l/'): + # it has the format 'l/ollybetts.htm' the file may contain images + with open(ms_path,'r') as blurbfile: + blrb = blurbfile.read() + pblurb=re.search(r'.*(.*)[^<]*', '', fragment) + # replace src="../i/ with src="/folk/i + person.blurb = fragment + else: + message = f"! Blurb parse error in {ms_filename}" + print(message) + DataIssue.objects.create(parser='people', message=message, url="/folk/") + + elif ms_filename == '': pass - elif mugShotPath[-3:]=='htm': #if person has an html page, find the image(s) and add it. Also, add the text from the html page to the "blurb" field in his model instance. - personPageOld=open(mugShotPath,'r').read() - if not person.blurb: - pblurb=re.search('.*.*