Пример #1
0
    def get_requests(self, *args, **kwargs):
        db = Database()
        result_set = db.query(
            f"SELECT * FROM requests WHERE id_user = {self.id}")
        requests = []
        for request in result_set:
            last_change = db.query(
                f"SELECT * FROM history WHERE id_request = {request.id}")

            last_ts = last_change[-1][-1] if len(last_change) > 0 else None

            requests.append({
                'id':
                request.id,
                'user':
                object_to_dict(self),
                'driver':
                db.get(Driver, request.id_driver, as_dict=True),
                'collector':
                db.get(Collector, request.id_collector, as_dict=True),
                'status':
                request.status,
                'points':
                request.points,
                'weight':
                request.weight,
                'timestamp':
                last_ts
            })
        return requests
Пример #2
0
    def deliver_request(self, id_request, delivery_code):
        db = Database()
        code_search = db.query(
            f"SELECT * FROM delivery_codes WHERE code = '{delivery_code}'")
        id_collector = code_search[0][1]
        delivered_request = {
            'id': id_request,
            'id_collector': id_collector,
            'status': Request.REQUEST_STATUS.get('evaluation')
        }
        request = Request(**delivered_request)
        db.update(request)

        driver = Driver(**{'id': self.id, 'current_request': None})
        db.update(driver)

        dc = db.get(DeliveryCode, code_search[0][0])
        db.delete(dc)

        new_history = {
            'id_request': request.id,
            'old_status': Request.REQUEST_STATUS.get('ongoing'),
            'new_status': Request.REQUEST_STATUS.get('evaluation'),
            'timestamp': str(datetime.datetime.now().timestamp())
        }

        history = History(**new_history)
        db.add(history)

        return {
            'request': object_to_dict(request),
            'msg': 'Request delivered successfully'
        }
Пример #3
0
    def approve_request(self, id_request, *args, **kwargs):
        db = Database()

        approved_request = {
            'id': id_request,
            'status': Request.REQUEST_STATUS.get('approved')
        }
        request = Request(**approved_request)
        db.update(request)

        new_history = {
            'id_request': request.id,
            'old_status': Request.REQUEST_STATUS.get('waiting_approval'),
            'new_status': Request.REQUEST_STATUS.get('approved'),
            'timestamp': str(datetime.datetime.now().timestamp())
        }

        history = History(**new_history)
        db.add(history)

        updated_user = {
            'id': self.id,
            'points': self.points + db.get(Request, id_request).points
        }
        user = User(**updated_user)
        db.update(user)

        req = db.get(Request, id_request)
        id_driver = req.id_driver

        drv = db.get(Driver, id_driver)

        updated_driver = {
            'id': id_driver,
            'points': drv.points + (req.points / 2)
        }
        driver = Driver(**updated_driver)
        db.update(driver)

        request.status = Request.REQUEST_STATUS.get('concluded')
        db.update(request)

        return {
            'request': object_to_dict(request),
            'msg': 'Request approved successfully'
        }
Пример #4
0
    def evaluate_request(self, id_request, weight, *args, **kwargs):
        db = Database()
        request_evaluated = {
            'id': id_request,
            'status': Request.REQUEST_STATUS.get('concluded'),
            'weight': weight,
            'points': weight / 5
        }
        request = Request(**request_evaluated)
        db.update(request)

        new_history = {
            'id_request': request.id,
            'old_status': Request.REQUEST_STATUS.get('evaluation'),
            'new_status': Request.REQUEST_STATUS.get('concluded'),
            'timestamp': str(datetime.datetime.now().timestamp())
        }

        history = History(**new_history)
        db.add(history)

        req = db.get(Request, id_request)

        us = db.get(User, req.id_user)
        updated_user = {'id': us.id, 'points': us.points + req.points}
        user = User(**updated_user)
        db.update(user)

        drv = db.get(Driver, req.id_driver)

        updated_driver = {
            'id': req.id_driver,
            'points': drv.points + (req.points / 2)
        }
        driver = Driver(**updated_driver)
        db.update(driver)

        return {
            'request': object_to_dict(request),
            'msg': 'Request evaluated successfully'
        }
Пример #5
0
 def exchange_points(self, product_id, *args, **kwargs):
     db = Database()
     product = db.get(Product, product_id)
     if self.points < product.price:
         return {'msg': 'Insufficient points'}
     user = User(**{'id': self.id, 'points': self.points - product.price})
     db.update(user)
     order = Order(id_user=self.id,
                   id_product=product.id,
                   timestamp=str(datetime.datetime.now().timestamp()))
     db.add(order)
     return {'msg': f'{product.name} bought successfully'}
Пример #6
0
def handle(event, context):

    queryStringParameters = event.get('queryStringParameters')

    id = queryStringParameters.get('id')

    db = Database()

    car = db.get(Car, id=id, as_dict=True)

    response = {'statusCode': 200, 'body': json.dumps(car)}

    return response
