Exemplo n.º 1
0
def set_language(lan_code):
    "Set language code"
    if lan_code != None and lan_code != "":
        setattr(threading.currentThread(), "language_code", lan_code)
        setattr(threading.current_thread(), "language_code", lan_code)
    else:
        language = applications.get_settings().LANGUAGE_CODE
        setattr(threading.currentThread(), "language_code", language)
        setattr(threading.current_thread(), "language_code", language)
Exemplo n.º 2
0
 def register_view():
     return applications.get_settings().AUTHORIZATION_ENGINE.register_view(
         app=get_app_name(), view=get_view_path())
Exemplo n.º 3
0
def call(request):
    """
    Call request handler
    :param request:
    :return:
    """
    try:
        user = request.user
        #if user.is_anonymous():
        #    return HttpResponse('401 Unauthorized', status=401)
        #if not user.is_staff and not user.is_superuser:
        #    return HttpResponse('401 Unauthorized', status=401)
        post_data = JSON.from_json(request.body)
        if not post_data.has_key("path"):
            raise Exception("Api post without using path")
        path = post_data["path"]
        view = post_data["view"]
        if not post_data.has_key("offset_minutes"):
            raise (Exception(
                "It look like you forget post 'offset_minutes' from client."
                "Remember that before ajax post please set 'offset_minutes' from browser."
                "How to calculate 'offset_minutes'?:"
                "var now = new Date();"
                "var offset_minutes = now.getTimezoneOffset();"))
        offset_minutes = post_data["offset_minutes"]
        setattr(threading.current_thread(), "client_offset_minutes",
                offset_minutes)
        setattr(threading.currentThread(), "client_offset_minutes",
                offset_minutes)

        path = get_api_path(path)

        view_privileges = applications.get_settings(
        ).AUTHORIZATION_ENGINE.get_view_of_user(view=view,
                                                username=user.username)
        if user.is_superuser:
            view_privileges = {"is_public": True}

        method_path = path.split("/")[path.split("/").__len__() - 1]
        module_path = path[0:path.__len__() - method_path.__len__() - 1]
        mdl = None
        try:
            mdl = importlib.import_module(module_path.replace("/", "."))
        except ImportError as ex:
            logger.debug(
                Exception(
                    "import {0} is error or not found".format(module_path)))
            logger.debug(ex)
            raise Exception(
                "import {0} is error or not found.Error description {1}".
                format(module_path, ex.message))

        except Exception as ex:
            if type(ex) is str:
                raise Exception("import '{0}' encountered '{1}'".format(
                    module_path, ex))
            elif hasattr(ex, "messagte"):
                raise Exception("import '{0}' encountered '{1}'".format(
                    module_path, ex))
            else:
                raise Exception("import '{0}' encountered '{1}'".format(
                    module_path, ex))

        ret = None

        if mdl != None:
            try:
                if post_data.has_key("data"):
                    ret = getattr(mdl, method_path)({
                        "privileges":
                        view_privileges,
                        "data":
                        post_data.get("data", {}),
                        "user":
                        user,
                        "request":
                        request,
                        "view":
                        view
                    })
                else:
                    ret = getattr(mdl, method_path)({
                        "privileges": view_privileges,
                        "data": {},
                        "user": user,
                        "request": request,
                        "view": view
                    })

            except Exception as ex:
                raise Exception(
                    "Call  '{0}' in '{1}' encountered '{2}'".format(
                        method_path, module_path, ex))
        ret_data = JSON.to_json(ret)

        return HttpResponse(ret_data)
    except Exception as ex:
        logger.debug(ex)
        raise ex