Ejemplo n.º 1
0
def partner_savedsearch_save(request):
    """
    Handles saving the PartnerSavedSearchForm and creating the inactive user
    if it is needed.

    """
    company, partner, user = prm_worthy(request)
    item_id = request.REQUEST.get("id", None)

    if item_id:
        item = get_object_or_404(PartnerSavedSearch, id=item_id, provider=company.id)
        form = PartnerSavedSearchForm(instance=item, auto_id=False, data=request.POST, partner=partner, request=request)
        if form.is_valid():
            form.save()
            return HttpResponse(status=200)
        else:
            return HttpResponse(json.dumps(form.errors))
    form = PartnerSavedSearchForm(request.POST, partner=partner, request=request)

    # Since the feed is created below, this will always be invalid.
    if "feed" in form.errors:
        del form.errors["feed"]

    if form.is_valid():
        instance = form.instance
        instance.provider = company
        instance.partner = partner
        instance.created_by = request.user
        instance.custom_message = instance.partner_message
        form.save()
        return HttpResponse(status=200)
    else:
        return HttpResponse(json.dumps(form.errors))
Ejemplo n.º 2
0
    def test_disable_partner_saved_search(self):
        pss = PartnerSavedSearch.objects.get()

        # Partner saved search can be activated/deactivated if unsubscriber
        # is not one of the recipient's email addresses.
        form = PartnerSavedSearchForm(instance=pss, request=self.request,
                                      data=self.partner_search_data)
        self.assertFalse(form.fields['is_active'].widget.attrs.get('disabled',
                                                                   False))

        pss.unsubscriber = pss.email
        pss.save()

        # PRM users can no longer toggle the state of this partner saved search
        # as the user has unsubscribed.
        form = PartnerSavedSearchForm(instance=pss, request=self.request,
                                      data=self.partner_search_data)
        self.assertTrue(form.fields['is_active'].widget.attrs.get('disabled',
                                                                  False))

        # Since the unsubscriber is also the recipient, the recipient can still
        # toggle the state of this partner saved search.
        self.request.user = pss.user
        form = PartnerSubSavedSearchForm(instance=pss, request=self.request,
                                         data=self.partner_search_data)
        self.assertFalse(form.fields['is_active'].widget.attrs.get('disabled',
                                                                   False))
        self.assertTrue(form.is_valid())
Ejemplo n.º 3
0
    def test_user_only_linked_to_contact_after_pss_created(self):
        """
        A contact who's email so happens to coincide with an existing user's
        should not be attached to that user until after a partner saved search
        is created for it.
        """

        # we don't have a saved search, so the contact shouldn't be associated
        # with a user
        contact = ContactFactory(user=None,
                                 email="*****@*****.**",
                                 partner=self.partner)
        user = UserFactory(email=contact.email)
        self.assertFalse(contact.user)
        self.partner_search_data['email'] = contact.email
        form = PartnerSavedSearchForm(partner=self.partner,
                                      data=self.partner_search_data,
                                      request=self.request)

        instance = form.instance
        instance.provider = self.company
        instance.partner = self.partner
        instance.created_by = self.user
        instance.custom_message = instance.partner_message
        self.assertTrue(form.is_valid())
        form.save()
        # after the saved search was created, the user should have been
        # associated automatically
        contact = Contact.objects.get(id=contact.id)
        self.assertTrue(contact.user)
Ejemplo n.º 4
0
    def test_disable_partner_saved_search(self):
        pss = PartnerSavedSearch.objects.get()

        # Partner saved search can be activated/deactivated if unsubscriber
        # is not one of the recipient's email addresses.
        form = PartnerSavedSearchForm(instance=pss, request=self.request,
                                      data=self.partner_search_data)
        self.assertFalse(form.fields['is_active'].widget.attrs.get('disabled',
                                                                   False))

        pss.unsubscriber = pss.email
        pss.save()

        # PRM users can no longer toggle the state of this partner saved search
        # as the user has unsubscribed.
        form = PartnerSavedSearchForm(instance=pss, request=self.request,
                                      data=self.partner_search_data)
        self.assertTrue(form.fields['is_active'].widget.attrs.get('disabled',
                                                                  False))

        # Since the unsubscriber is also the recipient, the recipient can still
        # toggle the state of this partner saved search.
        self.request.user = pss.user
        form = PartnerSubSavedSearchForm(instance=pss, request=self.request,
                                         data=self.partner_search_data)
        self.assertFalse(form.fields['is_active'].widget.attrs.get('disabled',
                                                                   False))
        self.assertTrue(form.is_valid())
