<commit_before>from django.conf.urls.defaults import *
from django.http import HttpResponse

from django.db import models
from django.utils.translation import ugettext as _

from ella.core.models import Category
from ella.newman.sites import site
from ella.newman.options import NewmanModelAdmin
from ella.newman import models as m
from ella.newman.filterspecs import filter_spec
from ella.newman.permission import is_category_fk, is_site_fk, applicable_categories
from ella.newman.utils import user_category_filter, get_user_config
from ella.newman.config import CATEGORY_FILTER

class DevMessageAdmin(NewmanModelAdmin):
    list_display = ('title', 'author', 'version', 'ts',)
    prepopulated_fields = {'slug': ('title',)}

    def save_model(self, request, obj, form, change):
        if not obj.id:
            obj.author = request.user
        obj.save()


class HelpItemAdmin(NewmanModelAdmin):
    list_display = ('__unicode__',)
    list_filter = ('ct', 'lang',)
    list_select_related = False


class CategoryUserRoleAdmin(NewmanModelAdmin):
    list_filter = ('user', 'group',)
    list_display = ('user', 'group',)

    def get_urls(self):
        urls = patterns('',
            url(r'^refresh/$',
                self.refresh_view,
                name='categoryuserrole-refresh'),
        )
        urls += super(CategoryUserRoleAdmin, self).get_urls()
        return urls

    def refresh_view(self, request, extra_context=None):
        from ella.newman.management.commands.syncroles import denormalize
        # TODO: don't wait for denormalize()
        denormalize()
        return HttpResponse(_('All roles is now refreshed.'))

site.register(m.DevMessage, DevMessageAdmin)
site.register(m.AdminHelpItem, HelpItemAdmin)
site.register(m.CategoryUserRole, CategoryUserRoleAdmin)

# testing Options from register.py
import register


# Category filter -- restricted categories accordingly to CategoryUserRoles and categories filtered via AdminSettings.
# custom registered DateField filter. Filter is inserted to the beginning of filter chain.
@filter_spec(lambda field: is_category_fk(field))
def category_field_filter(fspec):
    #qs = Category.objects.filter(pk__in=applicable_categories(fspec.user))
    print 'Categories %d' % fspec.model_admin.queryset(fspec.request).count()
    #print 'self.params %s' % fspec.params
    #print 'self.result_list %s' % fspec.result_list
    qs = Category.objects.filter(pk__in=applicable_categories(fspec.user))
    for cat in user_category_filter(qs, fspec.user):
        lookup_var = '%s__%s__exact' % (fspec.field_path, fspec.f.rel.to._meta.pk.name)
        link = ( cat, {lookup_var: cat.pk})
        fspec.links.append(link)
    return True

@filter_spec(lambda field: is_site_fk(field))
def site_field_filter(fspec):
    category_ids = get_user_config(fspec.user, CATEGORY_FILTER)
    if not category_ids:
        category_ids = m.DenormalizedCategoryUserRole.objects.root_categories_by_user(fspec.user)
    qs = Category.objects.filter(pk__in=category_ids)
    sites = map(lambda c: c.site, qs)
    for site in sites:
        #category__site__id__exact=1
        lookup_var = '%s__%s__exact' % (fspec.field_path, fspec.f.rel.to._meta.pk.name)
        link = ( site, {lookup_var: site.pk})
        fspec.links.append(link)
    return True

@filter_spec(lambda field: field.name == 'created' and isinstance(field, models.DateTimeField))
def created_field_filter(fspec):<commit_msg>Sitefilter bug fixed (occured when user category filter was empty).  refs #2278<commit_after>from django.conf.urls.defaults import *
from django.http import HttpResponse
from django.db import models
from django.utils.translation import ugettext as _

from ella.core.models import Category
from ella.newman.sites import site
from ella.newman.options import NewmanModelAdmin
from ella.newman import models as m
from ella.newman.filterspecs import filter_spec
from ella.newman.permission import is_category_fk, is_site_fk, applicable_categories
from ella.newman.utils import user_category_filter, get_user_config
from ella.newman.config import CATEGORY_FILTER

class DevMessageAdmin(NewmanModelAdmin):
    list_display = ('title', 'author', 'version', 'ts',)
    prepopulated_fields = {'slug': ('title',)}

    def save_model(self, request, obj, form, change):
        if not obj.id:
            obj.author = request.user
        obj.save()


class HelpItemAdmin(NewmanModelAdmin):
    list_display = ('__unicode__',)
    list_filter = ('ct', 'lang',)
    list_select_related = False


class CategoryUserRoleAdmin(NewmanModelAdmin):
    list_filter = ('user', 'group',)
    list_display = ('user', 'group',)

    def get_urls(self):
        urls = patterns('',
            url(r'^refresh/$',
                self.refresh_view,
                name='categoryuserrole-refresh'),
        )
        urls += super(CategoryUserRoleAdmin, self).get_urls()
        return urls

    def refresh_view(self, request, extra_context=None):
        from ella.newman.management.commands.syncroles import denormalize
        # TODO: don't wait for denormalize()
        denormalize()
        return HttpResponse(_('All roles is now refreshed.'))

site.register(m.DevMessage, DevMessageAdmin)
site.register(m.AdminHelpItem, HelpItemAdmin)
site.register(m.CategoryUserRole, CategoryUserRoleAdmin)

# testing Options from register.py
import register


# Category filter -- restricted categories accordingly to CategoryUserRoles and categories filtered via AdminSettings.
# custom registered DateField filter. Filter is inserted to the beginning of filter chain.
@filter_spec(lambda field: is_category_fk(field))
def category_field_filter(fspec):
    #qs = Category.objects.filter(pk__in=applicable_categories(fspec.user))
    print 'Categories %d' % fspec.model_admin.queryset(fspec.request).count()
    #print 'self.params %s' % fspec.params
    #print 'self.result_list %s' % fspec.result_list
    qs = Category.objects.filter(pk__in=applicable_categories(fspec.user))
    for cat in user_category_filter(qs, fspec.user):
        lookup_var = '%s__%s__exact' % (fspec.field_path, fspec.f.rel.to._meta.pk.name)
        link = ( cat, {lookup_var: cat.pk})
        fspec.links.append(link)
    return True

@filter_spec(lambda field: is_site_fk(field))
def site_field_filter(fspec):
    category_ids = get_user_config(fspec.user, CATEGORY_FILTER)
    if not category_ids:
        if not fspec.user.is_superuser:
            category_ids = m.DenormalizedCategoryUserRole.objects.root_categories_by_user(fspec.user)
        else:
            category_ids = Category.objects.filter(tree_parent=None)
    qs = Category.objects.filter(pk__in=category_ids)
    sites = map(lambda c: c.site, qs)
    for site in sites:
        #category__site__id__exact=1
        lookup_var = '%s__%s__exact' % (fspec.field_path, fspec.f.rel.to._meta.pk.name)
        link = ( site, {lookup_var: site.pk})
        fspec.links.append(link)
    return True

@filter_spec(lambda field: field.name == 'created' and isinstance(field, models.DateTimeField))
def created_field_filter(fspec):
{'fraction_non_alphanumeric': 0.1038961038961039, 'fraction_numerical': 0.0008469791078486731, 'mean_word_length': 4.017705382436261, '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': '5354686', 'n_tokens_mistral': 2201, 'n_tokens_neox': 2157, 'n_words': 502}