def get_popular_gbobjects(number=5, template='objectapp/tags/popular_gbobjects.html'): """Return popular gbobjects""" ctype = ContentType.objects.get_for_model(Gbobject) query = """SELECT object_pk, COUNT(*) AS score FROM %s WHERE content_type_id = %%s AND is_public = '1' GROUP BY object_pk ORDER BY score DESC""" % get_comment_model()._meta.db_table cursor = connection.cursor() cursor.execute(query, [ctype.id]) object_ids = [int(row[0]) for row in cursor.fetchall()] # Use ``in_bulk`` here instead of an ``id__in`` filter, because ``id__in`` # would clobber the ordering. object_dict = Gbobject.published.in_bulk(object_ids) return { 'template': template, 'gbobjects': [ object_dict[object_id] for object_id in object_ids if object_id in object_dict ][:number] }
def get_popular_gbobjects(number=5, template="objectapp/tags/popular_gbobjects.html"): """Return popular gbobjects""" ctype = ContentType.objects.get_for_model(Gbobject) query = ( """SELECT object_pk, COUNT(*) AS score FROM %s WHERE content_type_id = %%s AND is_public = '1' GROUP BY object_pk ORDER BY score DESC""" % get_comment_model()._meta.db_table ) cursor = connection.cursor() cursor.execute(query, [ctype.id]) object_ids = [int(row[0]) for row in cursor.fetchall()] # Use ``in_bulk`` here instead of an ``id__in`` filter, because ``id__in`` # would clobber the ordering. object_dict = Gbobject.published.in_bulk(object_ids) return { "template": template, "gbobjects": [object_dict[object_id] for object_id in object_ids if object_id in object_dict][:number], }
def comment_admin_urlname(action): """ Return the admin URLs for the comment app used. """ comment = get_comment_model() return 'admin:%s_%s_%s' % (comment._meta.app_label, comment._meta.model_name, action)
def comment_admin_urlname(action): """ Return the admin URLs for the comment app used. """ comment = get_comment_model() return 'admin:%s_%s_%s' % ( comment._meta.app_label, comment._meta.model_name, action)
def get_recent_comments(number=5, template='gstudio/tags/recent_comments.html'): """Return the most recent comments""" # Using map(smart_unicode... fix bug related to issue #8554 comments = get_comment_model().objects.filter(is_public=True).order_by('-submit_date')[:number] return {'template': template, 'comments': comments}
def moderation_queue(request): """ Displays a list of unapproved comments to be approved. Templates: `comments/moderation_queue.html` Context: comments Comments to be approved (paginated). empty Is the comment list empty? is_paginated Is there more than one page? results_per_page Number of comments per page has_next Is there a next page? has_previous Is there a previous page? page The current page number next The next page number pages Number of pages hits Total number of comments page_range Range of page numbers Copied from Django 1.1, since it was removed in Django 1.2. """ qs = get_comment_model().objects.filter(is_public=False, is_removed=False) paginator = Paginator(qs, 100) try: page = int(request.GET.get("page", 1)) except ValueError: raise Http404 try: comments_per_page = paginator.page(page) except InvalidPage: raise Http404 return render_to_response("comments/moderation_queue.html", { 'comments' : comments_per_page.object_list, 'empty' : page == 1 and paginator.count == 0, 'is_paginated': paginator.num_pages > 1, 'results_per_page': 100, 'has_next': comments_per_page.has_next(), 'has_previous': comments_per_page.has_previous(), 'page': page, 'next': page + 1, 'previous': page - 1, 'pages': paginator.num_pages, 'hits' : paginator.count, 'page_range' : paginator.page_range }, context_instance=template.RequestContext(request))
def undo(request): if request.method == 'POST' and 'actions' in request.POST: pks = request.POST['actions'].split('-') comments = get_comment_model().objects.filter(pk__in=pks) # hide the comments comments.update(is_public=False, is_removed=False) # remove flags CommentFlag.objects.filter(flag__in=(CommentFlag.MODERATOR_DELETION, CommentFlag.MODERATOR_APPROVAL), comment__in=comments).delete() return HttpResponseRedirect(reverse('comments-moderation-queue'))
def undo(request): if request.method == 'POST' and 'actions' in request.POST: pks = request.POST['actions'].split('-') comments = get_comment_model().objects.filter(pk__in=pks) # hide the comments comments.update(is_public=False, is_removed=False) # remove flags CommentFlag.objects.filter( flag__in=(CommentFlag.MODERATOR_DELETION, CommentFlag.MODERATOR_APPROVAL), comment__in=comments).delete() return HttpResponseRedirect(reverse('comments-moderation-queue'))
def get_recent_comments(number=5, template='zinnia/tags/recent_comments.html'): """Return the most recent comments""" # Using map(smart_unicode... fix bug related to issue #8554 entry_published_pks = map(smart_unicode, Entry.published.values_list('id', flat=True)) content_type = ContentType.objects.get_for_model(Entry) comments = get_comment_model().objects.filter( Q(flags=None) | Q(flags__flag=CommentFlag.MODERATOR_APPROVAL), content_type=content_type, object_pk__in=entry_published_pks, is_public=True).order_by('-submit_date')[:number] return {'template': template, 'comments': comments}
def get_recent_linkbacks(number=5, template='zinnia/tags/recent_linkbacks.html'): """Return the most recent linkbacks""" entry_published_pks = map(smart_unicode, Entry.published.values_list('id', flat=True)) content_type = ContentType.objects.get_for_model(Entry) linkbacks = get_comment_model().objects.filter( content_type=content_type, object_pk__in=entry_published_pks, flags__flag__in=[PINGBACK, TRACKBACK], is_public=True).order_by('-submit_date')[:number] return {'template': template, 'linkbacks': linkbacks}
def get_recent_linkbacks(number=5, template='objectapp/tags/recent_linkbacks.html'): """Return the most recent linkbacks""" gbobject_published_pks = map( smart_unicode, Gbobject.published.values_list('id', flat=True)) content_type = ContentType.objects.get_for_model(Gbobject) linkbacks = get_comment_model().objects.filter( content_type=content_type, object_pk__in=gbobject_published_pks, flags__flag__in=['pingback', 'trackback'], is_public=True).order_by('-submit_date')[:number] return {'template': template, 'linkbacks': linkbacks}
def get_recent_comments(number=5, template='zinnia/tags/recent_comments.html'): """Return the most recent comments""" # Using map(smart_unicode... fix bug related to issue #8554 entry_published_pks = map(smart_unicode, Entry.published.values_list('id', flat=True)) content_type = ContentType.objects.get_for_model(Entry) comments = get_comment_model().objects.filter( Q(flags=None) | Q(flags__flag=CommentFlag.MODERATOR_APPROVAL), content_type=content_type, object_pk__in=entry_published_pks, is_public=True).order_by('-submit_date')[:number] return {'template': template, 'comments': comments}
def get_recent_linkbacks(number=5, template='zinnia/tags/recent_linkbacks.html'): """Return the most recent linkbacks""" entry_published_pks = map(smart_unicode, Entry.published.values_list('id', flat=True)) content_type = ContentType.objects.get_for_model(Entry) linkbacks = get_comment_model().objects.filter( content_type=content_type, object_pk__in=entry_published_pks, flags__flag__in=['pingback', 'trackback'], is_public=True).order_by( '-submit_date')[:number] return {'template': template, 'linkbacks': linkbacks}
def get_popular_entries(number=5, template='zinnia/tags/popular_entries.html'): """Return popular entries""" ctype = ContentType.objects.get_for_model(Entry) objects_by_score = get_comment_model().objects.filter( content_type=ctype, is_public=True).values('object_pk').annotate( score=Count('id')).order_by('-score') object_ids = [int(obj['object_pk']) for obj in objects_by_score] # Use ``in_bulk`` here instead of an ``id__in`` filter, because ``id__in`` # would clobber the ordering. object_dict = Entry.published.in_bulk(object_ids) return {'template': template, 'entries': [object_dict[object_id] for object_id in object_ids if object_id in object_dict][:number]}
def get_popular_entries(number=5, template='zinnia/tags/popular_entries.html'): """Return popular entries""" ctype = ContentType.objects.get_for_model(Entry) objects_by_score = get_comment_model().objects.filter( content_type=ctype, is_public=True).values('object_pk').annotate( score=Count('id')).order_by('-score') object_ids = [int(obj['object_pk']) for obj in objects_by_score] # Use ``in_bulk`` here instead of an ``id__in`` filter, because ``id__in`` # would clobber the ordering. object_dict = Entry.published.in_bulk(object_ids) return {'template': template, 'entries': [object_dict[object_id] for object_id in object_ids if object_id in object_dict][:number]}
def get_recent_linkbacks(number=5, template="zinnia/tags/recent_linkbacks.html"): """Return the most recent linkbacks""" entry_published_pks = map(smart_unicode, Entry.published.values_list("id", flat=True)) content_type = ContentType.objects.get_for_model(Entry) linkbacks = ( get_comment_model() .objects.filter( content_type=content_type, object_pk__in=entry_published_pks, flags__flag__in=[PINGBACK, TRACKBACK], is_public=True, ) .order_by("-submit_date")[:number] ) return {"template": template, "linkbacks": linkbacks}
def get_recent_linkbacks(number=5, template='zinnia/tags/recent_linkbacks.html'): """Return the most recent linkbacks""" entry_published_pks = map(smart_text, Entry.published.values_list('id', flat=True)) content_type = ContentType.objects.get_for_model(Entry) linkbacks = get_comment_model().objects.filter( content_type=content_type, object_pk__in=entry_published_pks, flags__flag__in=[PINGBACK, TRACKBACK], is_public=True).order_by('-submit_date')[:number] linkbacks = linkbacks.prefetch_related('content_object') return {'template': template, 'linkbacks': linkbacks}
def get_recent_linkbacks(number=5, template="objectapp/tags/recent_linkbacks.html"): """Return the most recent linkbacks""" gbobject_published_pks = map(smart_unicode, Gbobject.published.values_list("id", flat=True)) content_type = ContentType.objects.get_for_model(Gbobject) linkbacks = ( get_comment_model() .objects.filter( content_type=content_type, object_pk__in=gbobject_published_pks, flags__flag__in=["pingback", "trackback"], is_public=True, ) .order_by("-submit_date")[:number] ) return {"template": template, "linkbacks": linkbacks}
def get_recent_comments(number=5, template="objectapp/tags/recent_comments.html"): """Return the most recent comments""" # Using map(smart_unicode... fix bug related to issue #8554 gbobject_published_pks = map(smart_unicode, Gbobject.published.values_list("id", flat=True)) content_type = ContentType.objects.get_for_model(Gbobject) comments = ( get_comment_model() .objects.filter( Q(flags=None) | Q(flags__flag=CommentFlag.MODERATOR_APPROVAL), content_type=content_type, object_pk__in=gbobject_published_pks, is_public=True, ) .order_by("-submit_date")[:number] ) return {"template": template, "comments": comments}
def get_popular_entries(number=5, template="zinnia/tags/popular_entries.html"): """Return popular entries""" ctype = ContentType.objects.get_for_model(Entry) objects_by_score = ( get_comment_model() .objects.filter(content_type=ctype, is_public=True) .values("object_pk") .annotate(score=Count("id")) .order_by("-score") ) object_ids = [int(obj["object_pk"]) for obj in objects_by_score] # Use ``in_bulk`` here instead of an ``id__in`` filter, because ``id__in`` # would clobber the ordering. object_dict = Entry.published.in_bulk(object_ids) return { "template": template, "entries": [object_dict[object_id] for object_id in object_ids if object_id in object_dict][:number], }
def moderation_queue(request): """ Displays a list of unapproved comments to be approved. Templates: `comments/moderation_queue.html` Context: comments Comments to be approved (paginated). empty Is the comment list empty? is_paginated Is there more than one page? results_per_page Number of comments per page has_next Is there a next page? has_previous Is there a previous page? page The current page number next The next page number pages Number of pages hits Total number of comments page_range Range of page numbers Originally copied from Django 1.1, since it was removed in Django 1.2. """ qs = get_comment_model().objects.filter(is_public=False, is_removed=False) paginator = Paginator(qs, 30) try: page = int(request.GET.get("page", 1)) except ValueError: raise Http404 try: comments_per_page = paginator.page(page) except InvalidPage: raise Http404 if request.method == 'POST': formset = BulkModerateFormSet(request.POST, queryset=comments_per_page.object_list, request=request) if formset.is_valid(): formset.save() if request.POST.get('bulk_action'): bulk_action = request.POST['bulk_action'] perform = None if bulk_action == 'approve': perform = perform_approve elif bulk_action == 'remove': perform = perform_delete if perform: for form in formset.bulk_forms: perform(request, form.instance) formset.actions.add(form.instance) path = request.path undo = '-'.join(str(instance.pk) for instance in formset.actions) if undo: path = '%s?undo=%s' % (path, undo) return HttpResponseRedirect(path) else: formset = BulkModerateFormSet(queryset=comments_per_page.object_list) return render_to_response( "comments/moderation_queue.html", { 'comments': comments_per_page.object_list, 'empty': page == 1 and paginator.count == 0, 'is_paginated': paginator.num_pages > 1, 'results_per_page': 100, 'has_next': comments_per_page.has_next(), 'has_previous': comments_per_page.has_previous(), 'page': page, 'next': page + 1, 'previous': page - 1, 'pages': paginator.num_pages, 'hits': paginator.count, 'page_range': paginator.page_range, 'page_obj': comments_per_page, 'formset': formset, }, context_instance=template.RequestContext(request))
def zinnia_statistics(template='zinnia/tags/statistics.html'): """ Return statistics on the content of Zinnia. """ content_type = ContentType.objects.get_for_model(Entry) discussions = get_comment_model().objects.filter( content_type=content_type) entries = Entry.published categories = Category.objects tags = tags_published() authors = Author.published replies = discussions.filter( flags=None, is_public=True) pingbacks = discussions.filter( flags__flag=PINGBACK, is_public=True) trackbacks = discussions.filter( flags__flag=TRACKBACK, is_public=True) rejects = discussions.filter(is_public=False) entries_count = entries.count() replies_count = replies.count() pingbacks_count = pingbacks.count() trackbacks_count = trackbacks.count() if entries_count: first_entry = entries.order_by('creation_date')[0] last_entry = entries.latest() months_count = (last_entry.creation_date - first_entry.creation_date).days / 31.0 entries_per_month = entries_count / (months_count or 1.0) comments_per_entry = float(replies_count) / entries_count linkbacks_per_entry = float(pingbacks_count + trackbacks_count) / \ entries_count total_words_entry = 0 for e in entries.all(): total_words_entry += e.word_count words_per_entry = float(total_words_entry) / entries_count words_per_comment = 0.0 if replies_count: total_words_comment = 0 for c in replies.all(): total_words_comment += len(c.comment.split()) words_per_comment = float(total_words_comment) / replies_count else: words_per_entry = words_per_comment = entries_per_month = \ comments_per_entry = linkbacks_per_entry = 0.0 return {'template': template, 'entries': entries_count, 'categories': categories.count(), 'tags': tags.count(), 'authors': authors.count(), 'comments': replies_count, 'pingbacks': pingbacks_count, 'trackbacks': trackbacks_count, 'rejects': rejects.count(), 'words_per_entry': words_per_entry, 'words_per_comment': words_per_comment, 'entries_per_month': entries_per_month, 'comments_per_entry': comments_per_entry, 'linkbacks_per_entry': linkbacks_per_entry}
def moderation_queue(request): """ Displays a list of unapproved comments to be approved. Templates: `comments/moderation_queue.html` Context: comments Comments to be approved (paginated). empty Is the comment list empty? is_paginated Is there more than one page? results_per_page Number of comments per page has_next Is there a next page? has_previous Is there a previous page? page The current page number next The next page number pages Number of pages hits Total number of comments page_range Range of page numbers Originally copied from Django 1.1, since it was removed in Django 1.2. """ qs = get_comment_model().objects.filter(is_public=False, is_removed=False) paginator = Paginator(qs, 30) try: page = int(request.GET.get("page", 1)) except ValueError: raise Http404 try: comments_per_page = paginator.page(page) except InvalidPage: raise Http404 if request.method == 'POST': formset = BulkModerateFormSet(request.POST, queryset=comments_per_page.object_list, request=request) if formset.is_valid(): formset.save() if request.POST.get('bulk_action'): bulk_action = request.POST['bulk_action'] perform = None if bulk_action == 'approve': perform = perform_approve elif bulk_action == 'remove': perform = perform_delete if perform: for form in formset.bulk_forms: perform(request, form.instance) formset.actions.add(form.instance) path = request.path undo = '-'.join(str(instance.pk) for instance in formset.actions) if undo: path = '%s?undo=%s' % (path, undo) return HttpResponseRedirect(path) else: formset = BulkModerateFormSet(queryset=comments_per_page.object_list) return render_to_response("comments/moderation_queue.html", { 'comments' : comments_per_page.object_list, 'empty' : page == 1 and paginator.count == 0, 'is_paginated': paginator.num_pages > 1, 'results_per_page': 100, 'has_next': comments_per_page.has_next(), 'has_previous': comments_per_page.has_previous(), 'page': page, 'next': page + 1, 'previous': page - 1, 'pages': paginator.num_pages, 'hits' : paginator.count, 'page_range' : paginator.page_range, 'page_obj': comments_per_page, 'formset': formset, }, context_instance=template.RequestContext(request))
from django.utils.encoding import smart_str from django.contrib.sites.models import Site from django.contrib.auth.models import User from django.template.defaultfilters import slugify from django.core.management.base import CommandError from django.core.management.base import NoArgsCommand from django.contrib.contenttypes.models import ContentType from django.contrib.comments import get_model as get_comment_model from zinnia import __version__ from zinnia.models.entry import Entry from zinnia.models.category import Category from zinnia.managers import DRAFT, PUBLISHED gdata_service = None Comment = get_comment_model() class Command(NoArgsCommand): """Command object for importing a Blogger blog into Zinnia via Google's gdata API.""" help = 'Import a Blogger blog into Zinnia.' option_list = NoArgsCommand.option_list + ( make_option('--blogger-username', dest='blogger_username', default='', help='The username to login to Blogger with'), make_option('--category-title', dest='category_title', default='',
from django.template.defaultfilters import slugify from django.core.management.base import CommandError from django.core.management.base import NoArgsCommand from django.contrib.contenttypes.models import ContentType from django.contrib.comments import get_model as get_comment_model from zinnia import __version__ from zinnia.models.entry import Entry from zinnia.models.author import Author from zinnia.models.category import Category from zinnia.managers import DRAFT, PUBLISHED from zinnia.signals import disconnect_entry_signals from zinnia.signals import disconnect_discussion_signals gdata_service = None Comment = get_comment_model() class Command(NoArgsCommand): """Command object for importing a Blogger blog into Zinnia via Google's gdata API.""" help = 'Import a Blogger blog into Zinnia.' option_list = NoArgsCommand.option_list + ( make_option('--blogger-username', dest='blogger_username', default='', help='The username to login to Blogger with'), make_option('--category-title', dest='category_title', default='', help='The Zinnia category to import Blogger posts to'), make_option('--blogger-blog-id', dest='blogger_blog_id', default='', help='The id of the Blogger blog to import'), make_option('--blogger-limit', dest='blogger_limit', default=25,
def zinnia_statistics(template="zinnia/tags/statistics.html"): """Return statistics on the content of Zinnia""" content_type = ContentType.objects.get_for_model(Entry) discussions = get_comment_model().objects.filter(content_type=content_type) entries = Entry.published categories = Category.objects tags = tags_published() authors = Author.published replies = discussions.filter(flags=None, is_public=True) pingbacks = discussions.filter(flags__flag=PINGBACK, is_public=True) trackbacks = discussions.filter(flags__flag=TRACKBACK, is_public=True) rejects = discussions.filter(is_public=False) entries_count = entries.count() replies_count = replies.count() pingbacks_count = pingbacks.count() trackbacks_count = trackbacks.count() if entries_count: first_entry = entries.order_by("creation_date")[0] last_entry = entries.latest() months_count = (last_entry.creation_date - first_entry.creation_date).days / 31.0 if months_count: entries_per_month = entries_count / months_count else: entries_per_month = entries_count / 1.0 comments_per_entry = float(replies_count) / entries_count linkbacks_per_entry = float(pingbacks_count + trackbacks_count) / entries_count total_words_entry = 0 for e in entries.all(): total_words_entry += e.word_count words_per_entry = float(total_words_entry) / entries_count if replies_count: total_words_comment = 0 for c in replies.all(): total_words_comment += len(c.comment.split()) words_per_comment = float(total_words_comment) / replies_count else: words_per_comment = 0.0 else: words_per_entry = words_per_comment = entries_per_month = comments_per_entry = linkbacks_per_entry = 0.0 return { "template": template, "entries": entries_count, "categories": categories.count(), "tags": tags.count(), "authors": authors.count(), "comments": replies_count, "pingbacks": pingbacks_count, "trackbacks": trackbacks_count, "rejects": rejects.count(), "words_per_entry": words_per_entry, "words_per_comment": words_per_comment, "entries_per_month": entries_per_month, "comments_per_entry": comments_per_entry, "linkbacks_per_entry": linkbacks_per_entry, }
def zinnia_statistics(template='zinnia/tags/statistics.html'): """Return statistics on the content of Zinnia""" content_type = ContentType.objects.get_for_model(Entry) discussions = get_comment_model().objects.filter(content_type=content_type) entries = Entry.published categories = Category.objects tags = tags_published() authors = Author.published replies = discussions.filter(flags=None, is_public=True) pingbacks = discussions.filter(flags__flag=PINGBACK, is_public=True) trackbacks = discussions.filter(flags__flag=TRACKBACK, is_public=True) rejects = discussions.filter(is_public=False) entries_count = entries.count() replies_count = replies.count() pingbacks_count = pingbacks.count() trackbacks_count = trackbacks.count() if entries_count: first_entry = entries.order_by('creation_date')[0] last_entry = entries.latest() months_count = (last_entry.creation_date - \ first_entry.creation_date).days / 31.0 entries_per_month = months_count / entries_count comments_per_entry = float(replies_count) / entries_count linkbacks_per_entry = float(pingbacks_count + trackbacks_count) / \ entries_count total_words_entry = 0 for e in entries.all(): total_words_entry += e.word_count words_per_entry = float(total_words_entry) / entries_count if replies_count: total_words_comment = 0 for c in replies.all(): total_words_comment += len(c.comment.split()) words_per_comment = float(total_words_comment) / replies_count else: words_per_comment = 0.0 else: words_per_entry = words_per_comment = entries_per_month = \ comments_per_entry = linkbacks_per_entry = 0.0 return { 'template': template, 'entries': entries_count, 'categories': categories.count(), 'tags': tags.count(), 'authors': authors.count(), 'comments': replies_count, 'pingbacks': pingbacks_count, 'trackbacks': trackbacks_count, 'rejects': rejects.count(), 'words_per_entry': words_per_entry, 'words_per_comment': words_per_comment, 'entries_per_month': entries_per_month, 'comments_per_entry': comments_per_entry, 'linkbacks_per_entry': linkbacks_per_entry }
def moderation_queue(request): """ Displays a list of unapproved comments to be approved. Templates: `comments/moderation_queue.html` Context: comments Comments to be approved (paginated). empty Is the comment list empty? is_paginated Is there more than one page? results_per_page Number of comments per page has_next Is there a next page? has_previous Is there a previous page? page The current page number next The next page number pages Number of pages hits Total number of comments page_range Range of page numbers Originally copied from Django 1.1, since it was removed in Django 1.2. """ qs = get_comment_model().objects.filter(is_public=False, is_removed=False) paginator = Paginator(qs, 30) try: page = int(request.GET.get("page", 1)) except ValueError: raise Http404 try: comments_per_page = paginator.page(page) except InvalidPage: raise Http404 return render_to_response( "comments/moderation_queue.html", { 'comments': comments_per_page.object_list, 'empty': page == 1 and paginator.count == 0, 'is_paginated': paginator.num_pages > 1, 'results_per_page': 100, 'has_next': comments_per_page.has_next(), 'has_previous': comments_per_page.has_previous(), 'page': page, 'next': page + 1, 'previous': page - 1, 'pages': paginator.num_pages, 'hits': paginator.count, 'page_range': paginator.page_range, 'page_obj': comments_per_page, }, context_instance=template.RequestContext(request))