def to_python(self, value): logger.debug('字典字段:反序列化数据(to_python) %s' % value) if value: data = literal_eval(value) else: data = dict() if not isinstance(data, dict): raise TypeError('字典字段:格式非法,反序列化结果不是字典。') return data
def from_db_value(self, value, expression, connection): logger.debug('字典字段:反序列化数据(from_db_value) %s' % value) if value: data = literal_eval(value) else: data = dict() if not isinstance(data, dict): raise TypeError('字典字段:格式非法,反序列化结果不是字典。') return data
def destroy_email_validation_token(self, token_to_destroy: str, commit=True): if token_to_destroy in self.email_validation_tokens: token_info = self.email_validation_tokens.pop(token_to_destroy) if commit: self.save() logger.debug( '邮件验证Token销毁:token %s,邮件地址 %s,过期时间 %d' % (token_to_destroy, token_info['email'], token_info['expire'])) return True logger.debug('邮件验证Token销毁:token %s 不存在' % token_to_destroy) return False
def create_email_validation_token(self, commit=True) -> str: # 生成token,指定token过期时间 token_expire = int(time.time()) + EMAIL_TOKEN_EXPIRE token = gen_random_char_string(TOKEN_LENGTH) email = self.user.email # 存储该token self.email_validation_tokens[token] = { 'email': email, 'expire': token_expire } if commit: self.save() logger.debug('邮件验证Token生成:token %s,邮件地址 %s,过期时间 %d' % (token, email, token_expire)) return token
def avatar_urls(self): logger.debug('头像urls:开始生成头像urls') avatar_urls = dotdict() storage = default_storage # type:Storage for size in AVATAR_SIZES.keys(): if size in self._avatar: if getattr(settings, 'USE_OSS', False): oss_storage = storage # type:AliyunBaseStorage url = oss_storage.url(self._avatar[size], sign=False) else: url = storage.url(self._avatar[size]) else: url = static('account/default.gif') avatar_urls[size] = url logger.debug('头像urls:完成√') return avatar_urls
def avatar(self): logger.debug('删除头像:开始删除头像') storage = default_storage # type:Storage for name in self._avatar.values(): storage.delete(name) logger.debug('删除头像:删除 {name}'.format(name=name)) self._avatar = dict() logger.debug('删除头像:完成√')
def avatar(self, avatar: File): logger.debug('设定头像:开始设定头像') if self._avatar: logger.debug('设定头像:删除旧头像') del self.avatar storage = default_storage # type:Storage logger.debug('设定头像:打开传入头像文件 {file}'.format(file=avatar)) image = Image.open(avatar) logger.debug('设定头像:生成不同尺寸头像') thumbs = generate_thumbnails(image, sizes=AVATAR_SIZES, target_format=AVATAR_FORMAT, target_mode=AVATAR_COLOR_MODE) logger.debug('设定头像:生成完成,尺寸 {sizes}'.format(sizes=list(thumbs.keys()))) for size, io in thumbs.items(): path = storage.save(self.avatar_path + size + '.' + AVATAR_FORMAT, io) # 如果启用了OSS,为了提高加载速度,将头像文件权限设置为公共读 if getattr(settings, 'USE_OSS', False): oss_storage = storage # type:AliyunBaseStorage from oss2 import OBJECT_ACL_PUBLIC_READ oss_storage.put_file_acl(path, OBJECT_ACL_PUBLIC_READ) self._avatar[size] = path logger.debug('设定头像:完成√')
def get_prep_value(self, value): logger.debug('字典字段:序列化数据 %s' % value) return repr(value)