<commit_before>from __future__ import absolute_import, unicode_literals

import base64
import hashlib
import hmac
import json
import phonenumbers
import plivo
import pycountry
import pytz
import time

from datetime import datetime, timedelta
from django import forms
from django.conf import settings
from django.contrib import messages
from django.core.exceptions import ValidationError
from django.core.urlresolvers import reverse
from django.db.models import Count, Sum
from django.http import Http404, HttpResponse, HttpResponseRedirect
from django.shortcuts import redirect
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from django_countries.data import COUNTRIES
from phonenumbers.phonenumberutil import region_code_for_number
from smartmin.views import SmartCRUDL, SmartReadView
from smartmin.views import SmartUpdateView, SmartDeleteView, SmartTemplateView, SmartListView, SmartFormView
from temba.contacts.models import TEL_SCHEME, TWITTER_SCHEME
from temba.msgs.models import Broadcast, Call, Msg, QUEUED, PENDING
from temba.orgs.models import Org, ACCOUNT_SID
from temba.orgs.views import OrgPermsMixin, OrgObjPermsMixin, ModalMixin
from temba.utils.middleware import disable_middleware
from temba.utils import analytics, non_atomic_when_eager
from twilio import TwilioRestException
from twython import Twython
from uuid import uuid4
from .models import Channel, SyncEvent, Alert, ChannelLog, ChannelCount, M3TECH
from .models import PLIVO_AUTH_ID, PLIVO_AUTH_TOKEN, PLIVO, BLACKMYNA, SMSCENTRAL, VERIFY_SSL
from .models import PASSWORD, RECEIVE, SEND, CALL, ANSWER, SEND_METHOD, SEND_URL, USERNAME, CLICKATELL, HIGH_CONNECTION
from .models import ANDROID, EXTERNAL, HUB9, INFOBIP, KANNEL, NEXMO, TWILIO, TWITTER, VUMI, VERBOICE, SHAQODOON
from .models import ENCODING, ENCODING_CHOICES, DEFAULT_ENCODING, YO

RELAYER_TYPE_ICONS = {ANDROID: "icon-channel-android",
                      EXTERNAL: "icon-channel-external",
                      KANNEL: "icon-channel-kannel",
                      NEXMO: "icon-channel-nexmo",
                      VERBOICE: "icon-channel-external",
                      TWILIO: "icon-channel-twilio",
                      PLIVO: "icon-channel-plivo",
                      CLICKATELL: "icon-channel-clickatell",
                      TWITTER: "icon-twitter"}

SESSION_TWITTER_TOKEN = '<PASSWORD>oauth_token'
SESSION_TWITTER_SECRET = 'twitter_oauth_token_secret'

TWILIO_SEARCH_COUNTRIES = (('BE', _("Belgium")),
                           ('CA', _("Canada")),
                           ('FI', _("Finland")),
                           ('NO', _("Norway")),
                           ('PL', _("Poland")),
                           ('ES', _("Spain")),
                           ('SE', _("Sweden")),
                           ('GB', _("United Kingdom")),
                           ('US', _("United States")))

TWILIO_SUPPORTED_COUNTRIES = (('AU', _("Australia")),
                              ('AT', _("Austria")),
                              ('BE', _("Belgium")),
                              ('CA', _("Canada")),
                              ('CL', _("Chile")), #Beta
                              ('CZ', _("Czech Republic")), #Beta
                              ('DK', _("Denmark")), #Beta
                              ('EE', _("Estonia")),
                              ('FI', _("Finland")),
                              ('FR', _("France")), #Beta
                              ('DE', _("Germany")),
                              ('HK', _("Hong Kong")),
                              ('HU', _("Hungary")), #Beta
                              ('IE', _("Ireland")),
                              ('IL', _("Israel")), #Beta
                              ('LT', _("Lithuania")),
                              ('MX', _("Mexico")), #Beta
                              ('NO', _("Norway")),
                              ('PL', _("Poland")),
                              ('ES', _("Spain")),
                              ('SE', _("Sweden")),
                              ('CH', _("Switzerland")),
                              ('GB', _("United Kingdom")),
                              ('US', _("United States")))

