def profile(): if session.get('email'): user = User.objects().filter(email=session.get('email')).first() total=User.objects().filter(user_type='student').count() attached=User.objects().filter(attached='true').count() # find the student assignments # for this we need to find the assignment which is open as well as same as the student school assignments = Assignment.objects(school=user.school.id) return render_template('teacher-profile.html', user=user, assignments=assignments,total=total,attached=attached) else: return redirect(url_for('login'))
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 progress(student_id): if session.get('email'): 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() return render_template('student-progress.html', user=teacher, student=student) else: return redirect(url_for('students.profile')) else: return redirect(url_for('login'))
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': student = User.objects().filter(id=student_id).first() msg = Message('Hello student you have been removed from your class', sender = '*****@*****.**', recipients = [student['email']]) mail.send(msg) 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': # get the logged in user by session.get('email') 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 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': # get the logged in user by session.get('email') students = User.objects().filter(user_type='student',attached='true') stud=User.objects().filter(user_type='student',attached=False) print(stud) total=User.objects().filter(user_type='student').count() attached=User.objects().filter(attached='true').count() return render_template('students.html', students=students,attach=attached, user=user,total=total,stud=stud) else: return redirect(url_for('students.profile')) 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 exist 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 flash('Email or password is incorrect') return redirect(url_for('login')) else: # if user not found then show the same login page # we can set a flash message that email or password is incorrect flash('No user found. please sign up') return redirect(url_for('login')) # 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('login.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': # Here get the question by question id from the database and pass to the view 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 upload(): if session.get('email'): form = UploadForm() user = User.objects().filter(email=session.get('email')).first() # Wrong Approach of selecting name and surname # Below two lines will find query mongodb two times which is inefficient # Rather we should find the user. it will return a user. # then from that user we can easily select name and surname # name = users.find_one({'email': session.get('email')})['name'] # surname = users.find_one({'email': session.get('email')})['surname'] if request.method == 'POST': file = form.file.data if file and allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) # save the file name in the database new_file = File() new_file.filename = filename new_file.user = user.id new_file.save() return redirect(url_for('files.file_list')) return render_template('upload-file.html', form=form, user=user) else: return redirect(url_for('login'))
def internupdate(): # get the logged in user by session.get('email') if session.get('email'): form = internForm(request.form) user = User.objects().filter(email=session.get('email')).first() if user.user_type == 'student' : if request.method == 'POST' and form.validate() : history=History() history.email=user.email history.title=user.company history.background=user.background history.save() # First save the Question user.attached="true" user.company = form.title.data # user.date = form.submission_date.data user.background=form.description.data user.save() flash('Internship Company Added !') return redirect(url_for('students.profile')) else: return redirect(url_for('students.profile')) return render_template('updatecomp.html',form=form, user=user) 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() files = File.objects() return render_template('files.html', files=files, user=user) 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 student = User.objects().filter(id=student_id).first() msg = Message('Hello', sender = '*****@*****.**', recipients = [student['email']]) msg.body = "Hello student you have been approved for this platform" mail.send(msg) User.objects(id=student_id).update_one(approved=True) return redirect(url_for('students.student_list')) else: return redirect(url_for('students.profile')) else: return redirect(url_for('login'))
def profile(): if session.get('email'): user = User.objects().filter(email=session.get('email')).first() # find the student assignments # for this we need to find the assignment which is open as well as same as the student school assignments = Assignment.objects(school=user.school.id) return render_template('student-profile.html', user=user, assignments=assignments) 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 remove(post_id): if session.get('email'): user = User.objects().filter(email=session.get('email')).first() if user.user_type == 'instructor' or user.user_type == 'admin': Jobs.objects(id=post_id).delete() return redirect(url_for('teachers.managejobs')) else: return redirect(url_for('teachers.managejobs')) else: return redirect(url_for('login'))
def edit(post_id): if session.get('email'): form = internForm(request.form) user = User.objects().filter(email=session.get('email')).first() if user.user_type == 'instructor' or user.user_type == 'admin': jo=Jobs.objects(id=post_id).first() return render_template('editpost.html',form=form, user=user,job=jo) else: return redirect(url_for('teachers.managejobs')) 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 and send to the view 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 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 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 add_to_module3(file_id): # Check if the user 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 # finally add the file to the module1 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_module3 = Module3.objects( id=user_school.module3.id).first() user_school_module3.files.append(file.id) user_school_module3.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 # question.comments.remove(comment.id) new_user_posts = list(filter(lambda x: x.id != question.id, user.posts)) user.posts = new_user_posts user.save() question.delete() comments.delete() # also remove the comment from the question comment array return redirect(url_for('forums.questions')) else: return redirect(url_for('login'))
def chat_create(module): if session.get('email'): form = ChatForm(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(): # save the chat and it will return id of that chat chat = Chat() chat.user = user.id chat.description = form.description.data chat.save() user_school = School.objects(id=user.school.id).first() # finally push the id of the chat in the user_school module if module == 'module1': flash('Chat Successfully Added to Module1 !') # get the module 1 by it's user_school 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() # save the user_school user_school.save() # Finally redirect the user to the chat-create # With a Flash Message that Chat Successfully Created return redirect(url_for('schools.chat_create', module=module)) 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 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 add_tag(file_id): if session.get('email'): new_tags = request.form.get('tags').split(',') 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() 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() 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 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': 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) return render_template('module3.html', files=files, user=user, chats=chats) else: redirect(url_for('login'))
def jobedit(post_id): # get the logged in user by session.get('email') if session.get('email'): form = internForm(request.form) teacher = User.objects().filter(email=session.get('email')).first() jo=Jobs.objects(id=post_id).first() if teacher.user_type == 'instructor' or teacher.user_type == 'admin' : if request.method == 'POST' and form.validate(): jo.title=form.title.data jo.email=form.email.data jo.about=form.about.data jo.save() # flash('Internship Company Added !') return redirect(url_for('teachers.managejobs')) else: return redirect(url_for('teachers.profile')) jo= Jobs.objects().filter() return render_template('manageposts.html',form=form, user=teacher,job=jo) 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 return render_template('search-module.html', files=tagged_files, user=user) 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': # Here get the question by the question id from the database question = Post.objects(id=question_id).first() # Also populate all the reply in the 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 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 we will get the id of the question post.save() # Finally in the user Post List 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'))