'''
Created on Dec 3, 2019
@author: HLQP
'''
import datetime
import re
from django import template
from app.models import School, Player, Goods, Adtype
from distutils.msvc9compiler import Reg
register=template.Library()
# 过滤器 距今时间的表示“刚刚、几分钟前...几小时前...几天前...几个月前...超过一年显示时间”
@register.filter(name="tillnow")
def tillnow(value):
# 截取掉微秒 并且转成datetime类型
value=str(value)[0:-7]
valuetime=datetime.datetime.strptime(value,"%Y-%m-%d %H:%M:%S")
# 当前时间截取掉微秒 并且转成datetime类型
nowtime=datetime.datetime.now()
nowtime=nowtime.strftime("%Y-%m-%d %H:%M:%S")
nowtime=datetime.datetime.strptime(nowtime,"%Y-%m-%d %H:%M:%S")
# 距今时间 返回是datetime类型 小于一天返回格式"0:00:21" 大于一天返回格式"1 day, 0:04:17"
timetill=nowtime-valuetime
timestr=str(timetill)
d="day"
# 小于一天的情况下
if d not in timestr:
hourstr=re.findall(r"(\d\d?):\d\d:\d\d",timestr)
minutestr=re.findall(r"\d\d?:(\d\d):\d\d",timestr)
# 转成int类型
hourint=int(str(hourstr)[2:-2])
minuteint=int(str(minutestr)[2:-2])
if hourint>0:
return str(hourint)+"小时前"
elif hourint==0 and minuteint>0:
return str(minuteint)+"分钟前"
else:
return "刚刚"
# 大于一天情况下 30天为一个月 365天为一年
else:
daystr=re.findall(r"(.+?)day.+?",timestr)
dayint=int(str(daystr)[2:-2])
if dayint<30:
return str(dayint)+"天前"
elif dayint>=30 and dayint<365:
monthint=dayint//30
return str(monthint)+"个月前"
else:
valuedate=valuetime.strftime("%Y-%m-%d") #超过一年显示日期
return valuedate
# 过滤器 新旧程度
@register.filter(name="newlevel")
def newlevel(value):
if value==10:
return "全新"
else:
return str(value)+"成新"
# 过滤器 交易模式
@register.filter(name="tradmod")
def tradmod(value):
if value==1:
return "线上交易"
elif value==-1:
return "线下交易"
else:
return "中介交易"
# 过滤器 取评论者姓名
@register.filter(name="commentuser")
def commentuser(value):
commentuser_id=value
userinfo=Player.objects.get(id=commentuser_id)
return userinfo.player_username
# 过滤器 获取被评论者姓名
@register.filter(name="replyname")
def replyname(value):
reply_to_id=value
userinfo=Player.objects.get(id=reply_to_id)
return userinfo.player_username
# 过滤器 取评论者头像
@register.filter(name="commentuserpic")
def commentuserpic(value):
commentuser_id=value
userinfo=Player.objects.get(id=commentuser_id)
return userinfo.player_photo
#过滤器 获取商品发布的学校名称
@register.filter(name="getschoolname")
def getschoolname(value):
goods_school_id=value
schoolinfo=School.objects.get(id=goods_school_id)
return schoolinfo.school_name
# 过滤器 获取商品标题
@register.filter(name="gettitle")
def gettitle(value):
collects_goods_id=value
goodsinfo=Goods.objects.get(id=co