Пример #1
0
    def all_images(self):
        cachekey = _cache_key(
            "{}main-all_images".format(self.__class__.__name__),
            self.__class__,
            self.site_domain,
            u"{}-{}".format(self.channel_long_slug, self.slug),
        )
        getcache = cache.get(cachekey)
        if getcache:
            return getcache

        imgs = super(Post, self).all_images()

        albums = self.albums.filter(published=True, date_available__lte=timezone.now())

        for album in albums:
            images = (
                album.images.prefetch_related("source")
                .filter(published=True, date_available__lte=timezone.now())
                .exclude(pk__in=[i.pk for i in imgs])
            )

            captions = {ci.image_id: ci.caption for ci in ContainerImage.objects.filter(container_id=album.pk)}

            for image in images:
                caption = captions.get(image.pk)
                if caption:
                    image.description = caption
                imgs.append(image)

        cache.set(cachekey, imgs)
        return imgs
Пример #2
0
    def recommendation(self, child_class=False, query_slice=[None, 10]):

        if not child_class:
            child_class = self.child_class

        now = timezone.now()
        start = now - timezone.timedelta(
            days=settings.OPPS_RECOMMENDATION_RANGE_DAYS
        )

        cachekey = _cache_key(
            u'{}-recommendation'.format(self.__class__.__name__),
            self.__class__, self.site_domain,
            u"{}-{}".format(self.channel_long_slug, self.slug))
        getcache = cache.get(cachekey)
        if getcache:
            return getcache

        tag_list = [t for t in self.tags.split(',')[:3]]
        _list = [a for a in Container.objects.filter(
            reduce(operator.or_, (Q(tags__contains=tag) for tag in tag_list)),
            site_domain=self.site_domain,
            child_class=child_class,
            channel_long_slug=self.channel_long_slug,
            date_available__range=(start, now),
            published=True
        ).exclude(pk=self.pk)
         .distinct().order_by('-date_available')[slice(*query_slice)]]

        cache.set(cachekey, _list, 3600)
        return _list
Пример #3
0
    def all_images(self):
        cachekey = _cache_key(
            '{}main-all_images'.format(self.__class__.__name__),
            self.__class__, self.site_domain,
            u"{}-{}".format(self.channel_long_slug, self.slug))
        getcache = cache.get(cachekey)
        if getcache:
            return getcache

        imgs = super(Post, self).all_images()

        albums = self.albums.filter(
            published=True,
            date_available__lte=timezone.now(),
        )

        for album in albums:
            images = album.images.filter(
                published=True,
                date_available__lte=timezone.now(),
            ).exclude(pk__in=[i.pk for i in imgs])

            captions = {
                ci.image_id: ci.caption
                for ci in ContainerImage.objects.filter(container_id=album.pk)
            }

            for image in images:
                caption = captions.get(image.pk)
                if caption:
                    image.description = caption
                imgs.append(image)

        cache.set(cachekey, imgs)
        return imgs
Пример #4
0
    def all_images(self):
        cachekey = _cache_key(
            '{}-all_images'.format(self.__class__.__name__),
            self.__class__, self.site_domain,
            u"{}-{}".format(self.channel_long_slug, self.slug))
        getcache = cache.get(cachekey)
        if getcache:
            return getcache

        imgs = [self.main_image]
        images = self.images.filter(
            published=True, date_available__lte=timezone.now()
        ).order_by('articleimage__order', '-date_available')

        if self.main_image:
            images = images.exclude(pk=self.main_image.pk)
        imgs += [i for i in images.distinct()]

        cache.set(cachekey, imgs)
        return imgs
Пример #5
0
    def recommendation(self):
        cachekey = _cache_key(
            '{}-recommendation'.format(self.__class__.__name__),
            self.__class__, self.site_domain,
            u"{}-{}".format(self.channel_long_slug, self.slug))
        getcache = cache.get(cachekey)
        if getcache:
            return getcache

        tag_list = [t for t in self.tags.all()[:3]]
        _list = [a for a in Article.objects.filter(
            site_domain=self.site_domain,
            child_class=self.child_class,
            channel_long_slug=self.channel_long_slug,
            date_available__lte=timezone.now(),
            published=True,
            tags__in=tag_list).exclude(
                pk=self.pk).distinct().all().order_by('pk')[:10]]

        cache.set(cachekey, _list)
        return _list
