<commit_before>#    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 logging

from django.core.urlresolvers import reverse
from django import template
from django.utils.translation import ugettext_lazy as _

from horizon import messages
from horizon import tables
from horizon.utils import memoized

import json
import urllib2

from openstack_dashboard import api
from openstack_dashboard.dashboards.admin.geotags \
    import constants

LOG = logging.getLogger(__name__)

class UpdateRow(tables.Row):
    ajax = True
    ajax_poll_interval = 10000
    classes = ['no-progress-bar']
    TAG_SERVICE = {'nova': api.nova.geo_tag_show,
                   'cinder': api.cinder.geo_tag_show}
                       
    def get_data(self, request, geotag_id):
        service_type = geotag_id.split("#")[1]
        tag_id = geotag_id.split("#")[0]
        try:
            geo_tag = UpdateRow.TAG_SERVICE[service_type](request, tag_id)
            setattr(geo_tag, 'service_type', service_type)
            return geo_tag
        except Exception as e:
            messages.error(request, e)


def get_service_type(geotag):
    cinder_template_name = 'admin/geotags/_cinder_service_type.html'
    nova_template_name = 'admin/geotags/_nova_service_type.html'
    if geotag.service_type == 'cinder':
        return template.loader.render_to_string(cinder_template_name)
    else:
        return template.loader.render_to_string(nova_template_name)


def get_rack_slot(geotag):
    if geotag.loc_or_error_msg:
        data = geotag.loc_or_error_msg.split(constants.ROOM_ROW_RACK_DELIMITER)
        return "DC: " + data[0] + ", Room: " + data[1] + ", Row: " + data[2] + ", Rack: " + data[3] + ", Slot: " + data[4]
    return '---'

@memoized.memoized_method
def get_country_from_lat_long(lat, longitude):
    try:
        data = json.load(urllib2.urlopen('https://maps.googleapis.com/maps/api/geocode/json?latlng=%s,%s&sensor=false'
                                     % (lat, longitude)))
    except Exception as e:
        LOG.error('cannot get country code %s' % e)
        data = None
    
    if not data or not data.get('results'):
        return '---'
    

    for result in data['results']:
        for component in result['address_components']:
            if 'country' in component['types']:
                return component['long_name']<commit_msg>Added account key on google maps so we have better limits on geocoding.<commit_after>#    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 logging

from django.core.urlresolvers import reverse
from django import template
from django.utils.translation import ugettext_lazy as _

from horizon import messages
from horizon import tables
from horizon.utils import memoized

import json
import urllib2

from openstack_dashboard import api
from openstack_dashboard.dashboards.admin.geotags \
    import constants

LOG = logging.getLogger(__name__)

class UpdateRow(tables.Row):
    ajax = True
    ajax_poll_interval = 10000
    classes = ['no-progress-bar']
    TAG_SERVICE = {'nova': api.nova.geo_tag_show,
                   'cinder': api.cinder.geo_tag_show}
                       
    def get_data(self, request, geotag_id):
        service_type = geotag_id.split("#")[1]
        tag_id = geotag_id.split("#")[0]
        try:
            geo_tag = UpdateRow.TAG_SERVICE[service_type](request, tag_id)
            setattr(geo_tag, 'service_type', service_type)
            return geo_tag
        except Exception as e:
            messages.error(request, e)


def get_service_type(geotag):
    cinder_template_name = 'admin/geotags/_cinder_service_type.html'
    nova_template_name = 'admin/geotags/_nova_service_type.html'
    if geotag.service_type == 'cinder':
        return template.loader.render_to_string(cinder_template_name)
    else:
        return template.loader.render_to_string(nova_template_name)


def get_rack_slot(geotag):
    if geotag.loc_or_error_msg:
        data = geotag.loc_or_error_msg.split(constants.ROOM_ROW_RACK_DELIMITER)
        return "DC: " + data[0] + ", Room: " + data[1] + ", Row: " + data[2] + ", Rack: " + data[3] + ", Slot: " + data[4]
    return '---'

@memoized.memoized_method
def get_country_from_lat_long(lat, longitude):
    try:
        data = json.load(urllib2.urlopen('https://maps.googleapis.com/maps/api/geocode/json?latlng=%s,%s&sensor=false&key=<KEY>YSHvJuVphuwbNtOYibicstAo_zVE'
                                     % (lat, longitude)))
    except Exception as e:
        LOG.error('cannot get country code %s' % e)
        data = None
    
    if not data or not data.get('results'):
        return '---'
    

    for result in data['results']:
        for component in result['address_components']:
            if 'country' in component['types']:
                return component['long_name']
{'fraction_non_alphanumeric': 0.09692307692307692, 'fraction_numerical': 0.006153846153846154, 'mean_word_length': 3.013031550068587, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 2, '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': '6536822', 'n_tokens_mistral': 1828, 'n_tokens_neox': 1793, 'n_words': 471}