Пример #1
0
 def item_enclosures(self, item) -> List[Enclosure]:
     if item["type"] == "file":
         file = File.objects.get(pk=item["id"])
         url = settings.ABSOLUTE_URI_BASE + reverse("file-content",
                                                    args=[file.id])
         return [Enclosure(url, str(file.filesize), file.mime_type)]
     return []
Пример #2
0
 def item_enclosures(self, item: Diapositive):
     thumbnail = item.thumbnail_instance(item.size_lg(), item.box_lg())
     return [
         Enclosure(
             self.get_absolute_url(thumbnail.url),
             str(thumbnail.size),
             guess_type(thumbnail.url)[0],
         )
     ]
Пример #3
0
 def test_add_item_warns_when_enclosure_kwarg_is_used(self):
     feed = SyndicationFeed(title='Example', link='http://example.com', description='Foo')
     msg = 'The enclosure keyword argument is deprecated, use enclosures instead.'
     with self.assertRaisesMessage(RemovedInDjango20Warning, msg):
         feed.add_item(
             title='Example Item',
             link='https://example.com/item',
             description='bar',
             enclosure=Enclosure('http://example.com/favicon.ico', 0, 'image/png'),
         )
Пример #4
0
    def get_feed_extras(self):
        from django.utils.feedgenerator import Rss201rev2Feed, Enclosure
        import mimetypes

        enclosure = Enclosure(
            'http://%s%s' % (settings.DOMAIN_NAME, self.download_url()),
            '%s' % self.resource.size,
            mimetypes.guess_type(self.resource.path)[0]
            or 'application/octet-stream')
        return {'enclosure': enclosure}
Пример #5
0
def populate_feed(request, data, feed_type):
    # now available only for search results
    if 'query' not in data:
        raise Http404()

    current_site = get_current_site(request)

    args = request.GET.copy()
    args.pop('format')

    link = '{}?{}'.format(request.path, args.urlencode())
    link = add_domain(current_site.domain, link, request.is_secure())
    guid = md5(request.get_full_path().encode('utf-8')).hexdigest()

    try:
        query = data['query']
        count = data['results']['paginator']['count']
        object_list = data['results']['object_list']
    except KeyError:
        raise Http404()

    title = 'Декларації за запитом "{}"'.format(query[:60])
    subtitle = 'Всього знайдено {} декларацій'.format(count)

    feed = feed_type(title=title,
                     subtitle=subtitle,
                     link=link,
                     description=subtitle,
                     language=settings.LANGUAGE_CODE,
                     feed_url=add_domain(
                         current_site.domain,
                         request.get_full_path(),
                         request.is_secure(),
                     ),
                     author_name=settings.RSS_AUTHOR_NAME,
                     author_link=settings.RSS_AUTHOR_LINK,
                     author_email=settings.RSS_AUTHOR_EMAIL,
                     categories=['Декларації'],
                     feed_copyright='Public domain',
                     feed_guid=guid,
                     ttl=settings.RSS_TTL)

    for item in object_list:
        try:
            item_id = item.get('id', '')
            if not item_id:
                item_id = item.get('meta', {}).get('id', '')
            general = item['general']
            full_name = general.get('full_name', '')
            if not full_name:
                full_name = '{} {} {}'.format(general['last_name'],
                                              general['name'],
                                              general['patronymic'])
            post = general['post']
            region = post.get('region', '')
            office = post.get('office', '')
            intro = item['intro']
            year = intro.get('declaration_year', '')
            doc_type = intro.get('doc_type', '')
            declaration = item['declaration']
            doc_url = declaration.get('url', '')
            pubdate = declaration.get('date', '') or intro.get('date', '')
            if pubdate:
                pubdate = parse_date(pubdate)
            else:
                pubdate = datetime.now()
            updateddate = datetime.now()
        except KeyError:
            continue

        url = ''
        if item_id:
            url = reverse('details', kwargs={'declaration_id': item_id})
            url = add_domain(current_site.domain, url)
        title = '{}, {}'.format(full_name, post.get('post'))
        description = '{}, {}, {}, {}'.format(year, doc_type, region, office)
        author_link = '{}?{}'.format(reverse('search'),
                                     urlencode({'q': full_name}))
        author_link = add_domain(current_site.domain, author_link)

        if doc_url:
            enclosure = Enclosure(doc_url, '', '')
            enclosures = [enclosure]
        else:
            enclosures = None

        feed.add_item(title=title,
                      link=url,
                      description=description,
                      unique_id=url,
                      unique_id_is_permalink='1',
                      enclosures=enclosures,
                      pubdate=pubdate,
                      updateddate=updateddate,
                      author_name=full_name,
                      author_email='',
                      author_link=author_link,
                      categories=['Декларації'],
                      item_copyright='Public domain')

    return feed
Пример #6
0
 def as_enclosure(self):
     """A Django enclosure object for including this exported format in the RSS feed."""
     try:
         return Enclosure(self.href, str(os.path.getsize(self.linkpath)), self.mime_type)
     except:
         return None
Пример #7
0
 def item_enclosures(self, item):
     return [
         Enclosure(item.image.url, str(item.image.size),
                   f"image/{item.image.name.split('.')[-1]}")
     ]
Пример #8
0
 def item_enclosures(self, item):
     mime_type = mimetypes.guess_type(item.recording.path)[0]
     url = self.request.build_absolute_uri(item.recording.url)
     length = str(item.recording.file.size)
     return [Enclosure(url=url, length=length, mime_type=mime_type)]