TWILIO_SUPPORTED_COUNTRY_CODES = [61, 43, 32, 1, 56, 420, 45, 372, 358, 33, 49, 852, 36, 353, 972, 370, 52, 47, 48, 34, 46, 41, 44]

NEXMO_SUPPORTED_COUNTRIES = (('AU', _('Australia')),
                             ('AT', _('Austria')),
                             ('BE', _('Belgium')),
                             ('CA', _('Canada')),
                             ('CL', _('Chile')),
                             ('CR', _('Costa Rica')),
                             ('CZ', _('Czech Republic')),
                             ('DK', _('Denmark')),
                             ('EE', _('Estonia')),
                             ('FI', _('Finland')),
                             ('FR', _('France')),
                             ('DE', _('Germany')),
                             ('HK', _('Hong Kong')),
                             ('HU', _('Hungary')),
                             ('ID', _('Indonesia')),
                             ('IE', _('Ireland')),
                             ('IL', _('Israel')),
                             ('IT', _('Italy')),
                             ('LV', _('Latvia')),
                             ('LT', _('Lithuania')),
                             ('MY', _('Malaysia')),
                             ('MX', _('Mexico')),
                             ('MW', _('Malawi')),
                             ('NL', _('Netherlands')),
                             ('NO', _('Norway')),
                             ('PK', _('Pakistan')),
                             ('PL', _('Poland')),
                             ('PR', _('Puerto Rico')),
                             ('RO', _('Romania')),
                             ('RU', _('Russia')),
                             ('RW', _('Rwanda')),
                             ('SK', _('Slovakia')),
                             ('ZA', _('South Africa')),
                             ('KR', _('South Korea')),
                             ('ES', _('Spain')),
                             ('SE', _('Sweden')),
                             ('CH', _('Switzerland')),
                             ('GB', _('United Kingdom')),
                             ('US', _('United States')))

NEXMO_SUPPORTED_COUNTRY_CODES = [61, 43, 32, 1, 56, 506, 420, 45, 372, 358, 33, 49, 852, 36, 353, 972, 39, 371, 370,
                                 60, 52, 31, 47, 92, 48, 1787, 40, 7, 250, 421, 27, 82, 34, 46, 41, 44, 265, 62]

PLIVO_SUPPORTED_COUNTRIES = (('AU', _('Australia')),
                             ('BE', _('Belgium')),
                             ('CA', _('Canada')),
                             ('CZ', _('Czech Republic')),
                             ('EE', _('Estonia')),
                             ('FI', _('Finland')),
                             ('DE', _('Germany')),
                             ('HK', _('Hong Kong')),
                             ('HU', _('Hungary')),
                             ('IL', _('Israel')),
                             ('LT', _('Lithuania')),
                             ('MX', _('Mexico')),
                             ('NO', _('Norway')),
                             ('PK', _('Pakistan')),
                             ('PL', _('Poland')),
                             ('SE', _('Sweden')),
                             ('CH', _('Switzerland')),
                             ('GB', _('United Kingdom')),
                             ('US', _('United States')))

PLIVO_SUPPORTED_COUNTRY_CODES = [61, 32, 1, 420, 372, 358, 49, 852, 36, 972, 370, 52, 47, 92, 48, 46, 41, 44]

# django_countries now uses a dict of countries, let's turn it in our tuple
# list of codes and countries sorted by country name
ALL_COUNTRIES = sorted(((code, name) for code, name in COUNTRIES.items()), key=lambda x: x[1])

def get_channel_icon(channel_type):
    return RELAYER_TYPE_ICONS.get(channel_type, "icon-channel-external")


def channel_status_processor(request):
    status = dict()
    user = request.user

    if user.is_superuser or user.is_anonymous():
        return status

    # from the logged in user get the channel
    org = user.get_org()

    allowed = False
    if org:
        allowed = user.has_org_perm(org, 'channels.channel_claim')

    if allowed:
        # only care about channels that are older than an hour
        cutoff = timezone.now() - timedelta(hours=1)
        send_channel = org.get_send_channel(scheme=TEL_SCHEME)
        call_channel = org.get_call_channel()
<commit_msg>update plivo supportted countries<commit_after>from __future__ import absolute_import, unicode_literals

