|
27
|
1 |
''' |
|
|
2 |
Created on 27 juil. 2012 |
|
|
3 |
|
|
|
4 |
@author: gerard |
|
|
5 |
''' |
|
|
6 |
import datetime |
|
|
7 |
import logging |
|
37
|
8 |
import simplejson |
|
27
|
9 |
from time import mktime |
|
|
10 |
from django.core.cache import cache |
|
|
11 |
|
|
|
12 |
from dataparser.ClientDocumentaryFileAttributes import ClientDocumentaryFileAttributes |
|
|
13 |
from dataparser.ClientDocumentaryFileVisibilityAttributes import ClientDocumentaryFileVisibilityAttribute |
|
|
14 |
from document.models import Cluster |
|
|
15 |
from document.models import Image |
|
|
16 |
from document.models import Document |
|
|
17 |
from document.models import Documentaryfile |
|
|
18 |
from document.models import Clusterdocumentweight |
|
|
19 |
from django.contrib.auth.models import User |
|
|
20 |
|
|
|
21 |
logger = logging.getLogger('document') |
|
|
22 |
|
|
|
23 |
class DocumentaryFile(object): |
|
|
24 |
|
|
|
25 |
def __init__(self, request): |
|
|
26 |
logger.info('DocumentaryFile init') |
|
|
27 |
self.request = request |
|
|
28 |
|
|
|
29 |
def create(self): |
|
|
30 |
logger.info('create DocumentaryFile') |
|
|
31 |
attr = ClientDocumentaryFileAttributes(self.request) |
|
37
|
32 |
'''logger.info('user = ' + str(attr.get_user())) |
|
27
|
33 |
logger.info('query_id = ' + str(attr.get_query_id())) |
|
37
|
34 |
logger.info('public = ' + str(attr.get_visibility()))''' |
|
|
35 |
|
|
|
36 |
#query_id = attr.get_query_id() |
|
|
37 |
if attr.get_query_id(): |
|
|
38 |
key1 = cache.get(attr.get_query_id()) |
|
27
|
39 |
context = cache.get(key1['weblab_data_key']) |
|
37
|
40 |
elif self.request.session['key1']: |
|
|
41 |
context = cache.get(self.request.session['key1']) |
|
27
|
42 |
else: |
|
|
43 |
json = '{"Error": "Invalid query id"}' |
|
|
44 |
logger.info(json) |
|
37
|
45 |
|
|
|
46 |
if context == None: |
|
|
47 |
logger.info("cache empty") |
|
|
48 |
json = '{"Error": "cache empty"}' |
|
|
49 |
logger.info(json) |
|
|
50 |
else: |
|
|
51 |
logger.info("cache not empty") |
|
|
52 |
list_concepts,concepts_with_detailed_documents_list = context |
|
|
53 |
logger.info('list_concepts' + str(list_concepts)) |
|
|
54 |
logger.info('concepts_with_detailed_documents_list' + str(concepts_with_detailed_documents_list)) |
|
|
55 |
#parse to get the value to save the documentary file |
|
|
56 |
json = self.parseAndSaveValue(list_concepts,concepts_with_detailed_documents_list, attr ) |
|
|
57 |
|
|
27
|
58 |
return json |
|
|
59 |
|
|
42
|
60 |
|
|
|
61 |
def modify(self,attr): |
|
|
62 |
logger.info('modify') |
|
|
63 |
|
|
|
64 |
logger.info('get_description = ' + str(attr.get_description().encode("utf8"))) |
|
|
65 |
logger.info('get_title = ' + str(attr.get_title().encode("utf8"))) |
|
|
66 |
logger.info('get_visibility = ' + str(attr.get_visibility())) |
|
|
67 |
try: |
|
|
68 |
documentaryfile = Documentaryfile.objects.get(pk=attr.get_documentary_file_id()) |
|
|
69 |
except Documentaryfile.DoesNotExist: |
|
|
70 |
json = '{"Error": "Invalid documentary id"}' |
|
|
71 |
logger.info(json) |
|
|
72 |
return json |
|
|
73 |
|
|
|
74 |
logger.info('documentaryfile.user.name = ' + str(documentaryfile.user.username)) |
|
|
75 |
if documentaryfile.user.username == attr.get_user(): |
|
|
76 |
if attr.get_visibility() == 'on': |
|
|
77 |
visibility_attr = True |
|
|
78 |
else: |
|
|
79 |
visibility_attr = False |
|
|
80 |
'''else: |
|
|
81 |
json = '{"Error" : "Invalid visibility parameter"}' |
|
|
82 |
logger.info(json) |
|
|
83 |
return json''' |
|
|
84 |
|
|
|
85 |
for thecluster in documentaryfile.cluster_set.all(): |
|
|
86 |
for thedocument in thecluster.document.all(): |
|
|
87 |
for theannotationdoc in thedocument.annotationdocument_set.all(): |
|
|
88 |
theannotationdoc.visibility = visibility_attr |
|
|
89 |
theannotationdoc.save() |
|
|
90 |
|
|
|
91 |
title_attr = str(attr.get_title().encode("utf8")) |
|
|
92 |
description_attr = str(attr.get_description().encode("utf8")) |
|
|
93 |
|
|
|
94 |
documentaryfile.visibility = visibility_attr |
|
|
95 |
documentaryfile.title = title_attr |
|
|
96 |
documentaryfile.description = description_attr |
|
|
97 |
|
|
|
98 |
documentaryfile.save() |
|
|
99 |
json = '{"modif": ok "}' |
|
|
100 |
logger.info(json) |
|
|
101 |
else: |
|
|
102 |
json = '{"Error": "User does not matched"}' |
|
|
103 |
logger.info(json) |
|
|
104 |
|
|
|
105 |
return json |
|
|
106 |
|
|
|
107 |
|
|
27
|
108 |
def visibilityChange(self): |
|
|
109 |
logger.info('visibilityChange') |
|
|
110 |
attr = ClientDocumentaryFileVisibilityAttribute(self.request) |
|
|
111 |
logger.info('get_user = ' + str(attr.get_user())) |
|
|
112 |
logger.info('get_documentary_file_id = ' + str(attr.get_documentary_file_id())) |
|
|
113 |
logger.info('get_visibility = ' + str(attr.get_visibility())) |
|
|
114 |
try: |
|
|
115 |
documentaryfile = Documentaryfile.objects.get(pk=attr.get_documentary_file_id()) |
|
|
116 |
except Documentaryfile.DoesNotExist: |
|
|
117 |
json = '{"Error": "Invalid documentary id"}' |
|
|
118 |
logger.info(json) |
|
|
119 |
return json |
|
|
120 |
|
|
|
121 |
logger.info('documentaryfile.user.name = ' + str(documentaryfile.user.username)) |
|
|
122 |
if documentaryfile.user.username == attr.get_user(): |
|
|
123 |
if attr.get_visibility() == 'public': |
|
|
124 |
visibility_attr = True |
|
|
125 |
elif attr.get_visibility() == 'private': |
|
|
126 |
visibility_attr = False |
|
|
127 |
else: |
|
|
128 |
json = '{"Error" : "Invalid visibility parameter"}' |
|
|
129 |
logger.info(json) |
|
|
130 |
return json |
|
|
131 |
|
|
|
132 |
for thecluster in documentaryfile.cluster_set.all(): |
|
|
133 |
for thedocument in thecluster.document.all(): |
|
|
134 |
for theannotationdoc in thedocument.annotationdocument_set.all(): |
|
|
135 |
theannotationdoc.visibility = visibility_attr |
|
|
136 |
theannotationdoc.save() |
|
|
137 |
|
|
|
138 |
documentaryfile.visibility = visibility_attr |
|
|
139 |
documentaryfile.save() |
|
|
140 |
json = '{"visibility": "' + attr.get_visibility() + '"}' |
|
|
141 |
logger.info(json) |
|
|
142 |
else: |
|
|
143 |
json = '{"Error": "User does not matched"}' |
|
|
144 |
logger.info(json) |
|
|
145 |
|
|
|
146 |
return json |
|
|
147 |
|
|
37
|
148 |
def delete(self,docId): |
|
27
|
149 |
logger.info('delete DocumentaryFile') |
|
37
|
150 |
#attr = ClientDocumentaryFileDeleteAttributes(self.request) |
|
|
151 |
'''logger.info('get_user = ' + str(attr.get_user())) |
|
27
|
152 |
logger.info('get_documentary_file_id = ' + str(attr.get_documentary_file_id())) |
|
37
|
153 |
docId = attr.get_documentary_file_id()''' |
|
27
|
154 |
if docId == '': |
|
|
155 |
json= '{"Error": "No documentary_file_id attribute in the http post request"}' |
|
|
156 |
logger.info(json) |
|
|
157 |
return json |
|
|
158 |
|
|
|
159 |
try: |
|
37
|
160 |
documentaryfile = Documentaryfile.objects.get(pk=docId) |
|
27
|
161 |
except Documentaryfile.DoesNotExist: |
|
|
162 |
json= '{"Error": "Invalid documentary_file_id"}' |
|
|
163 |
logger.info(json) |
|
|
164 |
return json |
|
|
165 |
|
|
|
166 |
logger.info('documentaryfile.user.username = ' + str(documentaryfile.user.username)) |
|
37
|
167 |
logger.info('self.request.user = ' + str(self.request.user)) |
|
|
168 |
if str(documentaryfile.user.username) == str(self.request.user): |
|
27
|
169 |
#verify if the associated documents are associated to another documentaryfile. if not delete the documents |
|
|
170 |
for thecluster in documentaryfile.cluster_set.all(): |
|
|
171 |
for thedocument in thecluster.document.all(): |
|
|
172 |
nb = (Clusterdocumentweight.objects.filter(document_id=thedocument.documentId)).count() |
|
|
173 |
logger.info('nb' + str(nb)) |
|
|
174 |
if nb == 1: |
|
|
175 |
thedocument.delete() |
|
|
176 |
|
|
|
177 |
documentaryfile.delete() |
|
|
178 |
json = '{"documentary_file_deleted" :' + str(docId) + '}' |
|
|
179 |
logger.info(json) |
|
|
180 |
else: |
|
|
181 |
json= '{"Error": "User does not match"}' |
|
|
182 |
logger.info(json) |
|
|
183 |
return json |
|
|
184 |
|
|
|
185 |
def parseAndSaveValue(self, list_concepts,concepts_with_detailed_documents_list, attr): |
|
|
186 |
#parse the context |
|
37
|
187 |
logger.info('query_context ********** ='+str(self.request.session.items())) |
|
27
|
188 |
try: |
|
37
|
189 |
#text = str(((attr.get_user()).replace(' ', '')).replace('\n', '')) |
|
|
190 |
text = str(attr.get_user()) |
|
|
191 |
logger.info('attr.get_user! !!! = '+text) |
|
|
192 |
tutu = 'cobled' |
|
|
193 |
logger.info('attr.get_user! !!! = '+text) |
|
|
194 |
user_attr = User.objects.get(username=tutu) |
|
27
|
195 |
except User.DoesNotExist, err: |
|
|
196 |
logger.info(' Error: '+ str(err)) |
|
|
197 |
json = '{"Error": "User does not existed"}' |
|
|
198 |
return json |
|
|
199 |
visibility_attr = attr.get_visibility() |
|
|
200 |
if visibility_attr == 'public': |
|
|
201 |
visibility_bool = True |
|
|
202 |
else: |
|
|
203 |
visibility_bool = False |
|
|
204 |
|
|
|
205 |
title_attr = attr.get_title() |
|
|
206 |
description_attr = attr.get_description() |
|
|
207 |
#query_id_attr = attr.get_query_id() |
|
|
208 |
#TODO url image |
|
37
|
209 |
|
|
27
|
210 |
now = datetime.datetime.now() |
|
|
211 |
mktime(now.timetuple()) |
|
37
|
212 |
logger.info(mktime(now.timetuple())) |
|
|
213 |
|
|
|
214 |
for concept_index, concept_with_detailed_documents_list in enumerate(concepts_with_detailed_documents_list) : |
|
|
215 |
logger.info('url image first cluster'+list_concepts[concept_index]['url_image']) |
|
|
216 |
image1 = Image(url=list_concepts[concept_index]['url_image']) |
|
|
217 |
image1.save() |
|
|
218 |
break |
|
|
219 |
if attr.get_json_streamgraph(): |
|
|
220 |
logger.info('attr.get_json_streamgraph ****'+str(attr.get_json_streamgraph())) |
|
|
221 |
jsonstreamgraph = attr.get_json_streamgraph() |
|
|
222 |
else: |
|
|
223 |
logger.info('request_streamgraph ****'+str(self.request.session['jsonStreamgraph'])) |
|
|
224 |
jsonstreamgraph = self.request.session['jsonStreamgraph'] |
|
|
225 |
|
|
|
226 |
if attr.get_json_treemap(): |
|
|
227 |
logger.info('attr.get_json_streamgraph ****'+str(attr.get_json_treemap())) |
|
|
228 |
jsontreemap = attr.get_json_treemap() |
|
|
229 |
else: |
|
|
230 |
logger.info('request_streamgraph ****'+str(self.request.session['jsonTreemap'])) |
|
|
231 |
jsontreemap = self.request.session['jsonTreemap'] |
|
|
232 |
|
|
27
|
233 |
# create the documentary file |
|
37
|
234 |
#dossierDoc1 = Documentaryfile(title=title_attr, date=now , description=description_attr, visibility=visibility_bool, list_concepts=list_concepts, concepts_with_detailed_documents_list = concepts_with_detailed_documents_list, image=image1, user=user_attr) |
|
|
235 |
dossierDoc1 = Documentaryfile(title=title_attr, date=now , description=description_attr, visibility=visibility_bool, jsonstreamgraph=jsonstreamgraph, jsontreemap = jsontreemap, image=image1, user=user_attr, list_concepts=list_concepts, concepts_with_detailed_documents_list = concepts_with_detailed_documents_list,) |
|
27
|
236 |
dossierDoc1.save() |
|
37
|
237 |
|
|
|
238 |
data = simplejson.loads(self.request.session['jsonTreemap']) |
|
|
239 |
logger.info('DATA BEFORE ***'+str(data)) |
|
|
240 |
for cluster in data['clusters']: |
|
|
241 |
cluster['doc_id'] = int(dossierDoc1.id) |
|
|
242 |
cluster['user'] = dossierDoc1.user.username |
|
|
243 |
logger.info('DATA AFTER ***'+str(data)) |
|
|
244 |
jsontreemap = simplejson.dumps(data) |
|
|
245 |
dossierDoc1.jsontreemap = jsontreemap |
|
|
246 |
dossierDoc1.save() |
|
|
247 |
#nb_concept = len(concepts_with_detailed_documents_list) |
|
|
248 |
|
|
27
|
249 |
for concept_index, concept_with_detailed_documents_list in enumerate(concepts_with_detailed_documents_list) : |
|
37
|
250 |
imagecluster = Image(url=list_concepts[concept_index]['url_image']) |
|
|
251 |
imagecluster.save() |
|
|
252 |
cluster1 = Cluster.objects.create(title=list_concepts[concept_index]['title'], description=list_concepts[concept_index]['abstract'], weight=list_concepts[concept_index]['score'], documentaryfile=dossierDoc1, image=imagecluster) |
|
|
253 |
logger.info('CLUSTERID'+str(cluster1.id)) |
|
27
|
254 |
for detailed_document in concept_with_detailed_documents_list: |
|
|
255 |
#Verify if the document exist already in database |
|
|
256 |
try: |
|
|
257 |
doc1 = Document.objects.get(pk=detailed_document['id']) |
|
|
258 |
except Document.DoesNotExist: |
|
|
259 |
doc1 = Document.objects.create(documentId=detailed_document['id'], title=detailed_document['title'],description=detailed_document['abstract'],date=datetime.datetime.fromtimestamp(int(detailed_document['date'])).isoformat(), image=image1) |
|
|
260 |
|
|
|
261 |
clusterDocWeight1 = Clusterdocumentweight(cluster=cluster1, document=doc1, weight=detailed_document['weight']) |
|
|
262 |
clusterDocWeight1.save() |
|
|
263 |
json= '{"documentary_file_created":' + str(dossierDoc1.id) + '}' |
|
|
264 |
logger.info(json) |
|
|
265 |
return json |