<commit_before># -*- coding: utf-8 -*-

#
# CDR-Stats License
# http://www.cdr-stats.org
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright (C) 2011-2012 Star2Billing S.L.
#
# The Initial Developer of the Original Code is
# <NAME> <<EMAIL>>
#
from django import forms
from django.conf import settings
from django.core.validators import validate_email
from django.utils.translation import ugettext_lazy as _
from common.common_functions import comp_day_range
from cdr.functions_def import get_switch_list, get_country_list, get_hc_list
from cdr.constants import STRING_SEARCH_TYPE_LIST
from user_profile.models import UserProfile

COMPARE_LIST = ((2, '>'),
    (3, '>='),
    (4, '<'),
    (5, '<='),
    (1, '='),)

PAGE_SIZE_LIST = ((10, '10'),
    (25, '25'),
    (50, '50'),
    (100, '100'),
    (250, '250'),
    (500, '500'),
    (1000, '1000'))


def sw_list_with_all():
    """Switch list"""
    list_sw = []
    list_sw.append((0, _('All Switches')))
    sw_list = get_switch_list()
    for i in sw_list:
        list_sw.append((i[0], i[1]))
    return list_sw


def hc_list_with_all():
    """Hangup cause list"""
    list_hc = []
    list_hc.append((0, _('All')))
    hc_list = get_hc_list()
    for i in hc_list:
        list_hc.append((i[0], i[1]))
    return list_hc


def country_list_with_all():
    """Country list"""
    list_ct = []
    list_ct.append((0, _('All')))
    ct_list = get_country_list()
    for i in ct_list:
        list_ct.append((i[0], i[1]))
    return list_ct


class SearchForm(forms.Form):
    """
    Form used to search on general parameters in the Customer UI.
    """

    caller = forms.CharField(label=_('Caller ID'), required=False)
    caller_type = forms.ChoiceField(label='', required=False,
                                    choices=list(STRING_SEARCH_TYPE_LIST))
    caller_type.widget.attrs['class'] = 'input-small'

    destination = forms.CharField(label=_('Destination'),
                                  required=False)
    destination_type = forms.ChoiceField(label='',
                                         required=False,
                                         choices=list(STRING_SEARCH_TYPE_LIST))
    destination_type.widget.attrs['class'] = 'input-small'
    accountcode = forms.CharField(label=_('Account Code'),
                                  required=False)
    accountcode_type = forms.ChoiceField(label='',
                                         required=False,
                                         choices=list(STRING_SEARCH_TYPE_LIST))
    accountcode_type.widget.attrs['class'] = 'input-small'
    duration = forms.CharField(label=_('Duration (Secs)'),
                               required=False)
    duration_type = forms.ChoiceField(label='',
                                      required=False,
                                      choices=COMPARE_LIST)
    duration_type.widget.attrs['class'] = 'input-small'
    hangup_cause = forms.ChoiceField(label=_('Hangup cause'),
                                     required=False,
                                     choices=hc_list_with_all())
    switch = forms.ChoiceField(label=_('Switch'),
                               required=False,
                               choices=get_switch_list())
    country_id = forms.MultipleChoiceField(label=_('Country'), required=False,
                                           choices=country_list_with_all())

    def __init__(self, *args, **kwargs):
        super(SearchForm, self).__init__(*args, **kwargs)
        self.fields['switch'].choices = sw_list_with_all()

    def clean_caller(self):
        """Retrieve valid caller & it should be integer
        else will raise from validation error
        """
        caller = self.cleaned_data['caller']
        if caller:
            try:
                int(caller)
            except:
                raise forms.ValidationError('%s is not a valid caller.'
                        % caller)
        return caller

    def clean_duration(self):
        """Retrieve valid duration & it should be integer
        else will raise from validation error
        """
        duration = self.cleaned_data['duration']
        if duration:
            try:
                int(duration)
            except:
                raise forms.ValidationError('%s is not a valid duration.'
                        % duration)
        return duration

    def clean_accountcode(self):
        """Retrieve valid accountcode"""
        accountcode = self.cleaned_data['accountcode']
        return accountcode

    def clean_destination(self):
        """Retrieve valid destination & it should be integer
        else will raise from validation error"""
        destination = self.cleaned_data['destination']
        if destination:
            try:
                int(destination)
            except:
                raise forms.ValidationError('%s is not a valid destination.'
                        % destination)
        return destination


class CdrSearchForm(SearchForm):
    """
    Form used to search calls in the Customer UI.
    """

    from_date = forms.CharField(label=_('From'),
                                required=True, max_length=10)
    to_date = forms.CharField(label=_('To'),
                              required=True, max_length=10)
    direction = forms.TypedChoiceField(label=_('Direction'),
                                       required=False,
                                       coerce=bool,
                                       choices=(('all', _('All')),
                                       ('inbound', _('Inbound')),
                                       ('outbound', _('Outbound')),
                                       ('unknown', _('Unknown'))))<commit_msg>Code style / ident<commit_after># -*- coding: utf-8 -*-

