def group(groupname): # define the current group(obj) current_group = Group.query.filter(Group.groupname == session['groupname']).first() # SWITCH GROUP in main page switchGroupForm = SwitchGroupForm() switchGroupForm.groupname.choices = [(g.groupname, g.groupname) for g in current_user.in_groups()] switchGroupForm.groupname.choices.insert(0, ("--", "--")) # Change the current group you are working on # If one view has multiple submit button, don't use "validate_on_submit()", # instead you have to specify which submit button is clicked if switchGroupForm.submitSwitchGroup.data and switchGroupForm.validate(): # request.form['group'] is the cell name from switchGroupForm in main\forms.py session['groupname'] = request.form['groupname'] session['eldername'] = Group.query.filter( Group.groupname == session['groupname']).first().get_elder().username session['elderphoto'] = Group.query.filter( Group.groupname == session['groupname']).first().get_elder().avatar(26) session['adminname'] = Group.query.filter( Group.groupname == session['groupname']).first().get_admin().username session['adminphoto'] = Group.query.filter( Group.groupname == session['groupname']).first().get_admin().avatar(26) flash(f"You are now manipulating on {session['groupname']}", 'info') return redirect(url_for('main.group', groupname=session['groupname'])) # ADD NOTES PART addNoteForm = AddNoteForm() addNoteForm.notetype.choices = [(n.type, n.type) for n in NoteType.query.all()] # cases those are closed should not be allowed to be linked to by notes cases = current_group.cases.filter(Case.endtag != True) addNoteForm.casename.choices = [(c.casename, c.casename) for c in cases] addNoteForm.casename.choices.insert(0, ("(Decide later)","(Decide later)")) if addNoteForm.submitAddNote.data and addNoteForm.validate(): # attention here, dont need to deal with Foreign Key!!! thistype = NoteType.query.filter(NoteType.type==addNoteForm.notetype.data).first() thiscase = Case.query.filter(Case.casename==addNoteForm.casename.data).first() note = Note(notetext=addNoteForm.notetext.data, lasteditor=current_user.username, user=current_user, group=current_group, notetype=thistype, case=thiscase) db.session.add(note) db.session.commit() flash("You have added a note", 'success') return redirect(url_for('main.group', groupname=session['groupname'])) # send a unique tasklist to html tasklist = [] for case in current_group.cases: for task in case.tasks: tasklist.append(task) tasklist = set(tasklist) return render_template('group.html', title='Care Group', user=current_user, group=current_group, tasklist=tasklist, switchGroupForm=switchGroupForm, addNoteForm=addNoteForm)
def get_user(userId): """ Find user by ID Returns a user :param userId: ID of userr that needs to be fetched :type userId: str :rtype: User """ if not current_user.in_groups("admin") and userId != current_user.get_id(): raise Unauthorized() return Users().get_user(id=userId).to_dict()
def onCall(*args, **kw): if current_user.is_anonymous: return Unauthorized().to_problem() if len(groups) and not current_user.in_groups(*groups): return Unauthorized().to_problem() if len(groups): flask.current_app.logger.debug( "%s check groups %s is authorized" % (current_user.get_id(), ','.join(*groups))) else: flask.current_app.logger.debug("%s is authorized" % (current_user.get_id())) return func(*args, **kw)
def select_group(): # If the user is an "elderly", directly direct to his/her default group if current_user.usertype == "elderly": session['groupname'] = current_user.default_group().groupname session['eldername'] = Group.query.filter( Group.groupname == session['groupname']).first().get_elder().username session['elderphoto'] = Group.query.filter( Group.groupname == session['groupname']).first().get_elder().avatar(26) session['adminname'] = Group.query.filter( Group.groupname == session['groupname']).first().get_admin().username session['adminphoto'] = Group.query.filter( Group.groupname == session['groupname']).first().get_admin().avatar(26) flash("Welcome old friend, you are now in your default group", 'info') return redirect(url_for('main.group', groupname=session['groupname'])) switchGroupForm = SwitchGroupForm() switchGroupForm.groupname.choices = [(g.groupname, g.groupname) for g in current_user.in_groups()] switchGroupForm.groupname.choices.insert(0, ("--", "--")) # If the user is a "family member/health aide", go the select_group page if switchGroupForm.validate_on_submit(): # request.form['group'] is the cell name from switchGroupForm in main\forms.py session['groupname'] = request.form['groupname'] session['eldername'] = Group.query.filter( Group.groupname == session['groupname']).first().get_elder().username session['elderphoto'] = Group.query.filter( Group.groupname == session['groupname']).first().get_elder().avatar(26) session['adminname'] = Group.query.filter( Group.groupname == session['groupname']).first().get_admin().username session['adminphoto'] = Group.query.filter( Group.groupname == session['groupname']).first().get_admin().avatar(26) flash(f"You are now manipulating on {session['groupname']}", 'info') return redirect(url_for('main.group', groupname=session['groupname'])) return render_template('select_group.html', title="Select Group", switchGroupForm=switchGroupForm)
def __call__(self): return self.groups <= current_user.in_groups()
def managegroup(): handleMessageForm = HandleMessageForm() addUserForm = AddUserForm() dropUserForm = DropUserForm() leaveGroupForm = LeaveGroupForm() addGroupForm = AddGroupForm() if current_user.usertype == "elderly": dropUserForm.dropuser.choices = [ (user.username, user.username) for user in current_user.users_in_mygroup().all()] leaveGroupForm.leavegroup.choices = [ (group.groupname, group.groupname) for group in current_user.in_groups().all()] # add user to your default group if addUserForm.validate_on_submit(): userToAdd = User.query.filter( User.username == func.lower(addUserForm.adduser.data)).first() if userToAdd and userToAdd not in current_user.default_group().users: current_user.default_group().add_member(userToAdd) db.session.commit() flash( f'New member {addUserForm.adduser.data} has been successfully added to your group', 'success') return redirect(url_for("managegroup.managegroup")) else: flash('User not found, or you have already added him/her', 'warning') # drop user from your default group if dropUserForm.validate_on_submit(): userToDrop = User.query.filter( User.username == dropUserForm.dropuser.data).first() if (userToDrop and userToDrop != current_user): current_user.default_group().drop_member(userToDrop) db.session.commit() flash( f'User {dropUserForm.dropuser.data} has been successfully removed from your group', 'success') return redirect(url_for("managegroup.managegroup")) else: flash( "Please choose a user to remove, and you cannot remove yourself", 'warning') # send application for adding into a group if addGroupForm.validate_on_submit(): userToSendTo = User.query.filter( User.username == func.lower(addGroupForm.addgroupadm.data)).first() groupToAdd = Group.query.filter( Group.groupid == userToSendTo.default_group().groupid).first() if (groupToAdd and groupToAdd not in current_user.groups): # avoid the same application to be sent twice message = Message.query.filter(and_( Message.messagesender == current_user.userid, Message.messagetype == 'addgroup', Message.userid == userToSendTo.userid )).first() if not message: message = Message(messagetype="addgroup", messagesender=current_user.userid, messagetext=f"{current_user.username.capitalize()}({current_user.usertype.capitalize()}) applied to add to your care group!", user=userToSendTo) # userToSendTo.messages.append(message) db.session.commit() flash('Application has been sent!', 'success') return redirect(url_for("managegroup.managegroup")) else: flash( "Your application is on process, please don't send the same application again!", 'warning') else: flash("No such elderly, or you are already in his/her care group.", 'warning') # leave group(must not leave user's default group) if leaveGroupForm.validate_on_submit(): groupToLeave = Group.query.filter( Group.groupname == leaveGroupForm.leavegroup.data).first() if (groupToLeave and groupToLeave != current_user.default_group()): current_user.leave_group(groupToLeave) db.session.commit() flash( f'You have successfully left group {leaveGroupForm.leavegroup.data}', 'success') if (session['groupname'] == groupToLeave.groupname): return redirect(url_for("main.select_group")) else: return redirect(url_for("managegroup.managegroup")) else: flash( 'Please choose a group, and you cannot leave your default group', 'warning') return render_template('managegroup.html', title='Manage Group', group=session, addUserForm=addUserForm, dropUserForm=dropUserForm, addGroupForm=addGroupForm, leaveGroupForm=leaveGroupForm, handleMessageForm=handleMessageForm)