playtender_service.py 14.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
# -*- coding: utf-8 -
import re
import copy
import urllib
import urllib3
import string
import requests

import dateutil.parser
from iso8601 import parse_date
from robot.libraries.BuiltIn import BuiltIn
from datetime import datetime, timedelta
import pytz

TZ = pytz.timezone('Europe/Kiev')


def get_library():
    return BuiltIn().get_library_instance('Selenium2Library')


def get_webdriver_instance():
    return get_library()._current_browser()


# return of variable is None
def get_variable_is_none(variable):
    if variable is None:
        return True
    return False


# run specified keyword if condition is not none type
def run_keyword_if_condition_is_not_none(condition, name, *args):
    if get_variable_is_none(condition) == False:
        BuiltIn().run_keyword(name, *args)


# run specified keyword if condition is none type
def run_keyword_if_condition_is_none(condition, name, *args):
    if get_variable_is_none(condition) == True:
        BuiltIn().run_keyword(name, *args)


# return value for *keys (nested) in `element` (dict).
def get_from_dictionary_by_keys(element, *keys):
    if not isinstance(element, dict):
        raise AttributeError('keys_exists() expects dict as first argument.')
    if len(keys) == 0:
        raise AttributeError('keys_exists() expects at least two arguments, one given.')

    _element = element
    for key in keys:
        try:
            _element = _element[key]
        except KeyError:
            return None
    return _element


# returns if element exists on page. optimization
def get_is_element_exist(locator):
    jquery_locator = convert_locator_to_jquery(locator)
    if get_variable_is_none(jquery_locator) == False:
        jquery_locator = jquery_locator.replace('"', '\\"')
        length = get_webdriver_instance().execute_script('return $("' + jquery_locator + '").length;')
        return length > 0

    try:
        get_library()._element_find(locator, None, True)
    except Exception:
        return False
    return True


# click
def js_click_element(locator):
    element = get_library()._element_find(locator, None, True)
    get_webdriver_instance().execute_script(
        'var $el = jQuery(arguments[0]); if($el.length) $el.click();',
        element
    )


# convert locator to jquery locator
def convert_locator_to_jquery(locator):
    locator_params = locator.split('=', 1)
    if locator_params[0] == 'id':
        return '#' + locator_params[1]
    if locator_params[0] == 'jquery':
        return locator_params[1]
    if locator_params[0] == 'css':
        return locator_params[1]
    return None


# set scroll to element in view
def set_element_scroll_into_view(locator):
    element = get_library()._element_find(locator, None, True)
    get_webdriver_instance().execute_script(
        'var $el = jQuery(arguments[0]); if($el.length) $el.get(0).scrollIntoView();',
        element
    )


# return text/value by specified locator
def get_value_by_locator(locator):
    element = get_library()._element_find(locator, None, True)
    text = get_webdriver_instance().execute_script(
        'var $element = jQuery(arguments[0]);'
        'if($element.is("input[type=checkbox]")) return $element.is(":checked") ? "1":"0";'
        'if($element.is("input,textarea,select")) return $element.val();'
        'return $element.text();',
        element
    )
    return text


# input text to hidden input
def input_text_to_hidden_input(locator, text):
    element = get_library()._element_find(locator, None, True)
    get_webdriver_instance().execute_script(
        'jQuery(arguments[0]).val("' + text.replace('"', '\\"') + '");',
        element
    )


# select option by label for hidden select
def select_from_hidden_list_by_label(locator, label):
    element = get_library()._element_find(locator, None, True)
    get_webdriver_instance().execute_script(
        'var $option = jQuery("option:contains(' + label.replace('"', '\\"') + ')", arguments[0]);' +
        'if($option.length) jQuery(arguments[0]).val($option.attr("value"));',
        element
    )


# trigger change event for input by locator
def trigger_input_change_event(locator):
    element = get_library()._element_find(locator, None, True)
    get_webdriver_instance().execute_script(
        'var $el = jQuery(arguments[0]); if($el.length) $el.trigger("change");',
        element
    )


# convert all numners to string
def convert_float_to_string(number):
    return repr(float(number))


def convert_esco__float_to_string(number):
    return '{0:.5f}'.format(float(number))


def convert_float_to_string_3f(number):
    return '{0:.3f}'.format(float(number))


# convert any variable to specified type
def convert_to_specified_type(value, type):
    value = "%s" % (value)
    if type == 'integer':
        value = value.split()
        value = ''.join(value)
        print(value)
        value = int(value)
    if type == 'float':
        value = value.split()
        value = ''.join(value)
        print(value)
        value = float(value)
    return value


