Ejemplo n.º 1
0
def equip_delete(request):
    if request.method == 'POST':
        if judge_cookie(request) is False:
            return JsonResponse({"error": "please login"})
        try:
            equip_id = request.POST.get('equip_id')
        except Exception:
            return JsonResponse({"error": "no such a equipment"})
        try:
            equip = Equipment.objects.get(id=equip_id)
            # user = User.objects.get(rand_str=request.COOKIES['session_id'])
            user = User.objects.get(rand_str=request.headers['jwt'])
            if user.username != equip.lessor_name and user.authority != 'admin':
                raise RuntimeError
        except Exception:
            return JsonResponse({"error": "this is not your equipment"})
        sale_requests = SaleRequest.objects.filter(equip_id=equip.id)
        rent_info = RentInformation.objects.filter(equip_id=equip.id)
        rent_req = RentRequest.objects.filter(equip_id=equip.id)
        delete_item(sale_requests)
        delete_item(rent_info)
        delete_item(rent_req)
        add_message('sys', user.id, 0, '删除设备', '删除设备'+equip.equip_name)
        equip.delete()
        return JsonResponse({"message": "ok"})
    return JsonResponse({"error": "wrong request method"})
Ejemplo n.º 2
0
def equip_request_add(request):
    if request.method == 'POST':
        if judge_cookie(request) is False:
            return JsonResponse({"error": "please login"})
        try:
            equip_id = request.POST.get('equip_id')
            end_time = request.POST.get('end_time')
        except Exception:
            return JsonResponse({"error": "invalid parameters"})
        try:
            equip = Equipment.objects.get(id=equip_id)
        except Exception:
            return JsonResponse({"error": "no such a equipment"})
        # user = User.objects.get(rand_str=request.COOKIES['session_id'])
        user = User.objects.get(rand_str=request.headers.get('jwt'))
        if user.username != equip.lessor_name:
            return JsonResponse({"error": "this is not your equipment"})
        sale_req = SaleRequest()
        sale_req.equip_id = equip_id
        sale_req.end_time = end_time
        sale_req.equip_name = equip.equip_name
        sale_req.lessor_name = equip.lessor_name
        sale_req.status = 'pending'
        sale_req.save()
        add_message('sys', 0, 0, '添加上架申请', '设备'+equip.equip_name+'添加了上架申请')
        return JsonResponse({"message": "ok"})
    return JsonResponse({"error": "wrong request method"})
Ejemplo n.º 3
0
def equip_add(request):
    if request.method == 'POST':
        if judge_cookie(request) is False:
            return JsonResponse({"error": "please login"})
        try:
            equip_name = request.POST.get('equip_name')
            address = request.POST.get('address')
        except Exception:
            return JsonResponse({"error": "invalid parameters"})
        # user = User.objects.get(rand_str=request.COOKIES['session_id'])
        user = User.objects.get(rand_str=request.headers.get('jwt'))
        equip = Equipment()
        equip.equip_name = equip_name
        equip.lessor_name = user.username
        equip.address = address
        equip.contact = user.contact
        equip.status = 'unavailable'
        equip.end_time = '1970-01-01'
        equip.username = '******'
        equip.score = '0'
        equip.score_count = '0'
        equip.save()
        add_message('sys', user.id, 0, '添加设备', '添加设备'+equip.equip_name)
        return JsonResponse({"message": "ok"})
    return JsonResponse({"error": "wrong request method"})
