예제 #1
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
예제 #2
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!")
예제 #3
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!")
예제 #4
0
파일: views.py 프로젝트: BIT-AI/Bitflix
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)