alcatel/controller/DocumentaryFile.py
changeset 37 3848e1813a30
parent 27 8ca7f2cea729
child 42 de4e97ded3c6
equal deleted inserted replaced
36:bad0e6c60b63 37:3848e1813a30
     3 
     3 
     4 @author: gerard
     4 @author: gerard
     5 '''
     5 '''
     6 import datetime
     6 import datetime
     7 import logging
     7 import logging
     8 
     8 import simplejson
     9 from time import  mktime 
     9 from time import  mktime 
    10 from django.core.cache import cache
    10 from django.core.cache import cache
    11 
    11 
    12 from dataparser.ClientDocumentaryFileAttributes import ClientDocumentaryFileAttributes
    12 from dataparser.ClientDocumentaryFileAttributes import ClientDocumentaryFileAttributes
    13 from dataparser.ClientDocumentaryFileVisibilityAttributes import ClientDocumentaryFileVisibilityAttribute
    13 from dataparser.ClientDocumentaryFileVisibilityAttributes import ClientDocumentaryFileVisibilityAttribute
    28         self.request = request
    28         self.request = request
    29            
    29            
    30     def create(self):
    30     def create(self):
    31         logger.info('create DocumentaryFile')
    31         logger.info('create DocumentaryFile')
    32         attr = ClientDocumentaryFileAttributes(self.request)
    32         attr = ClientDocumentaryFileAttributes(self.request)
    33         logger.info('user = ' + str(attr.get_user()))
    33         '''logger.info('user = ' + str(attr.get_user()))
    34         logger.info('query_id = ' + str(attr.get_query_id()))
    34         logger.info('query_id = ' + str(attr.get_query_id()))
    35         logger.info('public = ' + str(attr.get_visibility()))
    35         logger.info('public = ' + str(attr.get_visibility()))'''
    36         query_id = attr.get_query_id()   
    36         
    37         print query_id   
    37         #query_id = attr.get_query_id()
    38         key1 = cache.get(query_id)
    38         if attr.get_query_id():
    39         if key1:
    39             key1 = cache.get(attr.get_query_id())
    40             context = cache.get(key1['weblab_data_key'])
    40             context = cache.get(key1['weblab_data_key'])
    41             if context == None:
    41         elif self.request.session['key1']:
    42                 print "cache empty"
    42             context = cache.get(self.request.session['key1'])
    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:
    43         else:
    53             json = '{"Error": "Invalid query id"}'
    44             json = '{"Error": "Invalid query id"}'
    54             logger.info(json)
    45             logger.info(json)
       
    46                    
       
    47         if context == None:
       
    48             logger.info("cache empty")
       
    49             json = '{"Error": "cache empty"}'
       
    50             logger.info(json)
       
    51         else:
       
    52             logger.info("cache not empty")
       
    53             list_concepts,concepts_with_detailed_documents_list = context
       
    54             logger.info('list_concepts' + str(list_concepts))
       
    55             logger.info('concepts_with_detailed_documents_list' + str(concepts_with_detailed_documents_list))
       
    56             #parse to get the value to save the documentary file
       
    57             json = self.parseAndSaveValue(list_concepts,concepts_with_detailed_documents_list, attr )
       
    58        
    55         return json
    59         return json
    56     
    60     
    57     def visibilityChange(self):
    61     def visibilityChange(self):
    58         logger.info('visibilityChange')
    62         logger.info('visibilityChange')
    59         attr = ClientDocumentaryFileVisibilityAttribute(self.request)
    63         attr = ClientDocumentaryFileVisibilityAttribute(self.request)
    92             json = '{"Error": "User does not matched"}'
    96             json = '{"Error": "User does not matched"}'
    93             logger.info(json)
    97             logger.info(json)
    94        
    98        
    95         return json
    99         return json
    96         
   100         
    97     def delete(self):
   101     def delete(self,docId):
    98         logger.info('delete DocumentaryFile')
   102         logger.info('delete DocumentaryFile')
    99         attr = ClientDocumentaryFileDeleteAttributes(self.request)
   103         #attr = ClientDocumentaryFileDeleteAttributes(self.request)
   100         logger.info('get_user = ' + str(attr.get_user()))
   104         '''logger.info('get_user = ' + str(attr.get_user()))
   101         logger.info('get_documentary_file_id = ' + str(attr.get_documentary_file_id()))
   105         logger.info('get_documentary_file_id = ' + str(attr.get_documentary_file_id()))
   102         docId = attr.get_documentary_file_id()
   106         docId = attr.get_documentary_file_id()'''
   103         if docId == '':
   107         if docId == '':
   104             json= '{"Error": "No documentary_file_id attribute in the http post request"}'
   108             json= '{"Error": "No documentary_file_id attribute in the http post request"}'
   105             logger.info(json)
   109             logger.info(json)
   106             return json
   110             return json
   107         
   111         
   108         try:
   112         try:
   109             documentaryfile = Documentaryfile.objects.get(pk=attr.get_documentary_file_id())
   113             documentaryfile = Documentaryfile.objects.get(pk=docId)
   110         except Documentaryfile.DoesNotExist:
   114         except Documentaryfile.DoesNotExist:
   111             json= '{"Error": "Invalid documentary_file_id"}'
   115             json= '{"Error": "Invalid documentary_file_id"}'
   112             logger.info(json)
   116             logger.info(json)
   113             return json
   117             return json
   114         
   118         
   115         logger.info('documentaryfile.user.username = ' + str(documentaryfile.user.username))
   119         logger.info('documentaryfile.user.username = ' + str(documentaryfile.user.username))
   116         if documentaryfile.user.username == attr.get_user():
   120         logger.info('self.request.user = ' + str(self.request.user))
       
   121         if str(documentaryfile.user.username) == str(self.request.user):
   117             #verify if the associated documents are associated to another documentaryfile. if not delete the documents
   122             #verify if the associated documents are associated to another documentaryfile. if not delete the documents
   118             for thecluster in documentaryfile.cluster_set.all():     
   123             for thecluster in documentaryfile.cluster_set.all():     
   119                 for thedocument in thecluster.document.all():
   124                 for thedocument in thecluster.document.all():
   120                     nb = (Clusterdocumentweight.objects.filter(document_id=thedocument.documentId)).count()
   125                     nb = (Clusterdocumentweight.objects.filter(document_id=thedocument.documentId)).count()
   121                     logger.info('nb' + str(nb))
   126                     logger.info('nb' + str(nb))
   126             json = '{"documentary_file_deleted" :' + str(docId) + '}'
   131             json = '{"documentary_file_deleted" :' + str(docId) + '}'
   127             logger.info(json)
   132             logger.info(json)
   128         else:
   133         else:
   129             json= '{"Error": "User does not match"}'
   134             json= '{"Error": "User does not match"}'
   130             logger.info(json)
   135             logger.info(json)
   131        
       
   132         return json  
   136         return json  
   133     
   137     
   134     def parseAndSaveValue(self, list_concepts,concepts_with_detailed_documents_list, attr):
   138     def parseAndSaveValue(self, list_concepts,concepts_with_detailed_documents_list, attr):
   135         #parse the context
   139         #parse the context
   136 
   140         logger.info('query_context ********** ='+str(self.request.session.items()))
   137         try:
   141         try:
   138             user_attr = User.objects.get(username=attr.get_user)
   142             #text = str(((attr.get_user()).replace(' ', '')).replace('\n', ''))
       
   143             text = str(attr.get_user())
       
   144             logger.info('attr.get_user! !!! = '+text)
       
   145             tutu = 'cobled'
       
   146             logger.info('attr.get_user! !!! = '+text)
       
   147             user_attr = User.objects.get(username=tutu)
   139         except User.DoesNotExist, err:
   148         except User.DoesNotExist, err:
   140             logger.info(' Error: '+ str(err))
   149             logger.info(' Error: '+ str(err))
   141             json = '{"Error": "User does not existed"}' 
   150             json = '{"Error": "User does not existed"}' 
   142             return json
   151             return json
   143         
       
   144         visibility_attr = attr.get_visibility()
   152         visibility_attr = attr.get_visibility()
   145         if visibility_attr == 'public':
   153         if visibility_attr == 'public':
   146             visibility_bool = True
   154             visibility_bool = True
   147         else:
   155         else:
   148             visibility_bool = False
   156             visibility_bool = False
   149             
   157             
   150         title_attr = attr.get_title()
   158         title_attr = attr.get_title()
   151         description_attr = attr.get_description()
   159         description_attr = attr.get_description()
   152         #query_id_attr = attr.get_query_id()
   160         #query_id_attr = attr.get_query_id()
   153         #TODO url image
   161         #TODO url image
   154         image1 = Image(url='url')
   162                
   155         image1.save()
       
   156         
       
   157         now = datetime.datetime.now()
   163         now = datetime.datetime.now()
   158         mktime(now.timetuple())
   164         mktime(now.timetuple())
   159         print mktime(now.timetuple())
   165         logger.info(mktime(now.timetuple()))
   160            
   166         
       
   167         for concept_index, concept_with_detailed_documents_list in enumerate(concepts_with_detailed_documents_list) :
       
   168             logger.info('url image first cluster'+list_concepts[concept_index]['url_image'])
       
   169             image1 = Image(url=list_concepts[concept_index]['url_image'])
       
   170             image1.save()
       
   171             break
       
   172         if  attr.get_json_streamgraph():
       
   173             logger.info('attr.get_json_streamgraph ****'+str(attr.get_json_streamgraph()))
       
   174             jsonstreamgraph = attr.get_json_streamgraph()
       
   175         else:
       
   176             logger.info('request_streamgraph ****'+str(self.request.session['jsonStreamgraph']))
       
   177             jsonstreamgraph = self.request.session['jsonStreamgraph']
       
   178         
       
   179         if attr.get_json_treemap():
       
   180             logger.info('attr.get_json_streamgraph ****'+str(attr.get_json_treemap()))
       
   181             jsontreemap = attr.get_json_treemap()
       
   182         else:
       
   183             logger.info('request_streamgraph ****'+str(self.request.session['jsonTreemap']))
       
   184             jsontreemap = self.request.session['jsonTreemap']
       
   185       
   161         # create the documentary file
   186         # 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)
   187         #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)
       
   188         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,)
   163         dossierDoc1.save()
   189         dossierDoc1.save()
   164         nb_concept = len(concepts_with_detailed_documents_list)
   190 
   165         logger.info('nb_concept = ' + str(nb_concept))
   191         data = simplejson.loads(self.request.session['jsonTreemap'])
       
   192         logger.info('DATA BEFORE ***'+str(data))
       
   193         for cluster in data['clusters']:
       
   194             cluster['doc_id'] = int(dossierDoc1.id)
       
   195             cluster['user'] = dossierDoc1.user.username
       
   196         logger.info('DATA AFTER ***'+str(data))
       
   197         jsontreemap = simplejson.dumps(data)
       
   198         dossierDoc1.jsontreemap = jsontreemap
       
   199         dossierDoc1.save()
       
   200         #nb_concept = len(concepts_with_detailed_documents_list)
       
   201         
   166         for concept_index, concept_with_detailed_documents_list in enumerate(concepts_with_detailed_documents_list) :
   202         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)
   203             imagecluster = Image(url=list_concepts[concept_index]['url_image'])
       
   204             imagecluster.save()
       
   205             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)
       
   206             logger.info('CLUSTERID'+str(cluster1.id))
   168             for detailed_document in concept_with_detailed_documents_list:
   207             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
   208                 #Verify if the document exist already in database
   172                 try:
   209                 try:
   173                     doc1 = Document.objects.get(pk=detailed_document['id'])
   210                     doc1 = Document.objects.get(pk=detailed_document['id'])
   174                 except Document.DoesNotExist:
   211                 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)
   212                     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)