Ejemplo n.º 4
0
def rent_request_add(request):
    if request.method == 'POST':
        if judge_cookie(request) is False:
            return JsonResponse({"error": "please login"})
        try:
            equip_id = request.POST.get('equip_id')
            detail = request.POST.get('detail')
            start_time = request.POST.get('start_time')
            return_time = request.POST.get('return_time')
        except Exception:
            return JsonResponse({"error": "invaild parameters"})
        try:
            equip = Equipment.objects.get(id=equip_id)
        except Exception:
            return JsonResponse({"error": "no such a equipment"})
        if equip.status != 'onsale':
            return JsonResponse({"error": "this equipment is not available"})
        # user = User.objects.get(rand_str=request.COOKIES['session_id'])
        user = User.objects.get(rand_str=request.headers.get('jwt'))
        lessor = User.objects.get(username=equip.lessor_name)
        rent_req = RentRequest(
            username=user.username,
            lessor_name=equip.lessor_name,
            equip_name=equip.equip_name,
            equip_id=equip_id,
            status='pending',
            detail=detail,
            start_time=start_time,
            return_time=return_time
        )
        add_message('sys', user.id, 0, '添加租借申请', '用户'+user.username+'申请租借设备'+equip.equip_name)
        add_message('lessor', user.id, lessor.id, '添加租借申请', '用户'+user.username+'申请租借您的设备'+equip.equip_name)
        rent_req.save()
        return JsonResponse({"message": "ok"})
    return JsonResponse({"error": "wrong request method"})
Ejemplo n.º 5
0
def rent_request_delete(request):
    if request.method == 'POST':
        if judge_cookie(request) is False:
            return JsonResponse({"error": "please loggin"})
        if 'rent_req_id' not in request.POST:
            return JsonResponse({"error": "invalid parameters"})
        try:
            rent_req_id = request.POST.get('rent_req_id')
            rent_req = RentRequest.objects.get(id=rent_req_id)
            rent_req.delete()
            add_message('sys', 0, 0, '删除租借申请', '删除了租借申请,申请id:'+str(rent_req.id))
        except:
            return JsonResponse({"error": "no such a rent request"})
    return JsonResponse({"error": "wrong request method"})
Ejemplo n.º 6
0
def rent_request_decide(request):
    if request.method == 'POST':
        if judge_cookie(request) is False:
            return JsonResponse({"error": "please login"})
        if 'rent_req_id' in request.POST:
            rent_req_id = request.POST.get('rent_req_id')
        else:
            return JsonResponse({"error": "no such a rent request"})
        if 'decision' in request.POST:
            decision = request.POST.get('decision')
        else:
            return JsonResponse({"error": "invalid decision"})
        try:
            rent_req = RentRequest.objects.get(id=rent_req_id)
        except Exception:
            return JsonResponse({"error": "no such a rent request"})
        equip = Equipment.objects.get(id=rent_req.equip_id)
        lessor = User.objects.get(username=equip.lessor_name)
        user = User.objects.get(username=rent_req.username)
        if equip.status != 'onsale':
            return JsonResponse({"error": "this equipment is not available"})
        if decision == 'reject':
            rent_req.status = 'reject'
            rent_req.save()
            add_message('sys', lessor.id, 0, '拒绝租借申请', '用户'+lessor.username+'拒绝了用户'+user.username+'的拒绝申请')
            add_message('user', lessor.id, user.id, '拒绝租借申请', '出租方拒绝了您的租借申请')
            return JsonResponse({"message": "ok"})
        elif decision == 'apply':
            rent_req.status = 'apply'
            equip.status = 'rented'
            equip.username = rent_req.username
            rent_info = RentInformation(
                equip_id=rent_req.equip_id,
                equip_name=rent_req.equip_name,
                lessor_name=rent_req.lessor_name,
                username=rent_req.username,
                rent_time=rent_req.start_time,
                return_time=rent_req.return_time,
                end_time=equip.end_time,
                status='unreturned'
            )
            rent_req.save()
            equip.save()
            rent_info.save()
            add_message('sys', lessor.id, 0, '同意租借申请', '用户' + lessor.username + '同意了用户' + user.username + '的拒绝申请')
            add_message('user', lessor.id, user.id, '同意租借申请', '出租方同意了您的租借申请')
            return JsonResponse({"message": "ok"})
        else:
            return JsonResponse({"error": "invalid decision"})
    return JsonResponse({"error": "wrong request method"})