# prepare isodate in needed format
def isodate_format(isodate, format):
    iso_dt = parse_date(isodate)
    return iso_dt.strftime(format)


def procuring_entity_name(tender_data):
    tender_data.data.procuringEntity['name'] = u"ТОВ \"ПлейТендер\""
    tender_data.data.procuringEntity['name_en'] = u"TOV \"playtender\""
    tender_data.data.procuringEntity.identifier['id'] = u"1234567890-playtender"
    tender_data.data.procuringEntity.identifier['legalName'] = u"ТОВ \"ПлейТендер\""
    tender_data.data.procuringEntity.identifier['legalName_en'] = u"TOV \"playtender\""
    if 'address' in tender_data.data.procuringEntity:
        tender_data.data.procuringEntity.address['region'] = u"м. Київ"
        tender_data.data.procuringEntity.address['postalCode'] = u"123123"
        tender_data.data.procuringEntity.address['locality'] = u"Київ"
        tender_data.data.procuringEntity.address['streetAddress'] = u"address"
    if 'contactPoint' in tender_data.data.procuringEntity:
        tender_data.data.procuringEntity.contactPoint['name'] = u"Test ЗамовникОборони"
        tender_data.data.procuringEntity.contactPoint['name_en'] = u"Test"
        tender_data.data.procuringEntity.contactPoint['email'] = u"chuzhin@mail.ua"
        tender_data.data.procuringEntity.contactPoint['telephone'] = u"+3801111111111"
        tender_data.data.procuringEntity.contactPoint['url'] = u"http://playtender.com.ua"
    if 'buyers' in tender_data.data:
        tender_data.data.buyers[0]['name'] = u"ТОВ \"ПлейТендер\""
        tender_data.data.buyers[0].identifier['id'] = u"1234567890-playtender"
        tender_data.data.buyers[0].identifier['legalName'] = u"ТОВ \"ПлейТендер\""
    return tender_data


# prepare data
def prepare_procuring_entity_data(data):
    try:
        data['name'] = u"Playtender"
        data.identifier['id'] = u"playtender"
        data.identifier['legalName'] = u"Playtender"
        data.identifier['scheme'] = u"UA-EDR"
        if 'name_en' in data:
            data['name_en'] = u"Playtender"
        if 'legalName_en' in data.identifier:
            data.identifier['legalName_en'] = u"Playtender"
        if 'address' in data:
            data.address['countryName'] = u"Україна"
            data.address['locality'] = u"Київ"
            data.address['postalCode'] = u"01111"
            data.address['region'] = u"місто Київ"
            data.address['streetAddress'] = u"вулиця Тестова, 220, 8"
        if 'contactPoint' in data:
            data.contactPoint['email'] = u"chuzhin@mail.ua"
            data.contactPoint['faxNumber'] = u"+3801111111111"
            data.contactPoint['telephone'] = u"+3801111111111"
            data.contactPoint['name'] = u"Test"
            if 'name_en' in data.contactPoint:
                data.contactPoint['name_en'] = u"Test"
            data.contactPoint['url'] = u"https://playtender.com.ua"
    except Exception:
        raise Exception('data is not a dictionary')


# prepare data
def prepare_buyers_data(data):
    if type(data) is not list:
        raise Exception('data is not a list')

    # preventing console errors about changing buyer data in cases
    if len(data) != 1:
        return

    item = next(iter(data), None)
    item['name'] = u"Playtender"
    item.identifier['id'] = u"playtender"
    item.identifier['legalName'] = u"Playtender"
    item.identifier['scheme'] = u"UA-EDR"


# prepare dictionary from field path + value
def generate_dictionary_from_field_path_and_value(path, value):
    data = dict()
    path_keys_list = path.split('.')
    if len(path_keys_list) > 1:
        key = path_keys_list.pop(0)
        value = generate_dictionary_from_field_path_and_value('.'.join(path_keys_list), value)
        indexRegex = re.compile(r'(\[(\d+)\]$)')
        matchObj = indexRegex.search(key)
        print
        matchObj
        if matchObj:
            key = indexRegex.sub('', key)
            value['list_index'] = matchObj.group(2)
            value = [value]
        data[key] = value
    else:
        data = dict()
        data[path] = value
    return data


# Percentage conversion
def multiply_hundred(number):
    return number * 100


