Skip to content

Admin

LoginStatusAdmin

Bases: ModelAdmin

Admin page for LoginStatus model

OrganisationAdmin

Bases: ModelAdmin

Admin page for Organisation model

OrganisationInvitesAdmin

Bases: ModelAdmin

Admin page for OrganisationInvites model

OrganisationRepresentativeAdmin

Bases: OrganisationPersonnelBaseAdmin

Admin page for OrganisationRepresentative model

OrganisationUserAdmin

Bases: OrganisationPersonnelBaseAdmin

Admin page for OrganisationUser model

RemindersAdmin

Bases: ModelAdmin

Admin page for Reminders model

UserRoleTypeAdmin

Bases: ModelAdmin

Admin page for UserRoleType model

UserTitleAdmin

Bases: ModelAdmin

Admin page for UserTitle model

Factories

OrganisationInvitesFactory

Bases: DjangoModelFactory

Factory class for Organisation Invites model.

loginStatusFactory

Bases: DjangoModelFactory

Factory class for login status models.

organisationFactory

Bases: DjangoModelFactory

Factory class for organisation model.

organisationRepresentativeFactory

Bases: DjangoModelFactory

Factory class for organisation representative model.

organisationUserFactory

Bases: DjangoModelFactory

Factory class for organisation user model.

userFactory

Bases: DjangoModelFactory

Factory class for user models.

userLoginFactory

Bases: DjangoModelFactory

User login facfory class.

userProfileFactory

Bases: DjangoModelFactory

Factory class for user profile model.

userRoleTypeFactory

Bases: DjangoModelFactory

Factory class for user role type models.

userTitleFactory

Bases: DjangoModelFactory

Factory class for user title models.

Models

LoginStatus

Bases: Model

User login status model.

Organisation

Bases: Model

Organisation model.

OrganisationInvites

Bases: Model

OrganisationInvites model to store all invites

OrganisationPersonnel

Bases: Model

Organisation personnel abstract model.

OrganisationRepresentative

Bases: OrganisationPersonnel

Organisation representative model.

OrganisationUser

Bases: OrganisationPersonnel

Organisation user model.

Reminders

Bases: Model

Reminders model to store all reminders

UserLogin

Bases: Model

User login model.

UserProfile

Bases: Model

Extend User model with one-to-one mapping.

UserRoleType

Bases: Model

User role type (Base users, admins ..etc.) model.

UserTitle

Bases: Model

User title model.

create_user_profile

create_user_profile(sender, instance, created, **kwargs)

When a user is created, also create a UserProfile

Source code in django_project/stakeholder/models.py
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    """
    When a user is created, also create a UserProfile
    """
    if (
        created and
        not UserProfile.objects.filter(user=instance).exists()
    ):
        UserProfile.objects.create(user=instance)

post_create_organisation_representative

post_create_organisation_representative(
    sender, instance, created, **kwargs
)

Handle OrganisationRepresentative creation by automatically add them to Organisation Manager group.

Source code in django_project/stakeholder/models.py
@receiver(post_save, sender=OrganisationRepresentative)
def post_create_organisation_representative(
    sender,
    instance: OrganisationRepresentative,
    created,
    **kwargs
):
    """
    Handle OrganisationRepresentative creation by
    automatically add them to Organisation Manager group.
    """
    add_user_to_org_manager(instance, Group)
    # check if user becomes manager from member
    is_made_manager = False
    if created:
        organisation_user = OrganisationUser.objects.filter(
            user=instance.user,
            organisation=instance.organisation
        ).first()
        member_invite = OrganisationInvites.objects.filter(
            user=instance.user,
            joined=True
        ).first()
        if organisation_user is None:
            # case when representative is created in admin site
            # create organisation user
            OrganisationUser.objects.create(
                user=instance.user,
                organisation=instance.organisation
            )
            is_made_manager = True
        elif member_invite:
            # check if previously has been invited as member
            is_made_manager = member_invite.assigned_as == MEMBER
        else:
            # this case could be the member is added through admin site
            is_made_manager = True
    if is_made_manager:
        notify_user_becomes_manager(instance)

post_create_organisation_user

post_create_organisation_user(
    sender, instance, created, **kwargs
)

Handle OrganisationUser creation by automatically add them to Organisation Member group.

Source code in django_project/stakeholder/models.py
@receiver(post_save, sender=OrganisationUser)
def post_create_organisation_user(
    sender,
    instance: OrganisationUser,
    created,
    **kwargs
):
    """
    Handle OrganisationUser creation by
    automatically add them to Organisation Member group.
    """
    add_user_to_org_member(instance, OrganisationInvites, Group)

post_delete_organisation_representative

