Skip to content

Commit

Permalink
[refs #104483] Remove contact persons if company is rejected
Browse files Browse the repository at this point in the history
  • Loading branch information
dianaboiangiu committed Apr 3, 2019
1 parent f806ada commit 5f63415
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 40 deletions.
2 changes: 1 addition & 1 deletion cache_registry/sync/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def update_undertakings(undertakings, check_function):
else:
undertaking_exists = Undertaking.query.filter_by(external_id=undertaking['id']).first()
if undertaking_exists:
update_undertaking(undertaking)
update_undertaking(undertaking, check_failed=True)
return undertakings_count


Expand Down
81 changes: 42 additions & 39 deletions cache_registry/sync/undertakings.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def patch_undertaking(external_id, data):
return data


def update_undertaking(data):
def update_undertaking(data, check_failed=False):
""" Create or update undertaking from received data """
data = patch_undertaking(data['id'], data)
address = parsers.parse_address(data.pop('address'))
Expand Down Expand Up @@ -138,44 +138,47 @@ def update_undertaking(data):
if type_object not in undertaking.types:
undertaking.types.append(type_object)

unique_emails = set([cp.get('email') for cp in contact_persons])
existing_persons = undertaking.contact_persons
for contact_person in contact_persons:
user = None
username = contact_person['username']
# Check if we have a user with that username
by_username = User.query.filter_by(username=username).first()
if not by_username:
email = contact_person['email']
# Check if we have a user with that email
by_email = User.query.filter_by(email=email).first()
# If we have an email as username, check for duplicate emails
if '@' in username:
if len(unique_emails) != len(contact_persons):
# If we have duplicate emails, don't match any
by_email = None

user = by_username or by_email

if user:
do_update = False
for key, value in contact_person.items():
if value != getattr(user, key):
do_update = True
if do_update:
parsers.update_obj(user, contact_person)
else:
user = User(**contact_person)
db.session.add(user)
if user not in existing_persons:
undertaking.contact_persons.append(user)

current_emails = [p.get('email') for p in contact_persons]
current_usernames = [p.get('username') for p in contact_persons]
for person in undertaking.contact_persons:
if person.email not in current_emails or \
person.username not in current_usernames:
undertaking.contact_persons.remove(person)
if check_failed:
undertaking.contact_persons[:] = []
else:
unique_emails = set([cp.get('email') for cp in contact_persons])
existing_persons = undertaking.contact_persons
for contact_person in contact_persons:
user = None
username = contact_person['username']
# Check if we have a user with that username
by_username = User.query.filter_by(username=username).first()
if not by_username:
email = contact_person['email']
# Check if we have a user with that email
by_email = User.query.filter_by(email=email).first()
# If we have an email as username, check for duplicate emails
if '@' in username:
if len(unique_emails) != len(contact_persons):
# If we have duplicate emails, don't match any
by_email = None

user = by_username or by_email

if user:
do_update = False
for key, value in contact_person.items():
if value != getattr(user, key):
do_update = True
if do_update:
parsers.update_obj(user, contact_person)
else:
user = User(**contact_person)
db.session.add(user)
if user not in existing_persons:
undertaking.contact_persons.append(user)

current_emails = [p.get('email') for p in contact_persons]
current_usernames = [p.get('username') for p in contact_persons]
for person in undertaking.contact_persons:
if person.email not in current_emails or \
person.username not in current_usernames:
undertaking.contact_persons.remove(person)

undertaking.country_code = undertaking.get_country_code()
undertaking.country_code_orig = undertaking.get_country_code_orig()
Expand Down

0 comments on commit 5f63415

Please sign in to comment.