def create_invitation(request, team_pk): email = None if request.method == 'POST': if 'email' in request.data: email = request.data['email'] else: # TODO: Throw exception or return a 400 return Response(status=status.HTTP_400_BAD_REQUEST) user = Employee.objects.get(user=request.user) team = user.team invitation = Invitation.create(email, team) invitation.send() invitation.save() return Response(status=status.HTTP_201_CREATED)
def setUp(self): self.author = self.createUser(username="******") self.inviter = self.createUser(username="******") self.invitee = self.createUser(username="******") self.notification_day = Notification(name="1 day", duration=timedelta(days=1)) self.notification_day.save() self.notification_week = Notification(name="7 day", duration=timedelta(days=7)) self.notification_week.save() self.event = Event.create("Event name",self.author, "Some interesting place") self.event.starts = timezone.now() + timedelta(hours=23) self.event.save() self.invitation_context = InvitationContext(inviter=self.inviter, event=self.event) self.invitation_context.save() self.invitation = Invitation(context=self.invitation_context, invitee=self.invitee) self.invitation.save() self.invitation_notification_day = InvitationNotification( invitation=self.invitation, notification=self.notification_day) self.invitation_notification_week = InvitationNotification( invitation=self.invitation, notification=self.notification_week) self.invitation_notification_day.save() self.invitation_notification_week.save() self.signal_by_user = None self.signal_event = None self.signal_listened = False
def signup(request): if request.method == 'POST': form = VerifyForm(request.POST) if form.is_valid(): invitation = Invitation( name=form.cleaned_data['other_name'], organization=form.cleaned_data['organization'], email=form.cleaned_data['alternative_email'], code=User.objects.make_random_password(6) ) invitation.save() invitation.send_invite() messages.success(request, message='An invitation has been sent to %s, please check your email to ' 'access next steps.' % invitation.email ) Medic.objects.filter(other_name__iexact=form.cleaned_data['other_name'], surname__iexact=form.cleaned_data['surname']).\ update(verification_status=True, invitation_status=True) else: form = VerifyForm() return render(request, 'userprofile/signup.html', {'form': form})
def post(self, request, format=None, *args, **kwargs): influencer_id = kwargs['pk'] action = request.data['action'] campaign_id = request.data['campaign_id'] influencer = Analysis.objects.get(pk=influencer_id) campaign = Campaign.objects.get(pk=campaign_id) code = "" if action=='invite': #send invitation in instagram try: invitations = Invitation.objects.filter(client=request.user, campaign__id=campaign_id, analysis=influencer) if invitations.count() > 0: # invitations.update(status='SE', last_sent_at=datetime.now()) # code = 'success' if send_invitation(influencer.influencer_account, influencer.influencer_platform, campaign_id, invitations.first().invitation_key): invitations.update(status='SE', last_sent_at=datetime.now()) code = 'success' else: code = 'failed' else: invitation = Invitation() invitation.client = request.user invitation.campaign = campaign invitation.influencer_account = influencer.influencer_account invitation.influencer_platform = influencer.influencer_platform invitation.invitation_content = "We invite you to the marketing campaign you'd be interested in." invitation.status = 'CR' invitation.analysis = influencer invitation.save() # invitation.status = 'SE' # invitation.last_sent_at = datetime.now() # invitation.save() # code = 'success' if send_invitation(influencer.influencer_account, influencer.influencer_platform, campaign_id, invitation.invitation_key): invitation.status = 'SE' invitation.last_sent_at = datetime.now() print('api/v1/invitation/views.py/inviation.last_sent_at+++++++++++++++++++++++++++++++++++++++', invitation.last_sent_at) invitation.save() code = 'success' else: code = 'failed' except: code = 'failed' elif action=="reinvite": try: if send_invitation(influencer.influencer_account, influencer.influencer_platform, campaign_id, invitation.invitation_key): Invitation.objects.filter(client=request.user, campaign__id=campaign_id, analysis=influencer).update(status='SE') code = 'success' else: code = 'failed' except: code = 'failed' else: try: Invitation.objects.filter(client=request.user, campaign__id=campaign_id, analysis=influencer).update(status='CA') code = 'success' except: code = 'failed' if code == 'success': return Response(status=status.HTTP_201_CREATED) elif code == 'failed': return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR) else: return Response(status=status.HTTP_400_BAD_REQUEST)
class InvitationTest(TestCase): def createUser(self,username): user = User(username=username) user.save() return user def setUp(self): self.author = self.createUser(username="******") self.inviter = self.createUser(username="******") self.invitee = self.createUser(username="******") self.notification_day = Notification(name="1 day", duration=timedelta(days=1)) self.notification_day.save() self.notification_week = Notification(name="7 day", duration=timedelta(days=7)) self.notification_week.save() self.event = Event.create("Event name",self.author, "Some interesting place") self.event.starts = timezone.now() + timedelta(hours=23) self.event.save() self.invitation_context = InvitationContext(inviter=self.inviter, event=self.event) self.invitation_context.save() self.invitation = Invitation(context=self.invitation_context, invitee=self.invitee) self.invitation.save() self.invitation_notification_day = InvitationNotification( invitation=self.invitation, notification=self.notification_day) self.invitation_notification_week = InvitationNotification( invitation=self.invitation, notification=self.notification_week) self.invitation_notification_day.save() self.invitation_notification_week.save() self.signal_by_user = None self.signal_event = None self.signal_listened = False def test_accept_invitation(self): """ Tests the accept method on InvitationModel. """ def invitation_accepted_listener(sender, by_user, event, **kwargs): self.signal_by_user = by_user self.signal_event = event # Connects to the signal invitation_accepted.connect(invitation_accepted_listener, sender=self.invitation) # Default value of accepted is none then accepts it self.assertIsNone(self.invitation.accepted) self.assertTrue(self.invitation.accept()) # The accept action updated it. self.assertIsNotNone(self.invitation.accepted) # Tests that the signal has been sent and received. self.assertEqual(self.invitee, self.signal_by_user) self.assertEqual(self.event, self.signal_event) def test_reject_invitation(self): """ Tests the reject method on InvitationModel. """ def invitation_rejected_listener(sender, by_user, event, **kwargs): self.signal_by_user = by_user self.signal_event = event # Connects to the signal invitation_rejected.connect(invitation_rejected_listener, sender=self.invitation) # Default value of rejected is none then rejects it self.assertIsNone(self.invitation.rejected) self.assertTrue(self.invitation.reject()) # The reject action updated it. self.assertIsNotNone(self.invitation.rejected) # Tests that the signal has been sent and received. self.assertEqual(self.invitee, self.signal_by_user) self.assertEqual(self.event, self.signal_event) def test_mark_viewed_invitation(self): """ Tests the mark_viewed method on InvitationModel. """ def invitation_viewed_listener(sender, **kwargs): self.signal_listened = True # Connects to the signal invitation_viewed.connect(invitation_viewed_listener, sender=self.invitation) # Default value of viewed is none self.assertIsNone(self.invitation.viewed) self.assertTrue(self.invitation.mark_viewed()) # The view action updated it. self.assertIsNotNone(self.invitation.viewed) # Tests that the signal has been sent and received. self.assertTrue(self.signal_listened) def test_find_invitations_to_notify(self): invitations = find_invitations_to_notify() # Tests that the invitation was returned. self.assertEqual(len(invitations),1) # Tests that the invitation was returned. self.assertEqual(invitations[0][0].id,self.invitation.id) # This is the shortest delay which has been taken into account. self.assertEqual(invitations[0][1],self.notification_day.duration.days * 86400 * 1000000) # Checks hat the notification has been set to sent ! try: invitation_notification = InvitationNotification.objects.get(id=self.invitation_notification_day.id) except InvitationNotification.DoesNotExist: invitation_notification = None self.assertIsNotNone(invitation_notification) self.assertTrue(invitation_notification.sent)