post_delete_organisation_representative(
    sender, instance, *args, **kwargs
)

Handle OrganisationRepresentative deletion by removing them from Data contributor and Organisation Manager group, if they are no longer part of any organisation.

Source code in django_project/stakeholder/models.py
@receiver(post_delete, sender=OrganisationRepresentative)
def post_delete_organisation_representative(
    sender,
    instance: OrganisationRepresentative,
    *args,
    **kwargs
):
    """
    Handle OrganisationRepresentative deletion by removing them
    from Data contributor and Organisation Manager group, if they are no longer
    part of any organisation.
    """
    remove_user_from_org_manager(instance)

post_delete_organisation_user

post_delete_organisation_user(
    sender, instance, *args, **kwargs
)

Handle OrganisationUser deletion by removing them from Data contributor and Organisation Member group, if they are no longer part of any organisation.

Source code in django_project/stakeholder/models.py
@receiver(post_delete, sender=OrganisationUser)
def post_delete_organisation_user(
    sender,
    instance: OrganisationUser,
    *args,
    **kwargs
):
    """
    Handle OrganisationUser deletion by removing them
    from Data contributor and Organisation Member group, if they are no longer
    part of any organisation.
    """
    remove_user_from_org_member(instance)
    # when user is removed from organisation
    # also remove it from current_organisation in UserProfile
    profile = UserProfile.objects.filter(
        user=instance.user,
        current_organisation=instance.organisation
    ).first()
    if profile:
        profile.current_organisation = None
        profile.save(update_fields=['current_organisation'])
    # when user is removed, ensure that manager is removed as well
    OrganisationRepresentative.objects.filter(
        user=instance.user,
        organisation=instance.organisation
    ).delete()
    # ensure invite record is removed
    OrganisationInvites.objects.filter(
        Q(email=instance.user.email) | Q(user=instance.user),
        organisation=instance.organisation
    ).delete()

save_user_profile

save_user_profile(sender, instance, **kwargs)

Save the UserProfile whenever a save event occurs

Source code in django_project/stakeholder/models.py
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    """
    Save the UserProfile whenever a save event occurs
    """
    if UserProfile.objects.filter(
        user=instance
    ).exists():
        instance.user_profile.save()
    else:
        UserProfile.objects.create(user=instance)

Test Case

LoginStatusTestCase

Bases: TestCase

User login status test case.

test_create_login_status

test_create_login_status()

Test creating login status.

Source code in django_project/stakeholder/tests/test_stakeholder_models.py
def test_create_login_status(self):
    """Test creating login status."""
    self.assertEqual(LoginStatus.objects.count(), 1)
    self.assertTrue(
        isinstance(self.loginStatus, LoginStatus)
    )
    self.assertTrue(self.loginStatus.name in ['logged in', 'logged out'])

test_delete_login_status

test_delete_login_status()

Test deleting a login status.

Source code in django_project/stakeholder/tests/test_stakeholder_models.py
def test_delete_login_status(self):
    """Test deleting a login status."""
    self.loginStatus.delete()
    self.assertEqual(LoginStatus.objects.count(), 0)

test_update_login_status

test_update_login_status()

Test updating a login status.

Source code in django_project/stakeholder/tests/test_stakeholder_models.py
def test_update_login_status(self):
    """Test updating a login status."""
    self.loginStatus.name = 'logged in'
    loginStatus = LoginStatus.objects.get(
        id=self.loginStatus.id
    )
    self.assertTrue(loginStatus.name, 'logged in')
    self.loginStatus.name = 'logged out'
    loginStatus = LoginStatus.objects.get(
        id=self.loginStatus.id
    )
    self.assertTrue(loginStatus.name, 'logged out')

OrganisationInvitesModelTest

Bases: TestCase

test_create_organisation_invite

test_create_organisation_invite()

Test create organisation invite.

Source code in django_project/stakeholder/tests/test_stakeholder_models.py
def test_create_organisation_invite(self):
    """Test create organisation invite."""
    invite = OrganisationInvites.objects.create(
        organisation=self.organisation, email='test@kartoza.com')
    self.assertEqual(invite.organisation, self.organisation)
    self.assertEqual(invite.email, 'test@kartoza.com')

test_delete_organisation_invite

test_delete_organisation_invite()

Test delete organisation invite.

Source code in django_project/stakeholder/tests/test_stakeholder_models.py
def test_delete_organisation_invite(self):
    """Test delete organisation invite."""
    invite = OrganisationInvites.objects.create(
        organisation=self.organisation, email='test@kartoza.com')
    invite.delete()
    self.assertFalse(
        OrganisationInvites.objects.filter(pk=invite.pk).exists())

