Skip to content

Commit

Permalink
Add functionality to get invited users for a user and resend pending …
Browse files Browse the repository at this point in the history
…invites
  • Loading branch information
matthewelwell committed Sep 10, 2018
1 parent 8e84e58 commit 1192284
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 3 deletions.
20 changes: 20 additions & 0 deletions src/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,26 @@ def test_should_fail_if_invite_exists_already(self):
invites = Invite.objects.filter(email=email, organisation=organisation)
self.assertEquals(len(invites), 1)

def test_should_return_all_invites_and_can_resend(self):
# Given
client = self.set_up()
organisation_2 = Organisation.objects.create(name="Test org 2")
invite_1 = Invite.objects.create(email="test_1@example.com",
frontend_base_url="https://www.example.com",
organisation=organisation_2)
invite_2 = Invite.objects.create(email="test_2@example.com",
frontend_base_url="https://www.example.com",
organisation=organisation_2)

# When
invite_list_response = client.get('/api/v1/organisations/%s/invites/' % organisation_2.id)
invite_resend_response = client.post('/api/v1/organisations/%s/invites/%s/resend/' % (
organisation_2.id, invite_1.id))

# Then
self.assertEquals(invite_list_response.status_code, status.HTTP_200_OK)
self.assertEquals(invite_resend_response.status_code, status.HTTP_200_OK)


class ProjectTestCase(TestCase):

Expand Down
6 changes: 6 additions & 0 deletions src/organisations/urls.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
from django.conf.urls import url, include
from rest_framework_nested import routers

from organisations.views import InviteViewSet
from . import views


router = routers.DefaultRouter()
router.register(r'', views.OrganisationViewSet, base_name="organisation")

organisations_router = routers.NestedSimpleRouter(router, r'', lookup="organisation")
organisations_router.register(r'invites', InviteViewSet, base_name="organisation-invites")

app_name = "organisations"

urlpatterns = [
url(r'^', include(router.urls)),
url(r'^', include(organisations_router.urls))
]
18 changes: 17 additions & 1 deletion src/organisations/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from projects.serializers import ProjectSerializer
from organisations.serializers import OrganisationSerializer
from users.models import Invite
from users.serializers import UserFullSerializer, InviteSerializer, UserListSerializer
from users.serializers import UserFullSerializer, InviteSerializer, UserListSerializer, \
InviteListSerializer


class OrganisationViewSet(viewsets.ModelViewSet):
Expand Down Expand Up @@ -73,3 +74,18 @@ def invite(self, request, pk):
raise ValidationError(invites_serializer.errors)

return Response(status=status.HTTP_201_CREATED)


class InviteViewSet(viewsets.ModelViewSet):
serializer_class = InviteListSerializer

def get_queryset(self):
organisation_pk = self.kwargs.get('organisation_pk')
return Invite.objects.filter(organisation=organisation_pk)

@action(detail=True, methods=["POST"])
def resend(self, request, organisation_pk, pk):
invite = self.get_object()
invite.send_invite_mail()
return Response(status=status.HTTP_200_OK)

3 changes: 2 additions & 1 deletion src/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,13 @@ class Invite(models.Model):
email = models.EmailField()
hash = models.CharField(max_length=100, default=create_hash, unique=True)
date_created = models.DateTimeField('DateCreated', auto_now_add=True)
organisation = models.ForeignKey(Organisation, on_delete=models.CASCADE)
organisation = models.ForeignKey(Organisation, on_delete=models.CASCADE, related_name='invites')
frontend_base_url = models.CharField(max_length=500, null=False)
invited_by = models.ForeignKey(FFAdminUser, related_name='sent_invites', null=True)

class Meta:
unique_together = ('email', 'organisation')
ordering = ['organisation', 'date_created']

def save(self, *args, **kwargs):
# send email invite before saving invite
Expand Down
10 changes: 9 additions & 1 deletion src/users/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,12 @@ def save(self, request):
class InviteSerializer(serializers.ModelSerializer):
class Meta:
model = Invite
fields = ('email', 'organisation', 'frontend_base_url', 'invited_by')
fields = ('email', 'organisation', 'frontend_base_url', 'invited_by', 'date_created')


class InviteListSerializer(serializers.ModelSerializer):
invited_by = UserListSerializer()

class Meta:
model = Invite
fields = ('id', 'email', 'date_created', 'invited_by')

0 comments on commit 1192284

Please sign in to comment.