<commit_before>"""Admin settings for the static media management application."""

import os
from functools import partial

from django.contrib import admin
from django.contrib.admin.views.main import IS_POPUP_VAR
from django.shortcuts import render
from django.template.defaultfilters import filesizeformat
from django.utils.text import truncate_words

import optimizations
from optimizations.assetcache import default_asset_cache

from cms import permalinks, externals
from cms.apps.media.models import Label, File


class LabelAdmin(admin.ModelAdmin):
    
    """Admin settings for Label models."""
    
    list_display = ("name",)
    
    search_fields = ("name",)
    
    
admin.site.register(Label, LabelAdmin)
    
       
# Different types of file.
AUDIO_FILE_ICON = "media/img/audio-x-generic.png"
DOCUMENT_FILE_ICON = "media/img/x-office-document.png"
SPREADSHEET_FILE_ICON = "media/img/x-office-spreadsheet.png"
TEXT_FILE_ICON = "media/img/text-x-generic.png"
IMAGE_FILE_ICON = "media/img/image-x-generic.png"
MOVIE_FILE_ICON = "media/img/video-x-generic.png"
UNKNOWN_FILE_ICON = "media/img/text-x-generic-template.png"

# Different types of recognised file extensions.
FILE_ICONS = {
    "mp3": AUDIO_FILE_ICON,
    "m4a": AUDIO_FILE_ICON,
    "wav": AUDIO_FILE_ICON,
    "doc": DOCUMENT_FILE_ICON,
    "odt": DOCUMENT_FILE_ICON,
    "pdf": DOCUMENT_FILE_ICON,
    "xls": SPREADSHEET_FILE_ICON,
    "txt": TEXT_FILE_ICON,
    "png": IMAGE_FILE_ICON,
    "gif": IMAGE_FILE_ICON,
    "jpg": IMAGE_FILE_ICON,
    "jpeg": IMAGE_FILE_ICON,
    "swf": MOVIE_FILE_ICON,
    "flv": MOVIE_FILE_ICON,
    "mp4": MOVIE_FILE_ICON,
    "mov": MOVIE_FILE_ICON,
    "wmv": MOVIE_FILE_ICON,
}
    
    
class FileAdminBase(admin.ModelAdmin):
    
    """Admin settings for File models."""
    
    fieldsets = (
        (None, {
            "fields": ("title", "file",),
        },),
        ("Media management", {
            "fields": ("labels",),
            "classes": ("collapse",),
        },),
    )
    
    list_filter = ("labels",)
    
    search_fields = ("title",)
    
    list_display = ("get_preview", "get_title", "get_size", "get_date_modified")

    change_list_template = "admin/media/file/change_list.html"
    
    filter_horizontal = ("labels",)
    
    # Customizations.
    
    def lookup_allowed(self, lookup, *args, **kwargs):
        """Allows the file iregex lookup needed by TinyMCE integration."""
        if lookup == "file__iregex":
            return True
        return super(FileAdmin, self).lookup_allowed(lookup, *args, **kwargs)
    
    # Custom actions.
    
    def add_label_action(self, request, queryset, label):
        """Adds the label on the given queryset."""
        for file in queryset:
            file.labels.add(label)
            
    def remove_label_action(self, request, queryset, label):
        """Removes the label on the given queryset."""
        for file in queryset:
            file.labels.remove(label)
    
    def get_actions(self, request):
        """Generates the actions for assigning categories."""
        if IS_POPUP_VAR in request.GET:
            return []
        opts = self.model._meta
        verbose_name_plural = opts.verbose_name_plural
        actions = super(FileAdmin, self).get_actions(request)<commit_msg>Actually fixing infinite recursion in file admin.<commit_after>"""Admin settings for the static media management application."""

import os
from functools import partial

from django.contrib import admin
from django.contrib.admin.views.main import IS_POPUP_VAR
from django.shortcuts import render
from django.template.defaultfilters import filesizeformat
from django.utils.text import truncate_words

import optimizations
from optimizations.assetcache import default_asset_cache

from cms import permalinks, externals
from cms.apps.media.models import Label, File


class LabelAdmin(admin.ModelAdmin):
    
    """Admin settings for Label models."""
    
    list_display = ("name",)
    
    search_fields = ("name",)
    
    
admin.site.register(Label, LabelAdmin)
    
       
# Different types of file.
AUDIO_FILE_ICON = "media/img/audio-x-generic.png"
DOCUMENT_FILE_ICON = "media/img/x-office-document.png"
SPREADSHEET_FILE_ICON = "media/img/x-office-spreadsheet.png"
TEXT_FILE_ICON = "media/img/text-x-generic.png"
IMAGE_FILE_ICON = "media/img/image-x-generic.png"
MOVIE_FILE_ICON = "media/img/video-x-generic.png"
UNKNOWN_FILE_ICON = "media/img/text-x-generic-template.png"

# Different types of recognised file extensions.
FILE_ICONS = {
    "mp3": AUDIO_FILE_ICON,
    "m4a": AUDIO_FILE_ICON,
    "wav": AUDIO_FILE_ICON,
    "doc": DOCUMENT_FILE_ICON,
    "odt": DOCUMENT_FILE_ICON,
    "pdf": DOCUMENT_FILE_ICON,
    "xls": SPREADSHEET_FILE_ICON,
    "txt": TEXT_FILE_ICON,
    "png": IMAGE_FILE_ICON,
    "gif": IMAGE_FILE_ICON,
    "jpg": IMAGE_FILE_ICON,
    "jpeg": IMAGE_FILE_ICON,
    "swf": MOVIE_FILE_ICON,
    "flv": MOVIE_FILE_ICON,
    "mp4": MOVIE_FILE_ICON,
    "mov": MOVIE_FILE_ICON,
    "wmv": MOVIE_FILE_ICON,
}
    
    
class FileAdminBase(admin.ModelAdmin):
    
    """Admin settings for File models."""
    
    fieldsets = (
        (None, {
            "fields": ("title", "file",),
        },),
        ("Media management", {
            "fields": ("labels",),
            "classes": ("collapse",),
        },),
    )
    
    list_filter = ("labels",)
    
    search_fields = ("title",)
    
    list_display = ("get_preview", "get_title", "get_size", "get_date_modified")

    change_list_template = "admin/media/file/change_list.html"
    
    filter_horizontal = ("labels",)
    
    # Customizations.
    
    def lookup_allowed(self, lookup, *args, **kwargs):
        """Allows the file iregex lookup needed by TinyMCE integration."""
        if lookup == "file__iregex":
            return True
        return super(FileAdminBase, self).lookup_allowed(lookup, *args, **kwargs)
    
    # Custom actions.
    
    def add_label_action(self, request, queryset, label):
        """Adds the label on the given queryset."""
        for file in queryset:
            file.labels.add(label)
            
    def remove_label_action(self, request, queryset, label):
        """Removes the label on the given queryset."""
        for file in queryset:
            file.labels.remove(label)
    
    def get_actions(self, request):
        """Generates the actions for assigning categories."""
        if IS_POPUP_VAR in request.GET:
            return []
        opts = self.model._meta
        verbose_name_plural = opts.verbose_name_plural
        actions = super(FileAdminBase, self).get_actions(request)
{'fraction_non_alphanumeric': 0.11581325301204819, 'fraction_numerical': 0.0009036144578313253, 'mean_word_length': 3.039537712895377, 'pattern_counts': {'":': 42, '<': 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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4093011', 'n_tokens_mistral': 2185, 'n_tokens_neox': 2159, 'n_words': 475}