<commit_before>from datetime import time, timedelta, datetime, date

from django.contrib import admin, messages
from django.contrib.admin import register
from django.forms import Widget, ModelForm
from django.template.loader import render_to_string
from django.template.response import TemplateResponse
from django.urls import reverse
from django.utils.html import format_html
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _
from solo.admin import SingletonModelAdmin

from api.shows.models import ScheduleSlate, ShowSlot, Show
from .models import ShowApplication, TimeSlotRequest, ShowApplicationSettings


class AcceptedListFilter(admin.SimpleListFilter):
    title = 'accepted status'
    parameter_name = 'is_accepted'

    def lookups(self, request, model_admin):
        return [
            ('1', _('Accepted')),
            ('0', _('Not yet accepted')),
        ]

    def queryset(self, request, queryset):
        if self.value() == '1':
            return queryset.filter(assigned_slot__isnull=False)
        if self.value() == '0':
            return queryset.filter(assigned_slot__isnull=True)

        return queryset

class HasShowListFilter(admin.SimpleListFilter):
    title = 'show presence'
    parameter_name = 'has_show'

    def lookups(self, request, model_admin):
        return [
            ('1', _('Show attached')),
            ('0', _('Show not yet attached')),
        ]

    def queryset(self, request, queryset):
        if self.value() == '1':
            return queryset.filter(connected_show__isnull=False)
        elif self.value() == '0':
            return queryset.filter(connected_show__isnull=True)

        return queryset

class TimeSlotWidget(Widget):
    def render(self, name, value, attrs=None):
        if value is None:
            return ""

        slot = TimeSlotRequest.objects.get(pk=value)
        context = {
            'slot_request': slot,
            'taken': slot.is_taken(),
            'slot_name': name,
        }

        # if slot.is_taken():
        #     context['taken_by_self'] = slot.accepted_application ==

        return mark_safe(render_to_string('admin/time_slot_widget.html', context))

class ShowApplicationForm(ModelForm):
    class Meta:
        model = ShowApplication
        fields = '__all__'
        widgets = {
            'first_slot_choice': TimeSlotWidget,
            'second_slot_choice': TimeSlotWidget,
            'third_slot_choice': TimeSlotWidget,
        }

        help_texts = {
            'link_to_connected_show': 'When this slot has been turned into a show, it will be linked here',
        }

@register(ShowApplication)
class ShowApplicationAdmin(admin.ModelAdmin):
    form = ShowApplicationForm
    list_display = ('name', 'created_at', 'host_name', 'category',)
    list_select_related = ('owner', 'category',)
    search_fields = ('name', 'host_name', 'producer_name',)
    list_filter = ('category', AcceptedListFilter, HasShowListFilter,)

    actions = ('make_shows',)

    readonly_fields = (
        'name', 'host_name', 'contact_email', 'contact_phone',
        'cover', 'cover_width', 'cover_height',
        'banner', 'banner_width', 'banner_height',
        'created_at', 'link_to_connected_show'
    )

    fieldsets = (
        ('Applicant Info', {<commit_msg>Add default ordering to show applications admin<commit_after>from datetime import time, timedelta, datetime, date

from django.contrib import admin, messages
from django.contrib.admin import register
from django.forms import Widget, ModelForm
from django.template.loader import render_to_string
from django.template.response import TemplateResponse
from django.urls import reverse
from django.utils.html import format_html
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _
from solo.admin import SingletonModelAdmin

from api.shows.models import ScheduleSlate, ShowSlot, Show
from .models import ShowApplication, TimeSlotRequest, ShowApplicationSettings


class AcceptedListFilter(admin.SimpleListFilter):
    title = 'accepted status'
    parameter_name = 'is_accepted'

    def lookups(self, request, model_admin):
        return [
            ('1', _('Accepted')),
            ('0', _('Not yet accepted')),
        ]

    def queryset(self, request, queryset):
        if self.value() == '1':
            return queryset.filter(assigned_slot__isnull=False)
        if self.value() == '0':
            return queryset.filter(assigned_slot__isnull=True)

        return queryset

class HasShowListFilter(admin.SimpleListFilter):
    title = 'show presence'
    parameter_name = 'has_show'

    def lookups(self, request, model_admin):
        return [
            ('1', _('Show attached')),
            ('0', _('Show not yet attached')),
        ]

    def queryset(self, request, queryset):
        if self.value() == '1':
            return queryset.filter(connected_show__isnull=False)
        elif self.value() == '0':
            return queryset.filter(connected_show__isnull=True)

        return queryset

class TimeSlotWidget(Widget):
    def render(self, name, value, attrs=None):
        if value is None:
            return ""

        slot = TimeSlotRequest.objects.get(pk=value)
        context = {
            'slot_request': slot,
            'taken': slot.is_taken(),
            'slot_name': name,
        }

        # if slot.is_taken():
        #     context['taken_by_self'] = slot.accepted_application ==

        return mark_safe(render_to_string('admin/time_slot_widget.html', context))

class ShowApplicationForm(ModelForm):
    class Meta:
        model = ShowApplication
        fields = '__all__'
        widgets = {
            'first_slot_choice': TimeSlotWidget,
            'second_slot_choice': TimeSlotWidget,
            'third_slot_choice': TimeSlotWidget,
        }

        help_texts = {
            'link_to_connected_show': 'When this slot has been turned into a show, it will be linked here',
        }

@register(ShowApplication)
class ShowApplicationAdmin(admin.ModelAdmin):
    form = ShowApplicationForm
    list_display = ('name', 'created_at', 'host_name', 'category',)
    list_select_related = ('owner', 'category',)
    search_fields = ('name', 'host_name', 'producer_name',)
    list_filter = ('category', AcceptedListFilter, HasShowListFilter,)
    ordering = ('-created_at',)

    actions = ('make_shows',)

    readonly_fields = (
        'name', 'host_name', 'contact_email', 'contact_phone',
        'cover', 'cover_width', 'cover_height',
        'banner', 'banner_width', 'banner_height',
        'created_at', 'link_to_connected_show'
    )

    fieldsets = (
        ('Applicant Info', {
{'fraction_non_alphanumeric': 0.10766260766260766, 'fraction_numerical': 0.002376002376002376, 'mean_word_length': 3.045045045045045, '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': '324765', 'n_tokens_mistral': 1934, 'n_tokens_neox': 1901, 'n_words': 468}