alcatel/controller/DocumentaryFiles.py
author cobled@FRVILN0H401086.emea.lucent.com
Thu, 24 Jan 2013 16:58:55 +0100
changeset 27 8ca7f2cea729
child 37 3848e1813a30
permissions -rw-r--r--
add alcatel folder

'''
Created on 8 aout 2012

@author: gerard
'''
import logging
import simplejson

from dataparser.DocumentaryFilesGetAttributes import DocumentaryFilesGetAttributes
from document.models import AnnotationDocumentaryFile
from document.models import Documentaryfile
from document.models import Image
from django.contrib.auth.models import User

logger = logging.getLogger('document')

class DocumentaryFiles(object):

    def __init__(self, request):
        self.request = request
      
    def get_files(self,user,offset,count):
        print 'get_files'
        print user
        '''attr = DocumentaryFilesGetAttributes(self.request)'''
        
        '''if not attr.get_user():'''
        if not user:
            json = '{"error msg": "user is not defined"}'
            return json
        
        '''if attr.get_offset() == '':'''
        if not offset:
            json = '{"error msg": "no offset defined"}'
            return json
        
        '''if attr.get_count() == '':'''
        if not count:
            json = '{"error msg": "no count defined"}'
            return json
        
        json = {}
        '''json['offset'] = int(attr.get_offset())
        json['count'] = int(attr.get_count())'''
        json['offset'] = int(offset)
        json['count'] = int(count)
        total_count = 0
        json['documentary_files'] = []
        
        the_user = User.objects.filter(username=user)
        
        if the_user:
            documentaryFiles = Documentaryfile.objects.filter(user_id=the_user[0].id)
            for docfile in documentaryFiles:
                total_count += 1
                if total_count - 1 >= int(offset) and total_count - 1 < int(offset) + int(count):
                    jsonfile = {'id':docfile.id}
                    jsonfile['date'] = docfile.date.isoformat()
                    jsonfile['description'] = str(docfile.description)
                    jsonfile['title'] = str(docfile.title)
                    image = Image.objects.get(id=docfile.image_id)
                    jsonfile['url_image'] = str(image.url)
                    
                    jsonfile['annotations'] = []
                    annotations = AnnotationDocumentaryFile.objects.filter(documentaryFile_id=docfile.id)
                    for annotation in annotations:
                        jsonannotation = {'id':annotation.id}
                        jsonannotation['user'] = annotation.user_id
                        jsonannotation['text'] = annotation.description
                        jsonfile['annotations'].append(jsonannotation)
                    json['documentary_files'].append(jsonfile)
        json['total_count'] = total_count
        result = simplejson.dumps(json)
        logger.debug(result)
        return result