Пример #1
0
    def test_working_time(self):
        now = TIMEZONE.localize(datetime(2019, 3, 19, 14, 30, 45, 12))

        result = get_working_datetime(now)

        self.assertEqual(result,
                         TIMEZONE.localize(datetime(2019, 3, 19, 14, 31)))
Пример #2
0
    def test_custom_working_time(self):
        now = TIMEZONE.localize(datetime(2019, 3, 19, 16, 30, 45, 12))

        custom_work_time = dict(
            start=(7, 30),
            end=(16, 0),
        )
        result = get_working_datetime(now, custom_wd=custom_work_time)

        self.assertEqual(result,
                         TIMEZONE.localize(datetime(2019, 3, 20, 7, 30)))
Пример #3
0
def check_for_response_file(self, request_data, supplier, request_time,
                            requests_reties):

    days_passed = working_days_count_since(request_time,
                                           working_weekends_enabled=True)

    if days_passed > WORKING_DAYS_BEFORE_REQUEST_AGAIN:

        if requests_reties < REQUEST_MAX_RETRIES:
            prepare_receipt_request.delay(supplier=supplier,
                                          requests_reties=requests_reties + 1)
            logger.warning(
                "Request retry scheduled",
                extra={"MESSAGE_ID": "FISCAL_REQUEST_RETRY_SCHEDULED"})
        else:
            logger.warning("Additional requests number {} exceeded".format(
                REQUEST_MAX_RETRIES),
                           extra={"MESSAGE_ID": "FISCAL_REQUEST_RETRY_EXCEED"})

    else:
        try:
            response = requests.post(
                '{}/cabinet/public/api/exchange/kvt_by_id'.format(
                    FISCAL_API_HOST),
                timeout=(CONNECT_TIMEOUT, READ_TIMEOUT),
                proxies=FISCAL_API_PROXIES,
                data=request_data)
        except RETRY_REQUESTS_EXCEPTIONS as e:
            logger.exception(
                e, extra={"MESSAGE_ID": "FISCAL_API_CHECK_RESPONSE_ERROR"})
            raise self.retry(exc=e)
        else:

            if response.status_code != 200:
                logger.error(
                    "Unsuccessful status code: {} {}".format(
                        response.status_code, response.text),
                    extra={"MESSAGE_ID": "FISCAL_API_CHECK_RESPONSE_ERROR"})
                self.retry(countdown=response.headers.get(
                    'Retry-After', DEFAULT_RETRY_AFTER))
            else:
                data = response.json()

                kvt_list = data.get("kvtList") or []
                if data.get("status") != "OK" or not any(
                        kv.get("finalKvt") for kv in kvt_list):
                    for kv in kvt_list:  # strip file content for logger
                        if isinstance(kv, dict) and "kvtBase64" in kv:
                            kv["kvtBase64"] = "{}...".format(
                                kv["kvtBase64"][:10])
                    logger.warning("Unsuccessful: {}".format(data),
                                   extra={
                                       "MESSAGE_ID":
                                       "FISCAL_API_CHECK_UNSUCCESSFUL_RESPONSE"
                                   })

                    #  schedule next check on work time
                    eta = get_working_datetime(
                        get_now() + timedelta(seconds=60 * 60),
                        custom_wd=WORKING_TIME,
                        working_weekends_enabled=True,
                    )
                    raise self.retry(eta=eta)

                else:
                    for kvt in data["kvtList"]:
                        if kvt["finalKvt"]:
                            decode_and_save_data.delay(kvt["kvtFname"],
                                                       kvt["kvtBase64"],
                                                       supplier["tender_id"],
                                                       supplier["award_id"])
                            logger.info(
                                "Found kvt file: {}".format({
                                    k: v
                                    for k, v in kvt.items() if k != "kvtBase64"
                                }),
                                extra={"MESSAGE_ID": "FISCAL_API_KVT_FOUND"})
