<commit_before>from django.contrib import admin
from django.contrib.postgres.fields import JSONField
from django import forms

from .models import *
from .tasks import check_ip_status, check_bl
from .widgets import SplitJSONWidget


class IpAddressAdminForm(forms.ModelForm):
    class Meta:
        model = IpAddress
        fields = '__all__'

    def clean_address(self):
        address = self.cleaned_data['address']
        if not self.instance.pk:
            objects_list = IpAddress.objects.filter(address=address).exists()
        else:
            objects_list = IpAddress.objects.filter(address=address).exclude(id=self.instance.pk).exists()
        if objects_list:
            raise forms.ValidationError("An entry with same ip already exists.")
        return address


class IpAddressAdmin(admin.ModelAdmin):
    form = IpAddressAdminForm
    list_display = ("address", "hostname", "rdns", "status", "enabled", "blacklisted", "critical_blacklisted")
    search_fields = ["address", "hostname", "rdns"]
    list_filter = ("status", "enabled", "blacklisted", "critical_blacklisted")
    actions = ['update_ip_status', 'update_ip_blacklist']
    formfield_overrides = {
        JSONField: {'widget': SplitJSONWidget},
    }

    def update_ip_blacklist(self, request, queryset):
        for ip in queryset:
            check_bl.delay(ip)
        self.message_user(request, 'Blacklist update task has been placed, please allow it few minutes to update.')
    update_ip_blacklist.short_description = "Update ip status"

    def update_ip_status(self, request, queryset):
        for ip in queryset:
            check_ip_status.delay(ip)
        self.message_user(request, 'Selected IPs were updated.')
    update_ip_status.short_description = "Update ip blacklist"

    def get_queryset(self, request):
        """Limit Pages to those that belong to the request's user."""
        qs = super(IpAddressAdmin, self).get_queryset(request)
        if request.user.is_superuser:
            # It is mine, all mine. Just return everything.
            return qs

        # Now we just add an extra filter on the queryset and
        # we're done. Assumption: Page.owner is a foreignkey
        # to a User.
        return qs.filter(user=request.user)

    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == 'user':
            kwargs['initial'] = request.user.id
        return super(IpAddressAdmin, self).formfield_for_foreignkey(
            db_field, request, **kwargs
        )


class DnsBlacklistAdminForm(forms.ModelForm):
    class Meta:
        model = DnsBlacklist
        fields = '__all__'

    def clean_dns(self):
        dns = self.cleaned_data['dns']
        if not self.instance.pk:
            objects_list = DnsBlacklist.objects.filter(dns=dns).exists()
        else:
            objects_list = DnsBlacklist.objects.filter(dns=dns).exclude(pk=self.instance.pk).exists()
        if objects_list:
            raise forms.ValidationError("An entry with same dns already exists.")
        return dns
<commit_msg>testing<commit_after>from django.contrib import admin
from django.contrib.postgres.fields import JSONField
from django import forms

from .models import *
from .tasks import check_ip_status, check_bl
from .widgets import SplitJSONWidget


class IpAddressAdminForm(forms.ModelForm):
    class Meta:
        model = IpAddress
        fields = '__all__'

    def clean_address(self):
        address = self.cleaned_data['address']
        if not self.instance.pk:
            objects_list = IpAddress.objects.filter(address=address).exists()
        else:
            objects_list = IpAddress.objects.filter(address=address).exclude(id=self.instance.pk).exists()
        if objects_list:
            raise forms.ValidationError("An entry with same ip already exists.")
        return address


class IpAddressAdmin(admin.ModelAdmin):
    form = IpAddressAdminForm
    list_display = ("address", "hostname", "rdns", "status", "enabled", "blacklisted", "critical_blacklisted")
    search_fields = ["address", "hostname", "rdns"]
    list_filter = ("status", "enabled", "blacklisted", "critical_blacklisted")
    actions = ['update_ip_status', 'update_ip_blacklist']
    formfield_overrides = {
        JSONField: {'widget': SplitJSONWidget},
    }

    def update_ip_blacklist(self, request, queryset):
        for ip in queryset:
            check_bl.delay(ip)
        self.message_user(request, 'Blacklist update task has been placed, please allow it few minutes to update.')
    update_ip_blacklist.short_description = "Update ip status"

    def update_ip_status(self, request, queryset):
        for ip in queryset:
            check_ip_status.delay(ip)
        self.message_user(request, 'Selected IPs were updated.')
    update_ip_status.short_description = "Update ip blacklist"

    def get_queryset(self, request):
        """Limit Pages to those that belong to the request's user."""
        qs = super(IpAddressAdmin, self).get_queryset(request)
        if request.user.is_superuser:
            # It is mine, all mine. Just return everything.
            return qs

        # Now we just add an extra filter on the queryset and
        # we're done. Assumption: Page.owner is a foreignkey
        # to a User.
        return qs.filter(user=request.user)

    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == 'user':
            kwargs['initial'] = request.user.id
        return super(IpAddressAdmin, self).formfield_for_foreignkey(
            db_field, request, **kwargs
        )


class DnsBlacklistAdminForm(forms.ModelForm):
    class Meta:
        model = DnsBlacklist
        fields = '__all__'

    def clean_dns(self):
        dns = self.cleaned_data['dns']
        if self.instance.pk:
            if self.instance.dns == dns:
                return dns
            else:
                if DnsBlacklist.objects.filter(dns=dns).exists():
                    raise forms.ValidationError("An entry with same dns already exists.")
        else:
            if DnsBlacklist.objects.filter(dns=dns).exists():
                raise forms.ValidationError("An entry with same dns already exists.")
        return dns

{'fraction_non_alphanumeric': 0.09686199167467179, 'fraction_numerical': 0.0, 'mean_word_length': 2.8466748768472905, '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': '1647123', 'n_tokens_mistral': 1801, 'n_tokens_neox': 1753, 'n_words': 467}