# Path: app/blog_lab/cbv/src/dss/TimeFormatFactory.py
# class TimeFormatFactory(object):
#     def __init__(self):
#         super(TimeFormatFactory, self).__init__()
# 
#     @staticmethod
#     def datetime_to_string(datetime_time, time_format='%Y-%m-%d %H:%M:%S'):
#         if datetime_time.tzinfo is None:
#             return datetime_time.strftime(time_format)
#         datetime_time = datetime_time.astimezone(timezone.get_current_timezone())
#         return datetime_time.strftime(time_format)
# 
#     @staticmethod
#     def datetime_to_timestamp(datetime_time):
#         if datetime_time.tzinfo is None:
#             return time.mktime(datetime_time.timetuple())
#         datetime_time = datetime_time.astimezone(timezone.get_current_timezone())
#         return time.mktime(datetime_time.timetuple())
# 
#     @staticmethod
#     def get_time_func(func_type='string', time_format='%Y-%m-%d %H:%M:%S'):
#         if func_type == 'string':
#             return partial(TimeFormatFactory.datetime_to_string, time_format=time_format)
#         elif func_type == 'timestamp':
#             return TimeFormatFactory.datetime_to_timestamp
#         else:
#             return partial(TimeFormatFactory.datetime_to_string, time_format=time_format)
# 
# Path: app/blog_lab/cbv/src/dss/Serializer.py
# def _include_check(include_attr, attr_dict):
#     if include_attr:
#         ex_attr_dict = copy.deepcopy(attr_dict)
#         attr_dict.clear()
#         for attr in include_attr:
#             if attr in ex_attr_dict:
#                 attr_dict[attr] = ex_attr_dict.get(attr, None)
#     return attr_dict
# 
# def _exclude_check(exclude_attr, attr_dict):
#     if exclude_attr:
#         for attr in exclude_attr:
#             if attr in attr_dict:
#                 attr_dict.pop(attr)
#     return attr_dict
# 
# def _data_convert(data, time_func, foreign, many, include_attr, exclude_attr):
#     if isinstance(data, models.Model):
#         attr_list = _get_attr(data, time_func, foreign, many)
#         _include_check(include_attr, attr_list)
#         _exclude_check(exclude_attr, attr_list)
#         return attr_list
#     elif isinstance(data, (QuerySet, Page)):
#         result = []
#         for itm in data:
#             attr_list = _get_attr(itm, time_func, foreign, many)
#             _include_check(include_attr, attr_list)
#             _exclude_check(exclude_attr, attr_list)
#             result.append(copy.copy(attr_list))
#         return result
#     elif isinstance(data, datetime.datetime):
#         print '===='
#         print data
#         return time_func(data)
#     elif isinstance(data, (unicode, str, bool, float)):
#         return data
#     elif isinstance(data, dict):
#         for k, v in data.iteritems():
#             data[k] = _data_convert(v, time_func, foreign, many, include_attr, exclude_attr)
#         return data
#     elif isinstance(data, list):
#         for i, itm in enumerate(data):
#             data[i] = _data_convert(itm, time_func, foreign, many, include_attr, exclude_attr)
#         return data
#     else:
#         return None
# 
# def _output_convert(output_type, data):
#     output_switch = {'dict': data,
#                      'raw': data,
#                      'json': json.dumps(data, indent=4),
#                      'xml': xmltodict.unparse({'root': data})}
#     return output_switch.get(output_type, None)
# 
# def serializer(data, datetime_format='timestamp', output_type='raw', include_attr=None, exclude_attr=None,
#                foreign=False, many=False, **kwargs):
#     foreign = remove_check(**kwargs) or foreign
#     time_func = TimeFormatFactory.get_time_func(datetime_format)
#     result = _data_convert(data, time_func, foreign, many, include_attr, exclude_attr)
#     return _output_convert(output_type, result)


import json
import datetime
from unittest import TestCase
from django.db import models
from django.conf import settings
from ..dss.TimeFormatFactory import TimeFormatFactory
from ..dss.Serializer import _include_check, _exclude_check, _data_convert, _output_convert, serializer
                              {'content': 'list_content1',
                               'time': datetime.datetime(2015, 12, 22, 9)}]}
        result = _data_convert(test_data, time_func=self.time_func, foreign=False, many=False,
                               include_attr=None, exclude_attr=None)
        self.assertEqual(result['time'], '2015-10-10 12:00:00')
        self.assertIsInstance(result, dict)
        self.assertEqual(result['list'][0]['time'], '2015-10-11 09:00:00')

    def test_output_convert(self):
        test_data = {'title': 'test',
                     'id': 1,
                     'read': False,
                     'belong': None}
        result = _output_convert('raw', test_data)
        self.assertEqual(test_data, result)
        result = _output_convert('json', test_data)
        self.assertEqual(result, json.dumps(test_data, indent=4))

    def test_serializer(self):
        test_data = {'title': 'test',
                     'name': 'attr',
                     'time': datetime.datetime(2015, 10, 10, 12),
                     'list': [{'content': 'list_content',
                               'time': datetime.datetime(2015, 10, 11, 9)},
                              {'content': 'list_content1',
                               'time': datetime.datetime(2015, 12, 22, 9)}]}
        result = serializer(test_data, datetime_format='string', output_type='raw')
        self.assertEqual(test_data['time'], '2015-10-10 12:00:00')
        self.assertIsInstance(result, dict)
        self.assertEqual(result['list'][0]['time'], '2015-10-11 09:00:00')

