alcatel/controller/DocumentaryFiles.py
changeset 37 3848e1813a30
parent 27 8ca7f2cea729
child 42 de4e97ded3c6
equal deleted inserted replaced
36:bad0e6c60b63 37:3848e1813a30
     4 @author: gerard
     4 @author: gerard
     5 '''
     5 '''
     6 import logging
     6 import logging
     7 import simplejson
     7 import simplejson
     8 
     8 
     9 from dataparser.DocumentaryFilesGetAttributes import DocumentaryFilesGetAttributes
       
    10 from document.models import AnnotationDocumentaryFile
     9 from document.models import AnnotationDocumentaryFile
    11 from document.models import Documentaryfile
    10 from document.models import Documentaryfile
    12 from document.models import Image
    11 from document.models import Image
    13 from django.contrib.auth.models import User
    12 from django.contrib.auth.models import User
       
    13 from mediapartdb.MediapartReader import MediapartReader
    14 
    14 
    15 logger = logging.getLogger('document')
    15 logger = logging.getLogger('document')
    16 
    16 
    17 class DocumentaryFiles(object):
    17 class DocumentaryFiles(object):
    18 
    18 
    19     def __init__(self, request):
    19     def __init__(self, request):
    20         self.request = request
    20         self.request = request
    21       
    21       
    22     def get_files(self,user,offset,count):
    22     def get_files(self,user,offset,count):
    23         print 'get_files'
    23         logger.info('get_files user='+str(offset))
    24         print user
    24 
    25         '''attr = DocumentaryFilesGetAttributes(self.request)'''
    25         '''attr = DocumentaryFilesGetAttributes(self.request)'''
    26         
    26         
    27         '''if not attr.get_user():'''
    27         '''if not attr.get_user():'''
    28         if not user:
    28         if not user:
    29             json = '{"error msg": "user is not defined"}'
    29             json = '{"error msg": "user is not defined"}'
    42         json = {}
    42         json = {}
    43         '''json['offset'] = int(attr.get_offset())
    43         '''json['offset'] = int(attr.get_offset())
    44         json['count'] = int(attr.get_count())'''
    44         json['count'] = int(attr.get_count())'''
    45         json['offset'] = int(offset)
    45         json['offset'] = int(offset)
    46         json['count'] = int(count)
    46         json['count'] = int(count)
       
    47         json['user'] = user
    47         total_count = 0
    48         total_count = 0
       
    49         nb_articles = 0
    48         json['documentary_files'] = []
    50         json['documentary_files'] = []
       
    51        
    49         
    52         
    50         the_user = User.objects.filter(username=user)
    53         the_user = User.objects.filter(username=user)
    51         
    54         reader = MediapartReader()
    52         if the_user:
    55         if the_user:
    53             documentaryFiles = Documentaryfile.objects.filter(user_id=the_user[0].id)
    56             documentaryFiles = Documentaryfile.objects.filter(user_id=the_user[0].id)
    54             for docfile in documentaryFiles:
    57             for docfile in documentaryFiles:
    55                 total_count += 1
    58                 total_count += 1
    56                 if total_count - 1 >= int(offset) and total_count - 1 < int(offset) + int(count):
    59                 if total_count - 1 >= int(offset) and total_count - 1 < int(offset) + int(count):
    57                     jsonfile = {'id':docfile.id}
    60                     jsonfile = {'id':docfile.id}
       
    61                     jsonfile['articles'] = []
       
    62                     article_index = 0
       
    63                     for thecluster in docfile.cluster_set.all():
       
    64                         nb_articles += thecluster.document.count()
       
    65                         for thedocument in thecluster.document.all():
       
    66                             if article_index < 4:
       
    67                                 article_index += 1
       
    68                                 jsonarticle = {'id':str(thedocument.documentId)}
       
    69                                 jsonarticle['title'] = str(thedocument.title.encode("utf8"))
       
    70                                 jsonarticle['url_document'] = reader.get_url(str(thedocument.documentId))
       
    71                                 jsonfile['articles'].append(jsonarticle)
       
    72                     jsonfile['nb_articles'] = nb_articles
       
    73                     nb_articles = 0
    58                     jsonfile['date'] = docfile.date.isoformat()
    74                     jsonfile['date'] = docfile.date.isoformat()
    59                     jsonfile['description'] = str(docfile.description)
    75                     jsonfile['description'] = str(docfile.description)
    60                     jsonfile['title'] = str(docfile.title)
    76                     jsonfile['title'] = str(docfile.title)
    61                     image = Image.objects.get(id=docfile.image_id)
    77                     image = Image.objects.get(id=docfile.image_id)
    62                     jsonfile['url_image'] = str(image.url)
    78                     jsonfile['url_image'] = str(image.url)
    67                         jsonannotation = {'id':annotation.id}
    83                         jsonannotation = {'id':annotation.id}
    68                         jsonannotation['user'] = annotation.user_id
    84                         jsonannotation['user'] = annotation.user_id
    69                         jsonannotation['text'] = annotation.description
    85                         jsonannotation['text'] = annotation.description
    70                         jsonfile['annotations'].append(jsonannotation)
    86                         jsonfile['annotations'].append(jsonannotation)
    71                     json['documentary_files'].append(jsonfile)
    87                     json['documentary_files'].append(jsonfile)
       
    88                 json['nb_articles'] = nb_articles
       
    89                 
       
    90                 nb_articles = 0    
    72         json['total_count'] = total_count
    91         json['total_count'] = total_count
    73         result = simplejson.dumps(json)
    92         result = simplejson.dumps(json)
    74         logger.debug(result)
    93         logger.info(result)
    75         return result  
    94         return result