<commit_before>"""
Admin configuration for ``eventkit`` app.
"""

# Define `list_display`, `list_filter` and `search_fields` for each model.
# These go a long way to making the admin more usable.

from dateutil import rrule
import datetime
import json
import six

from django.contrib import admin
from django.core.serializers.json import DjangoJSONEncoder
from django.core.urlresolvers import reverse
from django.db.models import Q
from django.http import HttpResponse, JsonResponse
from django.template.response import TemplateResponse
from django.utils.translation import ugettext_lazy as _
from django.views.decorators.csrf import csrf_exempt
from icekit.admin import (
    ChildModelFilter, ChildModelPluginPolymorphicParentModelAdmin)
from polymorphic.admin import PolymorphicChildModelAdmin
from timezone import timezone

from eventkit import admin_forms, appsettings, forms, models, plugins


class EventChildAdmin(PolymorphicChildModelAdmin):
    """
    Abstract admin class for polymorphic child event models.
    """
    base_form = admin_forms.BaseEventForm
    base_model = models.Event
    formfield_overrides = {
        models.RecurrenceRuleField: {'widget': forms.RecurrenceRuleWidget},
    }

    def save_model(self, request, obj, form, change):
        """
        Propagate changes if requested.
        """
        obj.save(propagate=form.cleaned_data['propagate'])


class EventTypeFilter(ChildModelFilter):
    child_model_plugin_class = plugins.EventChildModelPlugin


class OriginalFilter(admin.SimpleListFilter):
    title = _('is original')
    parameter_name = 'is_original'

    YES = '1'
    NO = '0'

    def lookups(self, request, model_admin):
        lookups = (
            (self.YES, _('Yes')),
            (self.NO, _('No')),
        )
        return lookups

    def queryset(self, request, queryset):
        if self.value() == self.YES:
            return queryset.filter(parent=None)
        elif self.value() == self.NO:
            return queryset.exclude(parent=None)


class VariationFilter(admin.SimpleListFilter):
    title = _('is variation')
    parameter_name = 'is_variation'

    YES = '1'
    NO = '0'

    def lookups(self, request, model_admin):
        lookups = (
            (self.YES, _('Yes')),
            (self.NO, _('No')),
        )
        return lookups

    def queryset(self, request, queryset):
        if self.value() == self.YES:
            return queryset.exclude(parent=None).exclude(is_repeat=True)
        elif self.value() == self.NO:
            return queryset.filter(Q(parent=None) | Q(is_repeat=True))


class EventAdmin(ChildModelPluginPolymorphicParentModelAdmin):
    base_model = models.Event
    list_filter = (
        'all_day', 'starts', 'ends', EventTypeFilter, OriginalFilter,
        VariationFilter, 'is_repeat')
    list_display = (
        '__str__', 'all_day', 'starts', 'ends', 'is_original', 'is_variation',
        'is_repeat')
    search_fields = ('title', )

    child_model_plugin_class = plugins.EventChildModelPlugin
    child_model_admin = EventChildAdmin

    def get_urls(self):
        """
        Add a calendar URL.
        """
        from django.conf.urls import patterns, url
        urls = super(EventAdmin, self).get_urls()
        my_urls = patterns(
            '',
            url(
                r'^calendar/$',
                self.admin_site.admin_view(self.calendar),
                name='eventkit_event_calendar'
            ),
        )
        return my_urls + urls

    def calendar(self, request):
        """
        Return event data in JSON format for AJAX requests, or a calendar page
        to be loaded in an iframe.
        """
        if not request.is_ajax():
            return TemplateResponse(
                request, 'admin/eventkit/event/calendar.html')
        tz = timezone.get(request.GET.get('timezone'))<commit_msg>Show modified field on change list.<commit_after>"""
Admin configuration for ``eventkit`` app.
"""

# Define `list_display`, `list_filter` and `search_fields` for each model.
# These go a long way to making the admin more usable.

from dateutil import rrule
import datetime
import json
import six

from django.contrib import admin
from django.core.serializers.json import DjangoJSONEncoder
from django.core.urlresolvers import reverse
from django.db.models import Q
from django.http import HttpResponse, JsonResponse
from django.template.response import TemplateResponse
from django.utils.translation import ugettext_lazy as _
from django.views.decorators.csrf import csrf_exempt
from icekit.admin import (
    ChildModelFilter, ChildModelPluginPolymorphicParentModelAdmin)
from polymorphic.admin import PolymorphicChildModelAdmin
from timezone import timezone

from eventkit import admin_forms, appsettings, forms, models, plugins


class EventChildAdmin(PolymorphicChildModelAdmin):
    """
    Abstract admin class for polymorphic child event models.
    """
    base_form = admin_forms.BaseEventForm
    base_model = models.Event
    formfield_overrides = {
        models.RecurrenceRuleField: {'widget': forms.RecurrenceRuleWidget},
    }

    def save_model(self, request, obj, form, change):
        """
        Propagate changes if requested.
        """
        obj.save(propagate=form.cleaned_data['propagate'])


class EventTypeFilter(ChildModelFilter):
    child_model_plugin_class = plugins.EventChildModelPlugin


class OriginalFilter(admin.SimpleListFilter):
    title = _('is original')
    parameter_name = 'is_original'

    YES = '1'
    NO = '0'

    def lookups(self, request, model_admin):
        lookups = (
            (self.YES, _('Yes')),
            (self.NO, _('No')),
        )
        return lookups

    def queryset(self, request, queryset):
        if self.value() == self.YES:
            return queryset.filter(parent=None)
        elif self.value() == self.NO:
            return queryset.exclude(parent=None)


class VariationFilter(admin.SimpleListFilter):
    title = _('is variation')
    parameter_name = 'is_variation'

    YES = '1'
    NO = '0'

    def lookups(self, request, model_admin):
        lookups = (
            (self.YES, _('Yes')),
            (self.NO, _('No')),
        )
        return lookups

    def queryset(self, request, queryset):
        if self.value() == self.YES:
            return queryset.exclude(parent=None).exclude(is_repeat=True)
        elif self.value() == self.NO:
            return queryset.filter(Q(parent=None) | Q(is_repeat=True))


class EventAdmin(ChildModelPluginPolymorphicParentModelAdmin):
    base_model = models.Event
    list_filter = (
        'all_day', 'starts', 'ends', EventTypeFilter, OriginalFilter,
        VariationFilter, 'is_repeat', 'modified')
    list_display = (
        '__str__', 'all_day', 'starts', 'ends', 'is_original', 'is_variation',
        'is_repeat', 'modified')
    search_fields = ('title', )

    child_model_plugin_class = plugins.EventChildModelPlugin
    child_model_admin = EventChildAdmin

    def get_urls(self):
        """
        Add a calendar URL.
        """
        from django.conf.urls import patterns, url
        urls = super(EventAdmin, self).get_urls()
        my_urls = patterns(
            '',
            url(
                r'^calendar/$',
                self.admin_site.admin_view(self.calendar),
                name='eventkit_event_calendar'
            ),
        )
        return my_urls + urls

    def calendar(self, request):
        """
        Return event data in JSON format for AJAX requests, or a calendar page
        to be loaded in an iframe.
        """
        if not request.is_ajax():
            return TemplateResponse(
                request, 'admin/eventkit/event/calendar.html')
        tz = timezone.get(request.GET.get('timezone'))
{'fraction_non_alphanumeric': 0.10143995885831833, 'fraction_numerical': 0.0010285420416559527, 'mean_word_length': 2.8226044226044227, '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': '3345955', 'n_tokens_mistral': 2280, 'n_tokens_neox': 2203, 'n_words': 558}