from django.utils import timezone

from oscar.apps.order.utils import OrderNumberGenerator as CoreOrderNumberGenerator

from ocyan.core.fender import config

from ocyan.plugin.oscar_sequential_order_numbers.models import Sequence


class OrderNumberGenerator(CoreOrderNumberGenerator):
    def order_number(self, basket):
        obj = Sequence.objects.create()
        datetime = timezone.now()
        order_number_pattern = config.get(
            "oscar sequential order numbers", "order_number_pattern", "{number}"
        )
        order_number_pattern_with_date = datetime.strftime(order_number_pattern)
        return order_number_pattern_with_date.format(number=obj.pk)
