Esempio n. 1
0
 def get_by_id(cls, identifier: str) -> Optional["User"]:
     """通过学号或教工号获取用户,如果获取不到返回none"""
     try:
         return db_session.query(User).filter(
             User.identifier == identifier).one()
     except NoResultFound:
         return None
Esempio n. 2
0
 def has_grant(cls, user_id: str, to_user_id: str) -> bool:
     """检查是否有访问授权,user_id为访问的人,to_user_id为被访问的人"""
     try:
         result = db_session.query(cls). \
             filter(cls.user_id == user_id). \
             filter(cls.to_user_id == to_user_id). \
             filter(cls.status == GRANT_STATUS_VALID).all()
         if len(result) > 0:
             return True
         else:
             return False
     except NoResultFound:
         return False
Esempio n. 3
0
    def request_for_grant(cls, user_id: str, to_user_id: str) -> "Grant":
        from everyclass.server.user.exceptions import AlreadyGranted
        from everyclass.server.user.exceptions import HasPendingRequest

        pending_grants = db_session.query(cls). \
            filter(cls.user_id == user_id). \
            filter(cls.to_user_id == to_user_id). \
            filter(cls.status == GRANT_STATUS_PENDING).all()

        if len(pending_grants) > 0:
            raise HasPendingRequest('当前已有等待通过的申请,请勿重复申请')

        if cls.has_grant(user_id, to_user_id):
            raise AlreadyGranted('权限已具备,请勿重复申请')
        return cls.new(user_id, to_user_id)
    def sync_to_class_meta(cls):
        """同步class评价到class元信息表"""
        from everyclass.server.course.model import KlassMeta

        all_classes = db_session.query(KlassMeta).all()
        for klass in all_classes:
            if len(klass.reviews) == 0:
                klass.score = -1
                klass.rating_knowledge = -1
                klass.rating_attendance = -1
                klass.final_score = -1
                klass.gender_rate = -1

                db_session.commit()
                continue

            rating_knowledge = 0
            rating_attendance = 0
            final_score = 0
            gender_rate = 0
            for review in klass.reviews:
                rating_knowledge += review.rating_knowledge
                rating_attendance += review.rating_attendance
                final_score += review.final_score
                gender_rate += review.gender_rate

            rating_knowledge /= len(klass.reviews)
            rating_attendance /= len(klass.reviews)
            final_score /= len(klass.reviews)
            gender_rate /= len(klass.reviews)
            overall_score = (rating_knowledge * 4 + rating_attendance * 3 +
                             final_score / 20 * 3) / 10

            klass.score = round(overall_score, 2)
            klass.rating_knowledge = round(rating_knowledge, 2)
            klass.rating_attendance = round(rating_attendance, 2)
            klass.final_score = round(final_score, 2)
            klass.gender_rate = round(gender_rate, 2)

            db_session.commit()
        return
    def import_demo_content(cls):
        """给每一门class导入若干随机生成的评价"""
        from everyclass.server.course.model import KlassMeta

        comments_content = ["这个课挺好的", "不知道老师在讲啥", "老师讲的非常好", "英语学渣表示听不懂"]
        comments_score = ["分给的有点低", "分数还可以", "分数巨高", "分数很满意", "分太低了"]

        student_ids = []
        with open(os.path.join(os.path.dirname(__file__),
                               "student_ids.csv")) as f:
            for line in f.readlines():
                student_ids.append(line[:len(line) - 1])

        classes = db_session.query(KlassMeta).all()
        for klass in classes:
            for _ in range(random.randint(0, 3)):
                student_id = random.choice(student_ids)
                print("student_id: %s" % student_id)
                cls.new(
                    klass.klass_id, student_id, random.randint(2, 5),
                    random.randint(2, 5), random.randint(60, 100),
                    random.randint(3, 8),
                    f"{random.choice(comments_content)},{random.choice(comments_score)}"
                )
Esempio n. 6
0
 def find_by_id(cls, request_id: uuid.UUID) -> Optional["VerificationRequest"]:
     """通过ID查找注册请求,如果没找到返回None"""
     try:
         return db_session.query(cls).filter(cls.request_id == request_id).one()
     except NoResultFound:
         return None
Esempio n. 7
0
 def get_by_id(cls, record_id: int) -> Optional["Grant"]:
     try:
         return db_session.query(cls).filter(
             cls.record_id == record_id).one()
     except NoResultFound:
         return None
Esempio n. 8
0
 def get_requests(cls, user_id: str) -> List["Grant"]:
     pending_grants = db_session.query(cls). \
         filter(cls.to_user_id == user_id). \
         filter(cls.status == GRANT_STATUS_PENDING).all()
     return pending_grants
Esempio n. 9
0
 def get_all(cls):
     return db_session.query(cls).all()