test_read_organisation_invite

test_read_organisation_invite()

Test read organisation invite.

Source code in django_project/stakeholder/tests/test_stakeholder_models.py
def test_read_organisation_invite(self):
    """Test read organisation invite."""
    invite = OrganisationInvites.objects.create(
        organisation=self.organisation, email='test@kartoza.com')
    saved_invite = OrganisationInvites.objects.get(pk=invite.pk)
    self.assertEqual(saved_invite.organisation, self.organisation)
    self.assertEqual(saved_invite.email, 'test@kartoza.com')

test_update_organisation_invite

test_update_organisation_invite()

Test update organisation invite.

Source code in django_project/stakeholder/tests/test_stakeholder_models.py
def test_update_organisation_invite(self):
    """Test update organisation invite."""
    invite = OrganisationInvites.objects.create(
        organisation=self.organisation, email='test@kartoza.com')
    invite.organisation = self.organisation
    invite.save()
    updated_invite = OrganisationInvites.objects.get(pk=invite.pk)
    self.assertEqual(updated_invite.organisation, self.organisation)

OrganizationRepresentativeTestCase

Bases: TestCase

Test case for organization representative.

setUpTestData classmethod

setUpTestData()

Setup test data for organisation representative model.

Source code in django_project/stakeholder/tests/test_stakeholder_models.py
@classmethod
def setUpTestData(cls):
    """Setup test data for organisation representative model."""
    cls.organizationRep = organisationRepresentativeFactory()

test_create_organisation_user

test_create_organisation_user()

Test creating organisation representative.

Source code in django_project/stakeholder/tests/test_stakeholder_models.py
def test_create_organisation_user(self):
    """Test creating organisation representative."""
    self.assertEqual(OrganisationRepresentative.objects.count(), 1)
    self.assertTrue(
        isinstance(
            self.organizationRep,
            OrganisationRepresentative
        )
    )

test_delete_organisation_user

test_delete_organisation_user()

Test deleting organisation representative.

Source code in django_project/stakeholder/tests/test_stakeholder_models.py
def test_delete_organisation_user(self):
    """Test deleting organisation representative."""
    self.organizationRep.delete()
    self.assertEqual(OrganisationRepresentative.objects.count(), 0)

test_update_organisation_user

test_update_organisation_user()

Test updating organisation representative.

Source code in django_project/stakeholder/tests/test_stakeholder_models.py
def test_update_organisation_user(self):
    """ Test updating organisation representative."""
    self.organizationRep.user.username = 'test'
    self.organizationRep.user.save()
    self.assertEqual(
        self.organizationRep.user.username,
        'test'
    )

test_upgrade_to_manager

test_upgrade_to_manager(mocked_send_mail)

Test upgrade a member to manager.

Source code in django_project/stakeholder/tests/test_stakeholder_models.py
@mock.patch('stakeholder.utils.send_mail')
def test_upgrade_to_manager(self, mocked_send_mail):
    """Test upgrade a member to manager."""
    mocked_send_mail.side_effect = mocked_sending_email
    organisation_1 = organisationFactory()
    # user without email
    user_0 = User.objects.create_user(
        username='testuser0',
        password='testpassword0'
    )
    OrganisationRepresentative.objects.create(
        user=user_0,
        organisation=organisation_1
    )
    mocked_send_mail.assert_not_called()
    self.assertTrue(OrganisationUser.objects.filter(
        user=user_0,
        organisation=organisation_1
    ).exists())
    # from no org_user - possible from admin site
    mocked_send_mail.reset_mock()
    user_1 = User.objects.create_user(
        username='testuser1',
        password='testpassword1',
        email='testuser1@test.com'
    )
    OrganisationRepresentative.objects.create(
        user=user_1,
        organisation=organisation_1
    )
    mocked_send_mail.assert_called_once()
    self.assertEqual(OrganisationUser.objects.filter(
        user=user_1,
        organisation=organisation_1
    ).count(), 1)
    # from existing org_user with invite = Member
    mocked_send_mail.reset_mock()
    user_2 = User.objects.create_user(
        username='testuser2',
        password='testpassword2',
        email='testuser2@test.com'
    )
    OrganisationUser.objects.create(
        user=user_2,
        organisation=organisation_1
    )
    OrganisationInvites.objects.create(
        user=user_2,
        joined=True,
        assigned_as=MEMBER
    )
    OrganisationRepresentative.objects.create(
        user=user_2,
        organisation=organisation_1
    )
    mocked_send_mail.assert_called_once()
    self.assertEqual(OrganisationUser.objects.filter(
        user=user_2,
        organisation=organisation_1
    ).count(), 1)
    # from existing org_user with invite = Manager
    mocked_send_mail.reset_mock()
    user_3 = User.objects.create_user(
        username='testuser3',
        password='testpassword3',
        email='testuser3@test.com'
    )
    OrganisationUser.objects.create(
        user=user_3,
        organisation=organisation_1
    )
    OrganisationInvites.objects.create(
        user=user_3,
        joined=True,
        assigned_as=MANAGER
    )
    OrganisationRepresentative.objects.create(
        user=user_3,
        organisation=organisation_1
    )
    mocked_send_mail.assert_not_called()
    self.assertEqual(OrganisationUser.objects.filter(
        user=user_3,
        organisation=organisation_1
    ).count(), 1)
    # from existing org_user without any invite
    mocked_send_mail.reset_mock()
    user_4 = User.objects.create_user(
        username='testuser4',
        password='testpassword4',
        email='testuser4@test.com'
    )
    OrganisationUser.objects.create(
        user=user_4,
        organisation=organisation_1
    )
    OrganisationRepresentative.objects.create(
        user=user_4,
        organisation=organisation_1
    )
    mocked_send_mail.assert_called_once()
    self.assertEqual(OrganisationUser.objects.filter(
        user=user_4,
        organisation=organisation_1
    ).count(), 1)

