def upsert(user, title_or_thinking, text, time): # ensure unique today = time.date() tomorrow = today + timedelta(days=1) session = Session() count = session.query(func.count(Sharing.id))\ .filter( (Sharing.time >= today) & (Sharing.time < tomorrow) & (Sharing.name == user) & (Sharing.title.isnot(None)) & (Sharing.thinking.isnot(None)) ).scalar() if count >= settings.max_daka_per_day: return sharing = session.query(Sharing).filter( (Sharing.time >= today) & (Sharing.time < tomorrow) & (Sharing.name == user) & (getattr(Sharing, title_or_thinking) == None)).first() if sharing is None: sharing = Sharing(**{'name': user}) session.add(sharing) sharing.time = time setattr(sharing, title_or_thinking, text) try: session.commit() except IntegrityError as e: logger.error(str(e)) logger.warning('conflict %s from %s: %s' % (title_or_thinking, user, text)) except StaleDataError as e: logger.error(str(e)) logger.warning('conflict %s from %s: %s' % (title_or_thinking, user, text))
def add_user(id: int, chat_id: int, status="inactive") -> User: _user = User(id=id, chat_id=chat_id, status=status) _sess = Session() _sess.add(_user) _sess.commit() return _user
def add_user(ip: str) -> User: salt = ''.join([chr(randint(97, 122)) for _ in range(15)]) hash = md5((ip + salt).encode('utf-8')).hexdigest() _user = User(ip=ip, hash=hash, salt=salt) _sess = Session() _sess.add(_user) _sess.commit() return _user
def operations(msg): if True or msg.is_at: text = get_text_without_at_bot(msg).strip() if '今日打卡' == text: send_report() elif '打卡改名为' in text: if msg.sender != bot.self: return "没有权限" origin, new = text.split('打卡改名为') session = Session() sharings = session.query(Sharing).filter( Sharing.name.like('%' + origin + '%')).all() for s in sharings: s.name = new session.commit()
def set_args(args: dict, user: User) -> None: _sess = Session() _sess.query(User).filter(User.id == user.id). \ update(args, synchronize_session=False) _sess.commit()