'''
Created on 7 aout 2012

@author: gerard
'''
import logging
import time
logger = logging.getLogger('document')

class ClientDocumentsGetAttributes(object):

    def __init__(self, request):
        self.request = request
    
    def get_query_id(self):
        query_id = ''
        try:
            query_id = self.request.GET['query_id']
        except Exception, err:
            logger.info('No query_id in the http get request. ' + str(err))
        return query_id
    
    def get_cluster(self):
        cluster = ''
        try:
            cluster = self.request.GET['cluster_id']
        except Exception, err:
            logger.info('No cluster in the http get request. ' + str(err))
        return cluster
    
    def get_offset(self):
        offset = ''
        try:
            offset = self.request.GET['offset']
        except Exception, err:
            logger.info('No offset in the http get request. ' + str(err))
        return offset
    
    def get_count(self):
        count = ''
        try:
            count = self.request.GET['count']
        except Exception, err:
            logger.info('No count in the http get request. ' + str(err))
        return count
    
    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_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