Skip to content

Commit

Permalink
Staka lu: Skip personal email addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
Tschuppi81 committed Feb 11, 2025
1 parent 956bda2 commit bc2646a
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions src/onegov/agency/data_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from collections import defaultdict
from datetime import datetime

import click
from email_validator import validate_email, EmailNotValidError, \
EmailUndeliverableError
from markupsafe import Markup
Expand Down Expand Up @@ -285,7 +286,8 @@ def parse_person(line: DefaultRow) -> None:
)

else:
print(f'agency id {agency_id} not found in agencies')
click.echo(

Check warning on line 289 in src/onegov/agency/data_import.py

View check run for this annotation

Codecov / codecov/patch

src/onegov/agency/data_import.py#L289

Added line #L289 was not covered by tests
f'agency id {agency_id} not found in agencies', err=True)

for ix, line in enumerate(csvfile.lines):
if ix % 50 == 0:
Expand Down Expand Up @@ -336,25 +338,33 @@ def get_web_address(internet_adresse: str) -> str | None:
return f'http://{internet_adresse}'


def get_email(line: DefaultRow) -> str | None:
def get_email(line: DefaultRow, vorname: str, nachname: str) -> str | None:
email = v_(line.e_mail_adresse)
vorname = vorname.lower()
nachname = nachname.lower()

Check warning on line 344 in src/onegov/agency/data_import.py

View check run for this annotation

Codecov / codecov/patch

src/onegov/agency/data_import.py#L343-L344

Added lines #L343 - L344 were not covered by tests

if not email:
return None

# only keep valid generic email address, but not `[email protected]`
addr = email.split(' ')
for a in addr:
if a in ['[email protected]', '@lu.ch']:
# skip email address like
if a in ['[email protected]', '@lu.ch', 'vorname.name@']:

Check warning on line 353 in src/onegov/agency/data_import.py

View check run for this annotation

Codecov / codecov/patch

src/onegov/agency/data_import.py#L353

Added line #L353 was not covered by tests
continue
if '@' in a:

# Skip personal email address if it contains the first or last name
if vorname and nachname and vorname in a and nachname in a:
continue

Check warning on line 358 in src/onegov/agency/data_import.py

View check run for this annotation

Codecov / codecov/patch

src/onegov/agency/data_import.py#L357-L358

Added lines #L357 - L358 were not covered by tests

if '@' in a: # as it can be any word

Check warning on line 360 in src/onegov/agency/data_import.py

View check run for this annotation

Codecov / codecov/patch

src/onegov/agency/data_import.py#L360

Added line #L360 was not covered by tests
try:
validate_email(a)
except EmailUndeliverableError:
continue
except EmailNotValidError:
print(f'Error importing person with invalid email {a}; line '
f'{line.rownumber}')
click.echo(f'Error importing person with invalid email {a}; '

Check warning on line 366 in src/onegov/agency/data_import.py

View check run for this annotation

Codecov / codecov/patch

src/onegov/agency/data_import.py#L366

Added line #L366 was not covered by tests
f'line {line.rownumber}')
continue

return a
Expand Down Expand Up @@ -389,8 +399,6 @@ def check_skip_people(line: DefaultRow) -> bool:
return False

if kw_1 in line.nachname or kw_1 in line.vorname or kw_1 in line.funktion:
# print(f'Skipping person on line {line.rownumber} with keyword '
# f'{kw_1} {line.nachname}, {line.vorname}, {line.funktion}')
return True

return False
Expand Down Expand Up @@ -426,20 +434,20 @@ def import_lu_people(

def parse_person(line: DefaultRow) -> None:
vorname = v_(line.vorname) or ''

if vorname and vorname[-1].isdigit():
# some people have a number at the end of their first name
# indicating another membership
vorname = ' '.join(vorname.split(' ')[:-1])
nachname = v_(line.nachname) or ' '

Check warning on line 441 in src/onegov/agency/data_import.py

View check run for this annotation

Codecov / codecov/patch

src/onegov/agency/data_import.py#L441

Added line #L441 was not covered by tests

function = v_(line.funktion) or ''
person = people.add_or_get(
last_name=v_(line.nachname) or ' ',
last_name=nachname,
first_name=vorname,
salutation=None,
academic_title=v_(line.akad__titel),
function=function,
email=get_email(line),
email=get_email(line, vorname, nachname),
phone=get_phone(line.isdn_nummer),
phone_direct=get_phone(line.mobil),
website=v_(get_web_address(line.internet_adresse)),
Expand Down Expand Up @@ -470,7 +478,7 @@ def parse_membership(
agency.add_person(person.id,
title=function or 'Mitglied')
else:
print(f'Error agency id {agency_id} not found')
click.echo(f'Error agency id {agency_id} not found', err=True)

Check warning on line 481 in src/onegov/agency/data_import.py

View check run for this annotation

Codecov / codecov/patch

src/onegov/agency/data_import.py#L481

Added line #L481 was not covered by tests

for ix, line in enumerate(csvfile.lines):
if ix % 100 == 0:
Expand Down Expand Up @@ -719,7 +727,6 @@ def set_membership_title(

if not name:
if title:
# print('No function given but title set')
return
membership.title = 'Mitglied'
updated_memberships.append(membership)
Expand Down

0 comments on commit bc2646a

Please sign in to comment.