<commit_before>import re

from django import forms
from django.contrib import admin

import requests

from .models import ShortURL
from .models import ShortURLAlias


class ShortURLAliasAdminForm(forms.ModelForm):
    def clean_alias(self):
        """
        Validate characters for the provided alias.
        """
        alias = self.cleaned_data.get('alias')
        if bool(re.compile(r'[^a-zA-Z0-9-]').search(alias)):
            raise forms.ValidationError("Alias contains invalid characters")
        return alias


class ShortURLAliasInline(admin.StackedInline):
    model = ShortURLAlias
    form = ShortURLAliasAdminForm


class ShortURLAdminForm(forms.ModelForm):
    def clean_long_url(self):
        """
        Validate connectivity to the provided target URL.
        """
        url = self.cleaned_data.get('long_url')
        try:
            r = requests.head(url, allow_redirects=True, timeout=3.0)
        except requests.exceptions.ConnectionError:
            raise forms.ValidationError("Error connecting to URL")
        except requests.exceptions.SSLError:
            raise forms.ValidationError("Invalid SSL certificate")
        except requests.exceptions.Timeout:
            raise forms.ValidationError("Timeout connecting to URL")

        try:
            r.raise_for_status()
        except requests.exceptions.HTTPError:
            raise forms.ValidationError("Invalid status returned (%d)" % r.status_code)

        return r.url


class ShortURLAdmin(admin.ModelAdmin):
    form = ShortURLAdminForm
    inlines = [ShortURLAliasInline]
    list_display = ('long_url', 'short_url', 'hits', 'last_used', 'creator', 'campaign', 'medium')
    list_filter = ('creator__username', 'campaign', 'medium')
    ordering = ['-last_used']
    readonly_fields = ('created', 'short_url', 'qr_code', 'hits', 'last_used')
    search_fields = ['long_url', 'campaign', 'shorturlalias__alias']

    fieldsets = ((None, {'fields': ('long_url', 'short_url')}),
                 ('Google', {'fields': ('campaign', 'medium', 'content')}),
                 ('Additional Info', {'fields': ('description', 'qr_code')}),
                 ('Short URL Usage', {'classes': ('collapse grp-collapse grp-closed'),
                                      'fields': ('hits', 'created', 'last_used')}))

    def has_change_permission(self, request, obj=None):
        """
        Users should only be able to modify ``ShortURL``s they own,
        except for superusers.
        """
        has_class_permission = super(ShortURLAdmin, self).has_change_permission(request, obj)
        if not has_class_permission:
            return False
        if obj is not None and not request.user.is_superuser and request.user.id != obj.creator.id:
            return False
        return True

    def has_delete_permission(self, request, obj=None):
        """
        Users should only be able to delete ``ShortURL``s they own,
        except for superusers.
        """
        has_class_permission = super(ShortURLAdmin, self).has_delete_permission(request, obj)
        if not has_class_permission:
            return False
        if obj is not None and not request.user.is_superuser and request.user.id != obj.creator.id:
            return False
        return True

    def queryset(self, request):
        """
        Superusers can view all ``ShortURL``s. Non-superusers should
        only be able to view their own.
        """<commit_msg>Modify available filters for superusers<commit_after>import re

from django import forms
from django.contrib import admin

import requests

from .models import ShortURL
from .models import ShortURLAlias


class ShortURLAliasAdminForm(forms.ModelForm):
    def clean_alias(self):
        """
        Validate characters for the provided alias.
        """
        alias = self.cleaned_data.get('alias')
        if bool(re.compile(r'[^a-zA-Z0-9-]').search(alias)):
            raise forms.ValidationError("Alias contains invalid characters")
        return alias


class ShortURLAliasInline(admin.StackedInline):
    model = ShortURLAlias
    form = ShortURLAliasAdminForm


class ShortURLAdminForm(forms.ModelForm):
    def clean_long_url(self):
        """
        Validate connectivity to the provided target URL.
        """
        url = self.cleaned_data.get('long_url')
        try:
            r = requests.head(url, allow_redirects=True, timeout=3.0)
        except requests.exceptions.ConnectionError:
            raise forms.ValidationError("Error connecting to URL")
        except requests.exceptions.SSLError:
            raise forms.ValidationError("Invalid SSL certificate")
        except requests.exceptions.Timeout:
            raise forms.ValidationError("Timeout connecting to URL")

        try:
            r.raise_for_status()
        except requests.exceptions.HTTPError:
            raise forms.ValidationError("Invalid status returned (%d)" % r.status_code)

        return r.url


class ShortURLAdmin(admin.ModelAdmin):
    form = ShortURLAdminForm
    inlines = [ShortURLAliasInline]
    list_display = ('long_url', 'short_url', 'hits', 'last_used', 'creator', 'campaign', 'medium')
    list_filter = ('campaign', 'medium')
    _list_filter = list_filter
    ordering = ['-last_used']
    readonly_fields = ('created', 'short_url', 'qr_code', 'hits', 'last_used')
    search_fields = ['long_url', 'campaign', 'shorturlalias__alias']

    fieldsets = ((None, {'fields': ('long_url', 'short_url')}),
                 ('Google', {'fields': ('campaign', 'medium', 'content')}),
                 ('Additional Info', {'fields': ('description', 'qr_code')}),
                 ('Short URL Usage', {'classes': ('collapse grp-collapse grp-closed'),
                                      'fields': ('hits', 'created', 'last_used')}))

    def changelist_view(self, request, extra_context=None):
        """
        Inject additional filtering options for superusers.
        """
        if request.user.is_superuser:
            self.list_filter = self._list_filter + ('creator__username',)
        else:
            self.list_filter = self._list_filter
        return super(ShortURLAdmin, self).changelist_view(request, extra_context=extra_context)

    def has_change_permission(self, request, obj=None):
        """
        Users should only be able to modify ``ShortURL``s they own,
        except for superusers.
        """
        has_class_permission = super(ShortURLAdmin, self).has_change_permission(request, obj)
        if not has_class_permission:
            return False
        if obj is not None and not request.user.is_superuser and request.user.id != obj.creator.id:
            return False
        return True

    def has_delete_permission(self, request, obj=None):
        """
        Users should only be able to delete ``ShortURL``s they own,
        except for superusers.
        """
        has_class_permission = super(ShortURLAdmin, self).has_delete_permission(request, obj)
        if not has_class_permission:
            return False
        if obj is not None and not request.user.is_superuser and request.user.id != obj.creator.id:
            return False
        return True

    def queryset(self, request):
        """
        Superusers can view all ``ShortURL``s. Non-superusers should
        only be able to view their own.
        """
{'fraction_non_alphanumeric': 0.10891494002181025, 'fraction_numerical': 0.0010905125408942203, 'mean_word_length': 2.692501258178158, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '866937', 'n_tokens_mistral': 2000, 'n_tokens_neox': 1996, 'n_words': 531}