def put(self, cid): """ 用于修改cid对应的数据行, 表单只需填写需要修改的数据项即可 """ parser = reqparse.RequestParser() parser.add_argument('cid', type=str) parser.add_argument('cname', type=str) parser.add_argument('chours', type=int) parser.add_argument('credit', type=float) parser.add_argument('precid', type=str) args = parser.parse_args() ModifyCourse = Course.get({'cid': cid}) #判断是否存在数据 if ModifyCourse is None: return {'error' : "has no course data with cid: %s for modify(put), please send post request to create first" % cid}, 404 #若表单给出cid,此cid与url对应不同报错 if args['cid'] is not None and args['cid'] != cid: return {'error' : "url's(put) cid(%s) is not equal to form cid(%s)" % (cid, args['cid'])},400 for key, value in args.iteritems(): if value is not None: ModifyCourse[key] = value try: update_row = ModifyCourse.update() if update_row == 1: return ModifyCourse,201 except Exception,e: return {'error', e[1]},400
def get(self, cid): """ 获取一指定cid数据,Course表主键为cid """ data = Course.get({'cid':cid}) if data is not None: return data,200 else: return {'error': "has no course data with cid: %s" % cid},404
def delete(self, cid): """ 删除cid对应数据行 """ OldCourse = Course.get({'cid':cid}) #判断是否有sid对应数据 if OldCourse is None: return {'error': "has no course data with cid:%s for deleted" % cid},404 try: delete_row = OldCourse.delete() if delete_row == 1: return { 'success' : 'success delete data with cid:%s' % cid},204 except IntegrityError,e: if e[0] == 1451: return {'foreign error':e[1]},400 #外键约束 return {'error':e[1]},400
def setup(): table = Texttable().add_rows([['ID', 'Course title', 'Teacher']]) for course in Course.select().where(Course.is_active == False): table.add_row([course.id, course.title, get_teacher_name(course)]) message = 'Enter the id of the course you want to active!' status = 'Enter 0 to Cancel the process and back to the menu' try: id = curses.wrapper(get_input, table.draw(), message, status) id = int(id) if id == 0: curses.wrapper(show_message, 'The process has been canceled!') return True course = Course.get(Course.id == id) course.is_active = True course.save() curses.wrapper( show_message, 'course {} successfully activated!'.format(course.title)) return True except ValueError: curses.wrapper(show_message, 'You should enter a valid number!') return False
def setup(student): courses = Course.select().where(Course.is_active == True) if not len(courses): curses.wrapper(show_message, 'List is empty!') return t = [ ['ID', 'Course Title'], ] for course in courses: t.append([course.id, course.title]) table = Texttable().add_rows(t).draw() try: course_id = curses.wrapper(get_input, table) course_id = int(course_id) course = Course.get(Course.id == course_id) if course in student.courses: curses.wrapper(show_message, 'you already have this course!') else: student.courses.add(course) curses.wrapper(show_message, 'Course successfully added!') except: curses.wrapper(show_message, 'Failed :(\nYou should enter a valid number!')
def post(self): """ 新增数据 """ parser = reqparse.RequestParser() parser.add_argument('sid', type=int) parser.add_argument('cid', type=str) parser.add_argument('garde', type=int) #parser.add_argument('sname', type=str) #parser.add_argument('cname', type=str) args = parser.parse_args() #判断输入完整性 for key,value in args.iteritems(): if value is None: return {'error': "Please input field: %s" % key },400 try: sname = Student.get({'sid': args['sid']})['sname'] sclass = Student.get({'sid': args['sid']})['sclass'] cname = Course.get({'cid': args['cid']})['cname'] args['sname']=sname args['cname']=cname args['sclass']=sclass except Exception,e: return {'error': 'has no sid/cid data'},400
def try_course(course_id): try: return Course.get(Course.id == course_id) except Course.DoesNotExist: abort(404)
def get_course_name(course): course = Course.get(Course.id == course) return course.title