Esempio n. 1
0
def get_lease(request, term=1):
    response_data = dict()

    term = int(term)
    if term > 10:
        term = 10

    search = BoardLease.objects.filter(end_date__gte=safe_datetime.now())

    if search.count() > 0:
        response_data['result'] = 'failure'
        response_data['reason_code'] = 'in_use'
    else:
        m = md5.new()
        m.update(unicode(datetime.now().microsecond.__str__))
        lease_code = m.hexdigest()

        lease_expiry = datetime.now() + timedelta(seconds=term * 60)
        new_lease = BoardLease(board_lease_code=lease_code, is_active=True, start_date=datetime.now(), end_date=lease_expiry, creation_date=datetime.now())
        new_lease.save()

        response_data['result'] = 'success'
        response_data['lease_code'] = lease_code
        response_data['lease_expiry'] = str(lease_expiry)

    return HttpResponse(json.dumps(response_data), mimetype="application/json")
Esempio n. 2
0
def get_lease(request,
              term=60,
              top_row=0,
              left_col=0,
              bottom_row=11,
              right_col=79):
    response_data = dict()

    term = int(term)
    if term > MAX_LEASE_SECONDS:
        term = MAX_LEASE_SECONDS

    search = BoardLease.objects.filter(end_date__gte=safe_datetime.now())

    if search.count() > 0:
        # TODO: check for conflicts with coordinates per github issue #3
        generate_error(response_data, "in_use")
        add_lease_expiration(response_data, search.get())
    else:
        m = md5.new()
        m.update(unicode(datetime.now().microsecond.__str__))
        lease_code = m.hexdigest()

        lease_expiry = datetime.now() + timedelta(seconds=term)
        new_lease = BoardLease(board_lease_code=lease_code,
                               is_active=True,
                               start_date=datetime.now(),
                               end_date=lease_expiry,
                               top_row=int(top_row),
                               left_col=int(left_col),
                               bottom_row=int(bottom_row),
                               right_col=int(right_col),
                               creation_date=datetime.now())
        new_lease.save()

        response_data['result'] = 'success'
        response_data['lease_code'] = lease_code
        response_data['lease_expiry'] = str(lease_expiry)
        add_lease_expiration(response_data, new_lease)

    return HttpResponse(json.dumps(response_data), mimetype="application/json")
Esempio n. 3
0
def get_lease(request, term=60, top_row=0, left_col=0, bottom_row=11, right_col=79):
    response_data = dict()

    term = int(term)
    if term > MAX_LEASE_SECONDS:
        term = MAX_LEASE_SECONDS

    search = BoardLease.objects.filter(end_date__gte=safe_datetime.now())

    if search.count() > 0:
        # TODO: check for conflicts with coordinates per github issue #3
        generate_error(response_data, "in_use")
        add_lease_expiration(response_data, search.get())
    else:
        m = md5.new()
        m.update(unicode(datetime.now().microsecond.__str__))
        lease_code = m.hexdigest()

        lease_expiry = datetime.now() + timedelta(seconds=term)
        new_lease = BoardLease(
            board_lease_code=lease_code,
            is_active=True,
            start_date=datetime.now(),
            end_date=lease_expiry,
            top_row=int(top_row),
            left_col=int(left_col),
            bottom_row=int(bottom_row),
            right_col=int(right_col),
            creation_date=datetime.now(),
        )
        new_lease.save()

        response_data["result"] = "success"
        response_data["lease_code"] = lease_code
        response_data["lease_expiry"] = str(lease_expiry)
        add_lease_expiration(response_data, new_lease)

    return HttpResponse(json.dumps(response_data), mimetype="application/json")