<commit_before># Copyright 2016 Red Hat, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import os
import ast
import csv
import json
import shutil
from datetime import datetime
from pathlib import Path
import threading

# django
from django.contrib import messages
from django.contrib.messages.views import SuccessMessageMixin
from django.core.exceptions import PermissionDenied
from django.forms.utils import ErrorList
from django.http import (
    HttpResponse, HttpResponseRedirect, JsonResponse, Http404
)
from django.shortcuts import render
from django.template import Context, Template
from django.views.generic import (
    TemplateView, ListView, FormView, DetailView
)
from django.views.generic.edit import (CreateView, UpdateView, DeleteView)
from django.urls import reverse, reverse_lazy

# dashboard

from dashboard.constants import (
    TS_JOB_TYPES, TRANSPLATFORM_ENGINES, RELSTREAM_SLUGS,
    WEBLATE_SLUGS, TRANSIFEX_SLUGS, TS_CI_JOBS, PIPELINE_CONFIG_EVENTS,
    JOB_MULTIPLE_BRANCHES_VAR, TP_BRANCH_CALLING_NAME, SYS_EMAIL_ADDR
)
from dashboard.forms import (
    NewPackageForm, UpdatePackageForm, NewReleaseBranchForm, NewGraphRuleForm,
    NewLanguageForm, UpdateLanguageForm, LanguageSetForm, PackagePipelineForm,
    NewTransPlatformForm, UpdateTransPlatformForm, UpdateGraphRuleForm,
    CreateCIPipelineForm
)
from dashboard.managers.inventory import (
    InventoryManager, ReleaseBranchManager, SyncStatsManager
)
from dashboard.managers.packages import PackagesManager
from dashboard.managers.pipelines import (
    CIPipelineManager, PipelineConfigManager
)
from dashboard.managers.jobs import (
    YMLBasedJobManager, JobsLogManager, TransplatformSyncManager,
    ReleaseScheduleSyncManager, BuildTagsSyncManager, JobTemplateManager
)
from dashboard.managers.graphs import (
    GraphManager, ReportsManager, GeoLocationManager
)
from dashboard.models import (
    Job, Language, LanguageSet, Platform, Visitor, Package,
    GraphRule, SyncStats, CacheBuildDetails, CIPipeline, PipelineConfig
)


class ManagersMixin(object):
    """Managers Mixin"""
    inventory_manager = InventoryManager()
    packages_manager = PackagesManager()
    jobs_log_manager = JobsLogManager()
    jobs_template_manager = JobTemplateManager()
    release_branch_manager = ReleaseBranchManager()
    graph_manager = GraphManager()
    reports_manager = ReportsManager()
    geo_location_manager = GeoLocationManager()
    ci_pipeline_manager = CIPipelineManager()
    pipeline_config_manager = PipelineConfigManager()

    def get_summary(self):
        """Application Inventory Stats"""
        locales_set = self.inventory_manager.get_locales_set()
        summary = {}
        summary['locales_len'] = len(locales_set[0]) \
            if isinstance(locales_set, tuple) and len(locales_set) > 0 else 0
        platforms = self.inventory_manager.get_transplatform_slug_url()
        summary['platforms_len'] = len(platforms) if platforms else 0
        # relstreams = self.inventory_manager.get_relstream_slug_name()
        # summary['products_len'] = len(relstreams) if relstreams else 0
        # relbranches = self.release_branch_manager.get_release_branches()
        # summary['releases_len'] = relbranches.count() if relbranches else 0
        summary['packages_len'] = self.packages_manager.count_packages()
        jobs_templates_count = self.jobs_template_manager.get_job_templates().count()
        summary['jobs_templates_len'] = jobs_templates_count
        # coverage = self.graph_manager.get_graph_rules(only_active=True)
        # summary['graph_rules_len'] = coverage.count() if coverage else 0
        return summary

    @staticmethod
    def log_visitor(http_meta):
        """log visitors"""
        x_forwarded_for = http_meta.get('HTTP_X_FORWARDED_FOR')
        ip = x_forwarded_for.split(',')[-1].strip() \
            if x_forwarded_for else http_meta.get('REMOTE_ADDR')
        http_params = {}
        match_params = {
            'visitor_ip': ip,
            'visitor_user_agent': http_meta.get('HTTP_USER_AGENT')
        }
        http_params.update(match_params)
        http_params['visitor_accept'] = http_meta.get('HTTP_ACCEPT')
        http_params['visitor_encoding'] = http_meta.get('HTTP_ACCEPT_ENCODING')
        http_params['visitor_language'] = http_meta.get('HTTP_ACCEPT_LANGUAGE')
        http_params['visitor_host'] = http_meta.get('REMOTE_HOST')
        try:
            Visitor.objects.update_or_create(
                **match_params, defaults=http_params
            )
        except Exception as e:
            pass