# Path: app/blog_lab/cbv/src/dss/TimeFormatFactory.py
# class TimeFormatFactory(object):
#     def __init__(self):
#         super(TimeFormatFactory, self).__init__()
# 
#     @staticmethod
#     def datetime_to_string(datetime_time, time_format='%Y-%m-%d %H:%M:%S'):
#         if datetime_time.tzinfo is None:
#             return datetime_time.strftime(time_format)
#         datetime_time = datetime_time.astimezone(timezone.get_current_timezone())
#         return datetime_time.strftime(time_format)
# 
#     @staticmethod
#     def datetime_to_timestamp(datetime_time):
#         if datetime_time.tzinfo is None:
#             return time.mktime(datetime_time.timetuple())
#         datetime_time = datetime_time.astimezone(timezone.get_current_timezone())
#         return time.mktime(datetime_time.timetuple())
# 
#     @staticmethod
#     def get_time_func(func_type='string', time_format='%Y-%m-%d %H:%M:%S'):
#         if func_type == 'string':
#             return partial(TimeFormatFactory.datetime_to_string, time_format=time_format)
#         elif func_type == 'timestamp':
#             return TimeFormatFactory.datetime_to_timestamp
#         else:
#             return partial(TimeFormatFactory.datetime_to_string, time_format=time_format)
# 
# Path: app/blog_lab/cbv/src/dss/Serializer.py
# def _include_check(include_attr, attr_dict):
#     if include_attr:
#         ex_attr_dict = copy.deepcopy(attr_dict)
#         attr_dict.clear()
#         for attr in include_attr:
#             if attr in ex_attr_dict:
#                 attr_dict[attr] = ex_attr_dict.get(attr, None)
#     return attr_dict
# 
# def _exclude_check(exclude_attr, attr_dict):
#     if exclude_attr:
#         for attr in exclude_attr:
#             if attr in attr_dict:
#                 attr_dict.pop(attr)
#     return attr_dict
# 
# def _data_convert(data, time_func, foreign, many, include_attr, exclude_attr):
#     if isinstance(data, models.Model):
#         attr_list = _get_attr(data, time_func, foreign, many)
#         _include_check(include_attr, attr_list)
#         _exclude_check(exclude_attr, attr_list)
#         return attr_list
#     elif isinstance(data, (QuerySet, Page)):
#         result = []
#         for itm in data:
#             attr_list = _get_attr(itm, time_func, foreign, many)
#             _include_check(include_attr, attr_list)
#             _exclude_check(exclude_attr, attr_list)
#             result.append(copy.copy(attr_list))
#         return result
#     elif isinstance(data, datetime.datetime):
#         print '===='
#         print data
#         return time_func(data)
#     elif isinstance(data, (unicode, str, bool, float)):
#         return data
#     elif isinstance(data, dict):
#         for k, v in data.iteritems():
#             data[k] = _data_convert(v, time_func, foreign, many, include_attr, exclude_attr)
#         return data
#     elif isinstance(data, list):
#         for i, itm in enumerate(data):
#             data[i] = _data_convert(itm, time_func, foreign, many, include_attr, exclude_attr)
#         return data
#     else:
#         return None
# 
# def _output_convert(output_type, data):
#     output_switch = {'dict': data,
#                      'raw': data,
#                      'json': json.dumps(data, indent=4),
#                      'xml': xmltodict.unparse({'root': data})}
#     return output_switch.get(output_type, None)
# 
# def serializer(data, datetime_format='timestamp', output_type='raw', include_attr=None, exclude_attr=None,
#                foreign=False, many=False, **kwargs):
#     foreign = remove_check(**kwargs) or foreign
#     time_func = TimeFormatFactory.get_time_func(datetime_format)
#     result = _data_convert(data, time_func, foreign, many, include_attr, exclude_attr)
#     return _output_convert(output_type, result)


# Path: app/blog_lab/cbv/src/test/test_Serializer.py
import json
import datetime
from unittest import TestCase
from django.db import models
from django.conf import settings
from ..dss.TimeFormatFactory import TimeFormatFactory
from ..dss.Serializer import _include_check, _exclude_check, _data_convert, _output_convert, serializer

                              {'content': 'list_content1',
                               'time': datetime.datetime(2015, 12, 22, 9)}]}
        result = _data_convert(test_data, time_func=self.time_func, foreign=False, many=False,
                               include_attr=None, exclude_attr=None)
        self.assertEqual(result['time'], '2015-10-10 12:00:00')
        self.assertIsInstance(result, dict)
        self.assertEqual(result['list'][0]['time'], '2015-10-11 09:00:00')

    def test_output_convert(self):
        test_data = {'title': 'test',
                     'id': 1,
                     'read': False,
                     'belong': None}
        result = _output_convert('raw', test_data)
        self.assertEqual(test_data, result)
        result = _output_convert('json', test_data)
        self.assertEqual(result, json.dumps(test_data, indent=4))

    def test_serializer(self):
        test_data = {'title': 'test',
                     'name': 'attr',
                     'time': datetime.datetime(2015, 10, 10, 12),
                     'list': [{'content': 'list_content',
                               'time': datetime.datetime(2015, 10, 11, 9)},
                              {'content': 'list_content1',
                               'time': datetime.datetime(2015, 12, 22, 9)}]}
        result = serializer(test_data, datetime_format='string', output_type='raw')
        self.assertEqual(test_data['time'], '2015-10-10 12:00:00')
        self.assertIsInstance(result, dict)
        self.assertEqual(result['list'][0]['time'], '2015-10-11 09:00:00')

        result = serializer(test_data, datetime_format='timestamp', output_type='json')