Exemplo n.º 1
0
    def post(self, live_id, data=None, sess=None):
        keys = ('title', 'introduction', 'start', 'duration', 'classes')
        data = keys_filter(data, keys)

        selected = self.db.query(
            db.Live).filter(db.Live.live_id == live_id).first()
        if selected is None:
            return self.finish(404, 'no matched live')

        selected.title = data['title'],
        selected.introduction = data['introduction'],
        selected.start = data['start'],
        selected.duration = data['duration'],

        try:
            rels = []
            for _class in data['classes']:
                rels.append(
                    db.RelationLiveClass(live_id=selected.live_id,
                                         class_id=_class['class_id']))
            self.db.add_all(rels)
            self.db.commit()
        except sqlalchemy.exc.IntegrityError:
            # sqlalchemy would rollback automatically for us
            return self.finish(404, 'no matched class_id')
Exemplo n.º 2
0
    def post(self, score_id, data=None, sess=None):
        keys = ('score_id', 'score')
        data = keys_filter(data, keys)

        self.db.query(
            db.Score).filter(db.Score.score_id == score_id).update(**data)
        self.db.commit()
Exemplo n.º 3
0
    def put(self, data=None, sess=None):
        keys = ('title', 'introduction', 'start', 'duration', 'classes',
                'is_public')
        data = keys_filter(data, keys)

        new_item = db.Live(title=data['title'],
                           user_id=sess['user_id'],
                           introduction=data['introduction'],
                           start=data['start'],
                           duration=data['duration'],
                           is_public=data['is_public'],
                           is_streaming=False)
        self.db.add(new_item)
        self.db.commit()

        try:
            rels = []
            for _class in data['classes']:
                rels.append(
                    db.RelationLiveClass(live_id=new_item.live_id,
                                         class_id=_class['class_id']))
            self.db.add_all(rels)
            self.db.commit()
        except sqlalchemy.exc.IntegrityError:
            self.db.delete(new_item)  # rollback
            return self.finish(404, 'no matched class_id')
Exemplo n.º 4
0
    def put(self, data=None, sess=None):
        keys = ('score', 'staff_id', 'class_id')
        data = keys_filter(data, keys)

        user_id = self.db.query(db.User.user_id).filter(
            db.User.staff_id == data['staff_id']).first()
        if user_id is None:
            return self.finish(404, 'no matched user')

        keys = ('score', 'class_id')
        data = keys_filter(data, keys)
        data['user_id'] = user_id[0]

        new_score = db.Score(**data)
        self.db.add(new_score)
        self.db.commit()

        self.finish(score_id=new_score.score_id)
Exemplo n.º 5
0
    def put(self, data=None, sess=None):
        keys = ('class_name', 'weekday', 'start', 'end', 'teacher_id',
                'course_id')
        data = keys_filter(data, keys)

        new_class = db.Class(**data)
        self.db.add(new_class)
        self.db.commit()

        self.finish(class_id=new_class.class_id)
Exemplo n.º 6
0
    def put(self, data=None, sess=None):
        captcha_key = 'captcha:0cc175b9c0f1b6a8'

        keys = {'staff_id', 'password', 'role', 'name', 'is_male', 'captcha'}
        data = keys_filter(data, keys)

        if not reduce(lambda a, b: a and b,
                      map(lambda x: x[0] and
                          (x[1] is not None), data.items())):
            return self.finish(403, 'missing arguments')

        captcha = data['captcha'].lower()
        correct_captcha = (sess.get(captcha_key) or b'').decode()
        sess.delete(captcha_key)
        if captcha != correct_captcha:
            if conf.app.debug:
                return self.finish(405,
                                   'incorrect captcha',
                                   correct_captcha=correct_captcha)
            return self.finish(405, 'incorrect captcha')

        if not isinstance(data['role'], int):
            return self.finish(400, 'role must be a digit')
        data['role'] = db.int2UserRole[data['role']]

        if isinstance(data['is_male'], str) and not data['is_male'].isdigit():
            return self.finish(400, 'is_male must be a digit')
        data['is_male'] = bool(int(data['is_male']))

        selected = self.db.query(
            db.User).filter(db.User.staff_id == data['staff_id']).first()
        if selected is not None:
            return self.finish(409, 'conflict user information')

        data['password'] = hash_password(data['password'])

        keys = keys - {'captcha'}
        data = keys_filter(data, keys)
        new_user = db.User(**data)
        self.db.add(new_user)
        self.db.commit()
Exemplo n.º 7
0
    def put(self, data=None, sess=None):
        keys = ('course_name', 'start', 'end')
        data = keys_filter(data, keys)

        if not reduce(lambda a, b: a and b,
                      map(lambda x: x[0] and
                          (x[1] is not None), data.items())):
            return self.finish(403, 'missing arguments')

        new_course = db.Course(**data)
        self.db.add(new_course)
        self.db.commit()

        self.finish(course_id=new_course.course_id)
Exemplo n.º 8
0
    def put(self, class_or_course, id, topic_id, data=None, sess=None):
        keys = ('content', )
        data = keys_filter(data, keys)

        topic = self.db.query(
            db.Topic).filter(db.Topic.topic_id == topic_id).first()
        topic.replies = db.Topic.replies + 1
        topic.updated_at = int(time.time())

        new_item = db.Reply(topic_id=topic_id,
                            content=data['content'],
                            user_id=sess['user_id'],
                            created_at=int(time.time()))
        self.db.add(new_item)
        self.db.commit()
Exemplo n.º 9
0
    def get(self, class_id, sess=None):
        selected = self.db.query(db.Score, db.User.staff_id,
                                 db.User.name, db.Course.course_name) \
            .join(db.User, db.User.user_id == db.Score.user_id) \
            .join(db.Class, db.Class.class_id == db.Score.class_id) \
            .join(db.Course, db.Course.course_id == db.Class.course_id) \
            .filter(db.Score.class_id == class_id).all()

        list = []
        for score, staff_id, user_name, course_name in selected:
            keys = ('score_id', 'score', 'user_id', 'class_id')
            dict = keys_filter(score, keys)
            dict.update(course_name=course_name,
                        staff_id=staff_id,
                        user_name=user_name)
            list.append(dict)

        self.finish(list=list)
Exemplo n.º 10
0
    def post(self, data=None):
        keys = ('pattern', )
        data = keys_filter(data, keys)

        if not data['pattern']:
            return self.finish(403, 'missing arguments')
        pattern = '%{}%'.format(data['pattern'])

        selected = self.db.query(db.Course).filter(
            db.Course.course_name.like(pattern)).all()

        list = []
        for course in selected:
            list.append(
                dict(course_id=course.course_id,
                     course_name=course.course_name,
                     start=course.start,
                     end=course.end))

        self.finish(total=len(list), list=list)
Exemplo n.º 11
0
    def put(self, class_or_course, id, data=None, sess=None):
        belong_type = db.BelongType.CLASS if class_or_course == 'class' \
            else db.BelongType.COURSE

        keys = ('title', 'content')
        data = keys_filter(data, keys)

        at = int(time.time())

        new_item = db.Topic(title=data['title'],
                            content=data['content'],
                            user_id=sess['user_id'],
                            created_at=at,
                            updated_at=at,
                            replies=0,
                            belong_type=belong_type,
                            belong_id=id)
        self.db.add(new_item)
        self.db.commit()

        self.finish(topic_id=new_item.topic_id)