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

#
# Newfies-Dialer License
# http://www.newfies-dialer.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-2013 Star2Billing S.L.
#
# The Initial Developer of the Original Code is
# <NAME> <<EMAIL>>
#

from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.conf import settings

from tastypie.resources import ModelResource, ALL
from tastypie.authentication import BasicAuthentication
from tastypie.authorization import Authorization
from tastypie.validation import Validation
from tastypie.throttle import BaseThrottle
from tastypie.exceptions import BadRequest
from tastypie import fields

from api.user_api import UserResource
from api.gateway_api import GatewayResource
from api.audiofile_api import AudioFileResource
from api.content_type_api import ContentTypeResource
from api.phonebook_api import PhonebookResource
from api.resources import get_value_if_none
from dialer_contact.models import Phonebook
from dialer_campaign.models import Campaign
from dialer_gateway.models import Gateway
from dialer_campaign.function_def import \
    user_dialer_setting, check_dialer_setting, \
    dialer_setting_limit
from audiofield.models import AudioFile
import time
import logging

logger = logging.getLogger('newfies.filelog')


class CampaignValidation(Validation):
    """
    Campaign Validation Class
    """
    def is_valid(self, bundle, request=None):
        errors = {}
        if not bundle.data:
            errors['Data'] = ['Data set is empty']
        startingdate = bundle.data.get('startingdate')
        expirationdate = bundle.data.get('expirationdate')

        if request.method == 'POST':
            startingdate = get_value_if_none(startingdate, time.time())
            # expires in 90 days
            expirationdate = get_value_if_none(expirationdate,
                time.time() + 86400 * 90)
            #Startdate and expirationdate are UTC -> convert to localtime
            startingdate = float(startingdate) - time.altzone
            expirationdate = float(expirationdate) - time.altzone

            bundle.data['startingdate'] = time.strftime('%Y-%m-%d %H:%M:%S',
                time.gmtime(startingdate))
            bundle.data['expirationdate'] = time.strftime('%Y-%m-%d %H:%M:%S',
                time.gmtime(expirationdate))

        if request.method == 'PUT':
            if startingdate:
                bundle.data['startingdate'] = time.strftime(
                    '%Y-%m-%d %H:%M:%S', time.gmtime(float(startingdate)))
            if expirationdate:
                bundle.data['expirationdate'] = time.strftime(
                    '%Y-%m-%d %H:%M:%S', time.gmtime(float(expirationdate)))

        if not user_dialer_setting(request):
            errors['user_dialer_setting'] = ['Your settings are not configured properly, Please contact the administrator.']

        if check_dialer_setting(request, check_for="campaign"):
            errors['chk_campaign'] = ["Too many campaigns. Max allowed %s"
                % dialer_setting_limit(request, limit_for="campaign")]

        frequency = bundle.data.get('frequency')
        if frequency:
            if check_dialer_setting(request, check_for="frequency", field_value=int(frequency)):
                errors['chk_frequency'] = ["Frequency limit of %s exceeded."
                    % dialer_setting_limit(request, limit_for="frequency")]

        callmaxduration = bundle.data.get('callmaxduration')
        if callmaxduration:
            if check_dialer_setting(request, check_for="duration", field_value=int(callmaxduration)):
                errors['chk_duration'] = ["Duration limit of %s exceeded."
                    % dialer_setting_limit(request, limit_for="duration")]

        maxretry = bundle.data.get('maxretry')
        if maxretry:
            if check_dialer_setting(request, check_for="retry", field_value=int(maxretry)):
                errors['chk_duration'] = ["Retries limit of %s exceeded."
                    % dialer_setting_limit(request, limit_for="retry")]

        calltimeout = bundle.data.get('calltimeout')
        if calltimeout:<commit_msg>fix testcase for travis<commit_after># -*- coding: utf-8 -*-

#
# Newfies-Dialer License
# http://www.newfies-dialer.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-2013 Star2Billing S.L.
#
# The Initial Developer of the Original Code is
# <NAME> <<EMAIL>>
#

from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.conf import settings

from tastypie.resources import ModelResource, ALL
from tastypie.authentication import BasicAuthentication
from tastypie.authorization import Authorization
from tastypie.validation import Validation
from tastypie.throttle import BaseThrottle
from tastypie.exceptions import BadRequest
from tastypie import fields

from api.user_api import UserResource
from api.gateway_api import GatewayResource
from api.audiofile_api import AudioFileResource
from api.content_type_api import ContentTypeResource
from api.phonebook_api import PhonebookResource
from api.resources import get_value_if_none
from dialer_contact.models import Phonebook
from dialer_campaign.models import Campaign
from dialer_gateway.models import Gateway
from dialer_campaign.function_def import \
    user_dialer_setting, check_dialer_setting, \
    dialer_setting_limit
from audiofield.models import AudioFile
import time
import logging

logger = logging.getLogger('newfies.filelog')


class CampaignValidation(Validation):
    """
    Campaign Validation Class
    """
    def is_valid(self, bundle, request=None):
        errors = {}
        if not bundle.data:
            errors['Data'] = ['Data set is empty']
        startingdate = bundle.data.get('startingdate')
        expirationdate = bundle.data.get('expirationdate')

        if request.method == 'POST':
            startingdate = get_value_if_none(startingdate, time.time())
            # expires in 90 days
            expirationdate = get_value_if_none(expirationdate,
                time.time() + 86400 * 90)
            #Startdate and expirationdate are UTC -> convert to localtime
            startingdate = float(startingdate) - time.altzone
            expirationdate = float(expirationdate) - time.altzone

            bundle.data['startingdate'] = time.strftime('%Y-%m-%d %H:%M:%S',
                time.gmtime(startingdate))
            bundle.data['expirationdate'] = time.strftime('%Y-%m-%d %H:%M:%S',
                time.gmtime(expirationdate))

        if request.method == 'PUT':
            if startingdate:
                bundle.data['startingdate'] = time.strftime(
                    '%Y-%m-%d %H:%M:%S', time.gmtime(float(startingdate)))
            if expirationdate:
                bundle.data['expirationdate'] = time.strftime(
                    '%Y-%m-%d %H:%M:%S', time.gmtime(float(expirationdate)))

        if not user_dialer_setting(request.user):
            errors['user_dialer_setting'] = ['Your settings are not configured properly, Please contact the administrator.']

        if check_dialer_setting(request, check_for="campaign"):
            errors['chk_campaign'] = ["Too many campaigns. Max allowed %s"
                % dialer_setting_limit(request, limit_for="campaign")]

        frequency = bundle.data.get('frequency')
        if frequency:
            if check_dialer_setting(request, check_for="frequency", field_value=int(frequency)):
                errors['chk_frequency'] = ["Frequency limit of %s exceeded."
                    % dialer_setting_limit(request, limit_for="frequency")]

        callmaxduration = bundle.data.get('callmaxduration')
        if callmaxduration:
            if check_dialer_setting(request, check_for="duration", field_value=int(callmaxduration)):
                errors['chk_duration'] = ["Duration limit of %s exceeded."
                    % dialer_setting_limit(request, limit_for="duration")]

        maxretry = bundle.data.get('maxretry')
        if maxretry:
            if check_dialer_setting(request, check_for="retry", field_value=int(maxretry)):
                errors['chk_duration'] = ["Retries limit of %s exceeded."
                    % dialer_setting_limit(request, limit_for="retry")]

        calltimeout = bundle.data.get('calltimeout')
        if calltimeout:
{'fraction_non_alphanumeric': 0.10133974579182412, 'fraction_numerical': 0.005267376617428146, 'mean_word_length': 3.1139896373056994, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2459669', 'n_tokens_mistral': 2522, 'n_tokens_neox': 2383, 'n_words': 606}