def create(): form = TeacherForm(request.form) # get all the schools from the schools table school_list = [(str(row.id), row.name) for row in School.objects()] school_list.append(("not-listed", "My school is not listed")) form.school.choices = school_list if request.method == 'POST' and form.validate(): # check if a user exist with the given email or not existing_user = User.objects().filter(email=form.email.data).first() if existing_user is None: # before inserting the document into database we have to hash password hash_password = bcrypt.hashpw(form.password.data.encode('utf-8'), bcrypt.gensalt()) # check if the school is listed or not school_id = form.school.data if form.school.data == 'not-listed': # if the school name isn't listed when signing up: # get the name of the school school_name = request.form.get('school_name') # insert the school into the database school = School() school.name = school_name # need to create document in Module1, Module2, Module3 collections for this school module1 = Module1() module1.save() # blank module1 document is saved to the database school.module1 = module1.id # saving the id for the module1 document created for this school. # school.module1 is of type ReferenceField meaning that this will be a reference to the document in the Module1 collection for this school. module2 = Module2() module2.save() school.module2 = module2.id # same idea again, collection Module2 contains all information about all Module2s for every school # school.module1 is of type ReferenceField meaning that this will be a reference to the document in the Module1 collection for this school. module3 = Module3() module3.save() school.module3 = module3.id school.save() school_id = school.id user = User() # finally create document in User collection containing information about the registering user user.title = form.title.data user.user_type = 'instructor' user.name = form.name.data user.surname = form.surname.data user.email = form.email.data user.password = hash_password user.school = ObjectId(school_id) # save the user user.save() session['email'] = user.email return redirect(url_for('teachers.profile')) else: flash('User with this email already exist') return redirect(url_for('teachers.create')) return render_template('teacher-create.html', form=form)
def create_admin(): form = AdminForm(request.form) # get all the schools from the schools table school_list = [(str(row.id), row.name) for row in School.objects()] school_list.append(("not-listed", "My school is not listed")) form.school.choices = school_list if request.method == 'POST' and form.validate(): # check if a user exist with the given email or not existing_user = User.objects().filter(email=form.email.data).first() # Here check the SECRET KEY if existing_user is None and form.secret.data == 'some_super_secret_text_here': # before inserting the document into database we have to hash password hash_password = bcrypt.hashpw(form.password.data.encode('utf-8'), bcrypt.gensalt()) # check if the school is listed or not school_id = form.school.data if form.school.data == 'not-listed': # get the name of the school school_name = request.form.get('school_name') # insert the school into the database school = School() school.name = school_name # here create the module1, module2, module3 module1 = Module1() module1.save() school.module1 = module1.id module2 = Module2() module2.save() school.module2 = module2.id module3 = Module3() module3.save() school.module3 = module3.id school.save() school_id = school.id user = User() # create document in User collection to contain information about registering user user.title = form.title.data user.user_type = 'admin' user.name = form.name.data user.surname = form.surname.data user.email = form.email.data user.password = hash_password user.school = ObjectId(school_id) user.save() session['email'] = user.email return redirect(url_for('teachers.profile')) else: if form.secret.data != 'some_super_secret_text_here': flash('Wrong secret key !') else: flash('User with this email already exist') return redirect(url_for('teachers.create_admin')) return render_template('admin-create.html', form=form)
def create_assignment(): # get the logged in user by session.get('email') if session.get('email'): # get instance of assignment form form = AssignmentForm(request.form) user = User.objects().filter(email=session.get('email')).first() # get the user document from database from logged in user if user.user_type == 'instructor' or user.user_type == 'admin': if request.method == 'POST' and form.validate(): # check if the user type is teacher or admin and validate form for incoming POST request # First save the Question assignment = Assignment() # declare instance of assignment model for use and assign form data to it assignment.title = form.title.data assignment.description = form.description.data assignment.submission_date = form.submission_date.data assignment.created_by = user.id assignment.school = user.school.id assignment.save() # save the assignment to the database # get the user school user_school = School.objects(id=user.school.id).first() # get the school document from database based on the school id user_school.assignments.append(assignment.id) # add the assignment to the list of assignments for the school user_school.save() # redirect the user in the assignments list route return redirect(url_for('assignments.assignment_list')) else: return redirect(url_for('students.profile')) return render_template('assignment-create.html', form=form, user=user) else: return redirect(url_for('login'))
def create(): form = StudentForm(request.form) # get all the schools from the schools table school_list = [(str(row.id), row.name) for row in School.objects()] form.school.choices = school_list if request.method == 'POST' and form.validate(): # check if a user exist with the given email or not existing_user = User.objects().filter(email=form.email.data).first() if existing_user is None: # have to hash password #get the school id here. hash_password = bcrypt.hashpw(form.password.data.encode('utf-8'), bcrypt.gensalt()) # check if the school is listed or not school_id = form.school.data user = User() # create instance of User model and assign form data to it user.user_type = 'student' user.name = form.name.data user.surname = form.surname.data user.email = form.email.data user.password = hash_password user.approved = False user.login_counter = 1 user.login_array = [datetime.datetime.now()] user.school = ObjectId(school_id) user.save() session['email'] = user.email return redirect(url_for('students.PersonalQuestion')) else: flash('User with this email already exist') return redirect(url_for('students.create')) return render_template('student-create.html', form=form)
def chat_create(module): if session.get('email'): form = ChatForm(request.form) # get instance of ChatForm model user = User.objects().filter(email=session.get('email')).first() # get user data of logged in user if user.user_type == 'instructor' or user.user_type == 'admin': #check if teacher or admin if request.method == 'POST' and form.validate(): #validate the form chat = Chat() # create instance of Chat model and assign form data chat.user = user.id chat.description = form.description.data chat.save() user_school = School.objects(id=user.school.id).first() if module == 'module1': #variable module is taken as parameter to the function flash('Chat Successfully Added to Module1 !') user_school_module1 = Module1.objects( id=user_school.module1.id).first() user_school_module1.chats.append(chat.id) user_school_module1.save() elif module == 'module2': flash('Chat Successfully Added to Module2 !') user_school_module2 = Module2.objects( id=user_school.module2.id).first() user_school_module2.chats.append(chat.id) user_school_module2.save() elif module == 'module3': flash('Chat Successfully Added to Module3 !') user_school_module3 = Module3.objects( id=user_school.module3.id).first() user_school_module3.chats.append(chat.id) user_school_module3.save() user_school.save() if module == 'module1': return redirect(url_for('schools.module1')) if module == "module2": return redirect(url_for('schools.module2')) if module == "module3": return redirect(url_for('schools.module3')) # redirect the user to the chat-create # Flash Message that Chat Successfully Created else: return redirect(url_for('students.profile')) return render_template('chat-create.html', form=form, user=user, module=module) else: return redirect(url_for('login'))
def add_to_module2(file_id): # Check if the user is logged in or not. if not logged in then redirect to login if session.get('email'): file = File.objects(id=file_id).first() # get the logged in user school # add the file to the module2 array user = User.objects().filter(email=session.get('email')).first() if user.user_type == 'instructor' or user.user_type == 'admin': user_school = School.objects(id=user.school.id).first() user_school_module2 = Module2.objects( id=user_school.module2.id).first() user_school_module2.files.append(file.id) user_school_module2.save() return redirect(url_for('files.file_list')) else: return redirect(url_for('login'))
def module3(): # Check if the user logged in or not. if not logged in then redirect to login if session.get('email'): user = User.objects().filter(email=session.get('email')).first() # check if the user is teacher or student then also check if the student is approved or not if (user.user_type == 'instructor' or user.approved) or user.user_type == 'admin': user_school = School.objects(id=user.school.id).first() files = user_school.module3.files # Here Fetch All the user school chat chats = user_school.module3.chats chats.sort(key=lambda x: x['created_at'], reverse=True) # use lambda expression to reverse order of the post to module post board so newest ones are first return render_template('module3.html', files=files, user=user, chats=chats) else: redirect(url_for('login'))
def edit_profile(): if session.get('email'): form = EditForm() school_list = [(str(row.id), row.name) for row in School.objects()] form.school.choices = school_list user = User.objects().filter(email=session.get('email')).first() form.name.data = user.name form.surname.data = user.surname if request.method == 'POST' and form.validate(): user.name = form.name.data user.surname = form.surname.data if user.user_type == 'admin' and form.school.data is not None: user.school = ObjectId(form.school.data) user.save() flash('Profile successfully updated !') return render_template('teacher-edit-profile.html', user=user, form=form) else: return redirect(url_for('login'))
def module1(): # Check if the user is logged in or not. if not logged in then redirect to login if session.get('email'): user = User.objects().filter(email=session.get('email')).first() # check if the user is teacher or student then also check if the student is approved or not if (user.user_type == 'instructor' or user.approved) or user.user_type == 'admin': #get document user_school = School.objects(id=user.school.id).first() files = user_school.module1.files # get the list of files that are in module1 for this school print(files) # Here Fetch All the user school chat, chats = user_school.module1.chats chats.sort(key=lambda x: x['created_at'], reverse=True) # sort the order of the chat by created_at return render_template('module1.html', files=files, user=user, chats=chats) else: return redirect(url_for('login'))