OrganizationTestCase

Bases: TestCase

Organization test case.

test_create_organization

test_create_organization()

Test creating organization.

Source code in django_project/stakeholder/tests/test_stakeholder_models.py
def test_create_organization(self):
    """Test creating organization."""
    self.assertEqual(Organisation.objects.count(), 1)
    self.assertTrue(isinstance(self.organization, Organisation))
    self.assertTrue(self.organization.name, Organisation.objects.get(
        id=self.organization.id).name)
    self.assertEqual(
        self.organization.short_code,
        'CA0001'
    )

test_delete_organization

test_delete_organization()

Test deleting organization.

Source code in django_project/stakeholder/tests/test_stakeholder_models.py
def test_delete_organization(self):
    """Test deleting organization."""
    self.organization.delete()
    self.assertEqual(Organisation.objects.count(), 0)

test_update_organization

test_update_organization()

Test updating organization.

Source code in django_project/stakeholder/tests/test_stakeholder_models.py
def test_update_organization(self):
    """Test updating organization."""
    province, created = Province.objects.get_or_create(
                name="Limpopo"
    )
    property_1 = PropertyFactory.create(
        organisation=self.organization,
        province=province
    )
    property_2 = PropertyFactory.create(
        organisation=self.organization,
        province=province
    )
    self.organization.name = 'test'
    self.organization.national = True
    self.organization.province = province
    self.organization.save()
    self.organization.refresh_from_db()
    property_1.refresh_from_db()
    property_2.refresh_from_db()
    self.assertEqual(Organisation.objects.get(
        id=self.organization.id).name, 'test')
    self.assertEqual(Organisation.objects.filter(
        national=True).count(), 1)
    self.assertEqual(Organisation.objects.filter(
        province__name="Limpopo").count(), 1)

    self.assertEqual(
        self.organization.short_code,
        'LITE0001'
    )

OrganizationUserTestCase

Bases: TestCase

Test case for organization user.

setUpTestData classmethod

setUpTestData()

Setup test data for organisation user model.

Source code in django_project/stakeholder/tests/test_stakeholder_models.py
@classmethod
def setUpTestData(cls):
    """Setup test data for organisation user model."""
    cls.organizationUser = organisationUserFactory()
    cls.user = UserFactory()
    cls.organisation = organisationFactory()

test_create_organisation_user_default

test_create_organisation_user_default()

Test creating organisation user, defaulted to Organisation Member.

Source code in django_project/stakeholder/tests/test_stakeholder_models.py
def test_create_organisation_user_default(self):
    """Test creating organisation user, defaulted to Organisation Member."""
    self.assertEqual(OrganisationUser.objects.count(), 1)
    self.assertTrue(isinstance(self.organizationUser, OrganisationUser))
    self.assertEqual(
        self.organizationUser.user.groups.first().name,
        ORGANISATION_MEMBER
    )

test_create_organisation_user_manager

test_create_organisation_user_manager()

Test creating organisation user as manager.

Source code in django_project/stakeholder/tests/test_stakeholder_models.py
def test_create_organisation_user_manager(self):
    """Test creating organisation user as manager."""
    OrganisationRepresentative.objects.create(
        user=self.user,
        organisation=self.organisation
    )
    groups = Group.objects.filter(
        user=self.user
    )
    all_groups = [group.name for group in groups]
    self.assertIn(
        ORGANISATION_MANAGER,
        all_groups
    )
    self.assertIn(
        ORGANISATION_MEMBER,
        all_groups
    )