Ejemplo n.º 7
0
def equip_set(request):
    if request.method == 'POST':
        if judge_cookie(request) is False:
            return JsonResponse({"error": "please login"})
        try:
            equip_id = request.POST.get('equip_id')
        except Exception:
            return JsonResponse({"error": "no such a equipment"})
        try:
            equip = Equipment.objects.get(id=equip_id)
            # user = User.objects.get(rand_str=request.COOKIES['session_id'])
            user = User.objects.get(rand_str=request.headers.get('jwt'))
            if user.username != equip.lessor_name and user.authority != 'admin':
                raise RuntimeError
        except Exception:
            return JsonResponse({"error": "this is not your equipment"})
        try:
            set_info = {}
            info_name = {
                'equip_name': 'str',
                'address': 'str',
                'end_time': 'str',
                'status': 'str'
            }
            for item in info_name:
                if item in request.POST:
                    set_info[item] = request.POST.get(item)
            if len(set_info) == 0:
                raise RuntimeError
        except Exception:
            return JsonResponse({"error": "no avaliable change"})
        if user.authority != 'admin' and equip.status == 'rented':
            return JsonResponse({"error": "this is equipment is not returned"})
        for item in set_info:
            equip.__setattr__(item, set_info[item])
        equip.save()
        add_message('sys', user.id, 0, '修改设备信息', '修改设备'+equip.equip_name+'的信息')
        return JsonResponse({"message": "ok"})
    return JsonResponse({"error": "wrong request method"})
Ejemplo n.º 8
0
def rent_confirm(request):
    if request.method == 'POST':
        if judge_cookie(request) is False:
            return JsonResponse({"error": "please login"})
        if 'rent_info_id' in request.POST:
            rent_info_id = request.POST['rent_info_id']
        else:
            return JsonResponse({"error": "invalid parameters"})
        try:
            rent_info = RentInformation.objects.get(id=rent_info_id)
            equip = Equipment.objects.get(id=rent_info.equip_id)
        except Exception:
            return JsonResponse({"error": "no such rent infomation"})
        user = User.objects.get(username=equip.username)
        lessor = User.objects.get(username=equip.username)
        rent_info.status = 'returned'
        equip.status = 'onsale'
        rent_info.save()
        equip.username = ''
        equip.save()
        add_message('sys', user.id, 0, '确认归还','用户'+user.username+'归还了设备'+equip.equip_name)
        add_message('lessor', lessor.id, user.id, '确认归还', '出租方确认了您的归还')
        return JsonResponse({"message": "ok"})
    return JsonResponse({"error": "wrong request method"})
Ejemplo n.º 9
0
def equip_request_decide(request):
    if request.method == 'POST':
        if judge_cookie(request) is False:
            return JsonResponse({"error": "please login"})
        if judge_manager(request) is False:
            return JsonResponse({"error": "you are not administrator"})
        try:
            sale_req_id = request.POST.get('sale_req_id')
        except Exception:
            return JsonResponse({"error": "no such a sale request"})
        try:
            decision = request.POST.get('decision')
        except Exception:
            return JsonResponse({"error": "invalid decision"})
        try:
            sale_req = SaleRequest.objects.get(id=sale_req_id)
        except Exception:
            return JsonResponse({"error": "no such a sale request"})
        equip = Equipment.objects.get(id=sale_req.equip_id)
        lessor = User.objects.get(username=equip.lessor_name)
        if decision == 'reject':
            sale_req.status = 'reject'
            sale_req.save()
            add_message('sys', 0, 0, '拒绝上架申请'+equip.equip_name)
            add_message('lessor',0, lessor.id, '管理员拒绝了您的上架申请')
            return JsonResponse({"message": "ok"})
        elif decision == 'apply':
            sale_req.status = 'apply'
            equip.status = 'onsale'
            equip.end_time = sale_req.end_time
            equip.save()
            sale_req.save()
            add_message('sys', 0, 0, '同意上架申请'+equip.equip_name)
            add_message('lessor',0, lessor.id, '管理员同意了您的上架申请')
            return JsonResponse({"message": "ok"})
        else:
            return JsonResponse({"error": "invalid decision"})
    return JsonResponse({"error": "wrong request method"})