# 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 = models.CharField(default='test')
        #     author = models.ForeignKey(TestAuthor, related_name='author_art')
        #     tags = models.ManyToManyField(TestTags, related_name='tag_art')
        #     create_time = models.DateTimeField(auto_now=True)
        #
        #
        # self.author = TestAuthor()
        # self.author.save()
        # tags = TestTags(tag='tag1')
        # tags.save()
        # self.article = TestArticle(author=self.author)
        # self.article.tags.add(tags)
        # self.article.save()

    def test_include_check(self):
        include_attr = ['title', 'name', 'xx']
        attr_dict = {'title': 'test',
                     'name': 'attr',
                     'time': '20150909'}
        result = _include_check(include_attr, attr_dict)
        self.assertIn('title', result)
        self.assertIn('name', result)
        self.assertNotIn('time', result)
        self.assertNotIn('xx', result)

    def test_exclude_check(self):
        exclude_attr = ['title', 'xx']
        attr_dict = {'title': 'test',
                     'name': 'attr',
                     'time': '20150909'}

# 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 = models.CharField(default='test')
        #     author = models.ForeignKey(TestAuthor, related_name='author_art')
        #     tags = models.ManyToManyField(TestTags, related_name='tag_art')
        #     create_time = models.DateTimeField(auto_now=True)
        #
        #
        # self.author = TestAuthor()
        # self.author.save()
        # tags = TestTags(tag='tag1')
        # tags.save()
        # self.article = TestArticle(author=self.author)
        # self.article.tags.add(tags)
        # self.article.save()

    def test_include_check(self):
        include_attr = ['title', 'name', 'xx']
        attr_dict = {'title': 'test',
                     'name': 'attr',
                     'time': '20150909'}
        result = _include_check(include_attr, attr_dict)
        self.assertIn('title', result)
        self.assertIn('name', result)
        self.assertNotIn('time', result)
        self.assertNotIn('xx', result)

    def test_exclude_check(self):
        exclude_attr = ['title', 'xx']
        attr_dict = {'title': 'test',
                     'name': 'attr',
                     'time': '20150909'}

        result = _exclude_check(exclude_attr, attr_dict)