class TranStatusPackageView(ManagersMixin, TemplateView):
    """Translation Status Package View"""
    template_name = "packages/package_view.html"

    def get(self, request, *args, **kwargs):
        response = super(TranStatusPackageView, self).get(
            request, *args, **kwargs)
        if not self.packages_manager.is_package_exist(
                kwargs.get('package_name', '')):
            raise Http404("Package does not exist.")
        return response

    def get_context_data(self, **kwargs):
        """Build the Context Data"""
        context = super(TranStatusPackageView, self).get_context_data(**kwargs)
        packages = self.packages_manager.get_package_name_tuple()
        langs = self.inventory_manager.get_locale_lang_tuple()
        if packages:
            context['packages'] = packages
        if langs:
            context['languages'] = sorted(langs, key=lambda x: x[1])
        return context


class TranStatusReleasesView(ManagersMixin, TemplateView):
    """
    Translation Status Releases View
    This view is for current landing page.
    """
    template_name = "releases/release_list.html"

    def get(self, request, *args, **kwargs):
        http_meta = self.request.META.copy()
        self.log_visitor(http_meta)
        return super(TranStatusReleasesView, self).get(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        """Build the Context Data"""
        context = super(TranStatusReleasesView, self).get_context_data(**kwargs)
        relbranches = self.release_branch_manager.get_relbranch_name_slug_tuple()
        context['releases'] = relbranches
        products = self.inventory_manager.get_release_streams()
        if self.request.tenant in RELSTREAM_SLUGS:
            products = products.filter(**{'product_slug': self.request.tenant})
        context['relstreams'] = products
        product_releases = self.release_branch_manager.get_branches_of_relstreams(products)
        p_releases_dict = {}
        if product_releases is not None and isinstance(product_releases, dict):
            for product, releases in product_releases.items():
                p_releases_dict[product] = len(releases) or 0
        context['p_releases_dict'] = p_releases_dict if p_releases_dict \
            else {p.product_slug: 0 for p in products}
        context.update(self.get_summary())
        return context


class TranStatusReleaseView(ManagersMixin, TemplateView):
    """Translation Status Release View"""
    template_name = "releases/release_view.html"

    def get_context_data(self, **kwargs):
        context = super(TranStatusReleaseView, self).get_context_data(**kwargs)
        if not self.release_branch_manager.is_relbranch_exist(
                kwargs.get('release_branch', '')):
            raise Http404("Release does not exist.")
        langs = self.inventory_manager.get_locale_lang_tuple()
        if langs:
            context['languages'] = sorted(langs, key=lambda x: x[1])
        relbranches = self.release_branch_manager.get_relbranch_name_slug_tuple()
        context['releases'] = relbranches
        release_stream = self.release_branch_manager.get_release_branches(
            relbranch=kwargs.get('release_branch'), fields=['product_slug']).get()
        if release_stream:
            context['release_stream'] = release_stream.product_slug.product_slug
        context['release_branch'] = kwargs.get('release_branch')
        return context


class TransCoverageView(ManagersMixin, TemplateView):
    """Translation Coverage View"""
    template_name = "coverage/coverage_rule_view.html"

    def get(self, request, *args, **kwargs):
        rule = self.graph_manager.get_graph_rules(
            graph_rule=kwargs.get('coverage_rule', '')
        )
        if not rule:
            raise Http404("Coverage rule does not exist.")
        elif not rule.get().rule_visibility_public and \
                not request.user.is_authenticated:
            raise PermissionDenied
        return super(TransCoverageView, self).get(<commit_msg>Refactor unnecessary `else` / `elif` when `if` block has a `raise` statement<commit_after># Copyright 2016 Red Hat, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import os
import ast
import csv
import json
import shutil
from datetime import datetime
from pathlib import Path
import threading

# django
from django.contrib import messages
from django.contrib.messages.views import SuccessMessageMixin
from django.core.exceptions import PermissionDenied
from django.forms.utils import ErrorList
from django.http import (
    HttpResponse, HttpResponseRedirect, JsonResponse, Http404
)
from django.shortcuts import render
from django.template import Context, Template
from django.views.generic import (
    TemplateView, ListView, FormView, DetailView
)
from django.views.generic.edit import (CreateView, UpdateView, DeleteView)
from django.urls import reverse, reverse_lazy

# dashboard

from dashboard.constants import (
    TS_JOB_TYPES, TRANSPLATFORM_ENGINES, RELSTREAM_SLUGS,
    WEBLATE_SLUGS, TRANSIFEX_SLUGS, TS_CI_JOBS, PIPELINE_CONFIG_EVENTS,
    JOB_MULTIPLE_BRANCHES_VAR, TP_BRANCH_CALLING_NAME, SYS_EMAIL_ADDR
)
from dashboard.forms import (
    NewPackageForm, UpdatePackageForm, NewReleaseBranchForm, NewGraphRuleForm,
    NewLanguageForm, UpdateLanguageForm, LanguageSetForm, PackagePipelineForm,
    NewTransPlatformForm, UpdateTransPlatformForm, UpdateGraphRuleForm,
    CreateCIPipelineForm
)
from dashboard.managers.inventory import (
    InventoryManager, ReleaseBranchManager, SyncStatsManager
)
from dashboard.managers.packages import PackagesManager
from dashboard.managers.pipelines import (
    CIPipelineManager, PipelineConfigManager
)
from dashboard.managers.jobs import (
    YMLBasedJobManager, JobsLogManager, TransplatformSyncManager,
    ReleaseScheduleSyncManager, BuildTagsSyncManager, JobTemplateManager
)
from dashboard.managers.graphs import (
    GraphManager, ReportsManager, GeoLocationManager
)
from dashboard.models import (
    Job, Language, LanguageSet, Platform, Visitor, Package,
    GraphRule, SyncStats, CacheBuildDetails, CIPipeline, PipelineConfig
)


class ManagersMixin(object):
    """Managers Mixin"""
    inventory_manager = InventoryManager()
    packages_manager = PackagesManager()
    jobs_log_manager = JobsLogManager()
    jobs_template_manager = JobTemplateManager()
    release_branch_manager = ReleaseBranchManager()
    graph_manager = GraphManager()
    reports_manager = ReportsManager()
    geo_location_manager = GeoLocationManager()
    ci_pipeline_manager = CIPipelineManager()
    pipeline_config_manager = PipelineConfigManager()

    def get_summary(self):
        """Application Inventory Stats"""
        locales_set = self.inventory_manager.get_locales_set()
        summary = {}
        summary['locales_len'] = len(locales_set[0]) \
            if isinstance(locales_set, tuple) and len(locales_set) > 0 else 0
        platforms = self.inventory_manager.get_transplatform_slug_url()
        summary['platforms_len'] = len(platforms) if platforms else 0
        # relstreams = self.inventory_manager.get_relstream_slug_name()
        # summary['products_len'] = len(relstreams) if relstreams else 0
        # relbranches = self.release_branch_manager.get_release_branches()
        # summary['releases_len'] = relbranches.count() if relbranches else 0
        summary['packages_len'] = self.packages_manager.count_packages()
        jobs_templates_count = self.jobs_template_manager.get_job_templates().count()
        summary['jobs_templates_len'] = jobs_templates_count
        # coverage = self.graph_manager.get_graph_rules(only_active=True)
        # summary['graph_rules_len'] = coverage.count() if coverage else 0
        return summary

    @staticmethod
    def log_visitor(http_meta):
        """log visitors"""
        x_forwarded_for = http_meta.get('HTTP_X_FORWARDED_FOR')
        ip = x_forwarded_for.split(',')[-1].strip() \
            if x_forwarded_for else http_meta.get('REMOTE_ADDR')
        http_params = {}
        match_params = {
            'visitor_ip': ip,
            'visitor_user_agent': http_meta.get('HTTP_USER_AGENT')
        }
        http_params.update(match_params)
        http_params['visitor_accept'] = http_meta.get('HTTP_ACCEPT')
        http_params['visitor_encoding'] = http_meta.get('HTTP_ACCEPT_ENCODING')
        http_params['visitor_language'] = http_meta.get('HTTP_ACCEPT_LANGUAGE')
        http_params['visitor_host'] = http_meta.get('REMOTE_HOST')
        try:
            Visitor.objects.update_or_create(
                **match_params, defaults=http_params
            )
        except Exception as e:
            pass


class TranStatusPackageView(ManagersMixin, TemplateView):
    """Translation Status Package View"""
    template_name = "packages/package_view.html"

    def get(self, request, *args, **kwargs):
        response = super(TranStatusPackageView, self).get(
            request, *args, **kwargs)
        if not self.packages_manager.is_package_exist(
                kwargs.get('package_name', '')):
            raise Http404("Package does not exist.")
        return response

    def get_context_data(self, **kwargs):
        """Build the Context Data"""
        context = super(TranStatusPackageView, self).get_context_data(**kwargs)
        packages = self.packages_manager.get_package_name_tuple()
        langs = self.inventory_manager.get_locale_lang_tuple()
        if packages:
            context['packages'] = packages
        if langs:
            context['languages'] = sorted(langs, key=lambda x: x[1])
        return context


class TranStatusReleasesView(ManagersMixin, TemplateView):
    """
    Translation Status Releases View
    This view is for current landing page.
    """
    template_name = "releases/release_list.html"

    def get(self, request, *args, **kwargs):
        http_meta = self.request.META.copy()
        self.log_visitor(http_meta)
        return super(TranStatusReleasesView, self).get(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        """Build the Context Data"""
        context = super(TranStatusReleasesView, self).get_context_data(**kwargs)
        relbranches = self.release_branch_manager.get_relbranch_name_slug_tuple()
        context['releases'] = relbranches
        products = self.inventory_manager.get_release_streams()
        if self.request.tenant in RELSTREAM_SLUGS:
            products = products.filter(**{'product_slug': self.request.tenant})
        context['relstreams'] = products
        product_releases = self.release_branch_manager.get_branches_of_relstreams(products)
        p_releases_dict = {}
        if product_releases is not None and isinstance(product_releases, dict):
            for product, releases in product_releases.items():
                p_releases_dict[product] = len(releases) or 0
        context['p_releases_dict'] = p_releases_dict if p_releases_dict \
            else {p.product_slug: 0 for p in products}
        context.update(self.get_summary())
        return context


class TranStatusReleaseView(ManagersMixin, TemplateView):
    """Translation Status Release View"""
    template_name = "releases/release_view.html"

    def get_context_data(self, **kwargs):
        context = super(TranStatusReleaseView, self).get_context_data(**kwargs)
        if not self.release_branch_manager.is_relbranch_exist(
                kwargs.get('release_branch', '')):
            raise Http404("Release does not exist.")
        langs = self.inventory_manager.get_locale_lang_tuple()
        if langs:
            context['languages'] = sorted(langs, key=lambda x: x[1])
        relbranches = self.release_branch_manager.get_relbranch_name_slug_tuple()
        context['releases'] = relbranches
        release_stream = self.release_branch_manager.get_release_branches(
            relbranch=kwargs.get('release_branch'), fields=['product_slug']).get()
        if release_stream:
            context['release_stream'] = release_stream.product_slug.product_slug
        context['release_branch'] = kwargs.get('release_branch')
        return context


class TransCoverageView(ManagersMixin, TemplateView):
    """Translation Coverage View"""
    template_name = "coverage/coverage_rule_view.html"

    def get(self, request, *args, **kwargs):
        rule = self.graph_manager.get_graph_rules(
            graph_rule=kwargs.get('coverage_rule', '')
        )
        if not rule:
            raise Http404("Coverage rule does not exist.")
        if not rule.get().rule_visibility_public and \
                not request.user.is_authenticated:
            raise PermissionDenied
        return super(TransCoverageView, self).get(
{'fraction_non_alphanumeric': 0.08951406649616368, 'fraction_numerical': 0.0034826141372367634, 'mean_word_length': 3.7135162862272377, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1023792', 'n_tokens_mistral': 5341, 'n_tokens_neox': 5129, 'n_words': 1171}