def get_listings(self, offset=0, count=10): key, pipe = self._get_key() if pipe is None: pipe = client.pipeline() # get the score range based on the date range min_score, max_score = self._get_score_limits() # get all the relevant records if min_score or max_score: pipe = pipe.zrevrangebyscore(key, repr(max_score), repr(min_score), start=offset, num=offset + count - 1, withscores=True ) else: pipe = pipe.zrevrange(key, start=offset, num=offset + count - 1, withscores=True ) results = pipe.execute() # get the data from redis into proper format data = [] ids = [] for value, score in results[-1]: ct_id, pk = value.split(':') ids.append((int(ct_id), int(pk))) data.append(score) # and retrieve publishables from cache publishables = get_cached_objects(ids, missing=SKIP) # create mock Listing objects to return return map(lambda (p, score): self._get_listing(p, score), zip(publishables, data))
def test_get_many_objects(self): ct_ct = ContentType.objects.get_for_model(ContentType) site_ct = ContentType.objects.get_for_model(Site) objs = utils.get_cached_objects([(ct_ct.id, ct_ct.id), (ct_ct.id, site_ct.id), (site_ct.id, 1)]) tools.assert_equals([ct_ct, site_ct, Site.objects.get(pk=1)], objs)
def test_get_many_objects_can_replace_missing_with_none(self): ct_ct = ContentType.objects.get_for_model(ContentType) site_ct = ContentType.objects.get_for_model(Site) objs = utils.get_cached_objects( [(ct_ct.id, ct_ct.id), (ct_ct.id, site_ct.id), (site_ct.id, 1), (site_ct.id, 100)], missing=utils.NONE ) tools.assert_equals([ct_ct, site_ct, Site.objects.get(pk=1), None], objs)
def test_get_many_objects_can_skip(self): ct_ct = ContentType.objects.get_for_model(ContentType) site_ct = ContentType.objects.get_for_model(Site) objs = utils.get_cached_objects([(ct_ct.id, ct_ct.id), (ct_ct.id, site_ct.id), (site_ct.id, 1), (site_ct.id, 100)], missing=utils.SKIP) tools.assert_equals([ct_ct, site_ct, Site.objects.get(pk=1)], objs)
def test_get_many_objects_can_replace_missing_with_none(self): ct_ct = ContentType.objects.get_for_model(ContentType) site_ct = ContentType.objects.get_for_model(Site) objs = utils.get_cached_objects([(ct_ct.id, ct_ct.id), (ct_ct.id, site_ct.id), (site_ct.id, 1), (site_ct.id, 100)], missing=utils.NONE) tools.assert_equals( [ct_ct, site_ct, Site.objects.get(pk=1), None], objs)
def init_form(self, user): if user.is_superuser: cats = Category.objects.filter(tree_parent__isnull=True) else: category_ids = DenormalizedCategoryUserRole.objects.root_categories_by_user(user) cats = get_cached_objects(category_ids, Category) choices = () for c in cats: choices += (c.pk, c.__unicode__(),), self.sites_count = len(choices) self.fields['sites'] = forms.MultipleChoiceField(choices, widget=CheckboxSelectMultiple, required=False)
def directly_related(obj, count, collected_so_far, mods=[], only_from_same_site=True): """ Returns objects related to ``obj`` up to ``count`` by searching ``Related`` instances for the ``obj``. """ # manually entered dependencies qset = Related.objects.filter(publishable=obj) if mods: qset = qset.filter(related_ct__in=[ ContentType.objects.get_for_model(m).pk for m in mods]) return get_cached_objects(qset.values_list('related_ct', 'related_id')[:count], missing=SKIP)
def directly_related(obj, count, collected_so_far, mods=[], only_from_same_site=True): """ Returns objects related to ``obj`` up to ``count`` by searching ``Related`` instances for the ``obj``. """ # manually entered dependencies qset = Related.objects.filter(publishable=obj) if mods: qset = qset.filter(related_ct__in=[ ContentType.objects.get_for_model(m).pk for m in mods ]) return get_cached_objects(qset.values_list('related_ct', 'related_id')[:count], missing=SKIP)
def test_get_many_publishables_will_respect_their_content_type(self): create_basic_categories(self) create_and_place_a_publishable(self) objs = utils.get_cached_objects([self.publishable.pk], Publishable) tools.assert_true(isinstance(objs[0], Article))