openfun / joanie

πŸ‘› A headless ERP for education to manage course enrollment/subscription, payment and certificates delivery.
MIT License
20 stars 2 forks source link

πŸ›(backend) contract context for the course section data as strings (price, effort, course start, course end) #774

Closed jonathanreveille closed 2 months ago

jonathanreveille commented 2 months ago

On the contract template, the hours and the dates were not rendered as expected once it was generated.

To avoid facing this situation, we now transform those values of dates and duration into iso 8601 string format before returning the context. We want the value to be prepared at their creation before adding it to the contract.

Overall, we had to update the : course_start, course_end, course_effort and course_price.

Purpose

Make sure that our template tags for date and duration iso 8601 will work as expected into the template rendering. They are both waiting for the incoming input value to be strings and not datetime.datetime, or datetime.date, and not datetime.timedelta.

Proposal

Since we prepare the context that way it should be saved into the contract.context, we should also make sure the "order.total" is the string of the Decimal value, else this error comes up if we don't force the order.price to string representation in the generation of the context.

self = <json.encoder.JSONEncoder object at 0x711ffe726cb0>, o = Decimal('999.99')

    def default(self, o):
        """Implement this method in a subclass such that it returns
        a serializable object for ``o``, or calls the base implementation
        (to raise a ``TypeError``).

        For example, to support arbitrary iterators, you could
        implement default like this::

            def default(self, o):
                try:
                    iterable = iter(o)
                except TypeError:
                    pass
                else:
                    return list(iterable)
                # Let the base class default method raise the TypeError
                return JSONEncoder.default(self, o)

        """
>       raise TypeError(f'Object of type {o.__class__.__name__} '
                        f'is not JSON serializable')
E       TypeError: Object of type Decimal is not JSON serializable