|
27
|
1 |
''' |
|
|
2 |
Created on 27 juil. 2012 |
|
|
3 |
|
|
|
4 |
@author: gerard |
|
|
5 |
''' |
|
|
6 |
import datetime |
|
|
7 |
import logging |
|
|
8 |
|
|
|
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 dataparser.ClientDocumentaryFileDeleteAttributes import ClientDocumentaryFileDeleteAttributes |
|
|
15 |
from document.models import Cluster |
|
|
16 |
from document.models import Image |
|
|
17 |
from document.models import Document |
|
|
18 |
from document.models import Documentaryfile |
|
|
19 |
from document.models import Clusterdocumentweight |
|
|
20 |
from django.contrib.auth.models import User |
|
|
21 |
|
|
|
22 |
logger = logging.getLogger('document') |
|
|
23 |
|
|
|
24 |
class DocumentaryFile(object): |
|
|
25 |
|
|
|
26 |
def __init__(self, request): |
|
|
27 |
logger.info('DocumentaryFile init') |
|
|
28 |
self.request = request |
|
|
29 |
|
|
|
30 |
def create(self): |
|
|
31 |
logger.info('create DocumentaryFile') |
|
|
32 |
attr = ClientDocumentaryFileAttributes(self.request) |
|
|
33 |
logger.info('user = ' + str(attr.get_user())) |
|
|
34 |
logger.info('query_id = ' + str(attr.get_query_id())) |
|
|
35 |
logger.info('public = ' + str(attr.get_visibility())) |
|
|
36 |
query_id = attr.get_query_id() |
|
|
37 |
print query_id |
|
|
38 |
key1 = cache.get(query_id) |
|
|
39 |
if key1: |
|
|
40 |
context = cache.get(key1['weblab_data_key']) |
|
|
41 |
if context == None: |
|
|
42 |
print "cache empty" |
|
|
43 |
json = '{"Error": "Invalid query id"}' |
|
|
44 |
logger.info(json) |
|
|
45 |
else: |
|
|
46 |
print "cache not empty" |
|
|
47 |
list_concepts,concepts_with_detailed_documents_list = context |
|
|
48 |
logger.info('list_concepts' + str(list_concepts)) |
|
|
49 |
logger.info('concepts_with_detailed_documents_list' + str(concepts_with_detailed_documents_list)) |
|
|
50 |
#parse to get the value to save the documentary file |
|
|
51 |
json = self.parseAndSaveValue(list_concepts,concepts_with_detailed_documents_list, attr ) |
|
|
52 |
else: |
|
|
53 |
json = '{"Error": "Invalid query id"}' |
|
|
54 |
logger.info(json) |
|
|
55 |
return json |
|
|
56 |
|
|
|
57 |
def visibilityChange(self): |
|
|
58 |
logger.info('visibilityChange') |
|
|
59 |
attr = ClientDocumentaryFileVisibilityAttribute(self.request) |
|
|
60 |
logger.info('get_user = ' + str(attr.get_user())) |
|
|
61 |
logger.info('get_documentary_file_id = ' + str(attr.get_documentary_file_id())) |
|
|
62 |
logger.info('get_visibility = ' + str(attr.get_visibility())) |
|
|
63 |
try: |
|
|
64 |
documentaryfile = Documentaryfile.objects.get(pk=attr.get_documentary_file_id()) |
|
|
65 |
except Documentaryfile.DoesNotExist: |
|
|
66 |
json = '{"Error": "Invalid documentary id"}' |
|
|
67 |
logger.info(json) |
|
|
68 |
return json |
|
|
69 |
|
|
|
70 |
logger.info('documentaryfile.user.name = ' + str(documentaryfile.user.username)) |
|
|
71 |
if documentaryfile.user.username == attr.get_user(): |
|
|
72 |
if attr.get_visibility() == 'public': |
|
|
73 |
visibility_attr = True |
|
|
74 |
elif attr.get_visibility() == 'private': |
|
|
75 |
visibility_attr = False |
|
|
76 |
else: |
|
|
77 |
json = '{"Error" : "Invalid visibility parameter"}' |
|
|
78 |
logger.info(json) |
|
|
79 |
return json |
|
|
80 |
|
|
|
81 |
for thecluster in documentaryfile.cluster_set.all(): |
|
|
82 |
for thedocument in thecluster.document.all(): |
|
|
83 |
for theannotationdoc in thedocument.annotationdocument_set.all(): |
|
|
84 |
theannotationdoc.visibility = visibility_attr |
|
|
85 |
theannotationdoc.save() |
|
|
86 |
|
|
|
87 |
documentaryfile.visibility = visibility_attr |
|
|
88 |
documentaryfile.save() |
|
|
89 |
json = '{"visibility": "' + attr.get_visibility() + '"}' |
|
|
90 |
logger.info(json) |
|
|
91 |
else: |
|
|
92 |
json = '{"Error": "User does not matched"}' |
|
|
93 |
logger.info(json) |
|
|
94 |
|
|
|
95 |
return json |
|
|
96 |
|
|
|
97 |
def delete(self): |
|
|
98 |
logger.info('delete DocumentaryFile') |
|
|
99 |
attr = ClientDocumentaryFileDeleteAttributes(self.request) |
|
|
100 |
logger.info('get_user = ' + str(attr.get_user())) |
|
|
101 |
logger.info('get_documentary_file_id = ' + str(attr.get_documentary_file_id())) |
|
|
102 |
docId = attr.get_documentary_file_id() |
|
|
103 |
if docId == '': |
|
|
104 |
json= '{"Error": "No documentary_file_id attribute in the http post request"}' |
|
|
105 |
logger.info(json) |
|
|
106 |
return json |
|
|
107 |
|
|
|
108 |
try: |
|
|
109 |
documentaryfile = Documentaryfile.objects.get(pk=attr.get_documentary_file_id()) |
|
|
110 |
except Documentaryfile.DoesNotExist: |
|
|
111 |
json= '{"Error": "Invalid documentary_file_id"}' |
|
|
112 |
logger.info(json) |
|
|
113 |
return json |
|
|
114 |
|
|
|
115 |
logger.info('documentaryfile.user.username = ' + str(documentaryfile.user.username)) |
|
|
116 |
if documentaryfile.user.username == attr.get_user(): |
|
|
117 |
#verify if the associated documents are associated to another documentaryfile. if not delete the documents |
|
|
118 |
for thecluster in documentaryfile.cluster_set.all(): |
|
|
119 |
for thedocument in thecluster.document.all(): |
|
|
120 |
nb = (Clusterdocumentweight.objects.filter(document_id=thedocument.documentId)).count() |
|
|
121 |
logger.info('nb' + str(nb)) |
|
|
122 |
if nb == 1: |
|
|
123 |
thedocument.delete() |
|
|
124 |
|
|
|
125 |
documentaryfile.delete() |
|
|
126 |
json = '{"documentary_file_deleted" :' + str(docId) + '}' |
|
|
127 |
logger.info(json) |
|
|
128 |
else: |
|
|
129 |
json= '{"Error": "User does not match"}' |
|
|
130 |
logger.info(json) |
|
|
131 |
|
|
|
132 |
return json |
|
|
133 |
|
|
|
134 |
def parseAndSaveValue(self, list_concepts,concepts_with_detailed_documents_list, attr): |
|
|
135 |
#parse the context |
|
|
136 |
|
|
|
137 |
try: |
|
|
138 |
user_attr = User.objects.get(username=attr.get_user) |
|
|
139 |
except User.DoesNotExist, err: |
|
|
140 |
logger.info(' Error: '+ str(err)) |
|
|
141 |
json = '{"Error": "User does not existed"}' |
|
|
142 |
return json |
|
|
143 |
|
|
|
144 |
visibility_attr = attr.get_visibility() |
|
|
145 |
if visibility_attr == 'public': |
|
|
146 |
visibility_bool = True |
|
|
147 |
else: |
|
|
148 |
visibility_bool = False |
|
|
149 |
|
|
|
150 |
title_attr = attr.get_title() |
|
|
151 |
description_attr = attr.get_description() |
|
|
152 |
#query_id_attr = attr.get_query_id() |
|
|
153 |
#TODO url image |
|
|
154 |
image1 = Image(url='url') |
|
|
155 |
image1.save() |
|
|
156 |
|
|
|
157 |
now = datetime.datetime.now() |
|
|
158 |
mktime(now.timetuple()) |
|
|
159 |
print mktime(now.timetuple()) |
|
|
160 |
|
|
|
161 |
# create the documentary file |
|
|
162 |
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) |
|
|
163 |
dossierDoc1.save() |
|
|
164 |
nb_concept = len(concepts_with_detailed_documents_list) |
|
|
165 |
logger.info('nb_concept = ' + str(nb_concept)) |
|
|
166 |
for concept_index, concept_with_detailed_documents_list in enumerate(concepts_with_detailed_documents_list) : |
|
|
167 |
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=image1) |
|
|
168 |
for detailed_document in concept_with_detailed_documents_list: |
|
|
169 |
logger.info(detailed_document) |
|
|
170 |
logger.info('cluster_id and doc_id = ' + str(list_concepts[concept_index]['title']+' and '+detailed_document['id'])) |
|
|
171 |
#Verify if the document exist already in database |
|
|
172 |
try: |
|
|
173 |
doc1 = Document.objects.get(pk=detailed_document['id']) |
|
|
174 |
except Document.DoesNotExist: |
|
|
175 |
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) |
|
|
176 |
|
|
|
177 |
clusterDocWeight1 = Clusterdocumentweight(cluster=cluster1, document=doc1, weight=detailed_document['weight']) |
|
|
178 |
clusterDocWeight1.save() |
|
|
179 |
json= '{"documentary_file_created":' + str(dossierDoc1.id) + '}' |
|
|
180 |
logger.info(json) |
|
|
181 |
return json |