views.py 3.55 KB
Newer Older
1 2 3 4 5 6
from gargantext.util.http import *
from gargantext.util.db import *
from gargantext.util.db_cache import cache
from gargantext.models import *
from gargantext.constants import *
from gargantext.settings import *
7
from gargantext.constants import USER_LANG
8
from datetime import datetime
9
from gargantext.views.pages.main import get_user_params
10 11

@requires_auth
delanoe's avatar
delanoe committed
12
def explorer(request, project_id, corpus_id):
13 14 15 16
    '''
    Graph explorer, also known as TinaWebJS, using SigmaJS.
    Nodes are ngrams (from title or abstract or journal name.
    Links represent proximity measure.
delanoe's avatar
delanoe committed
17 18

    Data are received in RESTfull mode (see rest.py).
19 20 21 22
    '''

    # we pass our corpus
    corpus = cache.Node[corpus_id]
23

delanoe's avatar
delanoe committed
24 25
    # security check
    user = cache.User[request.user.id]
26

delanoe's avatar
delanoe committed
27 28 29 30
    if corpus is None:
        raise Http404()
    if not user.owns(corpus):
        return HttpResponseForbidden()
31

32 33 34
    # get the maplist_id for modifications
    maplist_id = corpus.children(typename="MAPLIST").first().id

35 36 37
    # and the project just for project.id in corpusBannerTop
    project = cache.Node[project_id]

38
    # rendered page : explorer.html
39
    return render(
40
        template_name = 'graphExplorer/explorer.html',
41
        request = request,
42 43 44 45 46 47 48 49 50
        context = {
            'debug'     : settings.DEBUG   ,
            'request'   : request          ,
            'user'      : request.user     ,
            'date'      : datetime.now()   ,
            'project'   : project          ,
            'corpus'    : corpus           ,
            'maplist_id': maplist_id       ,
            'view'      : 'graph'          ,
51
            'user_parameters': get_user_params(request.user),
52
            'languages': USER_LANG
53 54 55 56 57 58 59 60
        },
    )



@requires_auth
def myGraphs(request, project_id, corpus_id):
    '''
delanoe's avatar
delanoe committed
61 62 63 64
    List all of my Graphs.

    Each Graphs as one Node of Cooccurrences.
    Each Graph is save in hyperdata of each Node.
65 66 67
    '''

    user = cache.User[request.user.id]
c24b's avatar
c24b committed
68

69 70 71 72 73 74 75
    # we pass our corpus
    corpus = cache.Node[corpus_id]

    # and the project just for project.id in corpusBannerTop
    project = cache.Node[project_id]

    coocs = corpus.children('COOCCURRENCES', order=True).all()
76

77 78
    coocs_count = dict()
    for cooc in coocs:
79
        # FIXME : approximativ number of nodes (not exactly what user sees in explorer)
80 81
        # Need to be connected with Graph Clustering
        cooc_nodes = (session.query(Ngram.id,func.count(Ngram.id))
82 83
                             .join(NodeNgramNgram, NodeNgramNgram.ngram1_id == Ngram.id)
                             .filter(NodeNgramNgram.node_id==cooc.id)
84 85
                             .filter(NodeNgramNgram.weight >= 1)
                             .group_by(Ngram.id)
86 87
                             .all()
                     )
88

89 90
        #coocs_count[cooc.id] = len(cooc_nodes)
        coocs_count[cooc.id] = len([cooc_node for cooc_node in cooc_nodes if cooc_node[1] > 1])
91

92 93
        print("coocs_count a posteriori", coocs_count)

94 95 96
    return render(
        template_name = 'pages/corpora/myGraphs.html',
        request = request,
97
        context = {
98 99 100 101 102
            'debug'        : settings.DEBUG,
            'request'      : request,
            'user'         : request.user,
            'date'         : datetime.now(),
            'project'      : project,
103
            'resourcename' : get_resource_by_name(corpus),
104 105 106
            'corpus'       : corpus,
            'view'         : 'myGraph',
            'coocs'        : coocs,
107
            'coocs_count'  : coocs_count,
c24b's avatar
c24b committed
108
            'user_parameters': get_user_params(request.user),
109
            'languages': USER_LANG,
110 111
        },
    )