Example #1
0
def get_nsfw_collections_srnames():
    all_collections = Collection.get_all()
    nsfw_collections = [col for col in all_collections if col.over_18]
    srnames = itertools.chain.from_iterable(col.sr_names
                                            for col in nsfw_collections)

    return set(srnames)
Example #2
0
    def GET_promote_inventory(self, start, end, sr_name, collection_name):
        if not start or not end:
            start = promote.promo_datetime_now(offset=1).date()
            end = promote.promo_datetime_now(offset=8).date()
            c.errors.remove((errors.BAD_DATE, 'startdate'))
            c.errors.remove((errors.BAD_DATE, 'enddate'))

        target = Target(Frontpage.name)
        if sr_name:
            try:
                sr = Subreddit._by_name(sr_name)
                target = Target(sr.name)
            except NotFound:
                c.errors.add(errors.SUBREDDIT_NOEXIST, field='sr_name')
        elif collection_name:
            collection = Collection.by_name(collection_name)
            if not collection:
                c.errors.add(errors.COLLECTION_NOEXIST,
                             field='collection_name')
            else:
                target = Target(collection)

        content = PromoteInventory(start, end, target)
        return PromotePage(title=_("sponsored link inventory"),
                           content=content).render()
Example #3
0
    def GET_promote_inventory(self, start, end, sr_name, collection_name):
        if not start or not end:
            start = promote.promo_datetime_now(offset=1).date()
            end = promote.promo_datetime_now(offset=8).date()
            c.errors.remove((errors.BAD_DATE, 'startdate'))
            c.errors.remove((errors.BAD_DATE, 'enddate'))

        target = Target(Frontpage.name)
        if sr_name:
            try:
                sr = Subreddit._by_name(sr_name)
                target = Target(sr.name)
            except NotFound:
                c.errors.add(errors.SUBREDDIT_NOEXIST, field='sr_name')
        elif collection_name:
            collection = Collection.by_name(collection_name)
            if not collection:
                c.errors.add(errors.COLLECTION_NOEXIST, field='collection_name')
            else:
                target = Target(collection)

        content = PromoteInventory(start, end, target)

        if c.render_style == 'csv':
            return content.as_csv()
        else:
            return PromotePage(title=_("sponsored link inventory"),
                               content=content).render()
Example #4
0
    def test_is_spotlight_default(self):
        """Assert that is_spotlight defaults to False"""
        collection = Collection(name='fake name', sr_names=[])
        self.assertFalse(collection.is_spotlight)

        setattr(collection, 'is_spotlight', True)
        self.assertTrue(collection.is_spotlight)
Example #5
0
    def test_set_attributes(self):
        """Assert that _set_attributes properly handles invalid attributes"""
        attribute_error_message = 'No attribute on %s called %s'
        invalid_attribute = {'invalid_attribute': None}
        valid_attribute = {'is_spotlight': None}

        collection = Collection(name=self.name, sr_names=[])

        Collection.by_name = Mock()
        Collection.by_name.return_value = collection

        # Assert that a bad attribute will raise NotFoundException
        with self.assertRaises(AttributeError) as e:
            CollectionStorage._set_attributes(self.name, invalid_attribute)
        self.assertEqual(e.exception.message, attribute_error_message %
            (self.name, invalid_attribute.keys()[0]))

        # Should throw even if there's a bad attribute AND valid attribute
        with self.assertRaises(AttributeError) as e:
            CollectionStorage._set_attributes(self.name,
            dict(invalid_attribute, **valid_attribute))
        self.assertEqual(e.exception.message, attribute_error_message %
            (self.name, invalid_attribute.keys()[0]))

        CollectionStorage._set_values = MagicMock()
        CollectionStorage._set_attributes(self.name,
            valid_attribute)

        # Assert that no exception thrown if valid attributes passed
        CollectionStorage._set_values.assert_called_once_with(self.name,
            valid_attribute)
Example #6
0
def get_nsfw_collections_srnames():
    all_collections = Collection.get_all()
    nsfw_collections = [col for col in all_collections if col.over_18]
    srnames = itertools.chain.from_iterable(
        col.sr_names for col in nsfw_collections
    )

    return set(srnames)
Example #7
0
    def setUp(self):
        # Setup all collections
        self.spotlight_a = Collection(name='spotlight_a', sr_names=[],
            is_spotlight=True)
        self.spotlight_z = Collection(name='spotlight_z', sr_names=[],
            is_spotlight=True)
        self.sfw_a = Collection(name='sfw_a', sr_names=[])
        self.sfw_b = Collection(name='sfw_B', sr_names=[])
        self.sfw_z = Collection(name='sfw_z', sr_names=[])
        self.nsfw_spotlight = Collection(name='nsfw_spotlight', sr_names=[],
            over_18=True, is_spotlight=True)
        self.nsfw_non_spotlight = Collection(name='nsfw_non_spotlight',
            sr_names=[], over_18=True)

        self.correct_order = [
            'spotlight_a',
            'spotlight_z',
            'sfw_a',
            'sfw_B',
            'sfw_z',
            'nsfw_spotlight',
            'nsfw_non_spotlight',
        ]

        # Mock the get_all method on CollectionStorage,
        # which returns an unordered list of collections
        CollectionStorage.get_all = MagicMock()
Example #8
0
subscriptions = map(lambda srname: Subreddit(name=srname), subscriptions_srnames)
multi_srnames = ["bing", "bat"]
multi_subreddits = map(lambda srname: Subreddit(name=srname), multi_srnames)
nice_srname = "mylittlepony"
nsfw_srname = "pr0n"
questionably_nsfw = "sexstories"
quarantined_srname = "croontown"
naughty_subscriptions = [
    Subreddit(name=nice_srname),
    Subreddit(name=nsfw_srname, over_18=True),
    Subreddit(name=quarantined_srname, quarantine=True),
]
nsfw_collection_srnames = [questionably_nsfw, nsfw_srname]
nsfw_collection = Collection(
    name="after dark",
    sr_names=nsfw_collection_srnames,
    over_18=True
)

class TestSRNamesFromSite(unittest.TestCase):
    def setUp(self):
        self.logged_in = Account(name="test")
        self.logged_out = FakeAccount()

        from r2.lib.memoize import g
        self.autopatch(g, "memoizecache", NonCache())

    def test_frontpage_logged_out(self):
        srnames = srnames_from_site(self.logged_out, Frontpage)

        self.assertEqual(srnames, {Frontpage.name})
Example #9
0
 def _assert_scenario(self, unordered_collections):
     CollectionStorage.get_all.return_value = unordered_collections
     self.assertEqual(
         [collection.name for collection in Collection.get_all()],
         self.correct_order)