Exemplo n.º 1
0
def change_user_index(request):
    if not check_login(request):
        raise AValidation400Error(detail="Unknow",
                                  code=ErrorCodes['global']['not_allowed'],
                                  errcode=ErrorCodes['global']['not_allowed'])
    device_name = request.POST.get('device_name')
    if not device_name:
        raise AValidation400Error(detail="Unknow",
                                  code=ErrorCodes['global']['required'],
                                  errcode=ErrorCodes['global']['required'])
    user = User.objects.get(id=request.session.get("userid"))
    try:
        device_bind = DeviceBind.objects.get(user=user,
                                             device__device_name=device_name,
                                             onActive=True)
    except DeviceBind.DoesNotExist:
        device_bind = None

    try:
        user_config = UserConfig.objects.get(user=user)
        user_config.index_device = device_bind
        user_config.save(update_fields=['index_device'])
    except UserConfig.DoesNotExist:
        user_config = UserConfig(user=user, index_device=device_bind)
        user_config.save()

    return JsonResponse(response_json(data={}), status=status.HTTP_201_CREATED)
Exemplo n.º 2
0
def get_share_qrcode(request):
    if not check_login(request):
        raise AValidation400Error(detail="Unknow",
                                  code=ErrorCodes['global']['not_allowed'],
                                  errcode=ErrorCodes['global']['not_allowed'])
    # request.session["userid"] = 2
    device_id = request.POST.get('device_id')
    if not device_id:
        raise AValidation400Error(detail="Unknow",
                                  code=ErrorCodes['global']['required'],
                                  errcode=ErrorCodes['global']['required'])
    user = User.objects.get(id=request.session.get("userid"))
    try:
        device = Device.objects.get(device_name=device_id)
        device_bind = DeviceBind.objects.get(device=device,
                                             user=user,
                                             onActive=True)
    except Device.DoesNotExist or DeviceBind.DoesNotExist:
        raise AValidation400Error(detail="Unknow",
                                  code=ErrorCodes['global']['not_allowed'],
                                  errcode=ErrorCodes['global']['not_allowed'])

    code = ''.join(random.sample(string.ascii_letters + string.digits, 16))
    code += str(user.id)
    code += str(device.id)
    m = hashlib.md5()
    m.update(code.encode("utf8"))
    code = m.hexdigest()
    cache.set(code, {"user": user.id, "device": device.id}, QRCODE_TIME_OUTS)
    print(code)
    url = QRCODE_API + BASE_URL + '/user/share/?share_code=' + code
    return JsonResponse(response_json(data={"url": url}),
                        status=status.HTTP_201_CREATED)
Exemplo n.º 3
0
def get_device_users(request):
    if not check_login(request):
        raise AValidation400Error(detail="Unknow",
                                  code=ErrorCodes['global']['not_allowed'],
                                  errcode=ErrorCodes['global']['not_allowed'])
    device_name = request.POST.get('device_name')
    if not device_name:
        raise AValidation400Error(detail="Unknow",
                                  code=ErrorCodes['global']['required'],
                                  errcode=ErrorCodes['global']['required'])
    user = User.objects.get(id=request.session.get("userid"))
    device_bind = DeviceBind.objects.get(user=user,
                                         device__device_name=device_name,
                                         onActive=True)
    device_binds = DeviceBind.objects.filter(
        device=device_bind.device, onActive=True).order_by("origin_user")
    serialize_data = DeviceSerializers(device_binds, many=True)
    return JsonResponse(response_json(data=serialize_data.data),
                        status=status.HTTP_201_CREATED)
Exemplo n.º 4
0
def unbinding_device(request):
    if not check_login(request):
        raise AValidation400Error(detail="Unknow",
                                  code=ErrorCodes['global']['not_allowed'],
                                  errcode=ErrorCodes['global']['not_allowed'])
    device_name = request.POST.get('device_name')
    if not device_name:
        raise AValidation400Error(detail="Unknow",
                                  code=ErrorCodes['global']['required'],
                                  errcode=ErrorCodes['global']['required'])
    user = User.objects.get(id=request.session.get("userid"))
    try:
        device_bind = DeviceBind.objects.get(user=user,
                                             device__device_name=device_name,
                                             onActive=True)
    except DeviceBind.DoesNotExist:
        raise AValidation400Error(detail="Unknow",
                                  code=ErrorCodes['user']['not_binding'],
                                  errcode=ErrorCodes['user']['not_binding'])
    device_action = DeviceBindAction(device=device_bind.device, user=user)
    device_action.unbind_user_device()
    return JsonResponse(response_json(data={}), status=status.HTTP_201_CREATED)
Exemplo n.º 5
0
def upgrade_device_done(request):
    if not check_login(request):
        raise AValidation400Error(detail="Unknow",
                                  code=ErrorCodes['global']['not_allowed'],
                                  errcode=ErrorCodes['global']['not_allowed'])
    device_name = request.POST.get('device_name')
    version = request.POST.get('version')
    if not device_name or not version:
        raise AValidation400Error(detail="Unknow",
                                  code=ErrorCodes['global']['required'],
                                  errcode=ErrorCodes['global']['required'])
    user = User.objects.get(id=request.session.get("userid"))
    try:
        device_bind = DeviceBind.objects.get(user=user,
                                             device__device_name=device_name,
                                             onActive=True)
    except DeviceBind.DoesNotExist:
        raise AValidation400Error(detail="Unknow",
                                  code=ErrorCodes['user']['not_binding'],
                                  errcode=ErrorCodes['user']['not_binding'])
    device_bind.device.device_version = version
    device_bind.device.save(update_fields=['device_version'])
    return JsonResponse(response_json(data={}), status=status.HTTP_201_CREATED)