Ejemplo n.º 1
0
def reserve_device(request):
    # --------variables from the request
    user = User.objects.get(username=request.GET["user"])
    event = Event.objects.get(name=request.GET["event"])
    device = Device.objects.get(name=request.GET["device"])

    # --------number of devices that have been reserved/rented
    rentals = Rental.objects.filter(event=event, device=device, returned=False)
    user_rental = Rental.objects.filter(event=event, device=device, user=user, returned=False)
    free_inventory = Inventory.objects.filter(event=event, device=device).count() - rentals.count()

    if user_rental.count() > 0:
        return HttpResponseBadRequest("You already have a " + device.name + " under your name!")
    elif free_inventory > 0:
        new_rental = Rental(user=user, event=event, device=device)
        new_rental.save()
        r = Rental.objects.filter(
            event=event, device=device, returned=False
        )  # grab the current rentals again after saving the current reservation
        free = free_inventory - 1  # decrease the free count since a new rental is made

        return JsonResponse(
            {
                "rental_render": render(request, "partials/device_rentals.html", {"rentals": r, "free": free}).content,
                "free_inventory": free,
            }
        )

    else:
        return HttpResponseBadRequest("Sorry, there are no more " + device.name + " in stock!")
Ejemplo n.º 2
0
def ingest_rental_csv(with_lock):
    """
    Ingest csv function to combine extract and import gen functions,
    and populate data from generator in database
    """
    record_count = int(0)
    # Extract the CSV file from the zip archive
    extract_csv(DATA_ZIP_FILENAME, RENTAL_CSV_FILENAME, EXTRACT_PATH,
                with_lock)
    # Create a CSV import generator (next yields one db row)
    import_generator = import_csv_gen(EXTRACT_PATH + RENTAL_CSV_FILENAME)
    # Skip over the title row
    next(import_generator)
    # Iterate over all other rows
    with Connection():
        while True:
            try:
                data = next(import_generator)
                if len(data) != 6:
                    logger.error(f'Data item count: {len(data)}')
                    continue
                # extract items from list and add document to database
                rental = Rental(product_id=data[RENTAL_PROD_ID],
                                user_id=data[RENTAL_USER_ID])
                rental.save()  # This will perform an insert
                record_count += 1
            except StopIteration:
                break
    return record_count
Ejemplo n.º 3
0
def show_rentals(prod_id):
    """
    Returns a Python dictionary with the following user information from users
    that have rented products matching product_id:

        user_id.
        name.
        address.
        phone_number.
        email.

    For example:

    {‘user001’:{‘name’:’Elisa Miles’,’address’:‘4490 Union Street’,
    ’phone_number’:‘206-922-0882’,’email’:’[email protected]’},
    ’user002’:{‘name’:’Maya Data’,’address’:‘4936 Elliot Avenue’,
    ’phone_number’:‘206-777-1927’,’email’:’[email protected]’}}
    """
    ret_dict = {}

    with Connection():
        renters = Rental.objects(product_id=prod_id)
        for renter in renters:
            users = Customer.objects(user_id=renter.user_id)
            for user in users:
                user_info = {user.user_id : {'name' : user.name,
                                             'address' : user.address,
                                             'phone_number' : user.phone_number,
                                             'email' : user.email
                                             }
                            }
                ret_dict.update(user_info)
    return ret_dict
Ejemplo n.º 4
0
def reserve_device(request):
    #--------variables from the request
    user = User.objects.get(username=request.GET['user'])
    event = Event.objects.get(name=request.GET['event'])
    device = Device.objects.get(name=request.GET['device'])

    #--------number of devices that have been reserved/rented
    rentals = Rental.objects.filter(event=event, device=device, returned=False)
    user_rental = Rental.objects.filter(event=event,
                                        device=device,
                                        user=user,
                                        returned=False)
    free_inventory = Inventory.objects.filter(
        event=event, device=device).count() - rentals.count()

    if user_rental.count() > 0:
        return HttpResponseBadRequest("You already have a " + device.name +
                                      " under your name!")
    elif free_inventory > 0:
        new_rental = Rental(user=user, event=event, device=device)
        new_rental.save()
        r = Rental.objects.filter(
            event=event, device=device, returned=False
        )  #grab the current rentals again after saving the current reservation
        free = free_inventory - 1  #decrease the free count since a new rental is made

        return JsonResponse({
            'rental_render':
            render(request, 'partials/device_rentals.html', {
                'rentals': r,
                'free': free
            }).content,
            'free_inventory':
            free
        })

    else:
        return HttpResponseBadRequest("Sorry, there are no more " +
                                      device.name + " in stock!")
Ejemplo n.º 5
0
def rent(request,movie_id):
    user = _getuser(request)
    rental = Rental(user=user,movie=Movie.objects.get(id=movie_id),rental_date=datetime.datetime.now(),rental_length=7)
    rental.save()
    return redirect('/movie/'+movie_id)