示例#1
0
def upgrade_apply(request):
    if request.method != 'PUT':
        return JsonResponse({'error': 'require PUT'})
    username = check_username(request)
    if not username:
        return JsonResponse({'error': 'please login'})
    user = User.objects.filter(username=username)
    if not user:
        return JsonResponse({'error': 'please login'})
    user = user.first()

    data = QueryDict(request.body)
    lab_info = data.get('lab_info')
    if not lab_info:
        return JsonResponse({'error': 'require lab information'})

    try:
        if user.upgradeapply_set.all():  # 已经有申请了
            previous_apply = user.upgradeapply_set.all().first()
            if previous_apply.state == 1:
                return JsonResponse({'error': 'has upgraded'})
            previous_apply.lab_info = lab_info
            previous_apply.state = 0
            previous_apply.save()
            return HttpResponse('modify successfully')
        else:
            UpgradeApply.objects.create(applicant=user,
                                        lab_info=lab_info,
                                        state=0)
            return HttpResponse('ok')
    except (TypeError, ValueError):
        return JsonResponse({'error': 'fail to apply'})
示例#2
0
def get_borrow_apply_list(request):
    if request.method != 'GET':
        return JsonResponse({'error': 'require GET'})
    username = check_username(request)
    if not username:
        return JsonResponse({'error': 'please login'})
    borrower = User.objects.filter(username=username)
    if not borrower:
        return JsonResponse({'error': 'please login'})
    borrower = borrower.first()

    borrow_applies = borrower.user_apply_set.all()
    ret = []
    for apply in borrow_applies:
        count = apply.count
        target_equipment = apply.target_equipment.to_dict()
        end_time = apply.end_time
        reason = apply.reason
        state = apply.state
        ret.append({
            'count': count,
            'target_equipment': target_equipment,
            'end_time': end_time,
            'reason': reason,
            'state': state
        })

    return JsonResponse({'posts': ret})
示例#3
0
def off_shelf_equipment(request):
    if request.method == 'POST':
        equipment_id = request.POST.get('id')
        custom_equipment = Equipment.objects.get(id=equipment_id)
        username = check_username(request)
        if custom_equipment and custom_equipment.provider.username == username:
            custom_equipment.delete()
            return JsonResponse({'message': 'ok'})
    else:
        return JsonResponse({'error': 'require POST'})
示例#4
0
def on_shelf_equipment(request):
    if request.method == 'POST':
        name = request.POST.get('name')
        description = request.POST.get('description')
        count = request.POST.get('count')
        Equipment(name=name,
                  description=description,
                  count=count,
                  provider=User.objects.get(
                      username=check_username(request))).save()
        return JsonResponse({'message': 'ok'})
    else:
        return JsonResponse({'error': 'require POST'})
示例#5
0
def decrease_equipment(request):
    if request.method == 'POST':
        equipment_id = request.POST.get('id')
        delete_count = request.POST.get('count', 1)

        custom_equipment = Equipment.objects.get(id=equipment_id)
        username = check_username(request)
        if custom_equipment and custom_equipment.provider.username == username:
            custom_equipment.count -= delete_count
            custom_equipment.save()
            return JsonResponse({'message': 'ok'})
    else:
        return JsonResponse({'error': 'require POST'})
示例#6
0
def show_borrow_apply_list(request):
    if request.method == 'GET':
        page = request.GET.get('page', 1)
        username = check_username(request)
        user = User.objects.get(username=username)
        borrow_apply_list = user.borrowApplies.all()
        borrow_apply_list = [b.to_dict() for b in borrow_apply_list]
        total_page = int((len(borrow_apply_list) + PAGE_SIZE - 1) / PAGE_SIZE)
        borrow_apply_list = borrow_apply_list[(page - 1) * PAGE_SIZE:page *
                                              PAGE_SIZE]
        return JsonResponse({
            'page': page,
            'total_page': total_page,
            'borrow_apply_list': borrow_apply_list
        })
    else:
        return JsonResponse({'error': 'require POST'})
