def create_subjects(data, Subject): """Loading an initial set of subjects to the database """ print("Loading subject data to db") # retrieving subjects from data subjects = data['subjects']['yoga'] # for each subject data, create a subject entity in db for subject in subjects: try: # avoiding duplicate subjects subject_check = Subject.objects.get(name=subject['name']) except Subject.DoesNotExist: # subject with name does not exists, let's create it subject_entity = Subject(name=subject['name'], description=subject['description']) subject_entity.save() print("Subject data loaded to db")
def create_forums(settings, create_demo, Subject, User): """Creatin discussion data """ print("Loading forum data to db") # retrieving all subjects subjects = Subject.fetch_all() # retrieving all instructor instructors = User.objects.filter( groups__name=settings.GROUPS["INSTRUCTORS"]) instructors = [ins for ins in instructors] # calling Mike's super long function create_demo(subjects, instructors) print("Forum data loaded to db")
def create_sessions(data, settings, timezone, timedelta, Subject, Session, Location, User): """Creating sessions using current data in the database """ from random import randint print("Loading session data to db") # retrieving sessions from data sessions = data['sessions'] # retrieving all subjects subjects = Subject.fetch_all() # retrieving all locations locations = Location.fetch_all() # for each subject for subject in subjects: # for each session for index in range(0, len(sessions)): try: # avoiding duplicate sessions for subject session_check = Session.objects.get( subject=subject, name=sessions[index]['name']) except Session.DoesNotExist: start_date = timezone.now() + timedelta(days=randint(1, 90)) # session with name for subject doesn't exist, lets create it session_entity = Session( subject=subject, location=locations[index], instructor=User.objects.get( username=sessions[index]['instructor']), name=sessions[index]['name'], max_capacity=locations[index].max_capacity, start_date=start_date, end_date=start_date + timedelta(hours=randint(1, 4))) session_entity.save() print("Session data loaded to db")
# para importar los datos de los profesores: tmp_data = pd.read_csv('Horarios/professors/p.csv', header=[0], sep=',') professors = [ Professor(name=row['Name']) for index, row in tmp_data.iterrows() ] Professor.objects.bulk_create(professors) # para importar los datos de las materias tmp_data = pd.read_csv('Horarios/subjects/s.csv', header=[0], sep=',') subjects = [ Subject(name=row['Name'], hours_per_week=row['Hours At Week'], keycode=row['Keycode']) for index, row in tmp_data.iterrows() ] Subject.objects.bulk_create(subjects) # para importar los datos de los horarios tmp_data = pd.read_csv('Horarios/schedules/sch.csv', header=[0], sep=',') schedules = [ Schedule(monday=row['Monday'], tuesday=row['Tuesday'], wednesday=row['Wednesday'], thursday=row['Thursday'], friday=row['Friday'], saturday=row['Saturday']) for index, row in tmp_data.iterrows()