コード例 #1
0
ファイル: base_views.py プロジェクト: esimorre/openplm
def get_generic_data(request, type_='-', reference='-', revision='-', search=True):
    """
    Get a request and return a controller, a context dictionnary with elements common to all pages
    (search form, search data, search results, ...) and another dictionnary to update the
    request.session dictionnary.
    
    :param request: :class:`django.http.QueryDict`
    :param type_: :attr:`.PLMObject.type`
    :type type_: str
    :param reference: :attr:`.PLMObject.reference`
    :type reference: str
    :param revision: :attr:`.PLMObject.revision`
    :type revision: str
    :return: a :class:`PLMObjectController` or a :class:`UserController`
    :return: ctx
    :type ctx: dic
    :return: request.session
    :type request.session: dic
    """
    ctx = init_ctx(type_, reference, revision)
    # This case happens when we create an object (and therefore can't get a controller)
    if type_ == reference == revision == '-':
        obj = request.user
    else:
        obj = get_obj(type_, reference, revision, request.user)
    # Builds, update and treat Search form
    search_needed = "results" not in request.session
    search_id = "search_id_%s" 
    if request.method == "GET" and "type" in request.GET:
        type_form4creation = TypeFormWithoutUser(request.GET)
        search_form = SimpleSearchForm(request.GET, auto_id=search_id)
        request.session["type"] = request.GET["type"]
        request.session["q"] = request.GET.get("q", "")
        search_needed = True
    elif "type" in request.session:
        type_form4creation = TypeFormWithoutUser(request.session)
        search_form = SimpleSearchForm(request.session, auto_id=search_id)
    else:
        type_form4creation = TypeFormWithoutUser()
        request.session['type'] = 'Part'
        search_form = SimpleSearchForm(auto_id=search_id)

    if search and search_needed and search_form.is_valid():
        search_query = search_form.cleaned_data["q"]
        qset = search_form.search()
        request.session["search_query"] = search_query
        search_count = request.session["search_count"] = qset.count()
        qset = qset[:30]
        request.session["results"] = qset
    else:
        qset = request.session.get("results", [])
        search_query = request.session.get("search_query", "")
        search_count = request.session.get("search_count", 0)

    ctx.update({'results' : qset, 
                'search_query' : search_query,
                'search_count' : search_count,
                'type_form4creation' : type_form4creation,
                'search_form' : search_form,
                'link_creation' : False,
                'attach' : (obj, False),
                'obj' : obj,
              })
    if hasattr(obj, "menu_items"):
        ctx['object_menu'] = obj.menu_items
    if hasattr(obj, "check_permission"):
        ctx["is_owner"] = obj.check_permission("owner", False)
    if hasattr(obj, "check_readable"):
        ctx["is_readable"] = obj.check_readable(False)
    else:
        ctx["is_readable"] = True

    # little hack to avoid a KeyError
    # see https://github.com/toastdriven/django-haystack/issues/404
    from haystack import site
    for r in request.session.get("results", []):
        r.searchsite = site

    return obj, ctx
コード例 #2
0
ファイル: base.py プロジェクト: amarh/openPLM
def get_generic_data(request, type_='-', reference='-', revision='-', search=True,
        load_all=False):
    """
    Get a request and return a controller, a context dictionnary with elements common to all pages
    (search form, search data, search results, ...) and another dictionnary to update the
    request.session dictionnary.

    :param request: :class:`django.http.QueryDict`
    :param type_: :attr:`.PLMObject.type`
    :type type_: str
    :param reference: :attr:`.PLMObject.reference`
    :type reference: str
    :param revision: :attr:`.PLMObject.revision`
    :type revision: str
    :return: a :class:`.PLMObjectController` or a :class:`.UserController`
    :return: ctx
    :type ctx: dic
    :return: request.session
    :type request.session: dic
    """
    ctx = init_ctx(type_, reference, revision)
    # This case happens when we create an object (and therefore can't get a controller)
    save_session = False
    profile = request.user.profile
    restricted = profile.restricted
    if type_ == reference == revision == '-':
        obj = request.user
        obj_url = profile.plmobject_url
    else:
        obj = get_obj(type_, reference, revision, request.user)
        obj_url = obj.plmobject_url
        if not restricted:
            save_session = update_navigation_history(request, obj,
                type_, reference, revision)

    table = request.GET.get("table", "")
    save_session = save_session or table != ""
    if table == "1":
        request.session["as_table"] = True
    elif table == "0":
        request.session["as_table"] = False
    ctx["as_table"] = request.session.get("as_table")

    if not restricted: # a restricted account can not perform a search
        # Builds, update and treat Search form
        search_needed = "results" not in request.session or load_all
        if request.method == "GET" and "type" in request.GET:
            search_form = SimpleSearchForm(request.GET, auto_id=_SEARCH_ID)
            request.session["type"] = request.GET["type"]
            request.session["q"] = request.GET.get("q", "")
            request.session["search_official"] = request.GET.get("search_official", "")
            search_needed = True
            save_session = True
        elif "type" in request.session:
            search_form = SimpleSearchForm(request.session, auto_id=_SEARCH_ID)
        else:
            request.session['type'] = 'all'
            search_form = SimpleSearchForm(auto_id=_SEARCH_ID)
            save_session = True

        if search and search_needed and search_form.is_valid():
            search_query = search_form.cleaned_data["q"]
            qset = search_form.search()
            if load_all:
                qset = qset.load_all()
            request.session["search_query"] = search_query
            search_official = ["", "1"][search_form.cleaned_data["search_official"]]
            request.session["search_official"] = search_official
            search_count = request.session["search_count"] = qset.count()
            qset = qset[:30]
            request.session["results"] = qset
            save_session = True
        else:
            qset = request.session.get("results", [])
            search_query = request.session.get("search_query", "")
            search_count = request.session.get("search_count", 0)
            search_official = request.session.get("search_official", "")

        ctx.update({
           'results' : qset,
           'search_query' : search_query,
           'search_count' : search_count,
           'search_form' : search_form,
           'navigation_history' : request.session.get("navigation_history", []),
           'ctype': "Part" if request.session["type"] in ("all", "User") else request.session["type"],
        })

    ctx.update({
       'link_creation' : False,
       'attach' : (obj, False),
       'obj' : obj,
       'obj_url': obj_url,
       'restricted' : restricted,
       'is_contributor': profile.is_contributor,
    })
    if hasattr(obj, "menu_items"):
        ctx['object_menu'] = obj.menu_items
    if hasattr(obj, "check_permission"):
        ctx["is_owner"] = obj.check_permission("owner", False)
    if hasattr(obj, "check_readable"):
        ctx["is_readable"] = readable = obj.check_readable(False)
        if restricted and not readable:
            raise Http404
    else:
        ctx["is_readable"] = True
    # little hack to avoid a KeyError
    # see https://github.com/toastdriven/django-haystack/issues/404
    from haystack import site
    for r in request.session.get("results", []):
        r.searchsite = site

    if save_session:
        request.session.save()
    return obj, ctx
コード例 #3
0
ファイル: base.py プロジェクト: pcon-world/pcon_db
def get_generic_data(request,
                     type_='-',
                     reference='-',
                     revision='-',
                     search=True,
                     load_all=False):
    """
    Get a request and return a controller, a context dictionnary with elements common to all pages
    (search form, search data, search results, ...) and another dictionnary to update the
    request.session dictionnary.

    :param request: :class:`django.http.QueryDict`
    :param type_: :attr:`.PLMObject.type`
    :type type_: str
    :param reference: :attr:`.PLMObject.reference`
    :type reference: str
    :param revision: :attr:`.PLMObject.revision`
    :type revision: str
    :return: a :class:`.PLMObjectController` or a :class:`.UserController`
    :return: ctx
    :type ctx: dic
    :return: request.session
    :type request.session: dic
    """
    ctx = init_ctx(type_, reference, revision)
    # This case happens when we create an object (and therefore can't get a controller)
    save_session = False
    profile = request.user.profile
    restricted = profile.restricted
    if type_ == reference == revision == '-':
        obj = request.user
        obj_url = profile.plmobject_url
    else:
        obj = get_obj(type_, reference, revision, request.user)
        obj_url = obj.plmobject_url
        if not restricted:
            save_session = update_navigation_history(request, obj, type_,
                                                     reference, revision)

    table = request.GET.get("table", "")
    save_session = save_session or table != ""
    if table == "1":
        request.session["as_table"] = True
    elif table == "0":
        request.session["as_table"] = False
    ctx["as_table"] = request.session.get("as_table")

    if not restricted:  # a restricted account can not perform a search
        # Builds, update and treat Search form
        search_needed = "results" not in request.session or load_all
        if request.method == "GET" and "type" in request.GET:
            search_form = SimpleSearchForm(request.GET, auto_id=_SEARCH_ID)
            request.session["type"] = request.GET["type"]
            request.session["q"] = request.GET.get("q", "")
            request.session["search_official"] = request.GET.get(
                "search_official", "")
            search_needed = True
            save_session = True
        elif "type" in request.session:
            search_form = SimpleSearchForm(request.session, auto_id=_SEARCH_ID)
        else:
            request.session['type'] = 'all'
            search_form = SimpleSearchForm(auto_id=_SEARCH_ID)
            save_session = True

        if search and search_needed and search_form.is_valid():
            search_query = search_form.cleaned_data["q"]
            qset = search_form.search()
            if load_all:
                qset = qset.load_all()
            request.session["search_query"] = search_query
            search_official = ["", "1"
                               ][search_form.cleaned_data["search_official"]]
            request.session["search_official"] = search_official
            search_count = request.session["search_count"] = qset.count()
            qset = qset[:30]
            request.session["results"] = qset
            save_session = True
        else:
            qset = request.session.get("results", [])
            search_query = request.session.get("search_query", "")
            search_count = request.session.get("search_count", 0)
            search_official = request.session.get("search_official", "")

        ctx.update({
            'results':
            qset,
            'search_query':
            search_query,
            'search_count':
            search_count,
            'search_form':
            search_form,
            'navigation_history':
            request.session.get("navigation_history", []),
            'ctype':
            "Part" if request.session["type"] in ("all", "User") else
            request.session["type"],
        })

    ctx.update({
        'link_creation': False,
        'attach': (obj, False),
        'obj': obj,
        'obj_url': obj_url,
        'restricted': restricted,
        'is_contributor': profile.is_contributor,
    })
    if hasattr(obj, "menu_items"):
        ctx['object_menu'] = obj.menu_items
    if hasattr(obj, "check_permission"):
        ctx["is_owner"] = obj.check_permission("owner", False)
    if hasattr(obj, "check_readable"):
        ctx["is_readable"] = readable = obj.check_readable(False)
        if restricted and not readable:
            raise Http404
    else:
        ctx["is_readable"] = True
    # little hack to avoid a KeyError
    # see https://github.com/toastdriven/django-haystack/issues/404
    from haystack import site
    for r in request.session.get("results", []):
        r.searchsite = site

    if save_session:
        request.session.save()
    return obj, ctx