def has_isSubscribed_set_to_true_when_current_user_is_subscribed(self): from django.contrib.auth.models import User from subscriptions.models import Subscriber, Subscription from subscriptions.feeds import ContentFeedLibrary from main.feeds import NewLegislationFeed subscriber = Subscriber(username="******") subscriber.set_password("world") subscriber.save() library = ContentFeedLibrary() library.register(NewLegislationFeed, 'blah blah') feed = NewLegislationFeed() subscription = subscriber.subscribe(feed) subscriber.save() # Make the request. client = Client() assert client.login(username="******", password="******") response = client.get('/legislation/') # Check the context assert response.context['is_subscribed'] assert_equal(response.context['subscription'], subscription)
def causes_the_same_feed_record_to_be_returned_on_different_calls_to_getRecord(self): library = ContentFeedLibrary(shared=False) library.register(ListItemFeed, 'li') feed = ListItemFeed('[1, 2, 3]') record = library.get_record(feed) assert_is(record, library.get_record(feed))
def doesnt_return_the_same_feed_from_different_libraries(self): library1 = ContentFeedLibrary(shared=False) library1.register(ListItemFeed, 'li') library2 = ContentFeedLibrary(shared=False) library2.register(ListItemFeed, 'li') feed = ListItemFeed('[1, 2, 3]') record = library1.get_record(feed) assert_is_not(feed, library2.get_feed(record))
def setup(self): Subscription.objects.all().delete() Subscriber.objects.all().delete() ContentFeedRecord.objects.all().delete() self.library = ContentFeedLibrary(shared=False) self.library.register(ListItemFeed, 'my list items') self.subscriber = Subscriber.objects.create() self.feed = ListItemFeed('[1, 2, 3]') self.feed_record = self.library.get_record(self.feed)
def setUp(self): Subscriber.objects.all().delete() ContentFeedRecord.objects.all().delete() ContentFeedParameter.objects.all().delete() Subscription.objects.all().delete() ContentFeedLibrary().register(ContentFeed, 'generic content feed') record = self.record = ContentFeedRecord.objects.create(feed_name='generic content feed') subscriber = self.subscriber = Subscriber.objects.create()
def setUp(self): class DoNothinView (object): def get_context_data(self, **kwargs): # This is just so that SingleSubscriptionMixin.get_context_data # super has something to call. return {} library = ContentFeedLibrary(shared=False) library.register(ListItemFeed, 'my list item feed') class SubscriptionView (SingleSubscriptionMixin, DoNothinView): def get_content_feed_library(self): return library def get_content_feed(self): return ListItemFeed('[1,2,3]') self.view = SubscriptionView() self.view.request = Mock() self.view.feed_data = ContentFeed
def setUp(self): LegFile.objects.all().delete() key = 0 for intro, final in [ (datetime.date(2011, 1, 28), datetime.date(2011, 1, 29)), (datetime.date(2010, 7, 28), datetime.date(2010, 7, 29)), (datetime.date(2011, 8, 17), datetime.date(2011, 8, 18)), (datetime.date(2006, 12, 11), datetime.date(2006, 12, 12)), (datetime.date(2006, 12, 12), datetime.date(2006, 12, 13)) ]: LegFile.objects.create(intro_date=intro, final_date=final, key=key, date_scraped=datetime.date.today()) key += 1 self.library = ContentFeedLibrary(shared=False) self.library.register(NewLegFilesFeed, 'feed_class_123456') self.record = self.library.get_record(NewLegFilesFeed())
def setUp(self): library = self.library = ContentFeedLibrary(shared=False) library.register(ListItemFeed, 'list feed') self.feeds = [ ListItemFeed("['hello']"), ListItemFeed("['world']") ] for feed in self.feeds: feed.get_last_updated_time = Mock(return_value=datetime.date.today()) self.feed_records = [library.get_record(feed) for feed in self.feeds]
class Test_ContentFeedUpdater_update (TestCase): def setUp(self): LegFile.objects.all().delete() key = 0 for intro, final in [ (datetime.date(2011, 1, 28), datetime.date(2011, 1, 29)), (datetime.date(2010, 7, 28), datetime.date(2010, 7, 29)), (datetime.date(2011, 8, 17), datetime.date(2011, 8, 18)), (datetime.date(2006, 12, 11), datetime.date(2006, 12, 12)), (datetime.date(2006, 12, 12), datetime.date(2006, 12, 13)) ]: LegFile.objects.create(intro_date=intro, final_date=final, key=key, date_scraped=datetime.date.today()) key += 1 self.library = ContentFeedLibrary(shared=False) self.library.register(NewLegFilesFeed, 'feed_class_123456') self.record = self.library.get_record(NewLegFilesFeed()) @istest def changes_the_lastUpdated_of_a_legfiles_feed_to_most_recent_intro_date(self): updater = ContentFeedRecordUpdater() updater.update(self.record, self.library) assert_equal(self.record.last_updated, datetime.datetime(2011, 8, 17, 0, 0)) @istest def returns_date_min_when_no_content_is_available(self): LegFile.objects.all().delete() updater = ContentFeedRecordUpdater() updater.update(self.record, self.library) assert_equal(self.record.last_updated, datetime.datetime.min)
def setUp(self): Subscriber.objects.all().delete() ContentFeedRecord.objects.all().delete() ContentFeedParameter.objects.all().delete() Subscription.objects.all().delete() library = self.library = ContentFeedLibrary(shared=False) library.register(ListItemFeed, 'list feed') feed = self.feed = ListItemFeed('[1,2,3]') record = self.record = library.get_record(feed) subscriber = self.subscriber = Subscriber.objects.create() subscriber.subscribe(feed, library) subscriber.save()
class Test_SubscriptionForm_save: def setup(self): Subscription.objects.all().delete() Subscriber.objects.all().delete() ContentFeedRecord.objects.all().delete() self.library = ContentFeedLibrary(shared=False) self.library.register(ListItemFeed, 'my list items') self.subscriber = Subscriber.objects.create() self.feed = ListItemFeed('[1, 2, 3]') self.feed_record = self.library.get_record(self.feed) @istest def creates_a_subscription_for_the_subscriber_to_the_feed(self): form = SubscriptionForm({'subscriber': self.subscriber.pk, 'feed_record': self.feed_record.pk}) assert form.is_valid(), 'The form had errors: %r' % (form.errors,) form.save() subscription = self.subscriber.subscription(self.feed, self.library) assert subscription is not None
def setUp(self): ContentFeedRecord.objects.all().delete() Subscriber.objects.all().delete() library = self.library = ContentFeedLibrary(shared=False) library.register(ListItemFeed, 'list feed') feeds = [ ListItemFeed("['hello']"), ListItemFeed("['world']") ] feed_records = [library.get_record(feed) for feed in feeds] subscriber = Subscriber.objects.create() subscriber.subscribe(feeds[0], library) self.keeper = feed_records[0] self.tosser = feed_records[1]
def setup(self): library = self.library = ContentFeedLibrary() # Create a subscriber subscriber = self.subscriber = Mock() # Create a subscription subscription = self.subscription = Mock() subscription.last_sent = datetime.datetime(2011, 1, 1, 0, 0) subscription.feed_record.last_updated = datetime.datetime(2011, 8, 4, 6, 50) subscription.feed_record.feed_name = 'MockFeed' param1 = Mock(); param1.name = 'p1'; param1.value = '1' param2 = Mock(); param2.name = 'p2'; param2.value = '2' subscription.feed.feed_params.all = lambda: [param1, param2] # The subscriber is subscribed to the subscription we created subscriber.subscriptions.all = lambda: [subscription]
def setUp(self): key = 0 for intro, final in [ (datetime.date(2011, 1, 28), datetime.date(2011, 1, 29)), (datetime.date(2010, 7, 28), datetime.date(2010, 7, 29)), (datetime.date(2011, 8, 17), datetime.date(2011, 8, 18)), (datetime.date(2006, 12, 11), datetime.date(2006, 12, 12)), (datetime.date(2006, 12, 12), datetime.date(2006, 12, 13)) ]: LegFile(intro_date=intro, final_date=final, key=key, date_scraped=datetime.date.today()).save() key += 1 self.library = ContentFeedLibrary(shared=False) self.library.register(NewLegFilesFeed, 'feed_class_123456') self.record = self.library.get_record(NewLegFilesFeed())
def test_subscription_flow(): Subscriber.objects.all().delete() ContentFeedRecord.objects.all().delete() del mail.outbox[:] library = ContentFeedLibrary(shared=True) # First, let's make some content content = [(datetime.date(2011, 12, 13), 1), (datetime.date(2011, 12, 13), 2), (datetime.date(2011, 12, 13), 3), (datetime.date(2011, 12, 13), 4), (datetime.date(2011, 12, 13), 5)] # Then, let's make a ContentFeed that watches it. class EasyContentFeed(ContentFeed): def get_content(self): return content def get_params(self): return {} def get_updates_since(self, previous): return [t for t in content if t[0] > previous.date()] def get_last_updated_time(self): return max([t[0] for t in content]) def get_changes_to(self, item, since): if since.date() < item[0]: return { 'value': item[1] }, datetime.datetime(2011, 12, 13, 14, 15) def get_label(self): return 'Easy...' library.register(EasyContentFeed, 'easy') feed = EasyContentFeed() # Now we want a subscriber to subscribe to the feed. subscriber = Subscriber.objects.create() subscription = subscriber.subscribe(feed, library=library) # Assume that we last sent the subscription before the current items. subscription.last_sent = datetime.date(2011, 11, 11) subscription.save() # Now, update all the feeds dates/times and send out the updated content. update = updatefeeds.Command() update.handle() send = sendfeedupdates.Command() send.handle() # Check that we have a message to send. assert_equal(len(mail.outbox), 1) assert_equal(mail.outbox[0].subject[:19], 'Philly Councilmatic') assert_equal(mail.outbox[0].body, ( '\n\nPhiladelphia Councilmatic!\n==========================\n\nYou are subscribed to the following feeds:\n\n\n* Easy...\n\n\n--------------------------------------------------------------------------------\n\n \n\nvalue: 5\n\nMore at http://example.com\n\n\n--------------------------------------------------------------------------------\n\n \n\nvalue: 2\n\nMore at http://example.com\n\n\n--------------------------------------------------------------------------------\n\n \n\nvalue: 4\n\nMore at http://example.com\n\n\n--------------------------------------------------------------------------------\n\n \n\nvalue: 1\n\nMore at http://example.com\n\n\n--------------------------------------------------------------------------------\n\n \n\nvalue: 3\n\nMore at http://example.com\n\n\n\nTo manage your subscriptions, visit http://example.com/subscriptions/\n' )) # Cool. Clear the mailbox. del mail.outbox[:] # Now, if we update the feed times and send the updated content, there # should be nothing to send. update = updatefeeds.Command() update.handle() send = sendfeedupdates.Command() send.handle() assert_equal(len(mail.outbox), 0) # Put something else into the feed. content.append((datetime.date(2012, 2, 4), 6)) # Now, update all the feeds dates/times and send out the updated content. update = updatefeeds.Command() update.handle() send = sendfeedupdates.Command() send.handle() assert_equal(len(mail.outbox), 1)
def subscription_title(subscription, library=None): if library is None: library = ContentFeedLibrary() feed = library.get_feed(subscription.feed_record) return feed.get_label()
import json import logging from datetime import date, time, datetime from collections import defaultdict from itertools import chain from itertools import product from subscriptions.feeds import ContentFeed from subscriptions.feeds import ContentFeedLibrary from phillyleg.models import LegFile from phillyleg.models import LegMinutes from haystack.query import SearchQuerySet log = logging.getLogger(__name__) library = ContentFeedLibrary() class NewLegislationFeed (ContentFeed): def get_content(self): return LegFile.objects.all().order_by('-intro_date') def get_updates_since(self, datetime): return self.get_content().filter(intro_date__gt=datetime) def get_changes_to(self, legfile, since_datetime): if since_datetime.date() <= legfile.intro_date: return {'Title': legfile.title}, datetime.combine(legfile.intro_date, time()) else: return {}, datetime.min def get_last_updated_time(self):
def test_subscription_flow(): Subscriber.objects.all().delete() ContentFeedRecord.objects.all().delete() del mail.outbox[:] library = ContentFeedLibrary(shared=True) # First, let's make some content content = [ (datetime.date(2011, 12, 13), 1), (datetime.date(2011, 12, 13), 2), (datetime.date(2011, 12, 13), 3), (datetime.date(2011, 12, 13), 4), (datetime.date(2011, 12, 13), 5), ] # Then, let's make a ContentFeed that watches it. class EasyContentFeed(ContentFeed): def get_content(self): return content def get_params(self): return {} def get_updates_since(self, previous): return [t for t in content if t[0] > previous.date()] def get_last_updated_time(self): return max([t[0] for t in content]) def get_changes_to(self, item, since): if since.date() < item[0]: return {"value": item[1]}, datetime.datetime(2011, 12, 13, 14, 15) def get_label(self): return "Easy..." library.register(EasyContentFeed, "easy") feed = EasyContentFeed() # Now we want a subscriber to subscribe to the feed. subscriber = Subscriber.objects.create() subscription = subscriber.subscribe(feed, library=library) # Assume that we last sent the subscription before the current items. subscription.last_sent = datetime.date(2011, 11, 11) subscription.save() # Now, update all the feeds dates/times and send out the updated content. update = updatefeeds.Command() update.handle() send = sendfeedupdates.Command() send.handle() # Check that we have a message to send. assert_equal(len(mail.outbox), 1) assert_equal(mail.outbox[0].subject[:19], "Councilmatic") assert_equal( mail.outbox[0].body, ( "\n\nCouncilmatic!\n==========================\n\nYou are subscribed to the following feeds:\n\n\n* Easy...\n\n\n--------------------------------------------------------------------------------\n\n \n\nvalue: 5\n\nMore at http://example.com\n\n\n--------------------------------------------------------------------------------\n\n \n\nvalue: 2\n\nMore at http://example.com\n\n\n--------------------------------------------------------------------------------\n\n \n\nvalue: 4\n\nMore at http://example.com\n\n\n--------------------------------------------------------------------------------\n\n \n\nvalue: 1\n\nMore at http://example.com\n\n\n--------------------------------------------------------------------------------\n\n \n\nvalue: 3\n\nMore at http://example.com\n\n\n\nTo manage your subscriptions, visit http://example.com/subscriptions/\n" ), ) # Cool. Clear the mailbox. del mail.outbox[:] # Now, if we update the feed times and send the updated content, there # should be nothing to send. update = updatefeeds.Command() update.handle() send = sendfeedupdates.Command() send.handle() assert_equal(len(mail.outbox), 0) # Put something else into the feed. content.append((datetime.date(2012, 2, 4), 6)) # Now, update all the feeds dates/times and send out the updated content. update = updatefeeds.Command() update.handle() send = sendfeedupdates.Command() send.handle() assert_equal(len(mail.outbox), 1)
import datetime import logging from collections import defaultdict from itertools import chain from subscriptions.feeds import ContentFeed from subscriptions.feeds import ContentFeedLibrary from phillyleg.models import LegFile from phillyleg.models import LegMinutes from haystack.query import SearchQuerySet log = logging.getLogger(__name__) library = ContentFeedLibrary() class NewLegislationFeed (ContentFeed): def get_content(self): return LegFile.objects.all() def get_updates_since(self, datetime): return self.get_content().filter(intro_date__gt=datetime) def get_changes_to(self, legfile, since_datetime): if since_datetime.date() <= self.get_last_updated(legfile): return {'Title': legfile.title} else: return {} def get_last_updated(self, legfile): return legfile.intro_date