Пример #4
0
def send_request_receipt(self, request_data, filename, supplier,
                         requests_reties):
    task_args = supplier, requests_reties
    data = get_task_result(self, task_args)
    if data is None:
        try:
            response = requests.post(
                '{}/cabinet/public/api/exchange/report'.format(
                    FISCAL_API_HOST),
                proxies=FISCAL_API_PROXIES,
                json=[{
                    'contentBase64': request_data,
                    'fname': filename
                }])
        except RETRY_REQUESTS_EXCEPTIONS as e:
            logger.exception(
                e, extra={"MESSAGE_ID": "FISCAL_API_POST_REQUEST_ERROR"})
            raise self.retry(exc=e)
        else:
            if response.status_code != 200:
                logger.error(
                    "Unsuccessful status code: {} {}".format(
                        response.status_code, response.text),
                    extra={"MESSAGE_ID": "FISCAL_API_POST_REQUEST_ERROR"})
                self.retry(countdown=response.headers.get(
                    'Retry-After', DEFAULT_RETRY_AFTER))
            else:
                data = response.json()

                if data["status"] != "OK":
                    logger.error(
                        "Getting receipt failed: {} {}".format(
                            response.status_code, response.text),
                        extra={"MESSAGE_ID": "FISCAL_API_POST_REQUEST_ERROR"})
                    return
                else:
                    uid = save_task_result(self, data, task_args)
                    logger.info(
                        "Receipt requested successfully: {} {} {}; saved result: {}"
                        .format(response.status_code, data["id"],
                                data["kvt1Fname"], uid),
                        extra={
                            "MESSAGE_ID": "FISCAL_API_POST_REQUEST_SUCCESS"
                        })

    decode_and_save_data.apply_async(kwargs=dict(
        name=data["kvt1Fname"],
        data=data["kvt1Base64"],
        tender_id=supplier["tender_id"],
        award_id=supplier["award_id"],
    ))

    # response check should be after an hour
    # also later we will need to know how many working days have passed since now including this
    # one (if it's a working day)
    now = get_now()
    check_response_time = get_working_datetime(now +
                                               timedelta(seconds=60 * 60),
                                               custom_wd=WORKING_TIME,
                                               working_weekends_enabled=True)
    prepare_check_request.apply_async(eta=check_response_time,
                                      kwargs=dict(
                                          uid=data["id"],
                                          supplier=supplier,
                                          request_time=now,
                                          requests_reties=requests_reties,
                                      ))

    logger.info("Fiscal receipt check of {} scheduled at {}".format(
        data["id"], check_response_time),
                extra={"MESSAGE_ID": "FISCAL_API_CHECK_SCHEDULE"})
Пример #5
0
    def test_working_weekends_enabled(self):
        now = TIMEZONE.localize(datetime(2019, 6, 7, 18, 30))

        result = get_working_datetime(now, working_weekends_enabled=True)

        self.assertEqual(result, TIMEZONE.localize(datetime(2019, 6, 8, 9)))
Пример #6
0
    def test_holiday(self):
        now = TIMEZONE.localize(datetime(2019, 3, 8, 12, 30))

        result = get_working_datetime(now)

        self.assertEqual(result, TIMEZONE.localize(datetime(2019, 3, 11, 9)))
Пример #7
0
    def test_sunday(self):
        now = TIMEZONE.localize(datetime(2019, 3, 24, 6, 30))

        result = get_working_datetime(now)

        self.assertEqual(result, TIMEZONE.localize(datetime(2019, 3, 25, 9)))
Пример #8
0
    def test_evening(self):
        now = TIMEZONE.localize(datetime(2019, 3, 19, 18, 0, 0, 1))

        result = get_working_datetime(now)

        self.assertEqual(result, TIMEZONE.localize(datetime(2019, 3, 20, 9)))
Пример #9
0
    def test_morning(self):
        now = TIMEZONE.localize(datetime(2019, 3, 19, 6, 30, 1, 1))

        result = get_working_datetime(now)

        self.assertEqual(result, TIMEZONE.localize(datetime(2019, 3, 19, 9)))