Example #1
0
 def getStaffOrders(self, order_id, filters={}):
     try:
         order = Order.objects.get(id = order_id)
     except Order.DoesNotExist:
         return JsonMessage(status=400, result_msg="L'ordine richiesto non è presente nel sistema !")
 
     return JsonMessage(body=Order.serialize(order)) 
Example #2
0
 def getClientOrders(self, user_info, order_id):
     try:
         order = Order.objects.get(id = order_id, client = user_info)
     except Order.DoesNotExist:
         return JsonMessage(status=400,result_msg="L'ordine richiesto non è presente nel sistema o non appartiene all'utente!")
     
     return JsonMessage(body=Order.serialize(order))
Example #3
0
    def post(self, request):

        if (not self.authenticated(request)):
            return JsonMessage(status=400, result_msg="user not autenticated")

        logout(request)
        return JsonMessage(status=200, result_msg="user logged out ")
Example #4
0
    def post(self,request):
        if(not self.authenticated(request)): return JsonMessage(status=400,result_msg="login required")

        user = request.user
        user_info = UserInformation.objects.get(user__username = user)
        
        return self.getStaffOrders() if user.is_staff else self.getClientOrders(user_info)
Example #5
0
    def post(self,request):
        menus = Menu.objects.all()

        data = []
        for menu in menus:
            data.append(Menu.serialize(menu))

        return JsonMessage(body=data)
Example #6
0
    def post(self,request):
        ingredients = Ingredient.objects.all()
        
        data=[]
        for ingredient in ingredients:
            data.append(Ingredient.serialize(ingredient))

        return JsonMessage(body=data)
Example #7
0
    def getClientOrders(self,user_info):
        orders = Order.objects.filter(client=user_info)

        data = []

        for order in orders:
            data.append(Order.serialize(order))
        
        return JsonMessage(body=data)
Example #8
0
    def post(self, request):

        if (not self.not_authenticated(request)):
            return JsonMessage(status=400,
                               result_msg="user already autenticated")

        body = json.loads(request.body)

        first_name = body.get("first_name")
        last_name = body.get("last_name")
        email = body.get("email")
        password = body.get("password")
        phone_number = body.get("phone_number")

        if (not UserInformation.validateEmailCriteria(email)
                or not UserInformation.validatePassworCriteria(password)):
            return JsonMessage(
                status=400,
                result_msg=
                "email or/and password doesn't have the correct format")

        try:
            tmp_user = User.objects.get(email=email)
            if (tmp_user is not None):
                return JsonMessage(status=400,
                                   result_msg="email already exists")
        except:
            pass

        rnd_username = crypt.crypt(email, crypt.mksalt(crypt.METHOD_CRYPT))

        user = User.objects.create_user(username=rnd_username,
                                        first_name=first_name,
                                        last_name=last_name,
                                        email=email,
                                        password=password)
        user.save()

        user_info = UserInformation.objects.create(phone=phone_number,
                                                   user=user)
        user_info.save()

        login(request, user)
        return JsonMessage(result_msg="successfully registered")
Example #9
0
    def getStaffOrders(self,filters={}):

        orders = Order.objects.all()
        
        data = []

        for order in orders:
            data.append(Order.serialize(order))
        
        return JsonMessage(body=data)
Example #10
0
    def post(self, request, order):
        if(not self.authenticated(request)): return JsonMessage(status=400,result_msg="login required")

        body = json.loads(request.body)

        try:
            order = Order.objects.get(id = order)
        except Order.DoesNotExist:
            return JsonMessage(status=400,result_msg="L'ordine che si sta tentando di aggiornare non è presente nel sistema!")
        
        newState = OrderStatus.is_valid(body.get("status"))
        if newState != None :
            order.status = newState
        else :
            return JsonMessage(status=400,result_msg="Lo stato non è supportato dal sistema!")

        order.save()
        
        return JsonMessage(status=200,result_msg="L'ordine è stato aggiornato correttamente")
Example #11
0
    def post(self,request):

        body = json.loads(request.body)

        name = body.get("name")

        try: Menu.exists(name)
        except Exception:
            return JsonMessage(
                status="404",
                result_msg="Un'able to find menu {}".format(name)
                )


        menu_pizzas = Menu.objects.get(name=name)

        data = []
        for pizza in menu_pizzas.pizzas:
            data.append(Pizza.serialize(pizza))

        return JsonMessage(body=data)
Example #12
0
    def post(self,request):
        
        if(not self.authenticated(request)): return JsonMessage(status=400,result_msg="login required")

        body = json.loads(request.body)

        user = UserInformation.objects.get(user__username = request.user)
        
        order = Order()
        order.date = datetime.datetime.now()
        order.client = user
        order.withdrawal = datetime.datetime.strptime(body.get("withdrawal"), '%d-%m-%Y %H:%M')
        order.address = body.get("address")
        order.save()
        
        for pizz in body.get("pizza"):
            
            pizza = Pizza()
            pizza.name = pizz.get("name")
            pizza.totalSlices = pizz.get("totalSlices")
            pizza.save()
            
            for ingredient in pizz.get("ingredients"):

                if not Ingredient.exists(ingredient[0]):
                    return JsonMessage(status=400,result_msg="Uno degli ingredienti non è registrato nel sistema !")
                if not Slice.exists(ingredient[1]):
                    return JsonMessage(status=400,result_msg="Numero di slice non supportato !")

                ing = Ingredient.objects.get(name = ingredient[0])
                sli = Slice.objects.get(number = ingredient[1])

                ing_sli, created = PizzaIngredients.objects.get_or_create(ingredient = ing, pslice = sli)
                if created : ing_sli.save()
                pizza.pizzaIngredients.add(ing_sli)
            
            order.pizza.add(pizza) 
    
        order.save()
        return JsonMessage(status=200,result_msg="Congratulazioni, il tuo ordine è stato registrato con successo.", body=[order.id])
Example #13
0
    def post(self, request):

        if (not self.not_authenticated(request)):
            return JsonMessage(status=400,
                               result_msg="user already autenticated")

        body = json.loads(request.body)

        email = body.get("email")
        password = body.get("password")
        remember_me = body.get("remember_me")

        if (not UserInformation.validateEmailCriteria(email)
                or not UserInformation.validatePassworCriteria(password)):
            return JsonMessage(
                status=400,
                result_msg=
                "email or/and password doesn't have the correct format")

        try:
            tmp_user = User.objects.get(email=email)
        except:
            return JsonMessage(status=400,
                               result_msg="incorrect email or password")

        user = authenticate(request,
                            username=tmp_user.username,
                            password=password)
        if user is not None:
            # Redirect to a success page.
            login(request, user)
            if (not remember_me):
                request.session.set_expiry(
                    0)  #session restart after browser is closed
            return JsonMessage(result_msg="autenticated")
        else:
            # Return an 'invalid login' error message.
            return JsonMessage(status=400,
                               result_msg="incorrect email or password")
Example #14
0
    def post(self,request):

        body = json.loads(request.body)

        number = body.get("number")

        menus = Menu.objects.prefetch_related("pizzas").all()

        print(menus)

        pizzas = []
        for menu in menus:
            for pizza in menu.pizzas.all():
                pizzas.append(pizza)

        data = []

        for fpizza in random.sample(pizzas,number):
            data.append(Pizza.serialize(fpizza))

        return JsonMessage(body=data)