test_create_organisation_user_member

test_create_organisation_user_member()

Test creating organisation user as member.

Source code in django_project/stakeholder/tests/test_stakeholder_models.py
def test_create_organisation_user_member(self):
    """Test creating organisation user as member."""
    OrganisationInvites.objects.create(
        email=self.user.email,
        joined=True,
        assigned_as=MEMBER,
        organisation=self.organisation
    )
    organisation_user = organisationUserFactory(
        user=self.user,
        organisation=self.organisation
    )
    self.assertEqual(
        organisation_user.user.groups.first().name,
        ORGANISATION_MEMBER
    )

test_delete_organisation_user

test_delete_organisation_user()

Test deleting organisation user.

Source code in django_project/stakeholder/tests/test_stakeholder_models.py
def test_delete_organisation_user(self):
    """Test deleting organisation user."""
    user = self.organizationUser.user
    group = GroupF.create(name='Data contributor')
    user.groups.add(group)
    organisation_user_2 = organisationUserFactory.create(
        user=user
    )

    OrganisationInvites.objects.create(
        email=self.user.email,
        joined=True,
        assigned_as=MEMBER,
        organisation=self.organisation
    )
    organisation_user_3 = organisationUserFactory(
        user=self.user,
        organisation=self.organisation
    )
    organisation_user_4 = organisationUserFactory(
        user=self.user
    )
    self.assertEquals(
        list(organisation_user_3.user.groups.values_list('name', flat=True)),
        [ORGANISATION_MEMBER]
    )

    # delete organizationUser
    # user would still be in group Organisation Member
    # because he still belongs to other organisation
    self.organizationUser.delete()
    self.assertEqual(OrganisationUser.objects.count(), 3)
    self.assertEqual(len(user.groups.all()), 1)
    self.assertEqual(user.groups.first().name, ORGANISATION_MEMBER)

    # set userprofile to organisation 4
    user_profile = UserProfile.objects.filter(user=organisation_user_2.user).first()
    self.assertTrue(user_profile)
    user_profile.current_organisation = organisation_user_4.organisation
    user_profile.save()
    # delete organisation_user_2
    # user would be removed from group Organisation Member
    # because he no longer belongs any organisation
    organisation_user_2.delete()
    self.assertEqual(OrganisationUser.objects.count(), 2)
    self.assertFalse(
        user.groups.exists()
    )
    # ensure current_organisation is not removed
    user_profile.refresh_from_db()
    self.assertTrue(user_profile.current_organisation)
    self.assertEqual(user_profile.current_organisation.id, organisation_user_4.organisation.id)

    # set userprofile to organisation 4
    user_profile = UserProfile.objects.filter(user=organisation_user_4.user).first()
    self.assertTrue(user_profile)
    user_profile.current_organisation = organisation_user_4.organisation
    user_profile.save()
    # set as manager
    OrganisationRepresentative.objects.create(
        user=organisation_user_4.user,
        organisation=organisation_user_4.organisation
    )
    # create invite
    OrganisationInvites.objects.create(
        organisation=organisation_user_4.organisation,
        email=organisation_user_4.user.email,
        user=organisation_user_4.user,
    )
    # delete organisation_user_4
    # user would not be removed from group Organisation Member
    # because is still a member of other organisation
    organisation_user_4.delete()
    self.assertEqual(OrganisationUser.objects.count(), 1)
    self.assertEquals(
        list(self.user.groups.values_list('name', flat=True)),
        [ORGANISATION_MEMBER]
    )
    # ensure no current_organisation after organisation 4 is deleted
    user_profile.refresh_from_db()
    self.assertFalse(user_profile.current_organisation)
    # ensure no manager record
    self.assertFalse(OrganisationRepresentative.objects.filter(
        user=organisation_user_4.user,
        organisation=organisation_user_4.organisation
    ).exists())
    # ensure no invitation
    self.assertFalse(OrganisationInvites.objects.filter(
        user=organisation_user_4.user,
        organisation=organisation_user_4.organisation
    ).exists())

test_update_organisation_user

test_update_organisation_user()

Test updating organisation user.

Source code in django_project/stakeholder/tests/test_stakeholder_models.py
def test_update_organisation_user(self):
    """ Test updating organisation user."""
    self.organizationUser.user.username = 'test'
    self.organizationUser.user.save()
    self.assertEqual(
        self.organizationUser.user.username,
        'test'
    )

TestUser

Bases: TestCase

Test the main user model relation to the profie model.

test_create_new_user_with_new_profile

test_create_new_user_with_new_profile()

Test creating new user when new profile is created.

