Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RelatedObjectDoesNotExist: Organization has no owner #190

Open
danihodovic opened this issue Jan 19, 2020 · 3 comments
Open

RelatedObjectDoesNotExist: Organization has no owner #190

danihodovic opened this issue Jan 19, 2020 · 3 comments
Labels

Comments

@danihodovic
Copy link

danihodovic commented Jan 19, 2020

I'm using the helper function to create a default organization when a user signs up.

def create_organization(
user,
name,
slug=None,
is_active=None,
org_defaults=None,
org_user_defaults=None,
**kwargs
):

The docstring says:

Returns a new organization, also creating an initial organization user who is the owner.

However the first user doesn't in fact seem to be the organization owner:

>>> org.is_owner(user)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/dani/repos/ninjads/.venv/lib/python3.7/site-packages/organizations/abstract.py", line 181, in is_owner
    return self.owner.organization_user.user == user
  File "/home/dani/repos/ninjads/.venv/lib/python3.7/site-packages/django/db/models/fields/related_descriptors.py", line 423, in __get__
    self.related.get_accessor_name()
ninjads.organizations.models.Organization.owner.RelatedObjectDoesNotExist: Organization has no owner.

Organization has no owner.

The member and admin checks succeed:

>>> org.is_admin(user)
True
>>> org.is_member(user)
True
@bennylope
Copy link
Owner

That definitely does not sound right.

def test_create_organization(self):
acme = create_organization(
self.user, "Acme", org_defaults={"slug": "acme-slug"}
)
self.assertTrue(isinstance(acme, Organization))
self.assertEqual(self.user, acme.owner.organization_user.user)
self.assertTrue(acme.owner.organization_user.is_admin)
def test_create_custom_org(self):
custom = create_organization(self.user, "Custom", model=Account)
self.assertTrue(isinstance(custom, Account))
self.assertEqual(self.user, custom.owner.organization_user.user)
def test_create_custom_org_from_abstract(self):
custom = create_organization(self.user, "Custom", model=CustomOrganization)
self.assertTrue(isinstance(custom, CustomOrganization))
self.assertEqual(self.user, custom.owner.organization_user.user)

That function creates the owner right at the end.

org_owner_model.objects.create(
organization=organization, organization_user=new_user
)
return organization

Before I dig further, could you share how you're calling that function and what models you're using?

@bennylope bennylope added the bug label Jan 20, 2020
@danihodovic
Copy link
Author

Organization models

from hashid_field import HashidAutoField
from organizations.abstract import (
    AbstractOrganization,
    AbstractOrganizationOwner,
    AbstractOrganizationUser,
)


class Organization(AbstractOrganization):
    id = HashidAutoField(primary_key=True)


class OrganizationUser(AbstractOrganizationUser):
    pass


class OrganizationOwner(AbstractOrganizationOwner):
    pass

Organization creation on user sign up

import logging

from allauth.account.signals import user_signed_up
from django.dispatch import receiver
from organizations.utils import create_organization

from ninjads.organizations.models import Organization

logger = logging.LoggerAdapter(logging.getLogger(), extra={})


@receiver(user_signed_up)
def create_default_organization(
    request, user, **kwargs
):  # pylint: disable=unused-argument
    logger.info("User %s signed up, creating a default organization", user)
    create_organization(user, user.email, is_active=True, model=Organization)

@BoPeng BoPeng mentioned this issue Feb 3, 2020
@harikvpy
Copy link

Please check my comment here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants