<commit_before>from django.contrib import admin
from django.utils.translation import ugettext_lazy as _

from tagging.models import TaggingInlineOptions

from ella.core.admin.models import ListingInlineOptions, HitCountInlineOptions
from ella.ellaadmin import fields

from ella.articles.models import ArticleContents, Article, InfoBox


class ArticleContentInlineOptions(admin.TabularInline):
    model = ArticleContents
    extra = 1
    def formfield_for_dbfield(self, db_field, **kwargs):
        if db_field.name == 'content':
            return fields.RichTextAreaField(**kwargs)
        return super(self.__class__, self).formfield_for_dbfield(db_field, **kwargs)

class InfoBoxOptions(admin.ModelAdmin):
    list_display = ('title', 'created',)
    date_hierarchy = 'created'
    list_filter = ('created', 'updated',)
    search_fields = ('title', 'content',)

    def formfield_for_dbfield(self, db_field, **kwargs):
        if db_field.name == 'content':
            return fields.RichTextAreaField(**kwargs)
        return super(self.__class__, self).formfield_for_dbfield(db_field, **kwargs)

class ArticleOptions(admin.ModelAdmin):
    list_display = ('title', 'category', 'photo_thumbnail', 'created', 'article_age', 'full_url',)
    date_hierarchy = 'created'
    ordering = ('-created',)
    fieldsets = (
        (_("Article heading"), {'fields': ('title', 'upper_title', 'updated', 'slug')}),
        (_("Article contents"), {'fields': ('perex',)}),
        (_("Metadata"), {'fields': ('category', 'authors', 'source', 'photo')}),
)
    raw_id_fields = ('photo',)
    list_filter = ('category__site', 'created', 'category', 'authors',)
    search_fields = ('title', 'upper_title', 'perex', 'slug',)
#    inlines = (ArticleContentInlineOptions, ListingInlineOptions, TaggingInlineOptions, HitCountInlineOptions)
    inlines = (ArticleContentInlineOptions, ListingInlineOptions, TaggingInlineOptions,)
    prepopulated_fields = {'slug' : ('title',)}

    def formfield_for_dbfield(self, db_field, **kwargs):
        if db_field.name == 'perex':
            return fields.RichTextAreaField(**kwargs)
        return super(self.__class__, self).formfield_for_dbfield(db_field, **kwargs)

admin.site.register(InfoBox, InfoBoxOptions)
admin.site.register(Article, ArticleOptions)
<commit_msg>Added listings to ModelAdmin.queryset() so that it can be sorted according to Listings' publish_from.<commit_after>from django.contrib import admin
from django.contrib.contenttypes.models import ContentType
from django.utils.translation import ugettext_lazy as _
from django.db import connection

from tagging.models import TaggingInlineOptions

from ella.core.models import Listing
from ella.core.admin.models import ListingInlineOptions, HitCountInlineOptions
from ella.ellaadmin import fields
from ella.articles.models import ArticleContents, Article, InfoBox


class ArticleContentInlineOptions(admin.TabularInline):
    model = ArticleContents
    extra = 1
    def formfield_for_dbfield(self, db_field, **kwargs):
        if db_field.name == 'content':
            return fields.RichTextAreaField(**kwargs)
        return super(self.__class__, self).formfield_for_dbfield(db_field, **kwargs)

class InfoBoxOptions(admin.ModelAdmin):
    list_display = ('title', 'created',)
    date_hierarchy = 'created'
    list_filter = ('created', 'updated',)
    search_fields = ('title', 'content',)

    def formfield_for_dbfield(self, db_field, **kwargs):
        if db_field.name == 'content':
            return fields.RichTextAreaField(**kwargs)
        return super(self.__class__, self).formfield_for_dbfield(db_field, **kwargs)



##
# prepare lookup for ArticleOptions.queryset()
##
qn = connection.ops.quote_name
where_lookup = (
        '%s.target_ct_id = %d' % (qn(Listing._meta.db_table),ContentType.objects.get_for_model(Article).id),
        '%s.target_id = %s.id' % (qn(Listing._meta.db_table), qn(Article._meta.db_table)),
        '%s.category_id = %s.category_id' % (qn(Listing._meta.db_table), qn(Article._meta.db_table)),
)

class ArticleOptions(admin.ModelAdmin):
    list_display = ('title', 'category', 'photo_thumbnail', 'created', 'article_age', 'full_url',)
    date_hierarchy = 'created'
    ordering = ('-created',)
    fieldsets = (
        (_("Article heading"), {'fields': ('title', 'upper_title', 'updated', 'slug')}),
        (_("Article contents"), {'fields': ('perex',)}),
        (_("Metadata"), {'fields': ('category', 'authors', 'source', 'photo')}),
)
    raw_id_fields = ('photo',)
    list_filter = ('category__site', 'created', 'category', 'authors',)
    search_fields = ('title', 'upper_title', 'perex', 'slug',)
#    inlines = (ArticleContentInlineOptions, ListingInlineOptions, TaggingInlineOptions, HitCountInlineOptions)
    inlines = (ArticleContentInlineOptions, ListingInlineOptions, TaggingInlineOptions,)
    prepopulated_fields = {'slug' : ('title',)}

    def queryset(self, request):
        """
        Add listing to the query and sort on publish_from.
        """
        #FIXME: will omit articles without listing (outer join takes 1:30 to finish)
        #TODO: verigy if the sorting isn't overriden in ChangeList
        qset = super(ArticleOptions, self).queryset()
        qset = qset.extra(
                tables=[ Listing._meta.db_table, ],
                where=where_lookup,
                select={'publish_from' : '%s.publish_from' % qn(Listing._meta.db_table),},
).order_by('-publish_from')
        return qset


    def formfield_for_dbfield(self, db_field, **kwargs):
        if db_field.name == 'perex':
            return fields.RichTextAreaField(**kwargs)
        return super(self.__class__, self).formfield_for_dbfield(db_field, **kwargs)

admin.site.register(InfoBox, InfoBoxOptions)
admin.site.register(Article, ArticleOptions)

{'fraction_non_alphanumeric': 0.13486048914915605, 'fraction_numerical': 0.000861178091629349, 'mean_word_length': 4.38682745825603, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '732818', 'n_tokens_mistral': 1771, 'n_tokens_neox': 1747, 'n_words': 380}