import base64
import hashlib
import hmac
import json
import phonenumbers
import plivo
import pycountry
import pytz
import time

from datetime import datetime, timedelta
from django import forms
from django.conf import settings
from django.contrib import messages
from django.core.exceptions import ValidationError
from django.core.urlresolvers import reverse
from django.db.models import Count, Sum
from django.http import Http404, HttpResponse, HttpResponseRedirect
from django.shortcuts import redirect
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from django_countries.data import COUNTRIES
from phonenumbers.phonenumberutil import region_code_for_number
from smartmin.views import SmartCRUDL, SmartReadView
from smartmin.views import SmartUpdateView, SmartDeleteView, SmartTemplateView, SmartListView, SmartFormView
from temba.contacts.models import TEL_SCHEME, TWITTER_SCHEME
from temba.msgs.models import Broadcast, Call, Msg, QUEUED, PENDING
from temba.orgs.models import Org, ACCOUNT_SID
from temba.orgs.views import OrgPermsMixin, OrgObjPermsMixin, ModalMixin
from temba.utils.middleware import disable_middleware
from temba.utils import analytics, non_atomic_when_eager
from twilio import TwilioRestException
from twython import Twython
from uuid import uuid4
from .models import Channel, SyncEvent, Alert, ChannelLog, ChannelCount, M3TECH
from .models import PLIVO_AUTH_ID, PLIVO_AUTH_TOKEN, PLIVO, BLACKMYNA, SMSCENTRAL, VERIFY_SSL
from .models import PASSWORD, RECEIVE, SEND, CALL, ANSWER, SEND_METHOD, SEND_URL, USERNAME, CLICKATELL, HIGH_CONNECTION
from .models import ANDROID, EXTERNAL, HUB9, INFOBIP, KANNEL, NEXMO, TWILIO, TWITTER, VUMI, VERBOICE, SHAQODOON
from .models import ENCODING, ENCODING_CHOICES, DEFAULT_ENCODING, YO

RELAYER_TYPE_ICONS = {ANDROID: "icon-channel-android",
                      EXTERNAL: "icon-channel-external",
                      KANNEL: "icon-channel-kannel",
                      NEXMO: "icon-channel-nexmo",
                      VERBOICE: "icon-channel-external",
                      TWILIO: "icon-channel-twilio",
                      PLIVO: "icon-channel-plivo",
                      CLICKATELL: "icon-channel-clickatell",
                      TWITTER: "icon-twitter"}

SESSION_TWITTER_TOKEN = 'twitter_oauth_token'
SESSION_TWITTER_SECRET = 'twitter_oauth_token_secret'

TWILIO_SEARCH_COUNTRIES = (('BE', _("Belgium")),
                           ('CA', _("Canada")),
                           ('FI', _("Finland")),
                           ('NO', _("Norway")),
                           ('PL', _("Poland")),
                           ('ES', _("Spain")),
                           ('SE', _("Sweden")),
                           ('GB', _("United Kingdom")),
                           ('US', _("United States")))

TWILIO_SUPPORTED_COUNTRIES = (('AU', _("Australia")),
                              ('AT', _("Austria")),
                              ('BE', _("Belgium")),
                              ('CA', _("Canada")),
                              ('CL', _("Chile")), #Beta
                              ('CZ', _("Czech Republic")), #Beta
                              ('DK', _("Denmark")), #Beta
                              ('EE', _("Estonia")),
                              ('FI', _("Finland")),
                              ('FR', _("France")), #Beta
                              ('DE', _("Germany")),
                              ('HK', _("Hong Kong")),
                              ('HU', _("Hungary")), #Beta
                              ('IE', _("Ireland")),
                              ('IL', _("Israel")), #Beta
                              ('LT', _("Lithuania")),
                              ('MX', _("Mexico")), #Beta
                              ('NO', _("Norway")),
                              ('PL', _("Poland")),
                              ('ES', _("Spain")),
                              ('SE', _("Sweden")),
                              ('CH', _("Switzerland")),
                              ('GB', _("United Kingdom")),
                              ('US', _("United States")))

TWILIO_SUPPORTED_COUNTRY_CODES = [61, 43, 32, 1, 56, 420, 45, 372, 358, 33, 49, 852, 36, 353, 972, 370, 52, 47, 48, 34, 46, 41, 44]

