Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions squarelet/organizations/querysets.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,29 @@ def get_viewable(self, user):
if user.is_staff:
# staff can always view all organizations
return self
elif user.is_authenticated:

qs = self
if user.is_authenticated:
# other users may not see private organizations unless they are a member
# or they can auto join that org
# and they can only see public organizations that are visible
# (verified or have charges or paid invoices)
return self.filter(
viewable_filter = (
Q(private=False, verified_journalist=True)
| Q(private=False, charges__isnull=False)
| Q(private=False, invoices__status="paid")
| Q(users=user)
).distinct()
)

# Include auto-join orgs (pre-approved) in the same filter
if hasattr(user, "can_auto_join"):
auto_join_pks = [org.pk for org in qs if user.can_auto_join(org)]
if auto_join_pks:
viewable_filter |= Q(pk__in=auto_join_pks)

return qs.filter(viewable_filter).distinct()
else:
# anonymous users may only see public organizations that are visible
return self.filter(
return qs.filter(
Q(private=False, verified_journalist=True)
| Q(private=False, charges__isnull=False)
| Q(private=False, invoices__status="paid")
Expand Down
2 changes: 1 addition & 1 deletion squarelet/organizations/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from .. import views
from ..models import OrganizationEmailDomain, ReceiptEmail

# pylint: disable=invalid-name,too-many-public-methods,too-many-lines
# pylint: disable=invalid-name,too-many-public-methods


@pytest.mark.django_db()
Expand Down
4 changes: 3 additions & 1 deletion squarelet/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,9 @@ def get_potential_organizations(self):

# Find organizations matching any of the domains
return (
Organization.objects.filter(domains__domain__in=domains)
Organization.objects.filter(
domains__domain__in=domains, allow_auto_join=True
)
.distinct()
.exclude(users=self)
)
Expand Down
4 changes: 1 addition & 3 deletions squarelet/users/onboarding.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,7 @@ class OrganizationJoinStep(OnboardingStep):
def _get_joinable_orgs(self, user):
"""Get organizations the user can join"""
invitations = list(user.get_pending_invitations())
potential_orgs = list(
user.get_potential_organizations().filter(allow_auto_join=True)
)
potential_orgs = list(user.get_potential_organizations())
return invitations, potential_orgs

def should_execute(self, request):
Expand Down
21 changes: 10 additions & 11 deletions squarelet/users/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,26 +139,25 @@ def test_get_potential_organizations_with_multiple_emails(user_factory):
user.emailaddress_set.create(email="user@example.com", verified=True)
user.emailaddress_set.create(email="user@anotherdomain.com", verified=True)

# Create organizations using the factory
org1 = OrganizationFactory(name="Org 1")
domain1 = EmailDomainFactory.create(organization=org1, domain="example.com")
org1.domains.set([domain1])
org1 = OrganizationFactory(name="Org 1", allow_auto_join=True)
EmailDomainFactory.create(organization=org1, domain="example.com")

org2 = OrganizationFactory(name="Org 2")
domain2 = EmailDomainFactory.create(organization=org2, domain="anotherdomain.com")
org2.domains.set([domain2])
org2 = OrganizationFactory(name="Org 2", allow_auto_join=True)
EmailDomainFactory.create(organization=org2, domain="anotherdomain.com")

org3 = OrganizationFactory(name="Org 3")
domain3 = EmailDomainFactory.create(
organization=org3, domain="nonmatchingdomain.com"
)
org3.domains.set([domain3])
EmailDomainFactory.create(organization=org3, domain="nonmatchingdomain.com")
# Org 4 won't appear even though it has a matching domain
# because allow_auto_join isn't enabled
org4 = OrganizationFactory(name="Org 4")
EmailDomainFactory.create(organization=org4, domain="example.com")

# Test that the user can get the correct potential orgs
potential_orgs = user.get_potential_organizations()
assert org1 in potential_orgs
assert org2 in potential_orgs
assert org3 not in potential_orgs
assert org4 not in potential_orgs


@pytest.mark.django_db
Expand Down