# prepares data for filling form in easiest way
def prepare_tender_data(data_original):
    # preventing change data in global view
    data = copy.deepcopy(data_original)

    # check if data is for multilot
    if 'lots' not in data:
        return data

    # moves features to its related items
    if 'features' in data:
        i = 0
        l = len(data['features'])
        while i < l:
            if data['features'][i]['featureOf'] == 'lot':
                for lot in data['lots']:
                    if lot['id'] == data['features'][i]['relatedItem']:
                        if 'features' not in lot:
                            lot['features'] = []
                        lot['features'].append(data['features'].pop(i))
                        l = l - 1
                        i = i - 1
                        break
            if data['features'][i]['featureOf'] == 'item':
                for item in data['items']:
                    if item['id'] == data['features'][i]['relatedItem']:
                        if 'features' not in item:
                            item['features'] = []
                        item['features'].append(data['features'].pop(i))
                        l = l - 1
                        i = i - 1
                        break
            i = i + 1

    if 'features' in data:
        if len(data['features']) == 0:
            del data['features']

    # moves items to its related lots
    i = 0
    l = len(data['items'])
    while i < l:
        for lot in data['lots']:
            if lot['id'] == data['items'][i]['relatedLot']:
                if 'items' not in lot:
                    lot['items'] = []
                lot['items'].append(data['items'].pop(i))
                l = l - 1
                i = i - 1
                break
        i = i + 1

    del data['items']

    if 'milestones' not in data:
        return data
    # moves milestones to its related lots
    i = 0
    l = len(data['milestones'])
    while i < l:
        for lot in data['lots']:
            if lot['id'] == data['milestones'][i]['relatedLot']:
                if 'milestones' not in lot:
                    lot['milestones'] = []
                lot['milestones'].append(data['milestones'].pop(i))
                l = l - 1
                i = i - 1
                break
        i = i + 1

    del data['milestones']

    return data


def split_agreementDuration(str, type):
    if type in 'year':
        year_temp = str.split('Y', 1)
        value = year_temp[0].split('P', 1)
    elif type in 'month':
        month_temp = str.split('M', 1)
        value = month_temp[0].split('Y', 1)
    else:
        day_temp = str.split('D', 1)
        value = day_temp[0].split('M', 1)
    return value[1]


def convert_date_to_string_contr(date):
    date = dateutil.parser.parse(date)
    date = date.strftime("%d.%m.%Y %H:%M:%S")
    return date


def get_value_minimalStepPercentage(value):
    value = value / 100
    return value


def set_value_minimalStepPercentage(value):
    value = value * 100
    return value


def convert_esco__float_to_string(number):
    return '{0:.5f}'.format(float(number))


def convert_string_to_float(number):
    return float(number)


def download_file(url, file_name, output_dir):
    urllib.urlretrieve(url, ('{}/{}'.format(output_dir, file_name)))


def parse_complaintPeriod_date(date_string):
    date_str = datetime.strptime(date_string, "%d.%m.%Y %H:%M")
    date = datetime(date_str.year, date_str.month, date_str.day, date_str.hour, date_str.minute, date_str.second,
                    date_str.microsecond)
    date = TZ.localize(date).isoformat()
    return date


def parse_deliveryPeriod_date1(date):
    date = dateutil.parser.parse(date)
    date = date.strftime("%d.%m.%Y")
    return date


def parse_deliveryPeriod_date(date_string):
    #    date_str = datetime.strptime(date_string, "%Y-%m-%dT%H:%M:%S+03:00")
    if '+03' in date_string:
        date_str = datetime.strptime(date_string, "%Y-%m-%dT%H:%M:%S+03:00")
    else:
        date_str = datetime.strptime(date_string, "%Y-%m-%dT%H:%M:%S+02:00")
    date = datetime(date_str.year, date_str.month, date_str.day)
    date = date.strftime("%d.%m.%Y")
    return date


def split_joinvalue(str_value):
    str_value = str_value.split()
    str_value = ''.join(str_value)
    print(str_value)
    str_value.replace(" ", "")
    return str_value


def doGetRequest(url, data):
    r = requests.get(url, params=data)
    response = {}
    response['status_code'] = r.status_code
    response['reason'] = r.reason
    response['content'] = r.content
    response['json'] = r.json()
    return response


def getTenderDataByTenderUaId(apiBaseUrl, tenderUaId):
    requestData = {"opt_fields": "tenderID", "descending": "1", "mode": "_all_"}
    updatesResponse = doGetRequest(apiBaseUrl.strip("/") + "/tenders", data=requestData)
    if "json" not in updatesResponse:
        print('json key not found')
        return {}
    tenderId = None
    print("lastTenders:")
    print(updatesResponse['json']['data'])
    for item in updatesResponse['json']['data']:
        if tenderUaId == item['tenderID']:
            tenderId = item['id']
            break
    if tenderId is None:
        return {}
    tenderDataResponse = doGetRequest(apiBaseUrl.strip("/") + "/tenders/" + tenderId, data={})
    return tenderDataResponse['json']