1 Star 0 Fork 0

yq.fly/rewitter

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
model.py 2.80 KB
一键复制 编辑 原始数据 按行查看 历史
yq.fly 提交于 2020-04-11 13:53 . 第一次提交
from __future__ import annotations
from typing import Optional
from conver import to_dict,to_list,to_set,to_string
import settings
import time
r=settings.r
class User:
def __init__(self,id:int):
self.id=int(id)
user_data=to_dict(r.hgetall('user:{}'.format(self.id)))
self.username=user_data['username']
self.password=user_data['password']
@staticmethod
def find_by_id(id:int) -> Optional[User]:
if r.exists('user:{}'.format(int(id))):
return User(int(id))
return None
@staticmethod
def find_by_username(username:str) -> Optional[User]:
uid=r.hget('users',username)
if uid:
return User(int(uid))
return None
@staticmethod
def create(username:str,password:str) ->Optional[User]:
uid=r.hget('users',username)
if not uid:
uid=r.incr('user:uid')
udata={
'username':username,
'password':password
}
r.hmset('user:{}'.format(uid),udata)
r.hset('users',username,uid)
return User(int(uid))
return None
# 获取帖子
def posts(self) ->list[Post]:
posts_id=to_list(r.lrange('user:{}:timeline'.format(self.id ),0,9))
return [Post(pid) for pid in posts_id]
# 粉丝
def followers(self) ->list[User]:
followers=to_list(r.smembers('user:{}:followers'.format(self.id)))
return [User(uid) for uid in followers]
def following(self) ->list[User]:
following=to_list(r.smembers('user:{}:following'.format(self.id)))
return [User(uid) for uid in following]
def followers_num(self) ->int:
return r.scard('user:{}:followers'.format(self.id))
def following_num(self) ->int:
return r.scard('user:{}:following'.format(self.id))
class Post:
def __init__(self,id:int):
self.id=id
pdata=to_dict(r.hgetall('post:{}'.format(self.id)))
self.userid=pdata['userid']
self.content=pdata['content']
self.posttime=pdata['posttime']
@staticmethod
def find_by_id(id:int) -> Optional[Post]:
if r.sismember('post:id',int(id)):
return Post(int(id))
return None
@staticmethod
def create(user:User,content:str) ->Post:
pid=r.incr('post:uid')
pdata={
'userid':user.id,
'content':content,
'posttime':time.strftime("%Y-%M-%D %H:%M:%S",time.localtime())
}
r.hmset('post:{}'.format(pid),pdata)
r.lpush('user:{}:timeline'.format(user.id),pid)
r.lpush('user:{}:posts'.format(user.id),pid)
r.lpush('timeline',pid)
r.sadd('posts:id',pid)
return Post(int(pid))
@property
def username(self):
return to_string( r.hget('user:{}'.format(self.userid),'username'))
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/yqfly/rewitter.git
git@gitee.com:yqfly/rewitter.git
yqfly
rewitter
rewitter
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385