Ejemplo n.º 5
0
    def test_user_only_linked_to_contact_after_pss_created(self):
        """
        A contact who's email so happens to coincide with an existing user's
        should not be attached to that user until after a partner saved search
        is created for it.
        """

        # we don't have a saved search, so the contact shouldn't be associated
        # with a user
        contact = ContactFactory(user=None,
                                 email="*****@*****.**",
                                 partner=self.partner)
        user = UserFactory(email=contact.email)
        self.assertFalse(contact.user)
        self.partner_search_data['email'] = contact.email
        form = PartnerSavedSearchForm(partner=self.partner,
                                      data=self.partner_search_data,
                                      request=self.request)

        instance = form.instance
        instance.provider = self.company
        instance.partner = self.partner
        instance.created_by = self.user
        instance.custom_message = instance.partner_message
        self.assertTrue(form.is_valid())
        form.save()
        # after the saved search was created, the user should have been
        # associated automatically
        contact = Contact.objects.get(id=contact.id)
        self.assertTrue(contact.user)
Ejemplo n.º 6
0
 def pssform_last_action_time_updated_on_edit(self):
     """
         Verify saving partner saved search form causes last_action_time to update
     """
     original_time = self.instance.last_action_time
     new_form = PartnerSavedSearchForm(instance=self.instance, request=self.request, data=self.partner_search_data)
     self.assertTrue(new_form.is_valid())
     new_instance = new_form.save()
     self.assertEqual(self.instance.pk, new_instance.pk)
     self.assertNotEqual(new_instance.last_action_time, original_time)
Ejemplo n.º 7
0
    def setUp(self):
        super(PartnerSavedSearchFormTests, self).setUp()
        self.role.activities = self.activities
        CompanyUserFactory(user=self.user, company=self.company)
        self.partner = PartnerFactory(owner=self.company)

        self.contact = ContactFactory(user=None, email='*****@*****.**',
                                      partner=self.partner)
        self.partner_search_data = {
            'url': 'http://www.my.jobs/jobs',
            'feed': 'http://www.my.jobs/jobs/feed/rss?',
            'frequency': 'D',
            'label': 'All jobs from www.my.jobs',
            'sort_by': 'Relevance',
            'jobs_per_email': 5,
            'email': self.contact.email,
            'partner_message': "some partner message"
        }

        settings.SITE = SeoSite.objects.first()
        settings.SITE.canonical_company = self.company
        settings.SITE.save()
        # This request is only used in RequestForms, where all we care about
        # is request.user.
        self.request = RequestFactory().get(
            reverse('partner_savedsearch_save'))
        self.request.user = self.user
        self.request.impersonator = None

        form = PartnerSavedSearchForm(partner=self.partner,
                                      data=self.partner_search_data,
                                      request=self.request)
        instance = form.instance
        instance.provider = self.company
        instance.partner = self.partner
        instance.created_by = self.user
        instance.custom_message = instance.partner_message
        self.assertTrue(form.is_valid())
        self.instance = form.save()
Ejemplo n.º 8
0
    def setUp(self):
        super(PartnerSavedSearchFormTests, self).setUp()
        self.user = UserFactory()
        self.company = CompanyFactory()
        self.partner = PartnerFactory(owner=self.company)

        self.contact = ContactFactory(user=None, email='*****@*****.**',
                                      partner=self.partner)
        self.partner_search_data = {
            'url': 'http://www.my.jobs/jobs',
            'feed': 'http://www.my.jobs/jobs/feed/rss?',
            'frequency': 'D',
            'label': 'All jobs from www.my.jobs',
            'sort_by': 'Relevance',
            'jobs_per_email': 5,
            'email': self.contact.email,
        }

        settings.SITE = SeoSite.objects.first()
        # This request is only used in RequestForms, where all we care about
        # is request.user.
        self.request = RequestFactory().get(
            reverse('partner_savedsearch_save'))
        self.request.user = self.user

        form = PartnerSavedSearchForm(partner=self.partner,
                                      data=self.partner_search_data,
                                      request=self.request)
        instance = form.instance
        instance.provider = self.company
        instance.partner = self.partner
        instance.created_by = self.user
        instance.custom_message = instance.partner_message
        self.assertTrue(form.is_valid())
        form.save()

        self.patcher = patch('urllib2.urlopen', return_file())
        self.mock_urlopen = self.patcher.start()