from django.utils.deprecation import MiddlewareMixin
from .pos import PosSys
from .storefront import StoreFront

class PosMiddleware(MiddlewareMixin):
    def process_request(self, request):
        if not hasattr(request, "user"):
            raise ImproperlyConfigured(
                "The Django POS middleware requires auth middleware")
        user = request.user
        if user.is_authenticated:
            if not hasattr(request, 'pos') or request.pos is None:
                pos = PosSys.restoreStore(user)
                request.pos = pos
        else:
            request.pos = None

        cstr  = request.GET.get('cstr', None)
        if cstr:
            request.storeFront = StoreFront(int(cstr))
        else:
            request.storeFront = None