Source code in django_project/stakeholder/tests/test_stakeholder_models.py
def test_create_new_user_with_new_profile(self):
    """Test creating new user when new profile is created."""
    self.assertGreater(UserProfile.objects.count(), 0)

test_delete_profile

test_delete_profile()

Test deleting user when a profile is deleted.

Source code in django_project/stakeholder/tests/test_stakeholder_models.py
def test_delete_profile(self):
    """Test deleting user when a profile is deleted."""
    user_id = self.user_profile.user.id
    self.user_profile.delete()
    self.assertEqual(User.objects.count(), 1)

    users = User.objects.filter(id=user_id)
    self.assertFalse(users.exists())

test_update_user_profile

test_update_user_profile()

Test updating user through profile.

Source code in django_project/stakeholder/tests/test_stakeholder_models.py
def test_update_user_profile(self):
    """Test updating user through profile."""
    self.user_profile.user.username = 'test'
    self.user_profile.user.first_name = 'test123'
    self.user_profile.user.save()
    self.assertEqual(
        User.objects.get(username='test').first_name, 'test123'
    )

TestUserLogin

Bases: TestCase

"User login testcase.

create_user_login

create_user_login()

Test creating new user login.

Source code in django_project/stakeholder/tests/test_stakeholder_models.py
def create_user_login(self):
    """Test creating new user login."""
    self.assertEqual(UserLogin.objects.count(), 1)
    self.assertEqual(User.objects.count(), 2)

test_delete_user_login

test_delete_user_login()

Test deleting user login.

Source code in django_project/stakeholder/tests/test_stakeholder_models.py
def test_delete_user_login(self):
    """Test deleting user login."""
    self.user_login.delete()
    self.assertEqual(UserLogin.objects.count(), 0)
    self.assertEqual(User.objects.count(), 2)

test_update_user_login

test_update_user_login()

Test updating user login.

Source code in django_project/stakeholder/tests/test_stakeholder_models.py
def test_update_user_login(self):
    """Test updating user login."""
    self.user_login.login_status.name = 'logged out'
    self.user_login.login_status.save()
    self.assertEqual(
        UserLogin.objects.get(id=self.user_login.id).login_status.name,
        'logged out'
    )

TestUserRoleType

Bases: TestCase

Test user's role type model.

test_create_new_role

test_create_new_role()

Test creating new role.

Source code in django_project/stakeholder/tests/test_stakeholder_models.py
def test_create_new_role(self):
    """Test creating new role."""
    self.assertEqual(UserRoleType.objects.count(), 1)
    self.assertTrue(
        self.UserRoleTypeFactory.name
        in ['base user', 'admin', 'super user']
    )

test_delete_role

test_delete_role()

Test deleting new role.

Source code in django_project/stakeholder/tests/test_stakeholder_models.py
def test_delete_role(self):
    """Test deleting new role."""
    self.UserRoleTypeFactory.delete()
    self.assertEqual(UserRoleType.objects.count(), 0)

test_update_role

test_update_role()

Test updating a role.

Source code in django_project/stakeholder/tests/test_stakeholder_models.py
def test_update_role(self):
    """Test updating a role."""
    self.UserRoleTypeFactory.name = 'admin'
    self.UserRoleTypeFactory.save()
    UserRoleObject = UserRoleType.objects.get(
        id=self.UserRoleTypeFactory.id
    )
    self.assertEqual(UserRoleObject.name, 'admin')

UserTitleTestCase

Bases: TestCase

User title test case.

test_create_new_title

test_create_new_title()

Test creating new title.

Source code in django_project/stakeholder/tests/test_stakeholder_models.py
def test_create_new_title(self):
    """Test creating new title."""
    self.assertEqual(UserTitle.objects.count(), 1)
    self.assertTrue(self.userTitle.name in ['mr', 'mrs', 'miss', 'dr'])

test_delete_new_title

test_delete_new_title()

Test deleting a title.

Source code in django_project/stakeholder/tests/test_stakeholder_models.py
def test_delete_new_title(self):
    """Test deleting a title."""
    self.userTitle.delete()
    self.assertEqual(UserTitle.objects.count(), 0)

test_update_title

test_update_title()

Test updating a title.

Source code in django_project/stakeholder/tests/test_stakeholder_models.py
def test_update_title(self):
    """Test updating a title."""
    self.userTitle.name = 'mr'
    userTitle = UserTitle.objects.get(id=self.userTitle.id)
    self.assertTrue(userTitle.name, 'mr')

TestProfileView

Bases: TestCase

Tests CURD on Profile Model and test update profile view.

test_404

test_404()

Test 404 mismatch user

