def wrapper(self, *args, **kwargs): # Looking for request object for element in args: if isinstance(element, Request): # Found it request = element break # Getting session/user information for metacart retrieval session_key = request.session.session_key user = request.user # Getting metacart metacart = None if user.is_anonymous() and session_key is not None: # If not logged in, get metacart by session id metacart = MetaCart.objects.filter( session__session_key=session_key) if len(metacart) > 0: # Metacart retrieved metacart = metacart[0] else: # No metacart set for this session, create one and associate it to session metacart = MetaCart(session=Session.objects.get( session_key=session_key)) metacart.save() elif not user.is_anonymous(): # User is authenticated, retrieve metacart metacart = MetaCart.objects.get(user=user) # Setting cart cart_controller = DallizCartController(osm=metacart.current_osm, metacart=metacart) request.cart_controller = cart_controller # Default osm osm = { 'name': metacart.current_osm, 'type': 'shipping', 'location': None, } if request.method in ['GET', 'POST']: parameters = getattr(request, request.method) if 'osm_name' in parameters: osm['name'] = parameters['osm_name'] if 'osm_type' in parameters: osm['type'] = parameters['osm_type'] if 'osm_location' in parameters: osm['location'] = parameters['osm_location'] kwargs.update(dict([('osm_%s' % k, v) for k, v in osm.iteritems()])) data = function(self, *args, **kwargs) if not isinstance(data, Response): return Response(data) return data
def wrapper( self , *args, **kwargs): # Looking for request object for element in args: if isinstance(element, Request): # Found it request = element break # Getting session/user information for metacart retrieval session_key = request.session.session_key user = request.user # Getting metacart metacart = None if user.is_anonymous() and session_key is not None: # If not logged in, get metacart by session id metacart = MetaCart.objects.filter(session__session_key = session_key) if len(metacart)>0: # Metacart retrieved metacart = metacart[0] else: # No metacart set for this session, create one and associate it to session metacart = MetaCart(session = Session.objects.get(session_key = session_key)) metacart.save() elif not user.is_anonymous(): # User is authenticated, retrieve metacart metacart = MetaCart.objects.get(user = user) # Setting cart cart_controller = DallizCartController(osm = metacart.current_osm, metacart = metacart) request.cart_controller = cart_controller # Default osm osm = { 'name':metacart.current_osm, 'type':'shipping', 'location':None, } if request.method in ['GET', 'POST']: parameters = getattr(request, request.method) if 'osm_name' in parameters: osm['name'] = parameters['osm_name'] if 'osm_type' in parameters: osm['type'] = parameters['osm_type'] if 'osm_location' in parameters: osm['location'] = parameters['osm_location'] kwargs.update(dict([ ( 'osm_%s'%k , v) for k,v in osm.iteritems()])) data = function( self , *args, **kwargs) if not isinstance(data, Response): return Response(data) return data
def __init__(self, osm='monoprix', metacart=None): if osm is None: raise ErrorNoOSMSet(osm) elif osm in DallizCartController.AVAILABLE_OSMS.keys(): self.osm = osm else: raise ErrorNoOSMSet(osm) if metacart is not None: self.metacart = metacart else: self.metacart = MetaCart(current_osm=self.osm) self.metacart.save() self.carts = {} for osm, value in DallizCartController.AVAILABLE_OSMS.iteritems(): current_cart = getattr(self.metacart, osm + '_cart') cart_controller = value['class'](cart=current_cart) self.carts[osm] = cart_controller if current_cart is None: setattr(self.metacart, osm + '_cart', cart_controller.cart) self.metacart.save()
def post(self, request): if 'username' in request.POST and 'password' in request.POST: # login user username = request.POST['username'] password = request.POST['password'] user = authenticate(username=username, password=password) if user is not None: login(request, user) data = UserSerializer(user).data # Now checking if user is associated with meta cart metacart = MetaCart.objects.filter(user=user) if metacart.count() > 0: # Metacart retrieved, ok nothing to do pass else: # No metacart set for this user, check if the current session is associated with metacart metacart = MetaCart.objects.filter(session=request.session) if metacart.count() > 0: # Metacart retrieved, associate it with user metacart = metacart[0] metacart.user = user metacart.save() else: # No cart was found, create one and associate it with user metacart = MetaCart(user=user) metacart.save() else: raise AuthenticationFailed return Response(data) else: # logout user logout(request) return Response({})
def post(self, request): if 'username' in request.POST and 'password' in request.POST: # login user username = request.POST['username'] password = request.POST['password'] user = authenticate(username=username, password=password) if user is not None: login(request, user) data = UserSerializer(user).data # Now checking if user is associated with meta cart metacart = MetaCart.objects.filter(user = user) if metacart.count()>0: # Metacart retrieved, ok nothing to do pass else: # No metacart set for this user, check if the current session is associated with metacart metacart = MetaCart.objects.filter(session = request.session) if metacart.count()>0: # Metacart retrieved, associate it with user metacart = metacart[0] metacart.user = user metacart.save() else: # No cart was found, create one and associate it with user metacart = MetaCart(user = user) metacart.save() else: raise AuthenticationFailed return Response(data) else: # logout user logout(request) return Response({})
def __init__(self, osm = 'monoprix', metacart = None): if osm is None: raise ErrorNoOSMSet(osm) elif osm in DallizCartController.AVAILABLE_OSMS.keys(): self.osm = osm else: raise ErrorNoOSMSet(osm) if metacart is not None: self.metacart = metacart else: self.metacart = MetaCart(current_osm = self.osm) self.metacart.save() self.carts = {} for osm, value in DallizCartController.AVAILABLE_OSMS.iteritems(): current_cart = getattr(self.metacart, osm+'_cart') cart_controller = value['class'](cart = current_cart) self.carts[osm] = cart_controller if current_cart is None: setattr(self.metacart, osm+'_cart', cart_controller.cart) self.metacart.save()
class DallizCartController(object): """ This class is responsible for """ AVAILABLE_OSMS = { 'ooshop': { 'class': OoshopCartController }, 'monoprix': { 'class': MonoprixCartController }, 'auchan': { 'class': AuchanCartController } } def __init__(self, osm='monoprix', metacart=None): if osm is None: raise ErrorNoOSMSet(osm) elif osm in DallizCartController.AVAILABLE_OSMS.keys(): self.osm = osm else: raise ErrorNoOSMSet(osm) if metacart is not None: self.metacart = metacart else: self.metacart = MetaCart(current_osm=self.osm) self.metacart.save() self.carts = {} for osm, value in DallizCartController.AVAILABLE_OSMS.iteritems(): current_cart = getattr(self.metacart, osm + '_cart') cart_controller = value['class'](cart=current_cart) self.carts[osm] = cart_controller if current_cart is None: setattr(self.metacart, osm + '_cart', cart_controller.cart) self.metacart.save() def osm(function): """ This class ensures that a proper osm is set. """ def wrapper(self, *args, **kwargs): if self.osm is None: ErrorNoOSMSet(self.osm) elif self.osm in DallizCartController.AVAILABLE_OSMS.keys(): result = function(self, *args, **kwargs) return result else: raise ErrorNotProperOSM(self.osm) return wrapper def osm_verification(self, osm): """ Checks if osm is in valid """ return osm in DallizCartController.AVAILABLE_OSMS.keys() def set_osm(self, new_osm): if self.osm_verification(new_osm): self.osm = new_osm self.metacart.current_osm = new_osm self.metacart.save() else: raise ErrorNotProperOSM(new_osm) def set_cart(self, cart, osm): """ Setting a cart to osm """ # First step : setting osm self.set_osm(osm) self.carts[osm] = cart equivalences = {} setattr(self.metacart, osm + '_cart', cart.cart) # We now have to build other carts for other_osm in DallizCartController.AVAILABLE_OSMS.keys(): if other_osm != osm: if self.carts[other_osm] is None: self.carts[ other_osm] = DallizCartController.AVAILABLE_OSMS[ other_osm]['class']() try: setattr(self.metacart, other_osm + '_cart', self.carts[other_osm].cart) except Exception, e: pass equivalences[other_osm] = self.carts[ other_osm].set_equivalent_cart(self.carts[osm].cart) self.metacart.save() # Setting equivalent content for ohter osms for other_osm in DallizCartController.AVAILABLE_OSMS.keys(): if other_osm != osm: for other_osm_bis in DallizCartController.AVAILABLE_OSMS.keys( ): if other_osm_bis != osm and other_osm_bis != other_osm: for content in equivalences[other_osm].keys(): try: other_content = equivalences[other_osm][ content]['content'] except Exception, e: pass try: other_content_bis = equivalences[ other_osm_bis][content]['content'] except Exception, e: pass try: setattr(other_content_bis, other_content.cart.osm + '_content', other_content) except Exception, e: pass try: setattr( content, other_content_bis.cart.osm + '_content', other_content_bis) except Exception, e: pass try: setattr(content, other_content.cart.osm + '_content', other_content) except Exception, e: pass try: other_content_bis.save() except Exception, e: pass
class DallizCartController(object): """ This class is responsible for """ AVAILABLE_OSMS = { 'ooshop': { 'class': OoshopCartController }, 'monoprix': { 'class': MonoprixCartController }, 'auchan': { 'class': AuchanCartController } } def __init__(self, osm = 'monoprix', metacart = None): if osm is None: raise ErrorNoOSMSet(osm) elif osm in DallizCartController.AVAILABLE_OSMS.keys(): self.osm = osm else: raise ErrorNoOSMSet(osm) if metacart is not None: self.metacart = metacart else: self.metacart = MetaCart(current_osm = self.osm) self.metacart.save() self.carts = {} for osm, value in DallizCartController.AVAILABLE_OSMS.iteritems(): current_cart = getattr(self.metacart, osm+'_cart') cart_controller = value['class'](cart = current_cart) self.carts[osm] = cart_controller if current_cart is None: setattr(self.metacart, osm+'_cart', cart_controller.cart) self.metacart.save() def osm(function): """ This class ensures that a proper osm is set. """ def wrapper( self , *args, **kwargs) : if self.osm is None: ErrorNoOSMSet(self.osm) elif self.osm in DallizCartController.AVAILABLE_OSMS.keys(): result = function( self , *args, **kwargs) return result else: raise ErrorNotProperOSM(self.osm) return wrapper def osm_verification(self, osm): """ Checks if osm is in valid """ return osm in DallizCartController.AVAILABLE_OSMS.keys() def set_osm(self, new_osm): if self.osm_verification(new_osm): self.osm = new_osm self.metacart.current_osm = new_osm self.metacart.save() else: raise ErrorNotProperOSM(new_osm) def set_cart(self, cart, osm): """ Setting a cart to osm """ # First step : setting osm self.set_osm(osm) self.carts[osm] = cart equivalences = {} setattr(self.metacart, osm+'_cart', cart.cart) # We now have to build other carts for other_osm in DallizCartController.AVAILABLE_OSMS.keys(): if other_osm != osm: if self.carts[other_osm] is None: self.carts[other_osm] = DallizCartController.AVAILABLE_OSMS[other_osm]['class']() try: setattr(self.metacart, other_osm+'_cart', self.carts[other_osm].cart) except Exception, e: pass equivalences[other_osm] = self.carts[other_osm].set_equivalent_cart(self.carts[osm].cart) self.metacart.save() # Setting equivalent content for ohter osms for other_osm in DallizCartController.AVAILABLE_OSMS.keys(): if other_osm != osm: for other_osm_bis in DallizCartController.AVAILABLE_OSMS.keys(): if other_osm_bis != osm and other_osm_bis != other_osm: for content in equivalences[other_osm].keys(): try: other_content = equivalences[other_osm][content]['content'] except Exception, e: pass try: other_content_bis = equivalences[other_osm_bis][content]['content'] except Exception, e: pass try: setattr(other_content_bis, other_content.cart.osm+'_content', other_content) except Exception, e: pass try: setattr(content, other_content_bis.cart.osm+'_content', other_content_bis) except Exception, e: pass try: setattr(content, other_content.cart.osm+'_content', other_content) except Exception, e: pass try: other_content_bis.save() except Exception, e: pass