NEXMO_SUPPORTED_COUNTRIES = (('AU', _('Australia')),
                             ('AT', _('Austria')),
                             ('BE', _('Belgium')),
                             ('CA', _('Canada')),
                             ('CL', _('Chile')),
                             ('CR', _('Costa Rica')),
                             ('CZ', _('Czech Republic')),
                             ('DK', _('Denmark')),
                             ('EE', _('Estonia')),
                             ('FI', _('Finland')),
                             ('FR', _('France')),
                             ('DE', _('Germany')),
                             ('HK', _('Hong Kong')),
                             ('HU', _('Hungary')),
                             ('ID', _('Indonesia')),
                             ('IE', _('Ireland')),
                             ('IL', _('Israel')),
                             ('IT', _('Italy')),
                             ('LV', _('Latvia')),
                             ('LT', _('Lithuania')),
                             ('MY', _('Malaysia')),
                             ('MX', _('Mexico')),
                             ('MW', _('Malawi')),
                             ('NL', _('Netherlands')),
                             ('NO', _('Norway')),
                             ('PK', _('Pakistan')),
                             ('PL', _('Poland')),
                             ('PR', _('Puerto Rico')),
                             ('RO', _('Romania')),
                             ('RU', _('Russia')),
                             ('RW', _('Rwanda')),
                             ('SK', _('Slovakia')),
                             ('ZA', _('South Africa')),
                             ('KR', _('South Korea')),
                             ('ES', _('Spain')),
                             ('SE', _('Sweden')),
                             ('CH', _('Switzerland')),
                             ('GB', _('United Kingdom')),
                             ('US', _('United States')))

NEXMO_SUPPORTED_COUNTRY_CODES = [61, 43, 32, 1, 56, 506, 420, 45, 372, 358, 33, 49, 852, 36, 353, 972, 39, 371, 370,
                                 60, 52, 31, 47, 92, 48, 1787, 40, 7, 250, 421, 27, 82, 34, 46, 41, 44, 265, 62]

PLIVO_SUPPORTED_COUNTRIES = (('AU', _('Australia')),
                             ('BE', _('Belgium')),
                             ('CA', _('Canada')),
                             ('CZ', _('Czech Republic')),
                             ('EE', _('Estonia')),
                             ('FI', _('Finland')),
                             ('DE', _('Germany')),
                             ('HK', _('Hong Kong')),
                             ('HU', _('Hungary')),
                             ('IL', _('Israel')),
                             ('LT', _('Lithuania')),
                             ('MX', _('Mexico')),
                             ('NO', _('Norway')),
                             ('PL', _('Poland')),
                             ('ZA', _('South Africa')),
                             ('SE', _('Sweden')),
                             ('CH', _('Switzerland')),
                             ('GB', _('United Kingdom')),
                             ('US', _('United States')))

PLIVO_SUPPORTED_COUNTRY_CODES = [61, 32, 1, 420, 372, 358, 49, 852, 36, 972, 370, 52, 47, 48, 27, 46, 41, 44]

# django_countries now uses a dict of countries, let's turn it in our tuple
# list of codes and countries sorted by country name
ALL_COUNTRIES = sorted(((code, name) for code, name in COUNTRIES.items()), key=lambda x: x[1])

def get_channel_icon(channel_type):
    return RELAYER_TYPE_ICONS.get(channel_type, "icon-channel-external")


def channel_status_processor(request):
    status = dict()
    user = request.user

    if user.is_superuser or user.is_anonymous():
        return status

    # from the logged in user get the channel
    org = user.get_org()

    allowed = False
    if org:
        allowed = user.has_org_perm(org, 'channels.channel_claim')

    if allowed:
        # only care about channels that are older than an hour
        cutoff = timezone.now() - timedelta(hours=1)
        send_channel = org.get_send_channel(scheme=TEL_SCHEME)
        call_channel = org.get_call_channel()

{'fraction_non_alphanumeric': 0.14332021301227135, 'fraction_numerical': 0.022111599907385968, 'mean_word_length': 1.4502907389022834, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4192230', 'n_tokens_mistral': 5377, 'n_tokens_neox': 4812, 'n_words': 1188}