def get_user(chat_id=None, id=None): if not chat_id and not id: return None if chat_id: _res = [ x for x in Session().query(User).filter(User.chat_id == chat_id) ] return _res[0] if _res else None if id: _res = [x for x in Session().query(User).filter(User.id == id)] return _res[0] if _res else None
def on_msg(msg): msg_type = msg.type from_user = msg.member.name now = msg.create_time # 处理撤回的消息 if msg_type == wxpy.NOTE: revoked = ETree.fromstring(msg.raw['Content'].replace( '<', '<').replace('>', '>')).find('revokemsg') if revoked: # 根据找到的撤回消息 id 找到 bot.messages 中的原消息 revoked_msg = bot.messages.search( id=int(revoked.find('msgid').text))[0] if not revoked_msg: return with locks[from_user]: session = Session() title_or_thinking = get_column(revoked_msg.type) sharing = session.query(Sharing).filter( Sharing.name == from_user).filter( getattr(Sharing, title_or_thinking) == revoked_msg.text).first() setattr(sharing, title_or_thinking, None) else: with locks[from_user]: c = get_column(msg_type) min_length = getattr(settings, 'min_thinking_len', 1) if c == 'title' or len(msg.text) >= min_length: upsert(from_user, c, msg.text, now) else: logger.warning(msg.text + ' of length %d less than %d' % (len(msg.text), min_length))
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 send_report(): from_time = settings.from_time session = Session() res = '累计打卡(%s起)\n' % from_time for row in session.query(Sharing.name, func.count().label('credit'))\ .filter(Sharing.title.isnot(None) & Sharing.thinking.isnot(None))\ .group_by(Sharing.name)\ .order_by(desc('credit')): res += ' %s: %.1f\n' % (row.name, row.credit * 0.2) with engine.connect() as con: today_daka = pd.read_sql_query( "select * from sharing where date(time) >= current_date order by id desc", con) today_daka.rename(inplace=True, columns={ 'id': '序号', 'name': '姓名', 'title': '标题', 'thinking': '感想', 'time': '时间' }) now = datetime.datetime.now() if not os.path.isdir('daka'): os.mkdir('daka') filename = "daka/%s(00.00.00-%s).xlsx" % ( now.date().strftime('%Y-%m-%d'), now.time().strftime('%H.%M.%S')) to_excel(today_daka, filename) for group in bot.groups().search(settings.notice_group_name): group.send(res) group.send_file(filename)
def postHost(request): ip = request.POST.get('ip', '') uname = request.POST.get('username', '') pwd = request.POST.get('password', '') Client(ip=ip, uname=uname, pwd=pwd) session = Session(ip=ip) session.setHost() return HttpResponse(json.dumps({"info": "success"}))
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 __init__(self, gitRepo, app, system, settings): QWidget.__init__(self) from errors import ErrorHandler, DialogErrorView self.settings = settings self.errors = ErrorHandler(DialogErrorView(self, app)) self.session = Session(gitRepo, self.errors) self.text_saved = True self.buttons_bar = QWidget() self.project_tree = SourcesTree(self.session, system, self.errors) self.project_tree.source_selection_changed.connect( self._on_file_selection_) self.session.sources_changed.connect(self.project_tree.update_model) self.editor = EditorPanel(self.settings) self.session.content_changed.connect(self.editor.displayText) self.editor.content_changed.connect( lambda: self.session.set_active_file_content(self.editor.plainText )) self.webview = QWebView() self.images_gallery = ImagesPanel(self.session, self.settings) self.session.html_output_changed.connect( lambda: self.display_local_html_file(self.session. active_file_output)) self.session.html_content_changed.connect( lambda: self.webview.reload()) self.images_gallery.on_insert_image.connect( self.insert_image_in_current_position) self.layout_toolbar() self.layout_components() self.session.start() any_file = find_first_file(self.session.get_sources_structure()) if any_file: self.project_tree.setSelectedFile(any_file)
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()
import qdarkstyle from PyQt5 import QtGui from PyQt5.QtWidgets import QApplication from GUI import MainWindow if "local" in args.session: from core import Local_session as Session else: from core import Live_session as Session app = QApplication(sys.argv) app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5()) launch = MainWindow( Session(mode=args.mode, config=args.config, contract_type='classic')) launch.show() sys.exit(app.exec_()) elif args.build: from core import Local_session as Session args.build = "PPO" session = Session(mode=args.mode, config=args.config, contract_type='classic', agent=args.build) session._stop() else: if "local" in args.session:
def get_user(ip: str) -> User: _res = [x for x in Session().query(User).filter(User.ip == ip)] return _res[0] if _res else None
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()
def is_hash_unique(hash: str) -> bool: return not bool( [x for x in Session().query(User).filter(User.hash == hash)])
def session(self): sessionid = self.get_secure_cookie('sid') session = Session(self.application.session_store, sessionid) if not sessionid: self.set_secure_cookie('sid', session._sessionid) return session