def check_user(u):
'''
Check if one user shall be suspended. Returning the reason why suspend, or None.
@type u: user.User
'''
if u.suspended: return None
msg = None
for r in u.get_meta("limit", []):
if not 'type' in r : continue
if not 'amount' in r : continue
if not 'since' in r : continue
type, amount, since = r['type'], r['amount'], r['since']
now = datetime.datetime.now()
since = datetime.datetime(now.year, now.month, 1) if since == 'this-month' else \
datetime.datetime(now.year, now.month, now.day - now.weekday()) if since == 'this-week' else \
datetime.datetime.strptime(since, DATE_FORMAT)
if type == 'time': # Time-to-expire rule. amount: useless
if now >= since:
msg = "Expired: %s" % r['since']
break
elif type == 'traffic': # Traffic-limited rule. amount: traffic in bytes.
tq = traffic.query(uid=u.id, min_time=since.strftime(DATE_FORMAT), sum=traffic.QS_ALL)
if tq[0][2] > amount:
msg = "Traffic: used %s, limit %s" % (utils.sizeof_fmt(tq[0][2]), utils.sizeof_fmt(amount))
break
return msg
评论列表
文章目录