def put(self, _id): data = self.parser.parse_args() course = CourseModel.find_by_id(_id) if course: course.name = data["name"] course.category = data["category"] course.trainers = data["trainers"] course.description = data["description"] course.target_audience = data["target_audience"] course.outcomes = data["outcomes"] course.requirements = data["requirements"] course.start_date = date(*data["start_date"]) course.end_date = date(*data["end_date"]) course.total_lectures = data["total_lectures"] course.lectures_per_week = data["lectures_per_week"] course.lecture_duration = timedelta(*data["lecture_duration"]) course.min_spg = data["min_students_per_group"] course.max_spg = data["max_students_per_group"] else: valid_data = { key: val for key, val in data.items() if val is not None } course = CourseModel(**valid_data) try: course.save_to_db() except: return { "message": "An error occurred while saving to the database." }, 500 return course.json()
def create_course(): if request.method == "POST": name = request.form.get('coursename') city = request.form.get('city') state = request.form.get('state') slope = request.form.get('slope') course = CourseModel(name, city, state, slope) if course.find_by_name(name): return {'message': f'A course named {name} already exists.'} course.save_to_db() distance_list = [] handicap_list = [] par_list = [] for i in range(1, 19): distance_str = "h" + str(i) + "d" handicap_str = "h" + str(i) + "h" par_str = "h" + str(i) + "p" distance_list.append(int(request.form.get(distance_str))) handicap_list.append(int(request.form.get(handicap_str))) par_list.append(int(request.form.get(par_str))) hole = HoleModel(course.id, i, par_list[i - 1], handicap_list[i - 1], distance_list[i - 1]) hole.save_to_db() course_dict = { 'name': name, 'city': city, 'state': state, 'slope': slope, 'distance': { 'hole_distances': distance_list, 'front_9_distance': sum(distance_list[:9]), 'back_9_distance': sum(distance_list[9:]), 'total_distance': sum(distance_list) }, 'handicap': { 'hole_handicaps': handicap_list, 'front_9_handicap': sum(handicap_list[:9]), 'back_9_handicap': sum(handicap_list[9:]), 'total_handicap': sum(handicap_list) }, 'par': { 'hole_pars': par_list, 'front_9_par': sum(par_list[:9]), 'back_9_par': sum(par_list[9:]), 'total_par': sum(par_list) } } return render_template('course_scorecard.jinja2', course_dict=course_dict) return render_template('blank_scorecard.jinja2')
def post(self, _id): data = Course.parser.parse_args() print(data) course = CourseModel(**data) try: course.save_to_db() except: return {"message": "An error occurred inserting the course."}, 500 return course.json(), 201
def post(self): data = self.parser.parse_args() # course = CourseModel.find_by_email(data["email"]) # if course: # return {'message': "The email {} already exists.".format(data["email"])}, 400 valid_data = {key: val for key, val in data.items() if val is not None} course = CourseModel(**valid_data) try: course.save_to_db() except: return { "message": "An error occurred while saving to the database." }, 500 return course.json(), 201
def post(self, name): if CourseModel.find_by_name(name): return { 'message': f"A course with name '{name}' is already entered into the database." }, 400 data = Course.parser.parse_args() course = CourseModel(name, **data) try: course.save_to_db() except: return { 'message': 'An error occurred inserting the course into the database.' }, 500 return course.json(), 201
def put(self, _id): data = Course.parser.parse_args() course = CourseModel.find_by_id(_id) if course: course.price = data['price'] course.description = data['description'] course.on_discount = data['on_discount'] course.title = data['title'] course.date_updated = datetime.datetime.utcnow() course.image_path = data['image_path'] else: course = CourseModel(**data) course.save_to_db() return course.json()
def post(self): data = Course.postparser.parse_args() classname = data['classname'] startdate = datetime.strptime(data['startdate'], "%Y-%m-%d") enddate = datetime.strptime(data['enddate'], "%Y-%m-%d") starttime = datetime.strptime(data['starttime'], "%I:%M %p") endtime = datetime.strptime(data['endtime'], "%I:%M %p") orgid = data['orgid'] userid = data['userid'] if data['classdays'] is None: classdays = [datetime.weekday(startdate)] else: classdays = list(map(int, data['classdays'].split(','))) jsonstr = [] msgstr = [] for n in range(int (((enddate + timedelta(days=1)) - startdate).days)): classdate = startdate + timedelta(n) day = datetime.weekday(classdate) if day in classdays: course = CourseModel(orgid=orgid, name=classname, starttime=datetime.strftime(starttime, "%H:%M"), endtime=datetime.strftime(endtime, "%H:%M"), classdate=datetime.strftime(classdate, "%Y-%m-%d"), startdate=datetime.strftime(startdate, "%Y-%m-%d"), enddate=datetime.strftime(enddate, "%Y-%m-%d"), classdays=data['classdays'] if data['classdays'] != None else datetime.weekday(startdate), userid=userid, requestuserid=None, requestdate=None, acceptuserid=None, acceptdate=None) try: course.save_to_db() msgstr.append({'status': "SUCCESS", "code": 200}) except: msgstr.append({'status': "ERROR", "code": 500}) jsonstr.append(course.json()) return {'courses': jsonstr,'messages': msgstr}