Last active
January 13, 2022 13:04
-
-
Save dmop/b740d0a4d253d05f8df2ef467f70ac68 to your computer and use it in GitHub Desktop.
Fkp to force creation of new Redispatch order from the given id
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from datetime import datetime | |
| from typing import List | |
| from players.models import LoggiUser | |
| from proto.integration.api.label.v1.label_pb2 import LABEL_RESPONSE_TYPE_URL | |
| from logging import getLogger | |
| from base.db.router import in_eventual_consistence_database, in_read_only_database | |
| from proto.loggi_web.v1.api_pb2 import GetOrCreateRedispatchLabelResponse | |
| from wms.redispatch.redispatch_client import RedispatchClient | |
| from wms.redispatch.repository import ( | |
| get_package_by_id | |
| ) | |
| logger = getLogger(__name__) | |
| def create_new_redispatch( | |
| package_id: int, | |
| posting_card_number: str, | |
| distribution_center_id: int | |
| ) -> GetOrCreateRedispatchLabelResponse: | |
| client = RedispatchClient() | |
| pkg = get_package_by_id(package_id) | |
| created_by_id = pkg.redispatches.last().created_by_id | |
| response, label_error = client.get_or_create_redispatch_label( | |
| package=pkg, | |
| distribution_center_id=distribution_center_id, | |
| posting_card_number=posting_card_number, | |
| force_create=True, | |
| response_type=LABEL_RESPONSE_TYPE_URL, | |
| ) | |
| with in_eventual_consistence_database(): | |
| confirmed_by = LoggiUser.objects.get(id=created_by_id) | |
| pkg.make_sure_is_confirmed(confirmed_at=datetime.now(), | |
| whom=confirmed_by.full_name, | |
| distribution_center_id=distribution_center_id) | |
| client.save_redispatch( | |
| redispatch_response=response.redispatch, | |
| package=pkg, | |
| is_emergency=False, | |
| distribution_center_id=distribution_center_id, | |
| created_by_id=created_by_id, | |
| application_name="xd_app" | |
| ) | |
| with in_read_only_database(): | |
| task = pkg.tasks.latest('created') | |
| pkg.update_status(task) | |
| def force_create_new_redispatches( | |
| package_ids: List[int], | |
| posting_card_number: str, | |
| distribution_center_id: int | |
| ): | |
| pkgs_processed = 0 | |
| for pkg_id in package_ids: | |
| print(f"--------------------------------") | |
| print(f"iniciando -> {pkg_id}") | |
| create_new_redispatch(pkg_id, posting_card_number, distribution_center_id) | |
| pkgs_processed += 1 | |
| print(f"processado {pkgs_processed} -> {pkg_id}") | |
| print(f"--------------------------------") | |
| for pkg_id in package_ids: | |
| pkg = get_package_by_id(pkg_id) | |
| print(f'curl {pkg.redispatches.last().label_url} -o {pkg_id}.pdf') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment