Ejemplo n.º 1
0
def articlesets(request, project, what):
    """
    Project articlesets page
    """
    if what is None: what = "favourite"
    if what.startswith("/"): what = what[1:]
    

    tables = [("favourite", '<i class="icon-star"></i> <b>Favourites</b>', dict()),
              ("own", "Own Sets", dict(project=project, codingjob_set__id='null')),
              ("linked", "Linked Sets", dict(projects_set=project)),
              ("codingjob", "Coding Job Sets", dict()),
              ]
    selected_filter = {name : filter for (name, label, filter) in tables}[what]

    if what == "favourite":
        # ugly solution - get project ids that are favourite and use that to filter, otherwise would have to add many to many to api?
        # (or use api request.user to add only current user's favourite status). But good enough for now...

        # they need to be favourte AND still contained in the project
        ids = project.favourite_articlesets.filter(Q(project=project.id) | Q(projects_set=project.id)).values_list("id", flat=True)
        if ids: 
            selected_filter["pk"] = ids
        else:
            no_favourites = True
            # keep the table with all ids - better some output than none
            all_ids = ArticleSet.objects.filter(Q(project=project.id) | Q(projects_set=project.id)).values_list("id", flat=True)
            if all_ids:
                selected_filter["pk"] = all_ids
            else:
                no_sets = True
                selected_filter["name"] = "This is a really stupid way to force an empty table (so sue me!)"
            
    elif what == "codingjob":
        # more ugliness. Filtering the api on codingjob_set__id__isnull=False gives error from filter set
        ids = ArticleSet.objects.filter(Q(project=project.id) | Q(projects_set=project.id), codingjob_set__id__isnull=False)
        ids = [id for (id, ) in ids.values_list("id")]
        if ids: 
            selected_filter["pk"] = ids
        else:
            selected_filter["name"] = "This is a really stupid way to force an empty table (so sue me!)"
    
    url = reverse('articleset', args=[project.id, 123]) 

    table = FavouriteDatatable(resource=ArticleSet, label="article set", set_url=url + "?star=1", unset_url=url+"?star=0")
    table = table.rowlink_reverse('articleset', args=[project.id, '{id}'])
    table = table.filter(**selected_filter)
    table = table.hide("project")

    #table.url += "&project_for_favourites={project.id}".format(**locals())
    table = table.add_arguments(project_for_favourites=project.id)
    
    context = project
    menu = PROJECT_MENU
    deleted = session_pop(request.session, "deleted_articleset")
    unlinked = session_pop(request.session, "unlinked_articleset")
    selected = "article sets"
    
    return render(request, 'navigator/project/articlesets.html', locals())
Ejemplo n.º 2
0
def articlesets(request, project, what):
    """
    Project articlesets page
    """
    if what is None: what = "favourite"
    if what.startswith("/"): what = what[1:]

    tables = [
        ("favourite", '<i class="icon-star"></i> <b>Favourites</b>', dict()),
        ("own", "Own Sets", dict(project=project, codingjob_set__id='null')),
        ("linked", "Linked Sets", dict(projects_set=project)),
        ("codingjob", "Coding Job Sets", dict()),
    ]
    selected_filter = {name: filter for (name, label, filter) in tables}[what]

    if what == "favourite":
        # ugly solution - get project ids that are favourite and use that to filter, otherwise would have to add many to many to api?
        # (or use api request.user to add only current user's favourite status). But good enough for now...

        # they need to be favourte AND still contained in the project
        ids = project.favourite_articlesets.filter(
            Q(project=project.id) | Q(projects_set=project.id)).values_list(
                "id", flat=True)
        if ids:
            selected_filter["pk"] = ids
        else:
            no_favourites = True
            # keep the table with all ids - better some output than none
            all_ids = ArticleSet.objects.filter(
                Q(project=project.id)
                | Q(projects_set=project.id)).values_list("id", flat=True)
            if all_ids:
                selected_filter["pk"] = all_ids
            else:
                no_sets = True
                selected_filter[
                    "name"] = "This is a really stupid way to force an empty table (so sue me!)"

    elif what == "codingjob":
        # more ugliness. Filtering the api on codingjob_set__id__isnull=False gives error from filter set
        ids = ArticleSet.objects.filter(Q(project=project.id)
                                        | Q(projects_set=project.id),
                                        codingjob_set__id__isnull=False)
        ids = [id for (id, ) in ids.values_list("id")]
        if ids:
            selected_filter["pk"] = ids
        else:
            selected_filter[
                "name"] = "This is a really stupid way to force an empty table (so sue me!)"

    url = reverse('articleset', args=[project.id, 123])

    table = FavouriteDatatable(resource=ArticleSet,
                               label="article set",
                               set_url=url + "?star=1",
                               unset_url=url + "?star=0")
    table = table.rowlink_reverse('articleset', args=[project.id, '{id}'])
    table = table.filter(**selected_filter)
    table = table.hide("project")

    #table.url += "&project_for_favourites={project.id}".format(**locals())
    table = table.add_arguments(project_for_favourites=project.id)

    context = project
    menu = PROJECT_MENU
    deleted = session_pop(request.session, "deleted_articleset")
    unlinked = session_pop(request.session, "unlinked_articleset")
    selected = "article sets"

    return render(request, 'navigator/project/articlesets.html', locals())