'''
Created on 27 juil. 2012

@author: gerard
'''
import logging
import time

logger = logging.getLogger('document')

class ClientSearchAttributes(object):

    def __init__(self, request):
        print 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 = self.request.POST['filtre']
        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:
            from_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:
            to_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:
            print 'get_categories'
            print self.request.POST.getlist('categories')
            categories = self.request.POST.getlist('categories')
            '''categories = self.request.POST['categories']'''
            '''categories = eval(categories)'''

        except Exception, err:
            '''logger.info('Info: ' + str(err))'''
        
        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):
        print 'get_hash_value'
        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())
        print str(hash(repr(value)))
        return str(hash(repr(value)))
    
    def get_hash_value_from_text_image(self):
        print 'get_hash_value_from_text_image'
        value = 'text' + self.get_text()
        value += 'image' + self.get_picture_url()
        value += 'imageuploaded' + self.get_uploaded_picture_name()
        print str(hash(repr(value)))
        return str(hash(repr(value)))