Example #1
0
def videos_controller(request: wsgi.WSGIRequest):
    response = HttpResponse()

    if request.method == 'GET':
        user_name = session.get_user_name(request)
        user = User.get_user_by_user_name(user_name)
        videos = models.Video.objects.filter(user_id=user.id)
        ret = []
        for video in videos:
            ret.append(convert_video_model_to_dict(video))
        utils.serve_data_response(response, ret)
    elif request.method == 'POST':
        data = json.loads(request.body)
        if not isinstance(data, dict):
            response.status_code = 400
            response.content = 'wrong parameters: check the data'
        else:
            video_id = data.get('video_id')
            video_url = data.get('video_url')
            if video_id is None or video_url is None:
                utils.serve_400_response(
                    'wrong parameters: check the video_id and the video_url cannot be empty'
                )
            else:
                models.Video.objects.filter(id=video_id).update(
                    video_url=video_url)
            utils.serve_data_response(response, '')
    return response
Example #2
0
def approve_message_controller(request: wsgi.WSGIRequest):
    response = HttpResponse()

    if request.method == "POST":
        data = json.loads(request.body)
        params = data.get('params')
        print(params)
        if not isinstance(params, dict):
            response.content = 'wrong parameters : params'
            response.status_code = 400
        message_ids = params.get('message_ids')
        approve_status = params.get('approve_status')
        print(message_ids)
        print(approve_status)
        if message_ids is not None and approve_status is not None and isinstance(approve_status, bool):
            user_name = session.get_user_name(request)
            user = user_model.get_user_by_user_name(user_name)
            for message_id in message_ids:
                message = get_message_by_message_model(message_id)
                if models.User_Message.objects.filter(user_id=user.id, message_id=message_id).exists():
                    models.User_Message.objects.filter(user_id=user.id, message_id=message_id).update(
                        true_or_false=approve_status)
                else:
                    models.User_Message.objects.create(user_id=user, message_id=message,
                                                       true_or_false=approve_status)
            utils.serve_data_response(response,'')

        else:
            response.content = 'wrong parameters : message_ids or approve_status is empty or incorrect'
            response.status_code = 400
    return response
Example #3
0
def check_is_login_and_get_username_controller(request):
    response = HttpResponse()
    if session.check_is_login(request):
        response.status_code = 200
        user_name = session.get_user_name(request)
        print(user_name)
        utils.serve_data_response(response, user_name)
    else:
        response.status_code = 400
        response.content = 'Not login'
    return response
Example #4
0
def get_user_info_controller(request: wsgi.WSGIRequest):
    response = HttpResponse()
    if request.method == 'GET':
        utils.serve_data_response(
            response,
            convert_user_model_to_dict(
                get_user_by_user_name(session.get_user_name(request))))
    else:
        response.status_code = 400

    return response
Example #5
0
def get_all_device_controller(request: wsgi.WSGIRequest):
    response = HttpResponse()

    if request.method == 'GET':
        devices = all_devices()
        ret = []
        for d in devices:
            ret.append(convert_models_device_to_dict(d))
        utils.serve_data_response(response, ret)
    else:
        response.status_code = 400
        response.content = "wrong request method"
    return response
Example #6
0
def get_message_info_controller(request: wsgi.WSGIRequest):
    response = HttpResponse()

    if request.method == "GET":
        user_name = session.get_user_name(request)
        utils.serve_data_response(response, {
            "total": len(get_all_messages()),
            "unread": len(get_messages_unread_by_user_name(user_name))
        })
    else:
        response.status_code = 400
        response.content = 'wrong http method'

    return response
Example #7
0
def device_controller(request: wsgi.WSGIRequest):
    response = HttpResponse()
    if request.method == "GET":
        device_id = request.GET.get("device_id")
        if device_id is None:
            response.status_code = 400
            response.content = "parameters device_id is None"
        else:
            device = get_device_by_id(device_id)
            ret = convert_models_device_to_dict(device)
            utils.serve_data_response(response, ret)
    else:
        response.status_code = 400
        response.content = "wrong request method"
    return response
Example #8
0
def messages(request: wsgi.WSGIRequest):
    response = HttpResponse()
    if request.method == "GET":
        user_name = session.get_user_name(request)
        offset = int(request.GET.get('offset', 0))
        limit = int(request.GET.get('limit', - 1))
        the_type = request.GET.get('type', 'all')
        response.status_code = 200
        if the_type == 'all':
            utils.serve_data_response(response, get_all_messages_by_user_name(user_name, limit, offset))
        elif the_type == 'unread':
            utils.serve_data_response(response, get_messages_unread_by_user_name(user_name, limit, offset))
        else:
            response.content = "wrong parameter 'type' "
            response.status_code = 400

    elif request.method == "POST":
        data = json.loads(request.body)
        params = data.get('params')
        if not isinstance(params, dict):
            response.content = "wrong prameters params"
            response.status_code = 400
            return response
        messages_id = params.get('id')
        try:
            if messages_id is not None:
                if models.Message.objects.filter(id=messages_id).exists():
                    models.Message.objects.filter(id=messages_id).update(content=params.get('content'),
                                                                         img_url=params.get('img_url'))
        except db.DatabaseError:
            utils.serve_error_response(response, status_code.MESSAGE_MODEL_CANNOT_UPDATE, str(db.DatabaseError))
        except:
            utils.serve_error_response(response, status_code.MESSAGE_MODEL_CANNOT_UPDATE, "Error: post message")
        else:
            utils.serve_data_response(response, params.get('id'))

    return response