示例#7
0
def edit_equipment(request):
    if request.method == 'POST':
        equipment_id = request.POST.get('id')
        name = request.POST.get('name')
        description = request.POST.get('description')
        count = request.POST.get('count')

        custom_equipment = Equipment.objects.get(id=equipment_id)
        username = check_username(request)
        if custom_equipment and custom_equipment.provider.username == username:
            custom_equipment.name = name
            custom_equipment.name = description
            custom_equipment.name = count
            custom_equipment.save()
            return JsonResponse({'message': 'ok'})
    else:
        return JsonResponse({'error': 'require POST'})
示例#8
0
def get_my_equipment_list(request):
    if request.method == 'GET':
        page = request.GET.get('page', 1)
        username = check_username(request)
        user = User.objects.get(username=username)
        equipment_list = user.equipments.all()
        equipment_list = [e.to_dict() for e in equipment_list]
        total_page = int((len(equipment_list) + PAGE_SIZE - 1) / PAGE_SIZE)
        equipment_list = equipment_list[(page - 1) * PAGE_SIZE:page *
                                        PAGE_SIZE]
        return JsonResponse({
            'page': page,
            'total_page': total_page,
            'posts': equipment_list
        })
    else:
        return JsonResponse({'error': 'require GET'})
示例#9
0
def login(request):
    if request.method != 'POST':
        return JsonResponse({'error': 'require POST'})

    username = request.POST.get('username')
    user = User.objects.filter(username=username)
    if not user:
        return JsonResponse({'error': 'no such a user'})

    user = user.first()
    password_stored = user.password
    password = request.POST.get('password')
    if not check_password(password, password_stored):
        return JsonResponse({'error': 'password is wrong'})

    # has logged in
    session_username = check_username(request)
    if session_username:  # 已经登录
        if username == session_username:  # 同一用户
            # update upgrade info
            if not user.is_provider:
                apply = UpgradeApply.objects.filter(applicant=user)
                if apply and apply.first().state == 1:
                    user.is_provider = True
                    user.save()
                    apply.delete()
                    return HttpResponse('Notice, you have upgraded!')
            return JsonResponse({'error': 'has logged in'})
        else:  # 不同用户
            session_id = request.COOKIES.get('session_id', '')
            request.session.delete(session_id)  # 删除前一个登录状态

    # success
    random_id = str(uuid.uuid4())
    request.session[random_id] = username  # 服务器写入session
    res = JsonResponse({'user': username})
    res.set_cookie('session_id', random_id)  # 返回给浏览器cookies

    return res
示例#10
0
def borrow_apply(request):
    if request.method != 'POST':
        return JsonResponse({'error': 'require POST'})
    username = check_username(request)
    if not username:
        return JsonResponse({'error': 'please login'})
    borrower = User.objects.filter(username=username)
    if not borrower:
        return JsonResponse({'error': 'please login'})
    borrower = borrower.first()

    target_id = request.POST.get('id', "")
    end_time = request.POST.get('endtime', "")
    reason = request.POST.get('reason', "")
    try:
        count = int(request.POST.get('count', ""))
    except (ValueError, TypeError):
        return JsonResponse({'error': 'invalid parameters'})

    if not target_id or not end_time or count <= 0:
        return JsonResponse({'error': 'invalid parameters'})

    target = Equipment.objects.filter(id=target_id)
    if not target:
        return JsonResponse({'error': 'invalid id'})
    target = target.first()
    if target.count < count:
        return JsonResponse({'error': 'not enough'})

    try:
        BorrowApply.objects.create(borrower=borrower,
                                   count=count,
                                   target_equipment=target,
                                   end_time=end_time,
                                   reason=reason,
                                   state=0)
        return HttpResponse('ok')
    except django.core.exceptions.ValidationError:
        return JsonResponse({'error': 'format error'})
示例#11
0
def get_borrow_list(request):
    if request.method != 'GET':
        return JsonResponse({'error': 'require GET'})
    username = check_username(request)
    if not username:
        return JsonResponse({'error': 'please login'})
    borrower = User.objects.filter(username=username)
    if not borrower:
        return JsonResponse({'error': 'please login'})
    borrower = borrower.first()
    current_borrow = borrower.user_apply_set.filter(state=1)
    ret = []
    for equipment in current_borrow:
        count = equipment.count
        end_time = equipment.end_time
        target_equipment = equipment.target_equipment.to_dict()
        ret.append({
            'count': count,
            'target_equipment': target_equipment,
            'end_time': end_time
        })

    return JsonResponse({'posts': ret})