Source code in django_project/stakeholder/tests/test_profile.py
def test_404(self):
    """
    Test 404 mismatch user
    """
    user = get_user_model().objects.create(
        is_staff=False,
        is_active=True,
        is_superuser=False,
        username='test',
        email='test@test.com',
    )
    device = TOTPDevice(
        user=self.test_user,
        name='device_name'
    )
    device.save()

    resp = self.client.login(username='testuser', password='testpassword')
    self.assertTrue(resp)

    response = self.client.post(
        '/profile/{}/'.format(user.username)
    )
    # if mismatch user
    self.assertEqual(response.status_code, 404)

test_post_with_data

test_post_with_data()

Test update profile from the form page

Source code in django_project/stakeholder/tests/test_profile.py
def test_post_with_data(self):
    """
    Test update profile from the form page
    """
    device = TOTPDevice(
        user=self.test_user,
        name='device_name'
    )
    device.save()
    title = userTitleFactory.create(
        id=1,
        name = 'test',
    )
    role = userRoleTypeFactory.create(
        id=1,
        name = 'test',
    )
    resp = self.client.login(username='testuser', password='testpassword')
    self.assertTrue(resp)

    post_dict = {
        'first-name': 'Fan',
        'last-name': 'Fan',
        'email': self.test_user.email,
        'organization': 'Kartoza',
        'profile_picture': '/profile/pic/path',
        'title': '1',
        'role': '1',
    }

    response = self.client.post(
        '/profile/{}/'.format(self.test_user.username), post_dict
    )
    self.assertEqual(response.status_code, 302)
    updated_user = get_user_model().objects.get(id=self.test_user.id)
    self.assertEqual(updated_user.first_name, 'Fan')
    self.assertEqual(updated_user.last_name, 'Fan')
    self.assertIsNotNone(updated_user.user_profile.picture)
    self.assertEqual(updated_user.user_profile.title_id.name, title.name)
    self.assertEqual(updated_user.user_profile.user_role_type_id.name, role.name)

test_profile_create

test_profile_create()

Tests profile creation

Source code in django_project/stakeholder/tests/test_profile.py
def test_profile_create(self):
    """
    Tests profile creation
    """
    user = UserF.create()

    self.assertTrue(user.user_profile is not None)

    profile = UserProfile.objects.get(
        id=user.user_profile.id
    )

    profile.picture = 'profile_pictures/picture_P.jpg'
    profile.save()

    self.assertEqual(UserProfile.objects.get(
        id=profile.id
    ).picture, 'profile_pictures/picture_P.jpg')

test_profile_delete

test_profile_delete()

Tests profile delete

Source code in django_project/stakeholder/tests/test_profile.py
def test_profile_delete(self):
    """
    Tests profile delete
    """
    user = UserF.create()
    profile = user.user_profile
    profile.delete()

    self.assertTrue(profile.pk is None)

test_profile_update

test_profile_update()

Tests profile update

Source code in django_project/stakeholder/tests/test_profile.py
def test_profile_update(self):
    """
    Tests profile update
    """
    user = UserF.create()
    profile = user.user_profile
    profile_picture = {
        'picture': 'profile_pictures/picture_P.jpg',
    }
    profile.__dict__.update(profile_picture)
    profile.first_name = 'j'
    profile.last_name = 'jj'
    profile.save()

    user.email = 't@t.com'
    user.save()

    self.assertIsNotNone(profile.picture)
    self.assertIsNotNone(profile.first_name)
    self.assertIsNotNone(profile.last_name)
    self.assertIsNotNone(user.email)

TestUpdatePropertyShortCode

Bases: TestCase

Update Property Short Code test case.

test_update_short_code_from_organisation

test_update_short_code_from_organisation()

Test updating property short code when organization is updated.

Source code in django_project/stakeholder/tests/test_tasks.py
def test_update_short_code_from_organisation(self):
    """Test updating property short code when organization is updated."""
    self.assertEqual(
        self.organization.short_code,
        'LICA0001'
    )
    property_1 = PropertyFactory.create(
        organisation=self.organization,
        province=self.province
    )
    property_2 = PropertyFactory.create(
        organisation=self.organization,
        province=self.province
    )
    self.organization.name = 'test'
    self.organization.national = True
    self.organization.province = self.province
    self.organization.save()

    # call task function
    update_property_short_code(self.organization.id)

    property_1.refresh_from_db()
    property_2.refresh_from_db()

    self.assertEqual(
        self.organization.short_code,
        'LITE0002'
    )
    self.assertEqual(
        property_1.short_code,
        'LITEPR0001'
    )
    self.assertEqual(
        property_2.short_code,
        'LITEPR0002'
    )

test_update_short_code_from_province

