def get(key, tid, day=today(), hour=0, default=0, save=True): if callable(day): day = day() day = str(day)[:10] item = StatLog.objects(key=key, tid=tid, day=day).first() if item: return item.value if save: StatLog(key=key, tid=tid, day=day, hour=0, value=default).save() return default
def validate_account(self, field): admin = AdminUser.objects( username=self.account.data).first() if not admin: raise ValueError('用户不存在') if not admin.active: raise ValueError('用户被冻结') if admin.password != self.password.data: AdminUserLoginLog.error(admin.id) count = AdminUserLoginLog.objects( created__gte=max(today(), admin.freezed or today()), user=admin.id, type=AdminUserLoginLog.TYPE.ERROR).count() if count >= 20: admin.active = False admin.freezed = datetime.now() admin.save() raise ValueError('密码错误') self.admin = admin
def inc(key, tid, day=lambda: today(), hour=0, value=1): if callable(day): day = day() day = str(day)[:10] item = StatLog.objects(key=key, tid=tid, day=day).modify( inc__value=value, set__modified=datetime.now(), ) if not item: StatLog(key=key, tid=tid, day=day, hour=0, value=value).save() return value else: return item.value + 1
def token(self): now = datetime.now() key = self.config.get('config_key', 'siwei:token') token = json.loads(Item.data(key, '{}')) if not token or datetime.strptime(token['deadline'], '%Y-%m-%d %H:%M:%S') <= now: token = self.grant_token() if token: if not token['persisted']: token['deadline'] = today() + timedelta(seconds=1800) else: token['deadline'] = now + timedelta( seconds=token['persisted']) token['deadline'] = token['deadline'].strftime( '%Y-%m-%d %H:%M:%S') Item.set_data(key, json.dumps(token)) return token
def code_day(phone): PhoneCode = um.models.PhoneCode return PhoneCode.objects(phone=phone, created__gte=today()).count()