def add_or_activate_saved_search(user, url): """ Attempt to add a new saved search to a user, or it is already exists, make sure it is active. :param user: User recieving search :param url: URL for search :return: The new or activated search """ if not url: raise ValueError("No URL provided") url = urllib.unquote(url) label, feed = validate_dotjobs_url(url, user) if not (label and feed): raise ValueError("Invalid .JOBS URL Provided") # Create notes field noting that it was created as current date/time now = datetime.datetime.now().strftime('%A, %B %d, %Y %l:%M %p') notes = 'Saved on ' + now if url.find('//') == -1: url = 'http://' + url netloc = urlparse.urlparse(url).netloc notes += ' from ' + netloc search_args = {'url': url, 'label': label, 'feed': feed, 'user': user, 'email': user.email, 'frequency': 'D', 'day_of_week': None, 'day_of_month': None, 'notes': notes} try: # if search already exists, activate it saved_search = SavedSearch.objects.get(user=search_args['user'], email__iexact=search_args['email'], url=search_args['url']) saved_search.is_active = True saved_search.save() except SavedSearch.DoesNotExist: # if there's no search for that email/user, create it saved_search = SavedSearch(**search_args) saved_search.save() saved_search.initial_email() return saved_search
def test_batch_month_old_message_digest_with_searhes(self): """ Posting data created a month ago should result in one EmailLog instance being created per message and one email being sent per user """ # Create activation profile for user; Used when disabling an account custom_signals.create_activation_profile(sender=self, user=self.user, email=self.user.email) now = date.today() month_ago = date.today() - timedelta(days=30) self.user.last_response = month_ago - timedelta(days=1) self.user.save() SavedSearch(user=self.user).save() # Submit a batch of events created a month ago # The owners of these addresses should be sent an email messages = self.make_messages(month_ago) response = self.client.post( reverse('batch_message_digest'), data=messages, content_type="text/json", HTTP_AUTHORIZATION='BASIC %s' % base64.b64encode('accounts%40my.jobs:secret')) self.assertTrue(response.status_code, 200) self.assertEqual(EmailLog.objects.count(), 3) self.assertEqual( EmailLog.objects.filter(received=month_ago).count(), 3) process_batch_events() self.assertEqual(len(mail.outbox), 1) user = User.objects.get(pk=self.user.pk) self.assertEqual(user.last_response, month_ago)
def test_unicode_in_search(self): search = SavedSearch(url=u"http://www.my.jobs/search?q=%E2%80%93", user=self.user, feed=u"http://www.my.jobs/search/feed/rss?q=%E2%80%93", sort_by=u'Relevance') search.save() feed_url = url_sort_options(search.feed, search.sort_by) old = parse_qs(urlparse(search.feed).query) new = parse_qs(urlparse(feed_url).query) self.assertFalse(old.get('date_sort')) self.assertTrue(new['date_sort'][0]) del new['date_sort'] self.assertEqual(new, old)
def test_unicode_in_search(self): search = SavedSearch( url=u"http://www.my.jobs/search?q=%E2%80%93", user=self.user, feed=u"http://www.my.jobs/search/feed/rss?q=%E2%80%93", sort_by=u'Relevance') search.save() feed_url = url_sort_options(search.feed, search.sort_by) old = parse_qs(urlparse(search.feed).query) new = parse_qs(urlparse(feed_url).query) self.assertFalse(old.get('date_sort')) self.assertTrue(new['date_sort'][0]) del new['date_sort'] self.assertEqual(new, old)
def add_or_activate_saved_search(user, url): """ Attempt to add a new saved search to a user, or it is already exists, make sure it is active. :param user: User recieving search :param url: URL for search :return: The new or activated search """ if not url: raise ValueError("No URL provided") url = urllib.unquote(url) label, feed = validate_dotjobs_url(url, user) if not (label and feed): raise ValueError("Invalid .JOBS URL Provided") # Create notes field noting that it was created as current date/time now = datetime.datetime.now().strftime('%A, %B %d, %Y %l:%M %p') notes = 'Saved on ' + now if url.find('//') == -1: url = 'http://' + url netloc = urlparse.urlparse(url).netloc notes += ' from ' + netloc search_args = { 'url': url, 'label': label, 'feed': feed, 'user': user, 'email': user.email, 'frequency': 'D', 'day_of_week': None, 'day_of_month': None, 'notes': notes } try: # if search already exists, activate it saved_search = SavedSearch.objects.get( user=search_args['user'], email__iexact=search_args['email'], url=search_args['url']) saved_search.is_active = True saved_search.save() except SavedSearch.DoesNotExist: # if there's no search for that email/user, create it saved_search = SavedSearch(**search_args) saved_search.save() saved_search.initial_email() return saved_search
def create_response(self, request, data, response_class=HttpResponse, **response_kwargs): """ Intercepts the default create_reponse(). Checks for existing saved search matching the user and url. If one doesn't exist, it creates a new saved search with daily email and the date/time created in the notes. Creates new JSON formatted "data" based on the success, failure, or error in saved search creation. Returns this data to the default create_response(). """ # Confirm email was provided, and that the user exists email = request.GET.get('email', '') if not email: data = {'error': 'No email provided'} return super(SavedSearchResource, self).create_response(request, data, response_class=HttpResponse, **response_kwargs) else: user = User.objects.get_email_owner(email=email) if not user: data = {'error': 'No user with email %s exists' % email} return super(SavedSearchResource, self).create_response(request, data, response_class=HttpResponse, **response_kwargs) # Confirm that url was provided, and that it's a valid .jobs search url = request.GET.get('url', '') if not url: data = {'error': 'No .JOBS feed provided'} return super(SavedSearchResource, self).create_response(request, data, response_class=HttpResponse, **response_kwargs) else: label, feed = validate_dotjobs_url(url, user) if not (label and feed): data = {'error': 'This is not a valid .JOBS feed'} return super(SavedSearchResource, self).create_response(request, data, response_class=HttpResponse, **response_kwargs) # Create notes field noting that it was created as current date/time now = datetime.datetime.now().strftime('%A, %B %d, %Y %l:%M %p') notes = 'Saved on ' + now if url.find('//') == -1: url = 'http://' + url netloc = urlparse(url).netloc notes += ' from ' + netloc search_args = { 'url': url, 'label': label, 'feed': feed, 'user': user, 'email': email, 'frequency': 'D', 'day_of_week': None, 'day_of_month': None, 'notes': notes } # if there's no search for that email/user, create it new_search = False try: SavedSearch.objects.get(user=search_args['user'], email__iexact=search_args['email'], url=search_args['url']) except SavedSearch.DoesNotExist: search = SavedSearch(**search_args) search.save() search.initial_email() new_search = True data = {'email': email, 'frequency': 'D', 'new_search': new_search} return super(SavedSearchResource, self).create_response(request, data, response_class=HttpResponse, **response_kwargs)
def create_response(self, request, data, response_class=HttpResponse, **response_kwargs): """ Intercepts the default create_reponse(). Checks for existing saved search matching the user and url. If one doesn't exist, it creates a new saved search with daily email and the date/time created in the notes. Creates new JSON formatted "data" based on the success, failure, or error in saved search creation. Returns this data to the default create_response(). """ # Confirm email was provided, and that the user exists email = request.GET.get('email', '') if not email: data = {'error': 'No email provided'} return super(SavedSearchResource, self).create_response( request, data, response_class=HttpResponse, **response_kwargs) else: user = User.objects.get_email_owner(email=email) if not user: data = {'error': 'No user with email %s exists' % email} return super(SavedSearchResource, self).create_response( request, data, response_class=HttpResponse, **response_kwargs) # Confirm that url was provided, and that it's a valid .jobs search url = request.GET.get('url', '') if not url: data = {'error': 'No .JOBS feed provided'} return super(SavedSearchResource, self).create_response( request, data, response_class=HttpResponse, **response_kwargs) else: label, feed = validate_dotjobs_url(url, user) if not (label and feed): data = {'error': 'This is not a valid .JOBS feed'} return super(SavedSearchResource, self).create_response( request, data, response_class=HttpResponse, **response_kwargs) # Create notes field noting that it was created as current date/time now = datetime.datetime.now().strftime('%A, %B %d, %Y %l:%M %p') notes = 'Saved on ' + now if url.find('//') == -1: url = 'http://' + url netloc = urlparse(url).netloc notes += ' from ' + netloc search_args = {'url': url, 'label': label, 'feed': feed, 'user': user, 'email': email, 'frequency': 'D', 'day_of_week': None, 'day_of_month': None, 'notes': notes} # if there's no search for that email/user, create it new_search = False try: SavedSearch.objects.get(user=search_args['user'], email__iexact=search_args['email'], url=search_args['url']) except SavedSearch.DoesNotExist: search = SavedSearch(**search_args) search.save() search.initial_email() new_search = True data = {'email': email, 'frequency': 'D', 'new_search': new_search} return super(SavedSearchResource, self).create_response( request, data, response_class=HttpResponse, **response_kwargs)