def remove(student_id): if session.get('email'): user = User.objects().filter(email=session.get('email')).first() if user.user_type == 'instructor' or user.user_type == 'admin': # get the student by student_id User.objects(id=student_id).delete() return redirect(url_for('students.student_list')) else: return redirect(url_for('students.profile')) else: return redirect(url_for('login'))
def student_list(): if session.get('email'): user = User.objects().filter(email=session.get('email')).first() if user.user_type == 'instructor' or user.user_type == 'admin': students = User.objects().filter(user_type='student') return render_template('students.html', students=students, user=user) else: return redirect(url_for('students.profile')) else: return redirect(url_for('login'))
def approve(student_id): if session.get('email'): user = User.objects().filter(email=session.get('email')).first() if user.user_type == 'instructor' or user.user_type == 'admin': # get the student by student_id # update the approve value to true User.objects(id=student_id).update_one(approved=True) # Redirect to student list page return redirect(url_for('students.student_list')) else: return redirect(url_for('students.profile')) else: return redirect(url_for('login'))
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 login(): form = LoginForm() # first check if the request is get or post if request.method == 'POST': if form.validate(): # if request is post then grab the data from the request email = form.email.data password = form.password.data.encode('utf-8') # check if the user exists in the database using the given credentials user = User.objects().filter(email=email).first() # if user exists then check the password if user: if bcrypt.checkpw(password, user.password): session['email'] = form.email.data # Check the type of the user. # If user is a student then redirect that user to the student profile # If user is a instructor then redirect that user to the teacher profile if user.user_type == 'instructor' or user.user_type == 'admin': return redirect(url_for('teachers.profile')) return redirect(url_for('students.profile')) else: # if password is incorrect than show the below message and redirect flash('Email or password is incorrect') return render_template('login.html', form=form) else: # if user not found then flash the below message and redirect flash('No user found. please sign up') return render_template('login.html', form=form) # if everything is okay then delete the password from the user object # set the user into the session # redirect the user in profile page return render_template('home.html', form=form)
def question_reply(question_id): if session.get('email'): # get the logged in user by session.get('email') user = User.objects().filter(email=session.get('email')).first() if user.user_type == 'instructor' or user.user_type == 'admin': # get the question by question id from the database question = Post.objects(id=question_id).first() form = ReplyForm(request.form) form.questionId.data = question.id if request.method == 'POST' and form.validate(): comment = Comment() comment.author = user.name + ' ' + user.surname comment.description = form.description.data comment.post = question.id comment.save() question.comments.append(comment.id) question.save() return redirect('/forums/questions/details/' + str(form.questionId.data)) else: return redirect(url_for('students.profile')) return render_template('question-reply.html', form=form, question=question, user=user) else: return redirect(url_for('login'))
def search(): if session.get('email'): search_tag = request.form.get('search') user = User.objects().filter(email=session.get('email')).first() # check if the user is teacher or if student then also check if the student is approved or not if (user.user_type == 'instructor' or user.approved) or user.user_type == 'admin': files = File.objects() # Now check for each files in which file this tag exists tagged_files = list() for file in files: # Loop through the tag and match the file tag with the search tag for file_tag in file.tags: if file_tag.upper() == search_tag.upper(): tagged_files.append(file) break #Also loop through all the names of the files to see if the search matches for file in files: if search_tag.upper() in file.filename.upper(): tagged_files.append(file) return render_template('search-module.html', files=tagged_files, user=user) else: return redirect(url_for('login'))
def upload(): #check if the user is logged in by checking session.email if session.get('email'): form = UploadForm() #create instacnce of UploadForm user = User.objects().filter(email=session.get('email')).first() #get the user document from database based on the email of the logged in user if request.method == 'POST': file = form.file.data if file and allowed_file(file.filename): #check if file has been selected for upload and check if it is the allowed type filename = secure_filename(file.filename) #convert the file name into a 'secure version' so it can be stored in database #https://werkzeug.palletsprojects.com/en/0.15.x/utils/ file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) print('Hello world!', file=sys.stderr) # save the file name in the database new_file = File() #declare instance of File model and assign data new_file.filename = filename new_file.user = user.id new_file.save() #save the uploaded fle to database and store it in the uploads folder return redirect(url_for('files.file_list')) return render_template('upload-file.html', form=form, user=user) else: return redirect(url_for('login'))
def profile_edit(): if session.get('email'): user = User.objects().filter(email=session.get('email')).first() if user.user_type == 'instructor' or user.user_type == 'admin': return redirect(url_for('teachers.edit_profile')) else: return redirect(url_for('students.edit_profile')) else: return redirect(url_for('login'))
def file_list(): # get the logged in user by session.get('email') if session.get('email'): user = User.objects().filter(email=session.get('email')).first() #get the user document from database based on the email of the logged in user files = File.objects() return render_template('files.html', files=files, user=user) else: return redirect(url_for('login'))
def profile(): if session.get('email'): user = User.objects().filter(email=session.get('email')).first() # find the assignments # need to find the assignment which is open as well as same the same school assignments = Assignment.objects(school=user.school.id) return render_template('teacher-profile.html', user=user, assignments=assignments) else: return redirect(url_for('login'))
def details(file_id): if session.get('email'): form = TagForm() user = User.objects().filter(email=session.get('email')).first() if user.user_type == 'instructor' or user.approved or user.user_type == 'admin': file = File.objects(id=file_id).first() return render_template('file-details.html', file=file, user=user, form=form) else: return redirect(url_for('login'))
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 questions(): if session.get('email'): # get the logged in user by session.get('email') user = User.objects().filter(email=session.get('email')).first() if user.user_type == 'instructor' or user.user_type == 'admin': # get all the question from the Post table posts = Post.objects() else: return redirect(url_for('students.profile')) return render_template('questions.html', posts=posts, user=user) else: return redirect(url_for('login'))
def progress(student_id): if session.get('email'): # check if user is logged in teacher = User.objects().filter(email=session.get('email')).first() if teacher.user_type == 'instructor' or teacher.user_type == 'admin': student = User.objects(id=student_id).first() # get the students data teacher = User.objects().filter(email=session['email']).first() bebras1 = BebrasQuiz1.objects().filter(email=student.email).first() # get bebras 1 quiz results bebras2 = BebrasQuiz2.objects().filter(email=student.email).first() # get bebras 2 quiz results return render_template('student-progress.html', user=teacher, student=student, bebras1=bebras1, bebras2=bebras2) else: return redirect(url_for('students.profile')) else: return redirect(url_for('login'))
def upload_document(assignment_id): # get the logged in user by session.get('email') if session.get('email'): submitted = False assignment = Assignment.objects(id=assignment_id).first() # check if the user has already submitted the assignment or not form = AssignmentDocumentForm() # create instance of AssignmentDocumentForm user = User.objects().filter(email=session.get('email')).first() #get user document of logged in user by email of user in session previously_submitted_doc = Document.objects( assignment=assignment.id, submitted_by=user.id).first() #check if the user has already submitted a document for this assignment if previously_submitted_doc is not None: # if user has already submitted document for assignment submitted = True form.submit.label.text = 'Edit Upload' if request.method == 'POST' and form.validate(): file = form.file.data if file and allowed_file(file.filename): # check if the file submitted in the POST request is of the allowed type, check by extension if not submitted: filename = secure_filename(file.filename) # get name of the file file.save( os.path.join(app.config['UPLOAD_FOLDER'], filename)) # save the file name in the database document = Document() # declare instance of Document model and assign form data to it document.filename = filename document.assignment = assignment.id document.submitted_by = user.id document.save() assignment.documents.append(document.id) assignment.save() # set a flash message that document successfully updated else: filename = secure_filename(file.filename) file.save( os.path.join(app.config['UPLOAD_FOLDER'], filename)) previously_submitted_doc.filename = filename previously_submitted_doc.save() # set a flash message that document successfully updated # redirect the user in the questions routes return redirect(url_for('assignments.assignment_list')) return render_template('assignment-document.html', form=form, assignment=assignment, user=user, submitted=submitted, document=previously_submitted_doc) else: return redirect(url_for('login'))
def edit_profile(): if session.get('email'): form = EditForm() user = User.objects().filter(email=session.get('email')).first() if request.method == 'POST' and form.validate(): user.name = form.name.data user.surname = form.surname.data user.save() flash('Profile successfully updated !') return render_template('student-edit-profile.html', user=user, form=form) else: return redirect(url_for('login'))
def assignment_details(assignment_id): if session.get('email'): # get the logged in user by session.get('email') user = User.objects().filter(email=session.get('email')).first() if user.user_type == 'instructor' or user.user_type == 'admin': # get all the question from the Post table assignment = Assignment.objects(id=assignment_id).first() else: return redirect(url_for('students.profile')) return render_template('assignment-details.html', assignment=assignment, user=user) else: return redirect(url_for('login'))
def assignment_list(): if session.get('email'): # get the logged in user by session.get('email') user = User.objects().filter(email=session.get('email')).first() if user.user_type == 'instructor' or user.user_type == 'admin': assignments = Assignment.objects(school=user.school.id) # get all the assignments from the Assignments collection else: return redirect(url_for('students.profile')) #if logged in user is not either teacher or admin redirect to student homepage return render_template('assignments.html', assignments=assignments, user=user) else: return redirect(url_for('login'))
def open_assignment(assignment_id): if session.get('email'): # get the logged in user by session.get('email') user = User.objects().filter(email=session.get('email')).first() if user.user_type == 'instructor' or user.user_type == 'admin': # get the document from assignment collection in database based on assignment id assignment = Assignment.objects(id=assignment_id).first() # make the assignment status True assignment.status = True assignment.save() # update the status to open, now accepting submissions again return redirect( url_for('assignments.assignment_details', assignment_id=assignment_id)) 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 remove_question(question_id): if session.get('email'): user = User.objects().filter(email=session.get('email')).first() question = Post.objects(id=question_id).first() comments = Comment.objects(post=question.id) # remove the comment from the database new_user_posts = list(filter(lambda x: x.id != question.id, user.posts)) user.posts = new_user_posts user.save() question.delete() comments.delete() # remove the comment from the question comment array return redirect(url_for('forums.questions')) else: return redirect(url_for('login'))
def close_assignment(assignment_id): if session.get('email'): # get the logged in user by session.get('email') user = User.objects().filter(email=session.get('email')).first() if user.user_type == 'instructor' or user.user_type == 'admin': # check the user type assignment = Assignment.objects(id=assignment_id).first() # get document from assignment collection based on the id # make the assignment status false assignment.status = False assignment.save() # update the status of the assignment to false, meaning it is closed and no longer accepting submissions return redirect( url_for('assignments.assignment_details', assignment_id=assignment_id)) 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 question_details(question_id): if session.get('email'): # get the logged in user by session.get('email') user = User.objects().filter(email=session.get('email')).first() if user.user_type == 'instructor' or user.user_type == 'admin': # get the question by the question id from the database question = Post.objects(id=question_id).first() # Also populate all the replies to that question question_comments = Post.objects(comments__in=question.comments) comments = [] for q in question_comments: comments = q.comments else: return redirect(url_for('students.profile')) return render_template('question-details.html', question=question, comments=comments, user=user) 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'))
def add_tag(file_id): if session.get('email'): # check if the user is logged in new_tags = request.form.get('tags').split(',') # get the tags from the form data and split up accordingly user = User.objects().filter(email=session.get('email')).first() # get user data for logged in user if user.user_type == 'instructor' or user.approved or user.user_type == 'admin': file = File.objects(id=file_id).first() # get file data by file id tag_list = list() for tag in file.tags: tag_list.append(tag.upper()) for tag in new_tags: if tag.strip().upper() not in tag_list: # trim the tag. so that no empty space before the tag and also after the tag tag_list.append(tag.strip().upper()) file.tags = tag_list file.save() # save the file with its new tags return redirect(url_for('files.file_list')) else: return redirect(url_for('login'))
def create_question(): # get the logged in user by session.get('email') if session.get('email'): form = QuestionForm(request.form) user = User.objects().filter(email=session.get('email')).first() if user.user_type == 'instructor' or user.user_type == 'admin': if request.method == 'POST' and form.validate(): # First save the Question post = Post() post.subject = form.subject.data post.description = form.description.data post.author = user.name + ' ' + user.surname # After saving question get the id of the question post.save() # Push the id of the question user.posts.append(post.id) user.save() # redirect the user in the questions routes return redirect(url_for('forums.questions')) else: return redirect(url_for('students.profile')) return render_template('add-question.html', form=form, user=user) else: return redirect(url_for('login'))
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)