def do_action(self, session): lesson_id = self.get_argument('lesson_id', None) page_id = int(self.get_argument('page_id', 0)) wpm = int(self.get_argument('wpm', 0)) invalid_rate = float(self.get_argument('invalid_rate', 0.0)) error_array = self.get_argument('error_array', []) error_msg = None if lesson_id: new_page = { 'page_id': page_id, 'wpm': wpm, 'invalid_rate': invalid_rate, 'update_time': time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())), 'error_array': error_array } history_msg = [] try: history = session.query(History.history_msg).filter(and_(History.account_id == self.account_id, History.lesson_id == lesson_id)).one_or_none() if history: history_msg = json.loads(history.history_msg) history_msg.append(new_page) session.query(History).filter(and_(History.account_id == self.account_id, History.lesson_id == lesson_id)).update( {'history_msg': json.dumps(history_msg)}) else: history_msg.append(new_page) new_history = History(account_id=self.account_id, lesson_id=lesson_id, history_msg=json.dumps(history_msg)) session.add(new_history) except Exception, e: logger.api_logger().api_error(e) error_msg = Error.SERVER_ERROR
def do_action(self, session): error_msg = None lesson_name = self.get_argument('lesson_name', None) file_id = self.get_argument('file_id', None) notes = self.get_argument('notes', None) if not error_msg and not lesson_name: error_msg = Error.NO_LESSON_NAME if not error_msg and not file_id: error_msg = Error.LESSON_NO_FILE if not error_msg: try: if session.query(Lessons).filter( and_(Lessons.name == lesson_name, Lessons.account_id == self.account_id)).one_or_none(): error_msg = Error.DUPLICATE_LESSON elif not session.query(Files).filter( Files.id == file_id).one_or_none(): error_msg = Error.NO_FILE else: new_ld = tools.unique_id('ld') create_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) new_lesson = Lessons(id=new_ld, name=lesson_name, account_id=self.account_id, file_id=file_id, create_time=create_time, notes=notes) session.add(new_lesson) except Exception, e: logger.api_logger().api_error(e) error_msg = Error.SERVER_ERROR
def post(self, *args, **kwargs): try: body = json.loads(self.request.body if self.request.body else '{}') for key, value in body.items(): self.request.arguments[key] = [value] except Exception, e: logger.api_logger().api_error(e)
def do_action(self, session): try: account = session.query(Account.name, Account.email).filter( Account.id == self.account_id).one_or_none() if account: show = {'name': account.name, 'email': account.email} self.set_result({'show': show}) except Exception, e: print e logger.api_logger().api_error(e) self.set_error(1, Error.SERVER_ERROR)
def do_action(self, session): error_msg = None lesson_id = self.get_argument('lesson_id', ' ') try: if not session.query(Lessons).filter( and_(Lessons.account_id == self.account_id, Lessons.id == lesson_id)).delete(): error_msg = Error.NO_LESSON except Exception, e: logger.api_logger().api_error(e) error_msg = Error.SERVER_ERROR
def do_action(self, session): try: history_list = session.query( History.lesson_id, History.history_msg).filter( History.account_id == self.account_id) history = [] for h in history_list: _h = {'lesson_id': h.lesson_id, 'history_msg': h.history_msg} history.append(_h) self.set_result({'history': history}) except Exception, e: logger.api_logger().api_error(e) self.set_error(1, Error.SERVER_ERROR)
def do_action(self, session): email = self.get_argument('email', None) name = self.get_argument('name', None) old_password = self.get_argument('old_password', None) new_password = self.get_argument('new_password', None) error_msg = None try: update_bag = {} if email: if VerifyHelper.mail_check(email): if not session.query(Account.id).filter( Account.email == email).one_or_none(): update_bag['email'] = email else: error_msg = Error.DUPLICATE_MAIL else: error_msg = Error.MAIL_ERROR if not error_msg and name: if VerifyHelper().name_check(name): if not session.query(Account.id).filter( Account.name == name).one_or_none(): update_bag['name'] = name else: error_msg = Error.DUPLICATE_NAME else: error_msg = Error.NAME_ERROR if not error_msg and new_password and old_password: account = session.query(Account.password).filter( Account.id == self.account_id).one_or_none() if account and Md5Helper.ori_str_gen( old_password) == account.password: error_msg = VerifyHelper().password_verify(new_password) if new_password == old_password: error_msg = Error.NEW_PWD_ERROR else: error_msg = Error.OLD_PWD_ERROR if not error_msg: update_bag['password'] = Md5Helper.ori_str_gen( new_password) if not error_msg and update_bag: session.query(Account).filter( Account.id == self.account_id).update(update_bag) except Exception, e: logger.api_logger().api_error(e) error_msg = Error.SERVER_ERROR
def do_response(self): response = {"status": self._status} if self._error_message: response["error_message"] = self._error_message logger.api_logger().api_error(self._error_message) response.update(self._result) if self.request.method == 'OPTIONS': self.set_header("Access-Control-Allow-Headers", "Content-Type") origin = self.request.headers.get('Origin', '*') self.set_header("Access-Control-Allow-Origin", origin) self.set_header('Access-Control-Allow-Credentials', 'true') self.set_header("Content-Type", "application/json;charset=utf-8") self.write(json.dumps(response))
def do_action(self, session): name = self.get_argument('name', None) password = self.get_argument('password', None) email = self.get_argument('email', None) error_msg = None if not all((name, password, email)): error_msg = Error.NO_REGISTER_ARGS if not error_msg and not VerifyHelper().name_check(name): error_msg = Error.NAME_ERROR if not error_msg and not VerifyHelper().mail_check(email): error_msg = Error.MAIL_ERROR if not error_msg: error_msg = VerifyHelper().password_verify(password) if not error_msg: try: account = session.query(Account.id).filter( or_(Account.name == name, Account.email == email)).one_or_none() if not account: create_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) account = Account( id=tools.unique_id('ad'), name=name, password=Md5Helper().ori_str_gen(password), email=email, create_time=create_time) session.add(account) cookie_expire_time = int( config.get('global', 'cookie_expire_time')) domain = config.get('global', 'domain') self.set_secure_cookie("account_id", account.id, domain=domain, expires_days=cookie_expire_time) show = {'name': account.name, 'email': account.email} self.set_result({'show': show}) elif account.name == name: error_msg = Error.DUPLICATE_NAME else: error_msg = Error.DUPLICATE_MAIL except Exception, e: logger.api_logger().api_error(e) error_msg = Error.SERVER_ERROR
def do_action(self, session): try: files = session.query(Files.id, Files.name).filter(Files.account_id == self.account_id) file_list = [] for f in files: _file = { 'name': f.name, 'file_id': f.file_id, } file_list.append(_file) self.set_result({ 'file_list': file_list }) except Exception, e: logger.api_logger().api_error(e) self.set_error(1, Error.SERVER_ERROR)
def prepare(self): logger.api_logger().api_info( 'handler %s args %s' % (self.__class__.__name__, self.request.arguments))