# -*- coding: utf-8 -*-
# @Date    : 2018年7月10日 17:16:27
# @Author  : Young(237572892@qq.com)
# @Link    : http://www.YoungLynn.top

from django.db import models
import ast

class CompressedTextField(models.TextField):
	"""docstring for CompressedTextField
		model Fields for storing text in a compressed format (bz2 by default)
	"""

	def from_db_value(self,value,expression,connection,context):
		if not value:
			return value
		try:
			return value.decode('base64').decode('bz2').decode('utf-8')
		except Exception:
			return value
	
	def to_python(self,value):
		if not value:
			return value
		try:
			return value.decode('base64').decode('bz2').decode('utf-8')
		except Exception:
			return value
	
	def get_prep_value(self,value):
		if not value:
			return value
		try:
			value.decode('base64')
			return value
		except Exception:
			try:
				return value.encode('utf-8').encode('bz2').encode('base64')
			except Exception:
				return value



class ListField(models.TextField):
	#__metaclass__ = models.SubfieldBase
	description = "Stores a python list"

	def __init__(self, *args, **kwargs):
		super(ListField, self).__init__(*args, **kwargs)

	def to_python(self, value):
		if not value:
			value = []

		if isinstance(value, list):
			return value

		return ast.literal_eval(value)

	def get_prep_value(self, value):
		if value is None:
			return value
		return unicode(value)
	def value_to_string(self, obj):
		value = self._get_val_from_obj(obj)
		return self.get_db_prep_value(value)


		