def create_course(): try: print('创建课程'.center(60, '=')) school_list = School.get_all_obj_list() for k, obj in enumerate(school_list): print(k, obj['name'], obj['addr']) id = int(input('请选择学校: ')) school_obj = school_list[id] #print(school_obj,school_obj ['nid'] ) name = input('请输入课程名: ').strip() price = input('请输入课程价格: ').strip() period = input('请输入课程周期: ').strip() course_name_list = [(obj['name'], obj['school_nid']) for obj in Course.get_all_obj_list()] if name in course_name_list: raise Exception('\033[43;1m课程[%s] 已经存在,不可重复创建\033[0m' % (name)) obj = Course(name, price, period, school_obj['nid']) obj.save() status = True error = '' data = '\033[33;1m课程[%s] 价格[%s] 周期[%s]创建成功\033[0m' % ( obj.name, obj.price, obj.period) print(data) except Exception as e: status = False error = str(e) print('error: %s' % error) data = '' return {'status': status, 'error': error, 'data': data}
def handle(self, *args, **options): df = pd.read_csv(sys.argv[3]) courses = [ Course( id=df['id'][row], name=df['name'][row], ) for row in range(0, 9) ] courses = Course.objects.bulk_create(courses) if courses: self.stdout.write('Successfully loaded Courses..')
def import_course(filename): with open(filename, 'rt', encoding='utf-8') as csvfile: reader = csv.reader(csvfile, delimiter=',') course_list = list(reader) for i in course_list[1:]: try: c = Course(code=i[0], title=i[1], topic=i[2], type=i[3], opening=i[4], duration=i[5], venue=i[6], fee=i[7], grant=i[8], wsa=i[9], remark=i[10], overview=i[11], outline=i[12], testimonial=i[13], upcoming=i[14], hyperlink=i[15]) c.save() print('Added: {}'.format(i[0])) except IntegrityError as e: if 'UNIQUE constraint failed: core_course.code' in e.args: c = Course.objects.get(code=i[0]) c.title = i[1] c.topic = i[2] c.type = i[3] c.opening = i[4] c.duration = i[5] c.venue = i[6] c.fee = i[7] c.grant = i[8] c.wsa = i[9] c.remark = i[10] c.overview = i[11] c.outline = i[12] c.testimonial = i[13] c.upcoming = i[14] c.hyperlink = i[15] c.save() print('Updated: {}'.format(i[0]))
def create_course(): try: print('创建课程'.center(60, '=')) school_list = School.get_all_obj_list() for k, obj in enumerate(school_list): print(k, obj.name, obj.addr) sid = input("请选择学校: ").strip() if sid.isdigit(): sid = int(sid) if sid >= len(school_list): raise Exception("输入的学校不存在") else: raise Exception("输入的学校不存在") school_obj = school_list[sid] name = input("请输入课程名称: ").strip() price = input("请输入课程费用: ").strip() if name == '' or price == '': raise Exception("输入不能为空") if price.replace('.', '', 1).isdigit(): pass else: raise Exception("课程费用应该是数字") period = input("请输入课程周期: ").strip() if period == '': raise Exception("输入不能为空") course_name_list = [(obj.name, obj.school_nid.nid) for obj in Course.get_all_obj_list()] if (name, school_obj.nid.nid) in course_name_list: raise Exception('\033[43;1m课程[%s] 已经存在,不可重复创建\033[0m' % (name)) obj = Course(name, price, period, school_obj.nid) obj.save() status = True error = '' data = '\033[33;1m课程[%s] 价格[%s] 周期[%s]创建成功\033[0m' % ( obj.name, obj.price, obj.period) except Exception as e: status = False error = str(e) data = '' return {'status': status, 'error': error, 'data': data}
def register_course(request): # import pdb # pdb.set_trace() if request.method == "POST" and request.is_ajax(): name = request.POST.get('name', False) if len(name) < 5: return HttpResponse(short_name_message) #check database for any courses with this name courses = Course.objects.filter(name=name) if len(courses) > 0: return HttpResponse(name_taken_message) else: course = Course() course.name = name current_user = WebUser.objects.filter(userId = request.user.id)[0] course.save() course.teachers.add(current_user) #add lessons course.save() createCourseLessons(course) return HttpResponse(success_message)
def handle(self, *args, **options): fake = Faker() fake.add_provider(person) fake.add_provider(profile) fake.add_provider(lorem) fake.add_provider(company) for _ in range(60): profile_data = fake.simple_profile() CustomUser.objects.create_user( profile_data['username'], profile_data['mail'], ''.join( random.choice(string.ascii_letters) for _ in range(10)), first_name=fake.first_name(), last_name=fake.last_name()) teachers = CustomUser.objects.all()[:10] students = CustomUser.objects.all()[10:] for i in range(10): course = Course(name=fake.bs(), code=''.join( random.choice(string.ascii_letters) for _ in range(2)), responsibleTeacher=teachers[i]) course.save() random_student_ids = random.sample( list(students.values_list('id', flat=True)), 20) qs = CustomUser.objects.filter(id__in=random_student_ids) course.students.add(*qs) for student in qs: Grade(value=random.randint(10, 100) / 10, course=course, user=student).save() self.stdout.write(self.style.SUCCESS('Generated new users'))
def dummy_course(): """ Creates a dummy course """ return Course(uuid='805acb1c-739c-4f4a-a529-3027c95f8c60', title='Arithmetic')
def _get_parsed_schedule_param(param_collection) -> ScheduleParam: def _str_to_array(str_values): values = [] for i in str_values.split(","): if i != '': values.append(int(i)) return array(values) ROOMS, TIMESLOTS, COURSES, INSTRUCTORS, COURSE_GROUPS = param_collection all_theory_courses = list(filter(lambda x: x[4] == "Theory", COURSES)) all_theory_course_indices = [c[0] for c in all_theory_courses] # Rooms - col 3: allowed_courses for i in range(len(ROOMS)): allowed_course_idxs = ROOMS[i][3] if allowed_course_idxs == "ALL_THEORY": allowed_course_idxs = all_theory_course_indices elif allowed_course_idxs[:4] == "NOT:": not_allowed_courses = [ int(i) for i in allowed_course_idxs[4:].split(',') ] allowed_course_idxs = [] for c in range(len(COURSES)): if c not in not_allowed_courses: allowed_course_idxs.append(c) else: allowed_course_idxs = [ int(i) for i in allowed_course_idxs.split(',') ] ROOMS[i][3] = allowed_course_idxs # Instructors - col 2, 3 for i in range(len(INSTRUCTORS)): INSTRUCTORS[i][2] = _str_to_array(INSTRUCTORS[i][2]) INSTRUCTORS[i][3] = _str_to_array(INSTRUCTORS[i][3]) # CourseGroups - col 2, 3 for i in range(len(COURSE_GROUPS)): COURSE_GROUPS[i][2] = _str_to_array(COURSE_GROUPS[i][2]) COURSE_GROUPS[i][3] = _str_to_array(COURSE_GROUPS[i][3]) ''' NOTE: the following sanity check can be deduced: (if NUM_OF_LECS_BEING_OFFERED > MAX_LECS_THAT_CAN_BE_OFFERED) ''' for i in range(len(TIMESLOTS)): if TIMESLOTS[i][3] == '-': TIMESLOTS[i][3] = [] elif len(TIMESLOTS[i][3]) == 1: TIMESLOTS[i][3] = [int(TIMESLOTS[i][3])] else: TIMESLOTS[i][3] = _str_to_array(TIMESLOTS[i][3]) Rooms = [Room(r[0], r[1], r[2], r[3]) for r in ROOMS] Timeslots = [Timeslot(t[0], t[1], t[2], t[3]) for t in TIMESLOTS] Courses = [Course(c[0], c[1], c[2], c[3], c[4]) for c in COURSES] Instructors = [ Instructor(i[0], i[1], i[2], i[3], i[4]) for i in INSTRUCTORS ] CourseGroups = [ CourseGroup(cg[0], cg[1], cg[2], cg[3]) for cg in COURSE_GROUPS ] sections = _get_all_sections(Courses) daily_slots = _get_all_daily_slots(Timeslots) day_codes = _get_all_day_codes(Timeslots) return ScheduleParam(Rooms, Timeslots, Courses, Instructors, CourseGroups, sections, daily_slots, day_codes)