<commit_before>from django.db.models import Q

from .views import RestrictedListView, RestrictedCreateView, \
    RestrictedUpdateView, RestrictedDetailView, RestrictedDeleteView
from ..models.scan_model import Scan
from ..models.scanner_model import Scanner
from ..models.userprofile_model import UserProfile


class ScannerList(RestrictedListView):
    """Displays list of scanners."""

    template_name = 'os2webscanner/scanners.html'
    context_object_name = 'scanner_list'

    def get_queryset(self):
        """Get queryset, don't include non-visible scanners."""
        qs = super().get_queryset()
        # Dismiss scans that are not visible
        qs = qs.filter(is_visible=True)
        return qs


class ScannerCreate(RestrictedCreateView):
    template_name = 'os2webscanner/scanner_form.html'

    def get_form(self, form_class=None):
        """Get the form for the view.

        Querysets used for choices in the 'domains' and 'regex_rules' fields
        will be limited by the user's organization unless the user is a
        superuser.
        """
        if form_class is None:
            form_class = self.get_form_class()

        form = super().get_form(form_class)
        form.fields['schedule'].required = False
        try:
            organization = self.request.user.profile.organization
            groups = self.request.user.profile.groups.all()
        except UserProfile.DoesNotExist:
            organization = None
            groups = None

        # Exclude recipients with no email address
        form.fields[
            'recipients'
        ].queryset = form.fields[
            'recipients'
        ].queryset.exclude(user__email="")

        if not self.request.user.is_superuser:
            for field_name in ['domains', 'regex_rules', 'recipients']:
                queryset = form.fields[field_name].queryset
                queryset = queryset.filter(organization=organization)
                if (
                        self.request.user.profile.is_group_admin or
                        field_name == 'recipients'
                ):
                    # Already filtered by organization, nothing more to do.
                    pass
                else:
                    queryset = queryset.filter(
                        Q(group__in=groups) | Q(group__isnull=True)
                    )
                form.fields[field_name].queryset = queryset

        return form


class ScannerUpdate(RestrictedUpdateView):
    """Update a scanner view."""
    template_name = 'os2webscanner/scanner_form.html'

    def get_form(self, form_class=None):
        """Get the form for the view.

        Querysets used for choices in the 'domains' and 'regex_rules' fields
        will be limited by the user's organiztion unless the user is a
        superuser.
        """
        if form_class is None:
            form_class = self.get_form_class()

        self.fields = self.get_form_fields()
        form = super().get_form(form_class)
        form.fields['schedule'].required = False

        scanner = self.get_object()

        # Exclude recipients with no email address
        form.fields[
            'recipients'
        ].queryset = form.fields[
            'recipients'
        ].queryset.exclude(user__email="")

        for field_name in ['domains', 'regex_rules', 'recipients']:
            queryset = form.fields[field_name].queryset
            queryset = queryset.filter(organization=scanner.organization)

            if scanner.organization.do_use_groups:
                # TODO: This is not very elegant!
                if field_name == 'recipients':
                    if scanner.group:
                        queryset = queryset.filter(
                            Q(groups__in=scanner.group) |
                            Q(groups__isnull=True)
                        )
                else:
                    queryset = queryset.filter(
                        Q(group=scanner.group) | Q(group__isnull=True)
                    )
            form.fields[field_name].queryset = queryset

        return form


class ScannerDelete(RestrictedDeleteView):
    """Delete a scanner view."""
    template_name = 'os2webscanner/scanner_confirm_delete.html'

    def get_form(self, form_class=None):
        """Adds special field password and decrypts password."""
        if form_class is None:<commit_msg>* Removed redundant code by introducing a scannerbase class.<commit_after>from django.db.models import Q

from .views import RestrictedListView, RestrictedCreateView, \
    RestrictedUpdateView, RestrictedDetailView, RestrictedDeleteView
from ..models.scan_model import Scan
from ..models.scanner_model import Scanner
from ..models.userprofile_model import UserProfile


class ScannerList(RestrictedListView):
    """Displays list of scanners."""

    template_name = 'os2webscanner/scanners.html'
    context_object_name = 'scanner_list'

    def get_queryset(self):
        """Get queryset, don't include non-visible scanners."""
        qs = super().get_queryset()
        # Dismiss scans that are not visible
        qs = qs.filter(is_visible=True)
        return qs


class ScannerBase():
    template_name = 'os2webscanner/scanner_form.html'

    def get_form(self, form_class=None):

        if form_class is None:
            form_class = self.get_form_class()

        form = super().get_form(form_class)
        form.fields['schedule'].required = False

        # Exclude recipients with no email address
        form.fields[
            'recipients'
        ].queryset = form.fields[
            'recipients'
        ].queryset.exclude(user__email="")

        return form

    def get_scanner_object(self):
        return self.get_object()


class ScannerCreate(ScannerBase, RestrictedCreateView):

    def get_form(self, form_class=None):

        form = super().get_form(form_class)
        try:
            organization = self.request.user.profile.organization
            groups = self.request.user.profile.groups.all()
        except UserProfile.DoesNotExist:
            organization = None
            groups = None

        if not self.request.user.is_superuser:
            for field_name in ['domains', 'regex_rules', 'recipients']:
                queryset = form.fields[field_name].queryset
                queryset = queryset.filter(organization=organization)
                if (
                        self.request.user.profile.is_group_admin or
                        field_name == 'recipients'
                ):
                    # Already filtered by organization, nothing more to do.
                    pass
                else:
                    queryset = queryset.filter(
                        Q(group__in=groups) | Q(group__isnull=True)
                    )
                form.fields[field_name].queryset = queryset

        return form


class ScannerUpdate(ScannerBase, RestrictedUpdateView):
    """View for editing an existing scannerjob."""

    def get_form(self, form_class=None):
        """Get the form for the view.

        Querysets used for choices in the 'domains' and 'regex_rules' fields
        will be limited by the user's organiztion unless the user is a
        superuser.
        """
        form = super().get_form(form_class)

        scanner = self.get_scanner_object()
        
        for field_name in ['domains', 'regex_rules', 'recipients']:
            queryset = form.fields[field_name].queryset
            queryset = queryset.filter(organization=scanner.organization)

            if scanner.organization.do_use_groups:
                # TODO: This is not very elegant!
                if field_name == 'recipients':
                    if scanner.group:
                        queryset = queryset.filter(
                            Q(groups__in=scanner.group) |
                            Q(groups__isnull=True)
                        )
                else:
                    queryset = queryset.filter(
                        Q(group=scanner.group) | Q(group__isnull=True)
                    )
            form.fields[field_name].queryset = queryset

        return form


class ScannerDelete(RestrictedDeleteView):
    """Delete a scanner view."""
    template_name = 'os2webscanner/scanner_confirm_delete.html'

    def get_form(self, form_class=None):
        """Adds special field password and decrypts password."""
        if form_class is None:
{'fraction_non_alphanumeric': 0.08508365966536134, 'fraction_numerical': 0.0008306633440132907, 'mean_word_length': 1.9950248756218905, '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': '1214645', 'n_tokens_mistral': 2348, 'n_tokens_neox': 2172, 'n_words': 537}