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 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 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 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 create(): form = StudentForm(request.form) # get all the schools from the schools table # Issue row.id is mongodb objectId so we need to convert it into str. # otherwise the form validation will failed 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(): # for field, errors in form.errors.items(): # for error in errors: # flash(u"Error in the %s field - %s" % ( # getattr(form, field).label.text, # error # )) # 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 # we will also 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 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 # Assign module1, module2, module3 to the school 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() # Set the user data 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()] # Since we converted the row.id to string. for school we need to convert is ObjectId # Because school is a reference type user.school = ObjectId(school_id) # Finally save the user user.save() session['email'] = user.email return redirect(url_for('students.profile')) else: flash('User with this email already exist') return redirect(url_for('students.create')) return render_template('student-create.html', form=form)