|
27
|
1 |
''' |
|
|
2 |
Created on 7 aout 2012 |
|
|
3 |
|
|
|
4 |
@author: gerard |
|
|
5 |
''' |
|
|
6 |
import logging |
|
|
7 |
import simplejson |
|
|
8 |
|
|
|
9 |
from dataparser.ClustersGetAttributes import ClustersGetAttributes |
|
|
10 |
from document.models import Annotationcluster |
|
|
11 |
from document.models import Cluster |
|
|
12 |
from document.models import Clusterdocumentweight |
|
|
13 |
from document.models import Document |
|
|
14 |
from document.models import Documentaryfile |
|
|
15 |
from django.contrib.auth.models import User |
|
|
16 |
from mediapartdb.MediapartReader import MediapartReader |
|
|
17 |
|
|
|
18 |
logger = logging.getLogger('document') |
|
|
19 |
|
|
|
20 |
# List of documents of a cluster with annotations |
|
|
21 |
class Clusters(object): |
|
|
22 |
|
|
|
23 |
def __init__(self, request): |
|
|
24 |
self.request = request |
|
|
25 |
|
|
|
26 |
def get_treemap_of_clusters(self): |
|
|
27 |
attr = ClustersGetAttributes(self.request) |
|
|
28 |
|
|
|
29 |
if not attr.get_user(): |
|
|
30 |
json = '{"error msg": "user is not defined"}' |
|
|
31 |
return json |
|
|
32 |
if not attr.get_documentary_file_id(): |
|
|
33 |
json = '{"error msg": "documentary_file_id is not defined"}' |
|
|
34 |
return json |
|
|
35 |
|
|
|
36 |
the_user = User.objects.get(username=attr.get_user()) |
|
|
37 |
|
|
|
38 |
if the_user: |
|
|
39 |
docFiles = Documentaryfile.objects.filter(id=int(attr.get_documentary_file_id()), user_id=the_user.id ) |
|
|
40 |
if len(docFiles) == 0: |
|
|
41 |
json = '{"error msg": "There is no documentary file belonging to the user specified"}' |
|
|
42 |
return json |
|
|
43 |
|
|
|
44 |
reader = MediapartReader() |
|
|
45 |
|
|
|
46 |
json = {"clusters":[]} |
|
|
47 |
clusters = Cluster.objects.filter(documentaryfile=int(attr.get_documentary_file_id())) |
|
|
48 |
for cluster in clusters: |
|
|
49 |
jsoncluster = {'id':cluster.id} |
|
|
50 |
jsoncluster['title'] = cluster.title |
|
|
51 |
jsoncluster['abstract'] = cluster.description |
|
|
52 |
jsoncluster['weight'] = cluster.weight |
|
|
53 |
|
|
|
54 |
jsoncluster['annotations'] = [] |
|
|
55 |
|
|
|
56 |
annotations = Annotationcluster.objects.filter(cluster=cluster.id) |
|
|
57 |
for annotation in annotations: |
|
|
58 |
jsonanno = {'id':annotation.id} |
|
|
59 |
jsonanno['user'] = str(annotation.user) |
|
|
60 |
jsonanno['text'] = str(annotation.description) |
|
|
61 |
jsoncluster['annotations'].append(jsonanno) |
|
|
62 |
|
|
|
63 |
jsoncluster['documents'] = [] |
|
|
64 |
|
|
|
65 |
doc_weights = Clusterdocumentweight.objects.filter(cluster_id=cluster.id) |
|
|
66 |
sorted_doc_weights = sorted(doc_weights, key=lambda a_cluster_doc_weight: a_cluster_doc_weight.weight) |
|
|
67 |
|
|
|
68 |
for index_doc, doc_weights in enumerate(sorted_doc_weights): |
|
|
69 |
if index_doc < 3: |
|
|
70 |
jsondoc = {'id':doc_weights.document.documentId} |
|
|
71 |
doc = Document.objects.get(documentId=doc_weights.document.documentId) |
|
|
72 |
jsondoc['title'] = str(doc.title.encode("utf8")) |
|
|
73 |
url = reader.get_url(str(doc_weights.document.documentId)) |
|
|
74 |
jsondoc['url_document'] = url |
|
|
75 |
jsondoc['date'] = str(doc.date.isoformat()) |
|
|
76 |
category = reader.get_category(str(doc_weights.document.documentId)) |
|
|
77 |
jsondoc['category'] = category |
|
|
78 |
jsondoc['url_image'] = 'TO BE DEFINED' |
|
|
79 |
jsondoc['weight'] = float(doc_weights.weight) |
|
|
80 |
|
|
|
81 |
jsoncluster['documents'].append(jsondoc) |
|
|
82 |
else: |
|
|
83 |
break |
|
|
84 |
jsoncluster['document_number'] = len(sorted_doc_weights) |
|
|
85 |
json['clusters'].append(jsoncluster) |
|
|
86 |
|
|
|
87 |
result = simplejson.dumps(json) |
|
|
88 |
logger.debug(result) |
|
|
89 |
return result |