Exemplo n.º 1
0
    def save(self, *args, **kwargs):
        """
        The save method should create a new OrganizationUser linking the User
        matching the provided email address. If not matching User is found it
        should kick off the registration process. It needs to create a User in
        order to link it to the Organization.
        """
        try:
            user = get_user_model().objects.get(email__iexact=self.cleaned_data['email'])
        except get_user_model().MultipleObjectsReturned:
            raise forms.ValidationError(_("This email address has been used multiple times."))
        except get_user_model().DoesNotExist:
            user = invitation_backend().invite_by_email(
                    self.cleaned_data['email'],
                    **{'domain': get_current_site(self.request),
                        'organization': self.organization,
                        'sender': self.request.user})
        else:
            notification_backend().notify_by_email(self.cleaned_data['email'],
                    **{'domain': get_current_site(self.request),
                        'organization': self.organization,
                        'sender': self.request.user})

        return OrganizationUser.objects.create(user=user,
                organization=self.organization,
                is_admin=self.cleaned_data['is_admin'])
Exemplo n.º 2
0
 def clean_email(self):
     email = self.cleaned_data["email"]
     try:
         user = get_user_model().objects.get(email__iexact=self.cleaned_data["email"])
     except get_user_model().MultipleObjectsReturned:
         raise forms.ValidationError(_("This email address has been used multiple times."))
     except get_user_model().DoesNotExist:
         pass  # good
     else:
         if self.organization.organization_users.filter(user=user).count() > 0:
             raise forms.ValidationError(_("This user is already a member of this organization!"))
     return email
Exemplo n.º 3
0
 def save(self, **kwargs):
     """
     Create the organization, then get the user, then make the owner.
     """
     is_active = True
     try:
         user = get_user_model().objects.get(email=self.cleaned_data['email'])
     except get_user_model().DoesNotExist:
         user = invitation_backend().invite_by_email(
                 self.cleaned_data['email'],
                 **{'domain': get_current_site(self.request),
                     'organization': self.cleaned_data['name'],
                     'sender': self.request.user, 'created': True})
         is_active = False
     return create_organization(user, self.cleaned_data['name'],
             self.cleaned_data['slug'], is_active=is_active)
Exemplo n.º 4
0
 def save(self, *args, **kwargs):
     """
     The save method should create a new OrganizationUser linking the User
     matching the provided email address. If not matching User is found it
     should kick off the registration process. It needs to create a User in
     order to link it to the Organization.
     """
     try:
         user = get_user_model().objects.get(email__iexact=self.cleaned_data['email'])
     except get_user_model().MultipleObjectsReturned:
         raise forms.ValidationError(_("This email address has been used multiple times."))
     except get_user_model().DoesNotExist:
         user = invitation_backend().invite_by_email(
                 self.cleaned_data['email'],
                 **{'domain': get_current_site(self.request),
                     'organization': self.organization})
     return OrganizationUser.objects.create(user=user,
             organization=self.organization,
             is_admin=self.cleaned_data['is_admin'])
Exemplo n.º 5
0
 def save(self, **kwargs):
     """
     Create the organization, then get the user, then make the owner.
     """
     is_active = True
     try:
         user = get_user_model().objects.get(email=self.cleaned_data["email"])
     except get_user_model().DoesNotExist:
         user = invitation_backend().invite_by_email(
             self.cleaned_data["email"],
             **{
                 "domain": get_current_site(self.request),
                 "organization": self.cleaned_data["name"],
                 "sender": self.request.user,
                 "created": True,
             }
         )
         is_active = False
     return create_organization(user, self.cleaned_data["name"], self.cleaned_data["slug"], is_active=is_active)
Exemplo n.º 6
0
 def save(self, **kwargs):
     """
     Create the organization, then get the user, then make the owner.
     """
     is_active = True
     try:
         user = get_user_model().objects.get(
             email=self.cleaned_data['email'])
     except get_user_model().DoesNotExist:
         user = invitation_backend().invite_by_email(
             self.cleaned_data['email'], **{
                 'domain': get_current_site(self.request),
                 'organization': self.cleaned_data['name'],
                 'sender': self.request.user,
                 'created': True
             })
         is_active = False
     return create_organization(user,
                                self.cleaned_data['name'],
                                self.cleaned_data['slug'],
                                is_active=is_active)
Exemplo n.º 7
0
 def __init__(self, *args, **kwargs):
     self.user_model = get_user_model()
Exemplo n.º 8
0
 def __init__(self, *args, **kwargs):
     self.user_model = get_user_model()
Exemplo n.º 9
0
 class Meta:
     model = get_user_model()
     exclude = ('is_staff', 'is_superuser', 'is_active', 'last_login',
                'date_joined', 'groups', 'user_permissions')
Exemplo n.º 10
0
# -*- coding: utf-8 -*-

from django.test import TestCase
from django.test.utils import override_settings

from organizations.models import Organization, get_user_model
from organizations.utils import create_organization, model_field_attr

User = get_user_model()


@override_settings(USE_TZ=True)
class OrgManagerTests(TestCase):

    fixtures = ['users.json', 'orgs.json']

    def test_create_organization(self):
        user = User.objects.get(username="******")
        acme = create_organization(user, "Acme", "acme")
        self.assertTrue(isinstance(acme, Organization))
        self.assertEqual(user, acme.owner.organization_user.user)


class AttributeUtilTests(TestCase):

    def test_present_field(self):
        self.assertTrue(model_field_attr(User, 'username', 'max_length'))

    # def test_absent_field(self):
    #     self.assertRaises(KeyError, model_field_attr, User, 'blahblah',
    #         'max_length')
Exemplo n.º 11
0
# -*- coding: utf-8 -*-

from django.conf import settings

from organizations.models import Organization, OrganizationUser, OrganizationOwner, get_user_model

User = get_user_model()


def skip_request(request):
    """
    ORGANIZATIONS_SKIP_REQUEST_GATE

    define a dotted path to a method that returns True or False

    True = skip, no org switcher/setter
    False = go on...
    """
    if getattr(settings, "ORGANIZATIONS_SKIP_REQUEST_GATE", None) is not None:
        import importlib
        mod_name, func_name = getattr(settings,
                                      "ORGANIZATIONS_SKIP_REQUEST_GATE",
                                      '').rsplit('.', 1)
        mod = importlib.import_module(mod_name)
        func = getattr(mod, func_name)
        return func(request)
    return False


def create_organization(user, name, slug, is_active=True):
    """