Пример #6
0
    def recommendation(self, child_class=False, query_slice=[None, 10]):

        if not child_class:
            child_class = self.child_class

        now = timezone.now()
        start = now - timezone.timedelta(
            days=settings.OPPS_RECOMMENDATION_RANGE_DAYS
        )

        cachekey = _cache_key(
            u'{}-recommendation'.format(self.__class__.__name__),
            self.__class__, self.site_domain,
            u"{}-{}-{}".format(child_class, self.channel_long_slug, self.slug))
        getcache = cache.get(cachekey)
        if getcache:
            return getcache

        containers = Container.objects.filter(
            site_domain=self.site_domain,
            child_class=child_class,
            channel_long_slug=self.channel_long_slug,
            date_available__range=(start, now),
            published=True
        ).exclude(pk=self.pk)

        tag_list = []
        if self.tags:
            tag_list = [t for t in self.tags.split(',')[:3]]
            containers = containers.filter(
                reduce(operator.or_, (Q(tags__contains=tag) for tag in
                                      tag_list)),
            )
        containers = containers.distinct().order_by(
            '-date_available')[slice(*query_slice)]

        _list = [a for a in containers]

        cache.set(cachekey, _list, 3600)
        return _list
Пример #7
0
    def all_images(self, check_published=True):
        cachekey = _cache_key(
            '{}main-all_images'.format(self.__class__.__name__),
            self.__class__, self.site_domain,
            u"{}-{}".format(self.channel_long_slug, self.slug))
        getcache = cache.get(cachekey)
        if getcache and check_published:
            return getcache

        imgs = super(Post, self).all_images()

        albums = self.albums.filter(date_available__lte=timezone.now())
        if check_published:
            albums = albums.filter(published=True)

        for album in albums:
            images = album.images.filter(
                date_available__lte=timezone.now(),
            ).exclude(
                pk__in=[i.pk for i in imgs]
            ).order_by('containerimage__order')
            if check_published:
                images = images.filter(published=True)

            captions = {
                ci.image_id: ci.caption for ci in
                ContainerImage.objects.filter(
                    container_id=album.pk
                )
            }

            for image in images:
                caption = captions.get(image.pk)
                if caption:
                    image.description = caption
                imgs.append(image)

        cache.set(cachekey, imgs)
        return imgs
Пример #8
0
    def all_images(self, check_published=True):
        cachekey = _cache_key(
            '{0}main-all_images'.format(self.__class__.__name__),
            self.__class__, self.site_domain,
            u"{0}-{1}".format(self.channel_long_slug, self.slug))
        getcache = cache.get(cachekey)
        if getcache and check_published:
            return getcache

        imgs = super(Post, self).all_images()

        albums = self.albums.filter(date_available__lte=timezone.now())
        if check_published:
            albums = albums.filter(published=True)

        for album in albums:
            images = album.images.filter(
                date_available__lte=timezone.now(),
            ).exclude(
                pk__in=[i.pk for i in imgs]
            ).order_by('containerimage__order')
            if check_published:
                images = images.filter(published=True)

            captions = dict([
                (ci.image_id, ci.caption) for ci in
                ContainerImage.objects.filter(
                    container_id=album.pk
                )
            ])

            for image in images:
                caption = captions.get(image.pk)
                if caption:
                    image.description = caption
                imgs.append(image)

        cache.set(cachekey, imgs)
        return imgs
Пример #9
0
    def all_images(self):
        cachekey = _cache_key(
            '{}main-all_images'.format(self.__class__.__name__),
            self.__class__, self.site_domain,
            u"{}-{}".format(self.channel_long_slug, self.slug))
        getcache = cache.get(cachekey)
        if getcache:
            return getcache

        imgs = super(Post, self).all_images()
        imgs += [
            i for a in self.albums.filter(
                published=True,
                date_available__lte=timezone.now()
            ).distinct()
            for i in a.images.filter(
                published=True,
                date_available__lte=timezone.now()
            ).exclude(pk__in=[i.pk for i in imgs]).distinct()
        ]

        cache.set(cachekey, imgs)
        return imgs
Пример #10
0
    def all_images(self):
        cachekey = _cache_key(
            '{}main-all_images'.format(self.__class__.__name__),
            self.__class__, self.site_domain,
            u"{}-{}".format(self.channel_long_slug, self.slug))
        getcache = cache.get(cachekey)
        if getcache:
            return getcache

        imgs = super(Post, self).all_images()

        album_images = [
            (i, a) for a in self.albums.filter(
                published=True,
                date_available__lte=timezone.now()
            ).distinct()
            for i in a.images.filter(
                published=True,
                date_available__lte=timezone.now()
            ).exclude(
                pk__in=[i.pk for i in imgs]
            ).order_by('containerimage__order').distinct()
        ]

        for im, a in album_images:
            try:
                caption = im.containerimage_set.get(container__id=a.id).caption
                if caption:
                    im.caption = caption
            except:
                pass

        imgs += [item[0] for item in album_images]

        cache.set(cachekey, imgs)
        return imgs