示例#1
0
 def __init__(
     self,
     output: IO,
     envelope_id: int,
     message_counter: Counter = counter_generator(),
     max_envelope_size: Optional[int] = None,
     format: str = "xml",
     newline: bool = False,
 ) -> None:
     """
     :param output: The output stream to write to.
     :param envelope_id: The id of the envelope.
     :param message_counter: A counter for the message ids.
     :param max_envelope_size: The maximum size of an envelope, if None then no limit.
     :param format: Format to serialize to, defaults to xml.
     :param newline: Whether to add a newline after the envelope.
     """
     self.output = output
     self.message_counter = message_counter
     self.envelope_id = envelope_id
     self.envelope_size = 0
     self.max_envelope_size = max_envelope_size
     if (max_envelope_size is not None and
             self.max_envelope_size < EnvelopeSerializer.MIN_ENVELOPE_SIZE):
         raise ValueError(
             f"Max envelope size {max_envelope_size} is too small, it should be at least {EnvelopeSerializer.MIN_ENVELOPE_SIZE}.",
         )
     self.format = format
     self.newline = newline
示例#2
0
 def __init__(
     self,
     output: IO,
     envelope_id: int,
     transaction_counter: Counter = counter_generator(),
     message_counter: Counter = counter_generator(),
     max_envelope_size: Optional[int] = None,
     format: str = "xml",
     newline: bool = False,
 ) -> None:
     self.output = output
     self.message_counter = message_counter
     self.envelope_id = envelope_id
     self.envelope_size = 0
     self.max_envelope_size = max_envelope_size
     if (max_envelope_size is not None and
             self.max_envelope_size < EnvelopeSerializer.MIN_ENVELOPE_SIZE):
         raise ValueError(
             f"Max envelope size {max_envelope_size} is too small, it should be at least {EnvelopeSerializer.MIN_ENVELOPE_SIZE}.",
         )
     self.format = format
     self.newline = newline
示例#3
0
文件: util.py 项目: kintisheff/tamato
def generate_test_import_xml(obj: dict) -> BytesIO:

    xml = render_to_string(
        template_name="workbaskets/taric/transaction_detail.xml",
        context={
            "envelope_id": next(_transaction_counter),
            "tracked_models": [obj],
            "transaction_id": next(_transaction_counter),
            "message_counter": counter_generator(),
            "counter_generator": counter_generator,
        },
    )

    return BytesIO(xml.encode())
示例#4
0
文件: util.py 项目: uktrade/tamato
def generate_test_import_xml(
    objects: Sequence[dict],
    transaction_id: Optional[int] = None,
) -> BytesIO:
    last_transaction = Transaction.objects.last()
    next_transaction_id = (last_transaction.order
                           if last_transaction else 0) + 1
    xml = render_to_string(
        template_name="workbaskets/taric/transaction_detail.xml",
        context={
            "envelope_id": next_transaction_id,
            "tracked_models": objects,
            "transaction_id":
            next_transaction_id if transaction_id is None else transaction_id,
            "message_counter": counter_generator(),
            "counter_generator": counter_generator,
        },
    )

    return BytesIO(xml.encode())
示例#5
0
    def move_to_end_of_partition(self, partition) -> None:
        """
        Update Transaction partition and order fields to place them at the end
        of the specified partition.

        Transaction order is updated to be contiguous.
        """

        transactions = self.order_by("partition", "order")

        # Ensure order of the transactions in this query to start at end of the partition.
        # The order_by here is redundant - as it's the natural order of Transaction,
        # but it's included for clarity.
        existing_tx = (
            self.model.objects.order_by("order")
            .filter(
                partition=partition,
            )
            .exclude(pk__in=transactions.values_list("pk", flat=True))
            .last()
        )
        order_start = existing_tx.order + 1 if existing_tx else 1

        logger.debug(
            "Update transactions in query starting from %s "
            "to start after transaction %s. order_start: %s",
            transactions.first().pk,
            existing_tx.pk if existing_tx else None,
            order_start,
        )

        counter = counter_generator(start=order_start)

        for tx in transactions:
            tx.order = counter()
            tx.partition = partition

        self.model.objects.bulk_update(transactions, ["partition", "order"])
示例#6
0
 def measure_condition_sid_counter(self) -> Counter:
     last_sid = MeasureCondition.objects.values("sid").order_by(
         "sid").last()
     next_sid = 1 if last_sid is None else last_sid["sid"] + 1
     return counter_generator(next_sid)
示例#7
0
 def start_next_envelope(self) -> None:
     """Update any data ready ready for outputting the next envelope."""
     self.envelope_id += 1
     self.message_counter = counter_generator(start=1)