<commit_before>from django.conf import settings
from django.contrib import admin
from .models import *
import logging

from uw_saml.utils import is_member_of_group


admin_group = settings.ADMIN_AUTHZ_GROUP

logger = logging.getLogger('filter')


class SAMLAdminSite(admin.AdminSite):
    def has_permission(self, request):
        return (
            is_member_of_group(request, admin_group) and request.user.is_active
        )

    def __init__(self, *args, **kwargs):
        super(SAMLAdminSite, self).__init__(*args, **kwargs)
        self._registry.update(admin.site._registry)


admin_site = SAMLAdminSite(name="SAMLAdmin")


class PlatformInline(admin.TabularInline):
    model = Platform
    extra = 0


@admin.register(Platform, site=admin_site)
class PlatformAdmin(admin.ModelAdmin):
    model = Platform


class SponsorInLine(admin.TabularInline):
    model = Sponsor
    extra = 0


@admin.register(Sponsor, site=admin_site)
class SponsorAdmin(admin.ModelAdmin):
    model = Sponsor
    list_display = (
        '__str__',
        'netid',
        'department',
    )


class ManagerInLine(admin.TabularInline):
    model = Manager
    extra = 0


@admin.register(Manager, site=admin_site)
class ManagerAdmin(admin.ModelAdmin):
    model = Manager
    list_display = (
        '__str__',
        'netid',
        'email',
    )
    fields = ['first_name', 'last_name', 'netid', 'email']


# filters agreements into agreed, denied, or pending
class AgreementFilter(admin.SimpleListFilter):
    title = "Agreement"
    parameter_name = 'status'

    def lookups(self, request, model_admin):
        return [
            ('agreed', 'Agreed'),
            ('pending', 'Pending'),
            ('denied', 'Denied'),
            ('removed', 'Removed'),
        ]

    def queryset(self, request, queryset):
        # make list of app names that agreed (in status)
        agreed_apps = []
        for app in App.objects.all():
            if app.status().startswith('A'):
                agreed_apps.append(app.id)

        # make list of app names that denied (in status)
        denied_apps = []
        for app in App.objects.all():
            if app.status().startswith('D'):
                denied_apps.append(app.id)

        # make list of app names that got removed from platform (in status)
        removed_apps = []
        for app in App.objects.all():
            if app.status().startswith('R'):
                removed_apps.append(app.id)

        if self.value() == 'agreed':
            return queryset.filter(id__in=agreed_apps)

        if self.value() == 'denied':
            return queryset.filter(id__in=denied_apps)

        if self.value() == 'removed':
            return queryset.filter(id__in=removed_apps)

        if self.value() == 'pending':
            return queryset.filter(agreement=None)


class AgreementInLine(admin.TabularInline):
    model = Agreement
    extra = 0
    list_display = [
        '__str__',
        'status',
        'agree_time'
    ]
    readonly_fields = ['status', 'agree_time']
    ordering = ['-agree_time', ]

    def has_add_permission(self, request, obj=None):
        return False


class AddAgreementInLine(admin.TabularInline):
    model = Agreement
    extra = 0
    can_delete = False
    fields = ['status']

    def has_change_permission(self, request, obj=None):
        return False


@admin.register(Agreement, site=admin_site)
class AgreementAdmin(admin.ModelAdmin):
    model = Agreement
    date_hierarchy = 'agree_time'
    list_display = [
        '__str__',
        'status',
        'agree_time'
    ]


class AppInLine(admin.TabularInline):
    model = App
    extra = 0


@admin.register(App, site=admin_site)
class AppAdmin(admin.ModelAdmin):
    inlines = [AgreementInLine, AddAgreementInLine]
    model = App

    list_filter = (
        'platform',
        AgreementFilter
    )
    list_display = (
        '__str__',
        'app_manager',
        'manager_contact',
        'app_sponsor',
        'sponsor_contact',<commit_msg>allow agreements to be filtered by app<commit_after>from django.conf import settings
from django.contrib import admin
from .models import *
import logging

from uw_saml.utils import is_member_of_group


admin_group = settings.ADMIN_AUTHZ_GROUP

logger = logging.getLogger('filter')


class SAMLAdminSite(admin.AdminSite):
    def has_permission(self, request):
        return (
            is_member_of_group(request, admin_group) and request.user.is_active
        )

    def __init__(self, *args, **kwargs):
        super(SAMLAdminSite, self).__init__(*args, **kwargs)
        self._registry.update(admin.site._registry)


admin_site = SAMLAdminSite(name="SAMLAdmin")


class PlatformInline(admin.TabularInline):
    model = Platform
    extra = 0


@admin.register(Platform, site=admin_site)
class PlatformAdmin(admin.ModelAdmin):
    model = Platform


class SponsorInLine(admin.TabularInline):
    model = Sponsor
    extra = 0


@admin.register(Sponsor, site=admin_site)
class SponsorAdmin(admin.ModelAdmin):
    model = Sponsor
    list_display = (
        '__str__',
        'netid',
        'department',
    )


class ManagerInLine(admin.TabularInline):
    model = Manager
    extra = 0


@admin.register(Manager, site=admin_site)
class ManagerAdmin(admin.ModelAdmin):
    model = Manager
    list_display = (
        '__str__',
        'netid',
        'email',
    )
    fields = ['first_name', 'last_name', 'netid', 'email']


# filters agreements into agreed, denied, or pending
class AgreementFilter(admin.SimpleListFilter):
    title = "Agreement"
    parameter_name = 'status'

    def lookups(self, request, model_admin):
        return [
            ('agreed', 'Agreed'),
            ('pending', 'Pending'),
            ('denied', 'Denied'),
            ('removed', 'Removed'),
        ]

    def queryset(self, request, queryset):
        # make list of app names that agreed (in status)
        agreed_apps = []
        for app in App.objects.all():
            if app.status().startswith('A'):
                agreed_apps.append(app.id)

        # make list of app names that denied (in status)
        denied_apps = []
        for app in App.objects.all():
            if app.status().startswith('D'):
                denied_apps.append(app.id)

        # make list of app names that got removed from platform (in status)
        removed_apps = []
        for app in App.objects.all():
            if app.status().startswith('R'):
                removed_apps.append(app.id)

        if self.value() == 'agreed':
            return queryset.filter(id__in=agreed_apps)

        if self.value() == 'denied':
            return queryset.filter(id__in=denied_apps)

        if self.value() == 'removed':
            return queryset.filter(id__in=removed_apps)

        if self.value() == 'pending':
            return queryset.filter(agreement=None)


class AgreementInLine(admin.TabularInline):
    model = Agreement
    extra = 0
    list_display = [
        '__str__',
        'status',
        'agree_time'
    ]
    readonly_fields = ['status', 'agree_time']
    ordering = ['-agree_time', ]

    def has_add_permission(self, request, obj=None):
        return False


class AddAgreementInLine(admin.TabularInline):
    model = Agreement
    extra = 0
    can_delete = False
    fields = ['status']

    def has_change_permission(self, request, obj=None):
        return False


@admin.register(Agreement, site=admin_site)
class AgreementAdmin(admin.ModelAdmin):
    model = Agreement
    date_hierarchy = 'agree_time'
    list_display = [
        '__str__',
        'status',
        'agree_time'
    ]
    list_filter = ['app']


class AppInLine(admin.TabularInline):
    model = App
    extra = 0


@admin.register(App, site=admin_site)
class AppAdmin(admin.ModelAdmin):
    inlines = [AgreementInLine, AddAgreementInLine]
    model = App

    list_filter = (
        'platform',
        AgreementFilter
    )
    list_display = (
        '__str__',
        'app_manager',
        'manager_contact',
        'app_sponsor',
        'sponsor_contact',
{'fraction_non_alphanumeric': 0.11218067382450944, 'fraction_numerical': 0.001480932987782303, 'mean_word_length': 2.601777777777778, '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': '292409', 'n_tokens_mistral': 2516, 'n_tokens_neox': 2445, 'n_words': 541}