test_update_short_code_from_province()

Test updating property and organisaition short code when provincr is updated.

Source code in django_project/stakeholder/tests/test_tasks.py
def test_update_short_code_from_province(self):
    """
    Test updating property and organisaition short code when provincr is updated.
    """
    province, created = Province.objects.get_or_create(
        name="Western Cape"
    )
    property_1 = PropertyFactory.create(
        organisation=self.organization,
        province=self.province
    )
    property_2 = PropertyFactory.create(
        organisation=self.organization,
        province=self.province
    )
    self.organization.name = 'test'
    self.organization.national = True
    self.organization.province = province
    self.organization.save()

    # call task function
    update_property_short_code(self.organization.id)

    property_1.refresh_from_db()
    property_2.refresh_from_db()

    self.assertEqual(
        self.organization.short_code,
        'WCTE0001'
    )
    self.assertEqual(
        property_1.short_code,
        'LITEPR0001'
    )
    self.assertEqual(
        property_2.short_code,
        'LITEPR0002'
    )

Tasks

send_reminder_emails

send_reminder_emails(*args)

check any reminders that need to be sent, update reminder status and user notifications

Source code in django_project/stakeholder/tasks.py
@shared_task
def send_reminder_emails(*args):
    """check any reminders that need to be sent,
    update reminder status and user notifications"""
    current_datetime = timezone.now()
    due_reminders = Reminders.objects.filter(
        status=Reminders.ACTIVE,
        email_sent=False,
        date__lte=current_datetime
    )

    for reminder in due_reminders:
        if reminder.type == Reminders.PERSONAL:
            send_reminder_email(reminder.id)
            update_user_profile(reminder.user)
        else:
            org_users_list = OrganisationUser.objects.filter(
                organisation=reminder.organisation
            )
            for org_user in org_users_list:
                send_reminder_email(reminder.id, org_user.user.id)
                update_user_profile(org_user.user)
        reminder.status = Reminders.PASSED
        reminder.email_sent = True
        reminder.save()

Utils

add_user_to_org_manager

add_user_to_org_manager(instance, GroupModel=None)

Add user to Organisation Manager group.

Source code in django_project/stakeholder/utils.py
def add_user_to_org_manager(
    instance,
    GroupModel=None,
):
    """
    Add user to Organisation Manager group.
    """
    from django.contrib.auth.models import Group

    GroupModel = GroupModel if GroupModel else Group
    group, _ = GroupModel.objects.get_or_create(name=ORGANISATION_MANAGER)
    instance.user.groups.add(group)

add_user_to_org_member

add_user_to_org_member(
    instance, OrgInvModel=None, GroupModel=None
)

Add user to Organisation Member group.

Source code in django_project/stakeholder/utils.py
def add_user_to_org_member(
    instance,
    OrgInvModel=None,
    GroupModel=None,
):
    """
    Add user to Organisation Member group.
    """
    from stakeholder.models import OrganisationInvites
    from django.contrib.auth.models import Group

    OrgInvModel = OrgInvModel if OrgInvModel else OrganisationInvites
    GroupModel = GroupModel if GroupModel else Group
    group, _ = GroupModel.objects.get_or_create(name=ORGANISATION_MEMBER)
    instance.user.groups.add(group)

remove_user_from_org_manager

remove_user_from_org_manager(instance)

Remove user from Organisation Manager group.

Source code in django_project/stakeholder/utils.py
def remove_user_from_org_manager(instance):
    """
    Remove user from Organisation Manager group.
    """

    from stakeholder.models import OrganisationRepresentative
    from django.contrib.auth.models import Group

    organisation_reps = OrganisationRepresentative.objects.filter(
        user=instance.user
    )

    if not organisation_reps.exists():
        group = Group.objects.filter(name=ORGANISATION_MANAGER).first()
        if group:
            instance.user.groups.remove(group)

remove_user_from_org_member

remove_user_from_org_member(instance)

Remove user from Organisation Member group.

Source code in django_project/stakeholder/utils.py
def remove_user_from_org_member(instance):
    """
    Remove user from Organisation Member group.
    """

    from stakeholder.models import OrganisationUser
    from django.contrib.auth.models import Group

    organisation_users = OrganisationUser.objects.filter(user=instance.user)

    # Remove from Data Contributor groups if user is
    # no longer assigned to any organisation
    group = Group.objects.filter(name='Data contributor').first()
    if group:
        instance.user.groups.remove(group)

    if not organisation_users.exists():
        group = Group.objects.filter(name=ORGANISATION_MEMBER).first()
        if group:
            instance.user.groups.remove(group)

Views

OrganisationAPIView

Bases: APIView

Get organisation