def post(self):
        session = self.get_session()
        try:
            uid = self.get_secure_cookie('uid')
            if uid is None:
                self.return_status(self.STATUS_NOT_LOGINED)
                return

            uid = int(uid)
            user = get_user(session, uid)

            if (user.signup_status & 4) == 0:
                self.return_status(self.STATUS_FAILED)
                return

            h = hashlib.new('sha512')
            h.update((Config.ENTRANCE_SSO_LOGIN_PASSWORD + '||' + user.mail +
                      '||' + str(int(time.time()))).encode('utf-8'))

            for row in session.query(UserData).filter(UserData.uid == uid):
                realname = row.full_name

            redirect_url = 'http://%s/redirect_login' % Config.ENTRANCE_HOST

            self.write({
                'status': 'SUCCESS',
                'username': user.mail,
                'password': h.hexdigest(),
                'realname': realname,
                'url': redirect_url,
            })
        finally:
            session.close()
    def post(self):
        session = self.get_session()
        uid = self.get_secure_cookie('uid')
        if uid == None:
            self.return_status(self.STATUS_NOT_LOGINED)
        else:
            uid = int(uid)
            user = get_user(session, uid)

            try:
                class_type = int(self.get_argument('class_type'))
                data = []
                if user.signup_status & (1 << (class_type - 1)):
                    id_records = {}
                    res = session.query(ApplicationQuestion, ApplicationAnswer) \
                        .filter(and_(ApplicationQuestion.id == ApplicationAnswer.qid,
                                     ApplicationQuestion.class_type == class_type,
                                     ApplicationAnswer.uid == uid,
                                     ApplicationQuestion.status == 1)) \
                        .order_by(ApplicationQuestion.order)
                    for row in res:
                        element = row[0].as_dict()
                        element['answer'] = row[1].description
                        id_records[row[0].id] = True
                        data.append(element)

                    res = session.query(ApplicationQuestion) \
                        .filter(and_(ApplicationQuestion.class_type == class_type,
                                     ApplicationQuestion.status == 1)) \
                        .order_by(ApplicationQuestion.order)
                    for row in res:
                        if row.id not in id_records:
                            data.append(row.as_dict())
                else:
                    res = session.query(ApplicationQuestion) \
                        .filter(and_(ApplicationQuestion.class_type == class_type,
                                     ApplicationQuestion.status == 1)) \
                        .order_by(ApplicationQuestion.order)
                    for row in res:
                        data.append(row.as_dict())
                self.return_status(self.STATUS_SUCCESS, data=data)
            except Exception as e:
                if DEBUG:
                    print(e)
                self.return_status(self.STATUS_ERROR)
        session.close()
    def post(self):
        session = self.get_session()
        try:
            uid = self.get_secure_cookie('uid')
            if uid is None:
                self.return_status(self.STATUS_NOT_LOGINED)
                return

            uid = int(uid)
            user = get_user(session, uid)

            if user.rule_test == 0:
                self.return_status(self.STATUS_FAILED)
                return

            h = hashlib.new('ripemd160')
            h.update((Config.PRETEST_SSO_LOGIN_PASSWORD + '||' + user.mail +
                      '||' + str(int(time.time()))).encode('utf-8'))

            url = 'http://%s/user_score' % Config.PRETEST_HOST
            res = requests.get(url,
                               params={
                                   'username': user.mail,
                                   'password': h.hexdigest()
                               },
                               timeout=0.5)

            try:
                score = float(res.text.split('\n')[0].replace('*', ''))
            except Exception as e:
                if DEBUG:
                    print(e)
                    print(res.text)
                score = -1

            for row in session.query(User).filter(User.id == uid):
                row.pre_test = 1 if score >= Config.PRETEST_THRESHOLD else 0
            session.commit()

            self.write({
                'status': 'SUCCESS',
                'score': score,
            })
        finally:
            session.close()
    def post(self):
        session = self.get_session()
        uid = self.get_secure_cookie('uid')

        if uid == None:
            self.return_status(self.STATUS_NOT_LOGINED)
        else:
            uid = int(uid)
            user = get_user(session, uid)
            if user.power < 1:
                self.return_status(self.STATUS_PERMISSION_DENIED)
            else:
                try:
                    poll_id = int(self.get_argument('id'))
                    order = self.get_argument('order')
                    year = self.get_argument('year')
                    subject = self.get_argument('subject')
                    body = self.get_argument('body')

                    if poll_id != -1:
                        for row in session.query(Poll).filter(and_(Poll.id == poll_id, Poll.status == 1)):
                            row.order = order
                            row.year = year
                            row.subject = subject
                            row.body = body
                        session.commit()
                    else:
                        instance = Poll(order = order, year = year, subject = subject \
                            , body = body, status = 1)
                        db_insert(session, instance)

                    self.return_status(self.STATUS_SUCCESS)
                except Exception as e:
                    if DEBUG:
                        print(e)
                    self.return_status(self.STATUS_ERROR)
        session.close()
    def post(self):
        session = self.get_session()
        uid = self.get_secure_cookie('uid')

        if uid == None:
            self.return_status(self.STATUS_NOT_LOGINED)
        else:
            uid = int(uid)
            user = get_user(session, uid)
            if user.power < 1:
                self.return_status(self.STATUS_PERMISSION_DENIED)
            else:
                try:
                    qa_id = int(self.get_argument('id'))
                    order = self.get_argument('order')
                    question = self.get_argument('question')
                    answer = self.get_argument('answer')

                    if qa_id != -1:
                        for row in session.query(Qa).filter(
                                and_(Qa.id == qa_id, Qa.status == 1)):
                            row.order = order
                            row.question = question
                            row.answer = answer
                        session.commit()
                    else:
                        instance = Qa(order = order, question = question \
                            , answer = answer, status = 1)
                        db_insert(session, instance)

                    self.return_status(self.STATUS_SUCCESS)
                except Exception as e:
                    if DEBUG:
                        print(e)
                    self.return_status(self.STATUS_ERROR)
        session.close()