Пример #7
0
def handle(event, context):
    body = json.loads(event.get('body'))

    user_id = body.get('user_id')
    address_id = body.get('address_id')

    db = Database()

    user = db.get(User, user_id)

    result = user.start_request(address_id)

    response = {'statusCode': 200, 'body': json.dumps(result)}

    return response
Пример #8
0
 def get_orders(self, *args, **kwargs):
     db = Database()
     result_set = db.query(
         f"SELECT * FROM orders WHERE id_user = {self.id}")
     orders = []
     for order in result_set:
         orders.append({
             'user':
             object_to_dict(self),
             'product':
             db.get(Product, order.id_product, as_dict=True),
             'timestamp':
             order.timestamp
         })
     return orders
Пример #9
0
def handle(event, context):
    body = json.loads(event.get('body'))

    code = body.get('code')
    id_driver = body.get('id_driver')
    id_request = body.get('id_request')

    db = Database()

    driver = db.get(Driver, id_driver)

    result = driver.deliver_request(id_request=id_request, delivery_code=code)

    response = {'statusCode': 200, 'body': json.dumps(result)}

    return response
Пример #10
0
def handle(event, context):

    queryStringParameters = event.get('queryStringParameters')

    id = queryStringParameters.get('id')

    db = Database()

    adress = db.get(Adress, id=id)

    response = {
        'statusCode': 200,
        'body': json.dumps({'product': object_to_dict(adress)})
    }

    return response
Пример #11
0
def handle(event, context):
    body = json.loads(event.get('body'))

    id = body.get('id')

    db = Database()

    user = db.get(User, id)

    orders = user.get_orders()

    response = {
        'statusCode': 200,
        'body': json.dumps(orders)
    }

    return response
Пример #12
0
def handle(event, context):

    queryStringParameters = event.get('queryStringParameters')

    id = queryStringParameters.get('id')

    db = Database()

    user = db.get(User, id=id)

    requests = user.get_requests()

    response = {
        'statusCode': 200,
        'body': json.dumps(requests),
    }

    return response
Пример #13
0
def handle(event, context):
    body = json.loads(event.get('body'))

    driver_id = body.get('driver_id')
    request_id = body.get('request_id')

    db = Database()

    driver = db.get(Driver, driver_id)

    result = driver.attend_request(request_id)

    response = {
        'statusCode': 200,
        'body': json.dumps(result)
    }

    return response
Пример #14
0
def handle(event, context):

    body = json.loads(event.get('body'))

    user_id = body.get('user_id')
    product_id = body.get('product_id')

    db = Database()

    user = db.get(User, id=user_id)

    order = user.exchange_points(product_id=product_id)

    response = {
        'statusCode': 200,
        'body': json.dumps(order),
    }

    return response
Пример #15
0
def handle(event, context):

    queryStringParameters = event.get('queryStringParameters')

    id = queryStringParameters.get('id')

    db = Database()

    user = db.get(User, id=id)

    adresses = []

    for adress in user.adresses:
        adresses.append(object_to_dict(adress))

    response = {
        'statusCode': 200,
        'body': json.dumps(adresses),
    }

    return response
Пример #16
0
def handle(event, context):

    queryStringParameters = event.get('queryStringParameters')

    id = queryStringParameters.get('id')

    db = Database()

    driver = db.get(Driver, id=id)

    response = {
        'statusCode':
        200,
        'body':
        json.dumps({
            'driver': object_to_dict(driver),
            'cars': driver.get_cars()
        }),
    }

    return response
Пример #17
0
def handle(event, context):

    queryStringParameters = event.get('queryStringParameters')

    id = queryStringParameters.get('id')

    db = Database()

    partner = db.get(Partner, id=id)

    response = {
        'statusCode':
        200,
        'body':
        json.dumps({
            'partner': object_to_dict(partner),
            'products': partner.get_products()
        }),
    }

    return response
Пример #18
0
def handle(event, context):
    body = json.loads(event.get('body'))

    collector_id = body.get('collector_id')
    request_id = body.get('request_id')
    weight = body.get('weight')

    db = Database()

    collector = db.get(Collector, collector_id)

    response = collector.evaluate_request(id_request=request_id, weight=weight)

    response = {
        'statusCode': 200,
        'headers': {
            'Access-Control-Allow-Origin': '*',
            'Access-Control-Allow-Credentials': True
        },
        'body': json.dumps(response),
    }

    return response
Пример #19
0
def handle(event, context):
    db = Database()
    adresses = []

    for adress in db.get_all(Adress, as_dict=True):
        if adress.get('id_collector'):
            adress.append({
                'adress': adress,
                'collector': object_to_dict(adress.collector)
            })
        else:
            user = db.get(User, adress.id_user)
            adress.append({
                'adress': adress,
                'user': object_to_dict(user)
            })

    response = {
        'statusCode': 200,
        'body': json.dumps(adresses),
    }

    return response