desc
stringlengths
3
26.7k
decl
stringlengths
11
7.89k
bodies
stringlengths
8
553k
'Return the URL of the current site.'
@property def site_url(self):
return ('%s://%s' % (self.protocol, self.site.domain))
'Publication date of an entry.'
def item_pubdate(self, item):
return item.publication_date
'Update date of an entry.'
def item_updateddate(self, item):
return item.last_update
'Entry\'s categories.'
def item_categories(self, item):
return [category.title for category in item.categories.all()]
'Return the first author of an entry.'
def item_author_name(self, item):
if item.authors.count(): self.item_author = item.authors.all()[0] return self.item_author.__str__()
'Return the first author\'s email. Should not be called if self.item_author_name has returned None.'
def item_author_email(self, item):
return self.item_author.email
'Return the author\'s URL. Should not be called if self.item_author_name has returned None.'
def item_author_link(self, item):
try: author_url = self.item_author.get_absolute_url() return (self.site_url + author_url) except NoReverseMatch: return self.site_url
'Return an image for enclosure.'
def item_enclosure_url(self, item):
try: url = item.image.url except (AttributeError, ValueError): img = BeautifulSoup(item.html_content, 'html.parser').find('img') url = (img.get('src') if img else None) self.cached_enclosure_url = url if url: url = urljoin(self.site_url, url) if (self.feed_format ...
'Try to obtain the size of the enclosure if it\'s present on the FS, otherwise returns an hardcoded value. Note: this method is only called if item_enclosure_url has returned something.'
def item_enclosure_length(self, item):
try: return str(item.image.size) except (AttributeError, ValueError, os.error): pass return '100000'
'Guess the enclosure\'s mimetype. Note: this method is only called if item_enclosure_url has returned something.'
def item_enclosure_mime_type(self, item):
(mime_type, encoding) = guess_type(self.cached_enclosure_url) if mime_type: return mime_type return 'image/jpeg'
'URL of last entries.'
def link(self):
return reverse('zinnia:entry_archive_index')
'Items are published entries.'
def items(self):
return Entry.published.all()[:self.limit]
'Title of the feed'
def get_title(self, obj):
return _('Last entries')
'Description of the feed.'
def description(self):
return (_('The last entries on the site %(object)s') % {'object': self.site.name})
'Retrieve the category by his path.'
def get_object(self, request, path):
return get_category_or_404(path)
'Items are the published entries of the category.'
def items(self, obj):
return obj.entries_published()[:self.limit]
'URL of the category.'
def link(self, obj):
return obj.get_absolute_url()
'Title of the feed.'
def get_title(self, obj):
return (_('Entries for the category %(object)s') % {'object': obj.title})
'Description of the feed.'
def description(self, obj):
return (obj.description or (_('The last entries categorized under %(object)s') % {'object': obj.title}))
'Retrieve the author by his username.'
def get_object(self, request, username):
return get_object_or_404(Author, **{Author.USERNAME_FIELD: username})
'Items are the published entries of the author.'
def items(self, obj):
return obj.entries_published()[:self.limit]
'URL of the author.'
def link(self, obj):
return obj.get_absolute_url()
'Title of the feed.'
def get_title(self, obj):
return (_('Entries for the author %(object)s') % {'object': smart_text(obj.__str__())})
'Description of the feed.'
def description(self, obj):
return (_('The last entries by %(object)s') % {'object': smart_text(obj.__str__())})
'Retrieve the tag by his name.'
def get_object(self, request, tag):
return get_object_or_404(Tag, name=tag)
'Items are the published entries of the tag.'
def items(self, obj):
return TaggedItem.objects.get_by_model(Entry.published.all(), obj)[:self.limit]
'URL of the tag.'
def link(self, obj):
return reverse('zinnia:tag_detail', args=[obj.name])
'Title of the feed.'
def get_title(self, obj):
return (_('Entries for the tag %(object)s') % {'object': obj.name})
'Description of the feed.'
def description(self, obj):
return (_('The last entries tagged with %(object)s') % {'object': obj.name})
'The GET parameter \'pattern\' is the object.'
def get_object(self, request):
pattern = request.GET.get('pattern', '') if (len(pattern) < 3): raise ObjectDoesNotExist return pattern
'Items are the published entries founds.'
def items(self, obj):
return Entry.published.search(obj)[:self.limit]
'URL of the search request.'
def link(self, obj):
return ('%s?pattern=%s' % (reverse('zinnia:entry_search'), obj))
'Title of the feed.'
def get_title(self, obj):
return (_("Search results for '%(pattern)s'") % {'pattern': obj})
'Description of the feed.'
def description(self, obj):
return (_("The last entries containing the pattern '%(pattern)s'") % {'pattern': obj})
'Publication date of a discussion.'
def item_pubdate(self, item):
return item.submit_date
'URL of the discussion item.'
def item_link(self, item):
return item.get_absolute_url()
'Author of the discussion item.'
def item_author_name(self, item):
return item.name
'Author\'s email of the discussion item.'
def item_author_email(self, item):
return item.email
'Author\'s URL of the discussion.'
def item_author_link(self, item):
return item.url
'Items are the discussions on the entries.'
def items(self):
content_type = ContentType.objects.get_for_model(Entry) return comments.get_model().objects.filter(content_type=content_type, is_public=True).order_by('-submit_date')[:self.limit]
'URL of last discussions.'
def link(self):
return reverse('zinnia:entry_archive_index')
'Title of the feed.'
def get_title(self, obj):
return _('Last discussions')
'Description of the feed.'
def description(self):
return (_('The last discussions on the site %(object)s') % {'object': self.site.name})
'Retrieve the discussions by entry\'s slug.'
def get_object(self, request, year, month, day, slug):
return get_object_or_404(Entry, slug=slug, publication_date__year=year, publication_date__month=month, publication_date__day=day)
'Items are the discussions on the entry.'
def items(self, obj):
return obj.discussions[:self.limit]
'URL of the entry.'
def link(self, obj):
return obj.get_absolute_url()
'Title of the feed.'
def get_title(self, obj):
return (_('Discussions on %(object)s') % {'object': obj.title})
'Description of the feed.'
def description(self, obj):
return (_('The last discussions on the entry %(object)s') % {'object': obj.title})
'Items are the comments on the entry.'
def items(self, obj):
return obj.comments[:self.limit]
'URL of the comment.'
def item_link(self, item):
return (item.get_absolute_url('#comment-%(id)s-by-') + slugify(item.user_name))
'Title of the feed.'
def get_title(self, obj):
return (_('Comments on %(object)s') % {'object': obj.title})
'Description of the feed.'
def description(self, obj):
return (_('The last comments on the entry %(object)s') % {'object': obj.title})
'Return a gravatar image for enclosure.'
def item_enclosure_url(self, item):
return get_gravatar(item.email)
'Hardcoded enclosure length.'
def item_enclosure_length(self, item):
return '100000'
'Hardcoded enclosure mimetype.'
def item_enclosure_mime_type(self, item):
return 'image/jpeg'
'Items are the pingbacks on the entry.'
def items(self, obj):
return obj.pingbacks[:self.limit]
'URL of the pingback.'
def item_link(self, item):
return item.get_absolute_url('#pingback-%(id)s')
'Title of the feed.'
def get_title(self, obj):
return (_('Pingbacks on %(object)s') % {'object': obj.title})
'Description of the feed.'
def description(self, obj):
return (_('The last pingbacks on the entry %(object)s') % {'object': obj.title})
'Items are the trackbacks on the entry.'
def items(self, obj):
return obj.trackbacks[:self.limit]
'URL of the trackback.'
def item_link(self, item):
return item.get_absolute_url('#trackback-%(id)s')
'Title of the feed.'
def get_title(self, obj):
return (_('Trackbacks on %(object)s') % {'object': obj.title})
'Description of the feed.'
def description(self, obj):
return (_('The last trackbacks on the entry %(object)s') % {'object': obj.title})
'Determine if a new comment should be marked as non-public and await approval. Return ``True`` to put the comment into the moderator queue, or ``False`` to allow it to be showed up immediately.'
def moderate(self, comment, entry, request):
if self.auto_moderate_comments: return True if check_is_spam(comment, entry, request, self.spam_checker_backends): return True return False
'Send email notifications needed.'
def email(self, comment, entry, request):
current_language = get_language() try: activate(settings.LANGUAGE_CODE) site = Site.objects.get_current() if (self.auto_moderate_comments or comment.is_public): self.do_email_notification(comment, entry, site) if comment.is_public: self.do_email_authors(co...
'Send email notification of a new comment to site staff.'
def do_email_notification(self, comment, entry, site):
if (not self.mail_comment_notification_recipients): return template = loader.get_template('comments/zinnia/entry/email/notification.txt') context = {'comment': comment, 'entry': entry, 'site': site, 'protocol': PROTOCOL} subject = (_('[%(site)s] New comment posted on "%(title)s"')...
'Send email notification of a new comment to the authors of the entry.'
def do_email_authors(self, comment, entry, site):
if (not self.email_authors): return exclude_list = (self.mail_comment_notification_recipients + ['']) recipient_list = (set([author.email for author in entry.authors.all()]) - set(exclude_list)) if (not recipient_list): return template = loader.get_template('comments/zinnia/entry/ema...
'Send email notification of a new comment to the authors of the previous comments.'
def do_email_reply(self, comment, entry, site):
if (not self.email_reply): return exclude_list = ((self.mail_comment_notification_recipients + [author.email for author in entry.authors.all()]) + [comment.email]) recipient_list = (set([other_comment.email for other_comment in entry.comments if other_comment.email]) - set(exclude_list)) if (not...
'Return published entries.'
def items(self):
return Entry.published.all()
'Return last modification of an entry.'
def lastmod(self, obj):
return obj.last_update
'Get a queryset, cache infos for standardized access to them later then compute the maximum of entries to define the priority of each items.'
def items(self):
queryset = self.get_queryset() self.cache_infos(queryset) self.set_max_entries() return queryset
'Build a queryset of items with published entries and annotated with the number of entries and the latest modification date.'
def get_queryset(self):
return self.model.published.annotate(count_entries_published=Count('entries')).annotate(last_update=Max('entries__last_update')).order_by('-count_entries_published', '-last_update', '-pk')
'Cache infos like the number of entries published and the last modification date for standardized access later.'
def cache_infos(self, queryset):
self.cache = {} for item in queryset: self.cache[item.pk] = (item.count_entries_published, item.last_update)
'Define the maximum of entries for computing the priority of each items later.'
def set_max_entries(self):
if self.cache: self.max_entries = float(max([i[0] for i in self.cache.values()]))
'The last modification date is defined by the latest entry last update in the cache.'
def lastmod(self, item):
return self.cache[item.pk][1]
'The priority of the item depends of the number of entries published in the cache divided by the maximum of entries.'
def priority(self, item):
return ('%.1f' % max((self.cache[item.pk][0] / self.max_entries), 0.1))
'Return the published Tags with option counts.'
def get_queryset(self):
self.entries_qs = Entry.published.all() return Tag.objects.usage_for_queryset(self.entries_qs, counts=True)
'Cache the number of entries published and the last modification date under each tag.'
def cache_infos(self, queryset):
self.cache = {} for item in queryset: self.cache[item.pk] = (item.count, TaggedItem.objects.get_by_model(self.entries_qs, item)[0].last_update)
'Return URL of the tag.'
def location(self, item):
return reverse('zinnia:tag_detail', args=[item.name])
'Return a list of the most related objects to instance.'
def get_related(self, instance, number):
related_pks = self.compute_related(instance.pk)[:number] related_pks = [pk for (pk, score) in related_pks] related_objects = sorted(self.queryset.model.objects.filter(pk__in=related_pks), key=(lambda x: related_pks.index(x.pk))) return related_objects
'Compute the most related pks to an object\'s pk.'
def compute_related(self, object_id, score=pearson_score):
dataset = self.dataset object_vector = dataset.get(object_id) if (not object_vector): return [] object_related = {} for (o_id, o_vector) in dataset.items(): if (o_id != object_id): try: object_related[o_id] = score(object_vector, o_vector) exce...
'Generate a raw dataset based on the queryset and the specified fields.'
@cached_property def raw_dataset(self):
dataset = {} queryset = self.queryset.values_list(*(['pk'] + self.fields)) if self.limit: queryset = queryset[:self.limit] for item in queryset: item = list(item) item_pk = item.pop(0) datas = ' '.join(map(six.text_type, item)) dataset[item_pk] = self.raw_clean...
'Apply a cleaning on raw datas.'
def raw_clean(self, datas):
datas = strip_tags(datas) datas = STOP_WORDS.rebase(datas, '') datas = PUNCTUATION.sub('', datas) datas = datas.lower() return [d for d in datas.split() if (len(d) > 1)]
'Generate the columns and the whole dataset.'
@cached_property def columns_dataset(self):
data = {} words_total = {} for (instance, words) in self.raw_dataset.items(): words_item_total = {} for word in words: words_total.setdefault(word, 0) words_item_total.setdefault(word, 0) words_total[word] += 1 words_item_total[word] += 1 ...
'Access to columns.'
@property def columns(self):
return self.columns_dataset[0]
'Access to dataset.'
@property def dataset(self):
return self.columns_dataset[1]
'Try to access to ``comparison`` cache value, if fail use the ``default`` cache backend config.'
@property def cache_backend(self):
try: comparison_cache = caches['comparison'] except InvalidCacheBackendError: comparison_cache = caches['default'] return comparison_cache
'Key for the cache.'
@property def cache_key(self):
return self.__class__.__name__
'Get the cache from cache.'
def get_cache(self):
return self.cache_backend.get(self.cache_key, {})
'Assign the cache in cache.'
def set_cache(self, value):
value.update(self.cache) return self.cache_backend.set(self.cache_key, value)
'Flush the cache for this instance.'
def cache_flush(self):
return self.cache_backend.delete(self.cache_key)
'Implement high level cache system for get_related.'
def get_related(self, instance, number):
cache = self.cache cache_key = ('%s:%s' % (instance.pk, number)) if (cache_key not in cache): related_objects = super(CachedModelVectorBuilder, self).get_related(instance, number) cache[cache_key] = related_objects self.cache = cache return cache[cache_key]
'Implement high level cache system for columns and dataset.'
@property def columns_dataset(self):
cache = self.cache cache_key = 'columns_dataset' if (cache_key not in cache): columns_dataset = super(CachedModelVectorBuilder, self).columns_dataset cache[cache_key] = columns_dataset self.cache = cache return cache[cache_key]
'Key for the cache handling current site.'
@property def cache_key(self):
return ('%s:%s' % (super(EntryPublishedVectorBuilder, self).cache_key, Site.objects.get_current().pk))
'Checks if an entry is within his publication period.'
@property def is_actual(self):
now = timezone.now() if (self.start_publication and (now < self.start_publication)): return False if (self.end_publication and (now >= self.end_publication)): return False return True
'Checks if an entry is visible and published.'
@property def is_visible(self):
return (self.is_actual and (self.status == PUBLISHED))
'Returns the previous published entry if exists.'
@property def previous_entry(self):
return self.previous_next_entries[0]
'Returns the next published entry if exists.'
@property def next_entry(self):
return self.previous_next_entries[1]
'Returns and caches a tuple containing the next and previous published entries. Only available if the entry instance is published.'
@property def previous_next_entries(self):
previous_next = getattr(self, 'previous_next', None) if (previous_next is None): if (not self.is_visible): previous_next = (None, None) setattr(self, 'previous_next', previous_next) return previous_next entries = list(self.__class__.published.all()) in...
'Returns the entry\'s short url.'
@property def short_url(self):
return get_url_shortener()(self)