'''
Created on 27 juil. 2012

@author: gerard
'''
import logging
import time

logger = logging.getLogger('document')

class ClientSearchAttributes(object):

    def __init__(self, request):
        self.request = request

    def get_text(self):
        text = ''
        try:
            text = self.request.POST['recherche']
        except Exception, err:
            '''logger.info('No recherche attribute in the http post request.'+str(err))'''
        return text
    
    def get_filtre(self):
        filtre=''
        try:
            filtre = self.request.POST['filtre']
        except Exception, err:
            logger.info('No recherche attribute in the http post request.'+str(err))    
        return filtre
     
    def get_picture_url(self):
        picture_url = ''
        try:
            picture_url = self.request.POST['picture_image']
        except Exception, err:
            '''logger.info('Info: ' + str(err))'''
        return picture_url
    
    def get_uploaded_picture_name(self):
        uploaded_picture_name = ''
        if self.request.FILES.keys():
            for key in self.request.FILES.keys():    
                image_obj = self.request.FILES[key]
                uploaded_picture_name = image_obj.name
        return uploaded_picture_name
        
    def has_image_file(self):
        return len(self.request.FILES.keys())
         
    def get_from_date(self):
        date = ''
        try:
            date = self.request.POST['from_date']
            
            '''time_object = time.strptime(from_date, '%Y-%m-%dT%H:%M:%S.0Z')'''
            '''time_object = time.strptime(from_date, '%m/%d/%Y')
            date = str(int(time.mktime(time_object)))'''
        except Exception, err:
            logger.info('Info: ' + str(err))
        return date
    
    def get_to_date(self):
        date = ''
        try:
            date = self.request.POST['to_date']
                       
            '''time_object = time.strptime(to_date, '%Y-%m-%dT%H:%M:%S.0Z')'''
            '''time_object = time.strptime(to_date, '%m/%d/%Y')
            date = str(int(time.mktime(time_object)))'''
        except Exception, err:
            logger.info('Info: ' + str(err))
        return date
     
    def get_categories(self):
        categories = []
        #try:
            #categories = self.request.POST.getlist('categories')
        logger.info('get_categories ENTER')
       
        try:
            c = self.request.POST['categories1']
            categories.append(c)
        except Exception, err:
            logger.info('get_categories categories1'+ str(err))
            categories.append('')
           
        
        try:
            c = self.request.POST['categories2']
            categories.append(c)
        except Exception, err:
            logger.info('get_categories categories2'+ str(err))
            categories.append('')
        
        try:
            c = self.request.POST['categories3']
            categories.append(c)
        except Exception, err:
            logger.info('get_categories categories3'+ str(err))
            categories.append('')
           
        
        try:
            c = self.request.POST['categories4']
            categories.append(c)
        except Exception, err:
            logger.info('get_categories categories4'+ str(err))
            categories.append('')
                
        logger.info('categories = $$$$$$$$$$$'+str(categories))        
        return categories 
    
    def get_documentary_file(self):
        documentary_file = ''
        try:
            documentary_file = self.request.POST['documentary_file_id']
        except Exception, err:
            logger.info('Info: ' + str(err))
        
        return documentary_file
    
    def get_filtering_params(self):
        filtering_params = {'from_date':self.get_from_date()}
        filtering_params['to_date'] = self.get_to_date()
        filtering_params['categories'] = self.get_categories()
        return filtering_params
        
    def get_hash_value(self):
        value = 'text' + self.get_text()
        value += 'image' + self.get_picture_url()
        '''value += 'from_date' + self.get_from_date()
        value += 'get_to_date' + self.get_to_date()'''
        value += 'categories' + str(self.get_categories())
        return str(hash(repr(value)))
    
    def get_hash_value_from_text_image(self):
        value = 'text' + self.get_text()
        value += 'image' + self.get_picture_url()
        value += 'imageuploaded' + self.get_uploaded_picture_name()
        return str(hash(repr(value)))