Skip to content

Commit

Permalink
Merge pull request #24 from eea/unique_users
Browse files Browse the repository at this point in the history
Unique users
  • Loading branch information
dianaboiangiu committed Mar 13, 2020
2 parents b3ffa5f + cdf39db commit 07cef8d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
23 changes: 9 additions & 14 deletions notifications/management/commands/fetch.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import logging

from django.db.models import Q

from notifications.models import Person, Company

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -61,24 +63,17 @@ def create_person(self, **kwargs):
name = kwargs['name']
username = kwargs['username']
email = kwargs['email']
email_exists = Person.objects.filter(email=email).exists()
username_exists = Person.objects.filter(username=username).exists()
if username_exists and email_exists:
person = Person.objects.filter(username=username).first()

# XXX This will not catch duplicates
person = Person.objects.filter(
Q(email=email) | Q(username=username) | Q(username=email) | Q(email=username)
).first()

if person:
person.email = email
person.name = name
person.save()
created = False
elif username_exists:
person, created = Person.objects.update_or_create(
username=username,
defaults=kwargs
)
elif email_exists:
person, created = Person.objects.update_or_create(
email=email,
defaults=kwargs
)
else:
person, created = Person.objects.update_or_create(
username=username,
Expand Down
10 changes: 7 additions & 3 deletions notifications/management/commands/fetch_ecr.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,13 @@ def fetch_companies(self, registry):
continue
company = self.create_company(
**self.parse_company_data(item))
username_list = [user["username"] for user in item["users"]]
persons = Person.objects.filter(username__in=username_list)
self.set_current_user_true(company, persons)
unique_list = {user["username"] for user in item["users"]}
unique_list.update(user["email"] for user in item["users"])

people = set()
people.update(set(Person.objects.filter(email__in=unique_list)))
people.update(set(Person.objects.filter(username__in=unique_list)))
self.set_current_user_true(company, people)
company_count += 1
except IntegrityError as e:
logger.info('Skipped company: %s (%s)', item['name'], e)
Expand Down

0 comments on commit 07cef8d

Please sign in to comment.