<commit_before>from django import forms
from django.conf import settings
from django.contrib import admin
from django.contrib.auth.models import Group
from django.contrib.auth.models import User as BuiltInUser
from django.db.models import Q, Count
from django.urls import reverse
from django.utils.html import format_html

from .models import Case, CaseCourt, Default, User, Casebook, Section, \
    Resource, ContentCollaborator, ContentAnnotation, TextBlock, \
    Role, RolesUser


# remove builtin models
admin.site.unregister(Group)
admin.site.unregister(BuiltInUser)


#
# Filters
#

class InputFilter(admin.SimpleListFilter):
    """
    Text input filter, from:
    https://hakibenita.com/how-to-add-a-text-filter-to-django-admin
    """
    template = 'admin/input_filter.html'

    def lookups(self, request, model_admin):
        # Dummy, required to show the filter.
        return ((),)

    def choices(self, changelist):
        # Grab only the "all" option.
        all_choice = next(super().choices(changelist))
        all_choice['query_parts'] = (
            (k, v)
            for k, v in changelist.get_filters_params().items()
            if k != self.parameter_name
        )
        yield all_choice


class CasebookIdFilter(InputFilter):
    parameter_name = 'casebook'
    title = 'Casebook (by ID)'

    def queryset(self, request, queryset):
        value = self.value()
        if value is not None:
            return queryset.filter(casebook_id=value)


class CollaboratorNameFilter(InputFilter):
    parameter_name = 'collaborator-name'
    title = 'Collaborator (by name)'

    def queryset(self, request, queryset):
        value = self.value()
        if value is not None:
            users = User.objects.filter(Q(title__icontains=value) | Q(attribution__icontains=value))
            return queryset.filter(collaborators__in=users)


class CollaboratorIdFilter(InputFilter):
    parameter_name = 'collaborator-id'
    title = 'Collaborator (by id)'

    def queryset(self, request, queryset):
        value = self.value()
        if value is not None:
            users = User.objects.filter(id=value)
            return queryset.filter(collaborators__in=users)


class CourtIdFilter(InputFilter):
    parameter_name = 'court-id'
    title = 'Court (by id)'

    def queryset(self, request, queryset):
        value = self.value()
        if value is not None:
            return queryset.filter(case_court=value)


class ResourceIdFilter(InputFilter):
    parameter_name = 'resource-id'
    title = 'Resource (by id)'

    def queryset(self, request, queryset):
        value = self.value()
        if value is not None:
            return queryset.filter(resource_id=value)


class RoleNameFilter(InputFilter):
    parameter_name = 'role'
    title = 'Role'

    def queryset(self, request, queryset):
        value = self.value()
        if value is not None:
            return queryset.filter(roles__name__icontains=value)


#
# Inlines
#

class CollaboratorInline(admin.TabularInline):
    model = ContentCollaborator
    readonly_fields = ['created_at', 'updated_at']
    raw_id_fields = ['user', 'content']
    extra = 0


class AnnotationInline(admin.TabularInline):
    model = ContentAnnotation
    readonly_fields = ['created_at', 'updated_at', 'start_paragraph', 'end_paragraph', 'start_offset', 'end_offset', 'kind']
    fields = ['resource', ('global_start_offset', 'global_end_offset'), ('start_paragraph', 'end_paragraph'), ('start_offset', 'end_offset'), 'kind', 'content', 'created_at', 'updated_at']
    raw_id_fields = ['resource']
    extra = 0
    ordering = ['global_start_offset',  'global_end_offset']

    def formfield_for_dbfield(self, db_field, **kwargs):
        formfield = super().formfield_for_dbfield(db_field, **kwargs)
        if db_field.name == 'content':
            formfield.widget = forms.TextInput(attrs=formfield.widget.attrs)
        return formfield


class RolesUserInline(admin.TabularInline):
    model = RolesUser
    list_select_related = ['user', 'role']
    fields = ['user', 'role']
    raw_id_fields = ['user', 'role']
    extra = 0


#
# Admins
#


class NonLoggingAdmin(admin.ModelAdmin):
    """
    The LogEntry class tracks additions, changes, and deletions of objects
    done through the admin interface. It requires the Django app to be
    fully integrated with the AUTH_USER_MODEL... which we aren't yet. So,
    for now, disable logging.
    """
    def log_addition(self, request, object, message):
        pass

    def log_change(self, request, object, message):
        pass


## Casebooks<commit_msg>Missed this.<commit_after>from django import forms
from django.conf import settings
from django.contrib import admin
from django.contrib.auth.models import Group
from django.contrib.auth.models import User as BuiltInUser
from django.db.models import Q, Count
from django.urls import reverse
from django.utils.html import format_html

