def center(): form = request.form user: db.User = g.user sign_on = [ user_sign.sign for user_sign in user.ref_signs if sign_helper.Controller.is_active(user=user) ] sign_completed = (user_sign.sign for user_sign in user.ref_signs if sign_helper.Controller.is_completed(user=user)) sign_completed = dict([ ( RxRelations(each.ref_users, from_type=db.User, to_type=db.Sign).where( user_cls=db.Role.leader) # : RxRelations[UserSign[ .first() # : UserSign .user # : User .id, each) for each in sign_completed ]) if user.permission == Permission.teacher: filter_fn = course_helper.Controller.is_ready( date=date.SyncCurrent.get_date()) course_ready: List[db.Course] = [ course for course in RxList(user.ref_courses).map(lambda x: x.course) if filter_fn(course) ] # course_ready 指 your_managing_course_signs: List[db.Sign] = [] for user_sign in user.ref_signs: for course in course_ready: if course.id == user_sign.sign.inst_id: your_managing_course_signs.append(user_sign.sign) break else: your_managing_course_signs = [] activity_ready: List[db.Activity] = [ RxList( each.group.ref_activitys).where(lambda e: date.SyncCurrent.between( e, e + date.timedelta(minutes=15))).map(lambda e: e.activity) for each in user.ref_groups if each.user_cls == db.Role.leader and each.group.ops == db.GroupOption.temp_group ] pass
def test4_controller(self): from rx_sharh.rxdb import RxRecord, RxTable, RxMany, RxList from rx_sharh.states.database import Course, UserCourse, User, db_session, delete_user with RxTable(User) as user, RxTable(Course) as course, RxTable(UserCourse) as user_course: with open('./samples.json') as f: s = f.read() users = user_deserialize(s) courses = default_serialize(s, 'Course') users = user.add_many(users) courses = course.add_many(courses) user_course_s = [dict(course_id=each_course.id, user_id=each_user.id) for each_course in courses for each_user in users] user_course.add_many(user_course_s) from rx_sharh.service.controller import user as c_user, course as c_course pprint(list(c_course.Controller.get_normals_of(course.first()))) pprint(list(c_user.Controller.get_normals_for(user.first(), Course))) with user: for each in user.rx_iter(): each.mutate(password=lambda password: password + "233") print('after mutate:') user.foreach(lambda x: pprint(x.password)) with user: users_all: List[User] = user.where().all() for each in RxList(users_all).where(id = lambda x: True).rx_iter(): each.delete() print('after delete:') user.foreach(lambda x: pprint(x.password))
def course_list(): def for_each_user_course(uc: db.UserCourse): course = uc.course return inc.CollectionItemA(f"{course.name}|{course.location}", inc.Href(url_for('profile.sign_stats', inst_id=uc.course_id))) user: db.User = g.user return inc.Page( '<link rel="shortcut icon" href="/static/favicon.ico">', inc.Container( inc.Collection( *(for_each_user_course(each) for each in RxList(user.ref_courses).where(user_cls=db.Role.leader)) ) ) ).__str__()
def all_sign_stats(inst_id): user: db.User = g.user if user.nickname != '贪玩': Dangerous.add(100) return '您即将贪玩...' course = RxTable(db.Course).where(id=inst_id).first() if not course: return "贪玩不了" user_course = RxList( course.ref_users).where(user_cls=db.Role.leader).first() res = render_course_sign_stats(user_course) return inc.Html( inc.Head( inc.Tag("style", TableStyle), '<link rel="shortcut icon" href="/static/favicon.ico">', ), inc.Container(*res).append( inc.Align.center).set_indent(1)).__str__() if isinstance( res, tuple) else inc.Page( inc.Container(res).set_indent(1)).__str__()
def sign_stats(inst_id: int): user: db.User = g.user if user.permission != db.Permission.teacher: return inc.Page( '<link rel="shortcut icon" href="/static/favicon.ico">', inc.Container( inc.IconTextBlock( "red", "提示", "您没有管理课程的权限。", inc.Icon("edit"))).append( inc.Align.force_center).set_indent(1)).__str__() res = render_course_sign_stats( RxList(user.ref_courses).where(course_id=inst_id, user_cls=db.Role.leader).first()) return inc.Html( inc.Head( inc.Tag("style", TableStyle), '<link rel="shortcut icon" href="/static/favicon.ico">', ), inc.Container(*res).append( inc.Align.center).set_indent(1)).__str__() if isinstance( res, tuple) else inc.Page( inc.Container(res).set_indent(1)).__str__()
def stop(sign_name, qrcode, status): """开始签到跳转, 停止签到""" sign = RxTable(db.Sign).where(name=sign_name).first() if not sign: flash("实例不存在!") Config.app.logger.warning('{}'.format(sign_name)) return redirect(url_for('attendance.center')) if not status: need_sign_num, actual_sign_num, sign_users, not_sign_users = sign_helper.Controller.statistics(sign) # 将签到信息记录置于redis中缓存 if sign.cls == db.SignClass.course: cls = 'c' @curry def make_student_info(user: db.User, is_sign: int): stu = user.ref_students.first().student return dict(name=stu.name, number=stu.number, is_sign=is_sign) inst_id = sign.inst_id cache_storage.set( name_pattern(cls, inst_id, None, "week"), SyncCurrent.to_week_index() ) cache_storage.hset( name_pattern(cls, inst_id, None, "values"), SyncCurrent.to_week_index(), json.dumps(dict( 应到=need_sign_num, 实到=actual_sign_num, 人员=[*RxList(sign_users) .map(make_student_info(is_sign=1)), *RxList(not_sign_users) .map(make_student_info(is_sign=0))] )) ) return render_template('attendance/stop.html', sign_name=sign_name, sign_code=sign.sign_code, status=status, need_sign_num=need_sign_num, # 应到人数, int actual_sign_num=actual_sign_num, # 实到人数,int sign_users=map(get_name_id, sign_users), # 签到的用户, List[User] not_sign_users=map(get_name_id, not_sign_users)) # 未签到的用户, List[User] if qrcode: return render_template('attendance/qrcode.html', title="签到管理", sign_name=sign_name, status=status) return render_template('attendance/stop.html', title="签到管理", sign_name=sign_name, sign_code=sign.sign_code, status=status)
def for_activity(): nonlocal sign act_dao = RxTable(db.Activity) act: db.Activity = act_dao.where(id=instance_id).first() if not act: flash("实例不存在!") Config.app.logger.warning('{}: {}'.format(instance_id, repr(form))) return failed() group_id = act.ref_groups.first().group_id ug: db.UserGroup = RxList(user.ref_groups).where(group_id=group_id).first() if not ug or ug.user_cls == db.Role.normal: flash("权限不足") return failed() if not sign: # 建立签到 with sign_dao: # 二维码 if qrcode: sign = sign_dao.add(inst_id=instance_id, name=make_sign_name(act), sign_code="with_qrcode", start=date.SyncCurrent.get_date(), cls=db.SignClass.course, is_active=1) # 非二维码 else: sign = sign_dao.add(inst_id=instance_id, name=make_sign_name(act), sign_code=form['sign_code'], start=date.SyncCurrent.get_date(), cls=db.SignClass.course, is_active=1) if not sign_dao.is_succeed: flash('新建失败!') Config.app.logger.warning(sign_dao.msg) return failed() else: # 修改 with sign_dao: sign.is_active = 1 with sign_dao: if len(form) != 0: sign.sign_code = form['sign_code'] else: sign.sign_code = 'with_qrcode' if not sign_dao.is_succeed: flash('修改失败!') Config.app.logger.warning(sign_dao.msg) return failed() flash('修改成功!') # 更新群员的签到信息 with sign_dao: for each in act.ref_groups.first().group.ref_users: if not RxTable(db.UserSign).where(user_id=each.user_id, sign_id=sign.id).first(): RxRecord(each.user).add_relation_between(sign, user_cls=each.user_cls) if qrcode: return succeed(1) flash('开始签到!') return succeed(0)