예제 #1
0
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))
예제 #2
0
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(
            '&lt;', '<').replace('&gt;', '>')).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))
예제 #3
0
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)
예제 #4
0
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()
예제 #5
0
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()