from .models import Case, CaseCourt, Default, User, Casebook, Section, \
    Resource, ContentCollaborator, ContentAnnotation, TextBlock, \
    Role, RolesUser


# remove builtin models
admin.site.unregister(Group)
admin.site.unregister(BuiltInUser)


#
# Filters
#

class InputFilter(admin.SimpleListFilter):
    """
    Text input filter, from:
    https://hakibenita.com/how-to-add-a-text-filter-to-django-admin
    """
    template = 'admin/input_filter.html'

    def lookups(self, request, model_admin):
        # Dummy, required to show the filter.
        return ((),)

    def choices(self, changelist):
        # Grab only the "all" option.
        all_choice = next(super().choices(changelist))
        all_choice['query_parts'] = (
            (k, v)
            for k, v in changelist.get_filters_params().items()
            if k != self.parameter_name
        )
        yield all_choice


class CasebookIdFilter(InputFilter):
    parameter_name = 'casebook'
    title = 'Casebook (by ID)'

    def queryset(self, request, queryset):
        value = self.value()
        if value is not None:
            return queryset.filter(casebook_id=value)


class CollaboratorNameFilter(InputFilter):
    parameter_name = 'collaborator-name'
    title = 'Collaborator (by name)'

    def queryset(self, request, queryset):
        value = self.value()
        if value is not None:
            users = User.objects.filter(Q(title__icontains=value) | Q(attribution__icontains=value))
            return queryset.filter(collaborators__in=users)


class CollaboratorIdFilter(InputFilter):
    parameter_name = 'collaborator-id'
    title = 'Collaborator (by id)'

    def queryset(self, request, queryset):
        value = self.value()
        if value is not None:
            users = User.objects.filter(id=value)
            return queryset.filter(collaborators__in=users)


class CourtIdFilter(InputFilter):
    parameter_name = 'court-id'
    title = 'Court (by id)'

    def queryset(self, request, queryset):
        value = self.value()
        if value is not None:
            return queryset.filter(case_court=value)


class ResourceIdFilter(InputFilter):
    parameter_name = 'resource-id'
    title = 'Resource (by id)'

    def queryset(self, request, queryset):
        value = self.value()
        if value is not None:
            return queryset.filter(resource_id=value)


class RoleNameFilter(InputFilter):
    parameter_name = 'role'
    title = 'Role'

    def queryset(self, request, queryset):
        value = self.value()
        if value is not None:
            return queryset.filter(roles__name__icontains=value)


#
# Inlines
#

class CollaboratorInline(admin.TabularInline):
    model = ContentCollaborator
    readonly_fields = ['created_at', 'updated_at']
    raw_id_fields = ['user', 'content']
    extra = 0


class AnnotationInline(admin.TabularInline):
    model = ContentAnnotation
    readonly_fields = ['created_at', 'updated_at', 'start_paragraph', 'end_paragraph', 'start_offset', 'end_offset', 'kind']
    fields = ['resource', ('global_start_offset', 'global_end_offset'), ('start_paragraph', 'end_paragraph'), ('start_offset', 'end_offset'), 'kind', 'content', 'created_at', 'updated_at']
    raw_id_fields = ['resource']
    extra = 0
    ordering = ['global_start_offset',  'global_end_offset']

    def formfield_for_dbfield(self, db_field, **kwargs):
        formfield = super().formfield_for_dbfield(db_field, **kwargs)
        if db_field.name == 'content':
            formfield.widget = forms.TextInput(attrs=formfield.widget.attrs)
        return formfield


class RolesUserInline(admin.TabularInline):
    model = RolesUser
    list_select_related = ['user', 'role']
    fields = ['user', 'role']
    raw_id_fields = ['user', 'role']
    extra = 0


#
# Admins
#


class NonLoggingAdmin(admin.ModelAdmin):
    """
    The LogEntry class tracks additions, changes, and deletions of objects
    done through the admin interface. It requires the Django app to be
    fully integrated with the AUTH_USER_MODEL... which we aren't yet. So,
    for now, disable logging.
    """
    def log_addition(self, request, object, message):
        pass

    def log_change(self, request, object, message):
        pass

    def log_deletion(self, request, object, object_repr):
        pass


## Casebooks
{'fraction_non_alphanumeric': 0.1079307005272786, 'fraction_numerical': 0.000645647261379533, 'mean_word_length': 3.2750689972401106, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '2058043', 'n_tokens_mistral': 2807, 'n_tokens_neox': 2740, 'n_words': 712}