#
# CDR-Stats License
# http://www.cdr-stats.org
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright (C) 2011-2012 Star2Billing S.L.
#
# The Initial Developer of the Original Code is
# <NAME> <<EMAIL>>
#
from django import forms
from django.conf import settings
from django.core.validators import validate_email
from django.utils.translation import ugettext_lazy as _
from common.common_functions import comp_day_range
from cdr.functions_def import get_switch_list, get_country_list, get_hc_list
from cdr.constants import STRING_SEARCH_TYPE_LIST
from user_profile.models import UserProfile

COMPARE_LIST = ((2, '>'),
    (3, '>='),
    (4, '<'),
    (5, '<='),
    (1, '='),)

PAGE_SIZE_LIST = ((10, '10'),
    (25, '25'),
    (50, '50'),
    (100, '100'),
    (250, '250'),
    (500, '500'),
    (1000, '1000'))


def sw_list_with_all():
    """Switch list"""
    list_sw = []
    list_sw.append((0, _('All Switches')))
    sw_list = get_switch_list()
    for i in sw_list:
        list_sw.append((i[0], i[1]))
    return list_sw


def hc_list_with_all():
    """Hangup cause list"""
    list_hc = []
    list_hc.append((0, _('All')))
    hc_list = get_hc_list()
    for i in hc_list:
        list_hc.append((i[0], i[1]))
    return list_hc


def country_list_with_all():
    """Country list"""
    list_ct = []
    list_ct.append((0, _('All')))
    ct_list = get_country_list()
    for i in ct_list:
        list_ct.append((i[0], i[1]))
    return list_ct


class SearchForm(forms.Form):
    """
    Form used to search on general parameters in the Customer UI.
    """

    caller = forms.CharField(label=_('Caller ID'), required=False)
    caller_type = forms.ChoiceField(label='', required=False,
                                    choices=list(STRING_SEARCH_TYPE_LIST))
    caller_type.widget.attrs['class'] = 'input-small'

    destination = forms.CharField(label=_('Destination'),
                                  required=False)
    destination_type = forms.ChoiceField(label='',
                                         required=False,
                                         choices=list(STRING_SEARCH_TYPE_LIST))
    destination_type.widget.attrs['class'] = 'input-small'
    accountcode = forms.CharField(label=_('Account Code'),
                                  required=False)
    accountcode_type = forms.ChoiceField(label='',
                                         required=False,
                                         choices=list(STRING_SEARCH_TYPE_LIST))
    accountcode_type.widget.attrs['class'] = 'input-small'
    duration = forms.CharField(label=_('Duration (Secs)'),
                               required=False)
    duration_type = forms.ChoiceField(label='',
                                      required=False,
                                      choices=COMPARE_LIST)
    duration_type.widget.attrs['class'] = 'input-small'
    hangup_cause = forms.ChoiceField(label=_('Hangup cause'),
                                     required=False,
                                     choices=hc_list_with_all())
    switch = forms.ChoiceField(label=_('Switch'),
                               required=False,
                               choices=get_switch_list())
    country_id = forms.MultipleChoiceField(label=_('Country'), required=False,
                                           choices=country_list_with_all())

    def __init__(self, *args, **kwargs):
        super(SearchForm, self).__init__(*args, **kwargs)
        self.fields['switch'].choices = sw_list_with_all()

    def clean_caller(self):
        """Retrieve valid caller & it should be integer
        else will raise from validation error
        """
        caller = self.cleaned_data['caller']
        if caller:
            try:
                int(caller)
            except:
                raise forms.ValidationError('%s is not a valid caller.'
                    % caller)
        return caller

    def clean_duration(self):
        """Retrieve valid duration & it should be integer
        else will raise from validation error
        """
        duration = self.cleaned_data['duration']
        if duration:
            try:
                int(duration)
            except:
                raise forms.ValidationError('%s is not a valid duration.'
                    % duration)
        return duration

    def clean_accountcode(self):
        """Retrieve valid accountcode"""
        accountcode = self.cleaned_data['accountcode']
        return accountcode

    def clean_destination(self):
        """Retrieve valid destination & it should be integer
        else will raise from validation error"""
        destination = self.cleaned_data['destination']
        if destination:
            try:
                int(destination)
            except:
                raise forms.ValidationError('%s is not a valid destination.'
                    % destination)
        return destination


class CdrSearchForm(SearchForm):
    """
    Form used to search calls in the Customer UI.
    """

    from_date = forms.CharField(label=_('From'),
                                required=True, max_length=10)
    to_date = forms.CharField(label=_('To'),
                              required=True, max_length=10)
    direction = forms.TypedChoiceField(label=_('Direction'),
                                       required=False,
                                       coerce=bool,
                                       choices=(('all', _('All')),
                                       ('inbound', _('Inbound')),
                                       ('outbound', _('Outbound')),
                                       ('unknown', _('Unknown'))))
{'fraction_non_alphanumeric': 0.11431723669693626, 'fraction_numerical': 0.011881524229822625, 'mean_word_length': 1.8636695018226002, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 48, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7112056', 'n_tokens_mistral': 3280, 'n_tokens_neox': 3161, 'n_words': 749}