Exemplo n.º 1
0
    def test_str_invitation_context(self):
        """
        Test that creating an invitation context from a string
        produces a context with a message.

        """
        context = invitation_context("This is a test")
        self.assertEqual(context, {"message": " This is a test."})
Exemplo n.º 2
0
    def test_str_invitation_context(self):
        """
        Test that creating an invitation context from a string
        produces a context with a message.

        """
        context = invitation_context("This is a test")
        self.assertEqual(context, {"message": " This is a test."})
Exemplo n.º 3
0
    def send(self, context_object=""):
        """
        Inputs:
            :context_object: An object used to determine the invitation
                             context. A string signifies a custom message
                             context. Other types can specify a context by
                             registering with
                             `universal.helpers.invitation_context`.

        Outputs:
            An invitation email is sent to the ```Invitation.invitee``` using
            the ```Invitation.email``` address. Tailored messages are sent if
            ```Invitation.added_saved_search``` or
            ```Invitation.added_permission are set. If neither are set and no
            ```context``` is set, a generic email is sent.

        """
        activation_profile, _ = ActivationProfile.objects.get_or_create(
            user=self.invitee, email=self.invitee.email)

        if activation_profile.activation_key_expired():
            activation_profile.reset_activation()
            activation_profile  = ActivationProfile.objects.get(
                pk=activation_profile.pk)

        context = {'invitation': self,
                   'activation_key': activation_profile.activation_key}
        context.update(invitation_context(context_object))

        body = Pynliner().from_string(render_to_string(
            'registration/invitation_email.html', context)).run()

        if self.inviting_user:
            from_ = self.inviting_user.email
        else:
            from_ = self.inviting_company.name

        headers = {
            'X-SMTPAPI': '{"category": "Invitation Sent (%s)"}' % self.pk
        }

        fail_message = None
        try:
            self.invitee.email_user(body, email_type=settings.INVITATION,
                                    inviter=from_, headers=headers,
                                    text_only=context.get('text_only', False))
        except Exception as e:
            fail_message = getattr(e, 'smtp_error', e.message)
        else:
            activation_profile.sent = datetime_now()
            activation_profile.save()
        finally:
            saved_search = context.get('saved_search')
            if saved_search and hasattr(saved_search, 'partnersavedsearch'):
                saved_search.partnersavedsearch.create_record(
                    "Automatic sending of initial partner saved search",
                    body=context['initial_search_email'],
                    failure_message=fail_message)
Exemplo n.º 4
0
 def test_role_invitation_context(self):
     """
     Test that reating an invitation context from a role instance produces
     a context with a message and the role itself.
     """
     role = RoleFactory()
     context = invitation_context(role)
     self.assertEqual(context, {
         "message": " as a(n) %s for %s." % (role.name, role.company),
         "role": role})
Exemplo n.º 5
0
    def test_unimplemented_invitation_context(self):
        """
        Test that trying to call invitation_context for an unregistered type
        should raise a sensible error.
        """

        with self.assertRaises(TypeError) as cm:
            context = invitation_context(None)

        self.assertEqual(
            cm.exception.message,
            "object of type 'NoneType' has no invitation_context.")
Exemplo n.º 6
0
    def test_unimplemented_invitation_context(self):
        """
        Test that trying to call invitation_context for an unregistered type
        should raise a sensible error.
        """

        with self.assertRaises(TypeError) as cm:
            context = invitation_context(None)

        self.assertEqual(
            cm.exception.message,
            "object of type 'NoneType' has no invitation_context.")
Exemplo n.º 7
0
 def test_role_invitation_context(self):
     """
     Test that reating an invitation context from a role instance produces
     a context with a message and the role itself.
     """
     role = RoleFactory()
     context = invitation_context(role)
     self.assertEqual(
         context, {
             "message": " as a(n) %s for %s." % (role.name, role.company),
             "role": role
         })
Exemplo n.º 8
0
    def test_saved_search_invitation_context(self):
        """
        Test that creating an invitation context from a saved search instance
        produces a context with a message, the saved search itself, an unsent
        initial email, and whether that saved search is text only.

        """
        saved_search = SavedSearchFactory(user=self.user, text_only=True)
        context = invitation_context(saved_search)

        self.assertEqual(context, {
            "message": " in order to begin receiving their available job "
                       "opportunities on a regular basis.",
            "saved_search": saved_search,
            "initial_search_email": saved_search.initial_email(send=False),
            "text_only": saved_search.text_only})
Exemplo n.º 9
0
    def test_saved_search_invitation_context(self):
        """
        Test that creating an invitation context from a saved search instance
        produces a context with a message, the saved search itself, an unsent
        initial email, and whether that saved search is text only.

        """
        saved_search = SavedSearchFactory(user=self.user, text_only=True)
        context = invitation_context(saved_search)

        self.assertEqual(
            context, {
                "message": " in order to begin receiving their available job "
                "opportunities on a regular basis.",
                "saved_search": saved_search,
                "initial_search_email": saved_search.initial_email(send=False),
                "text_only": saved_search.text_only
            })
Exemplo n.º 10
0
    def send(self, context_object=""):
        """
        Inputs:
            :context_object: An object used to determine the invitation
                             context. A string signifies a custom message
                             context. Other types can specify a context by
                             registering with
                             `universal.helpers.invitation_context`.

        Outputs:
            An invitation email is sent to the ```Invitation.invitee``` using
            the ```Invitation.email``` address. Tailored messages are sent if
            ```Invitation.added_saved_search``` or
            ```Invitation.added_permission are set. If neither are set and no
            ```context``` is set, a generic email is sent.

        """
        activation_profile, _ = ActivationProfile.objects.get_or_create(
            user=self.invitee, email=self.invitee.email)

        if activation_profile.activation_key_expired():
            activation_profile.reset_activation()
            activation_profile = ActivationProfile.objects.get(
                pk=activation_profile.pk)

        context = {
            'invitation': self,
            'activation_key': activation_profile.activation_key
        }
        context.update(invitation_context(context_object))

        body = Pynliner().from_string(
            render_to_string('registration/invitation_email.html',
                             context)).run()

        if self.inviting_user:
            from_ = self.inviting_user.email
        else:
            from_ = self.inviting_company.name

        headers = {
            'X-SMTPAPI': '{"category": "Invitation Sent (%s)"}' % self.pk
        }

        fail_message = None
        try:
            self.invitee.email_user(body,
                                    email_type=settings.INVITATION,
                                    inviter=from_,
                                    headers=headers,
                                    text_only=context.get('text_only', False))
        except Exception as e:
            fail_message = getattr(e, 'smtp_error', e.message)
        else:
            activation_profile.sent = datetime_now()
            activation_profile.save()
        finally:
            saved_search = context.get('saved_search')
            if saved_search and hasattr(saved_search, 'partnersavedsearch'):
                saved_search.partnersavedsearch.create_record(
                    "Automatic sending of initial partner saved search",
                    body=context['initial_search_email'],
                    failure_message=fail_message)