def update(name): ''' Update the team owner of the current user ''' team = cached_teams.get_team(name) def handle_valid_form(form): new_team = Team(id=form.id.data, name=form.name.data, description=form.description.data, public=form.public.data) cached_teams.delete_team_summary() db.session.merge(new_team) db.session.commit() flash(gettext('Team updated!'), 'success') return redirect(url_for('.detail', name=new_team.name)) if not require.team.update(team): abort(403) title = gettext('Update Team') if request.method == 'GET': form = TeamForm(obj=team) form.populate_obj(team) if request.method == 'POST': form = TeamForm(request.form) if form.validate(): return handle_valid_form(form) flash(gettext('Please correct the errors'), 'error') return render_template('/team/update.html', form=form, title=title, team=team)
def detail(name=None): ''' Team details ''' if not require.team.read(): abort(403) team = cached_teams.get_team(name) title = team_title(team, team.name) ''' Get extra data ''' data = dict( belong = cached_teams.user_belong_team(team.id), members = cached_teams.get_number_members(team.id) ) data['rank'], data['score'] = cached_teams.get_rank(team.id) try: require.team.read(team) template = '/team/settings.html' except HTTPException: template = '/team/index.html' template_args = { "team": team, "title": title, "data": data } return render_template(template, **template_args)
def delete(name): ''' Delete the team owner of de current_user ''' team = cached_teams.get_team(name) title = gettext('Delete Team') if not require.team.delete(team): abort(403) if request.method == 'GET': return render_template('/team/delete.html', title=title, team=team) print "delete_team_summary" cached_teams.delete_team_summary() db.session.delete(team) db.session.commit() flash(gettext('Team deleted!'), 'success') return redirect(url_for('team.myteams'))
def search_users(name): ''' Search users in a team''' if not require.team.read(): abort(403) team = cached_teams.get_team(name) form = SearchForm(request.form) users = db.session.query(User).all() if request.method == 'POST' and form.user.data: query = '%' + form.user.data.lower() + '%' founds = db.session.query(User)\ .filter(or_(func.lower(User.name).like(query), func.lower(User.fullname).like(query)))\ .all() if not founds: msg = gettext('Ooops! We didn\'t find a user matching your query:') flash(msg) return render_template( '/team/search_users.html', founds = [], team = team, title = gettext('Search name of User')) else: for found in founds: user2team = User2Team.query\ .filter(User2Team.team_id==team.id)\ .filter(User2Team.user_id==found.id)\ .first() found.belong = (1, 0)[user2team is None] return render_template( '/team/search_users.html', founds = founds, team = team, title = gettext('Search User')) return render_template( '/team/search_users.html', founds = [], team = team, title = gettext('Search User'))
def user_delete(name, user=None): team = cached_teams.get_team(name) title = gettext('Delete User from a Team') if not require.team.read(): abort(403) if request.method == 'GET': return render_template('/team/user_separate.html', title=title, team=team, user=user) if user: user_search = User.query.filter_by(name=user).first() if not user_search: flash(gettext('This user don\t exists!!!'), 'error') return redirect(url_for('team.myteams', name=team.name)) else: ''' Check to see if the current_user is the owner or admin ''' if current_user.admin is True or team.owner_id == current_user.id: user_id = user_search.id else: flash( gettext( 'You do not have right to separate to this team!!!'), 'error') return redirect(url_for('team.myteams', name=team.name)) else: user_id = current_user.id ''' Check if exits association''' user2team = db.session.query(User2Team)\ .filter(User2Team.user_id == user_id )\ .filter(User2Team.team_id == team.id )\ .first() if user2team: cached_teams.delete_team_members() db.session.delete(user2team) db.session.commit() flash(gettext('Association to the team deleted'), 'success') return redirect(url_for('team.myteams'))
def search_users(name): ''' Search users in a team''' if not require.team.read(): abort(403) team = cached_teams.get_team(name) form = SearchForm(request.form) users = db.session.query(User).all() if request.method == 'POST' and form.user.data: query = '%' + form.user.data.lower() + '%' founds = db.session.query(User)\ .filter(or_(func.lower(User.name).like(query), func.lower(User.fullname).like(query)))\ .all() if not founds: msg = gettext( 'Ooops! We didn\'t find a user matching your query:') flash(msg) return render_template('/team/search_users.html', founds=[], team=team, title=gettext('Search name of User')) else: for found in founds: user2team = User2Team.query\ .filter(User2Team.team_id==team.id)\ .filter(User2Team.user_id==found.id)\ .first() found.belong = (1, 0)[user2team is None] return render_template('/team/search_users.html', founds=founds, team=team, title=gettext('Search User')) return render_template('/team/search_users.html', founds=[], team=team, title=gettext('Search User'))
def user_delete(name,user=None): team = cached_teams.get_team(name) title = gettext('Delete User from a Team') if not require.team.read(): abort(403) if request.method == 'GET': return render_template('/team/user_separate.html', title=title, team=team, user=user ) if user: user_search = User.query.filter_by(name=user).first() if not user_search: flash(gettext('This user don\t exists!!!'), 'error') return redirect(url_for('team.myteams', name=team.name )) else: ''' Check to see if the current_user is the owner or admin ''' if current_user.admin is True or team.owner_id == current_user.id: user_id = user_search.id else: flash(gettext('You do not have right to separate to this team!!!'), 'error') return redirect(url_for('team.myteams', name=team.name )) else: user_id = current_user.id ''' Check if exits association''' user2team = db.session.query(User2Team)\ .filter(User2Team.user_id == user_id )\ .filter(User2Team.team_id == team.id )\ .first() if user2team: cached_teams.delete_team_members() db.session.delete(user2team) db.session.commit() flash(gettext('Association to the team deleted'), 'success') return redirect(url_for('team.myteams'))
def delete(name): ''' Delete the team owner of de current_user ''' team = cached_teams.get_team(name) title = gettext('Delete Team') if not require.team.delete(team): abort(403) if request.method == 'GET': return render_template( '/team/delete.html', title=title, team=team) print "delete_team_summary" cached_teams.delete_team_summary() db.session.delete(team) db.session.commit() flash(gettext('Team deleted!'), 'success') return redirect(url_for('team.myteams'))
def detail(name=None): ''' Team details ''' if not require.team.read(): abort(403) team = cached_teams.get_team(name) title = team_title(team, team.name) ''' Get extra data ''' data = dict(belong=cached_teams.user_belong_team(team.id), members=cached_teams.get_number_members(team.id)) data['rank'], data['score'] = cached_teams.get_rank(team.id) try: require.team.read(team) template = '/team/settings.html' except HTTPException: template = '/team/index.html' template_args = {"team": team, "title": title, "data": data} return render_template(template, **template_args)
def join_private_team(): key = request.args.get('key') if key is None: abort(403) userdict = {} try: userdict = signer.loads(key, max_age=3600, salt='join-private-team') except BadData: abort(403) username = userdict.get('user') teamname = userdict.get('team') if not username or not teamname or current_user.name != username: abort (403) ''' Add to Public with invitation team ''' team = cached_teams.get_team(teamname) if not team: flash(gettext('This team doesn\'t exists'), 'error') return redirect(url_for('team.myteams')) ''' Search relationship ''' user2team = db.session.query(User2Team)\ .filter(User2Team.user_id == current_user.id)\ .filter(User2Team.team_id == team.id )\ .first() if user2team: flash(gettext('This user is already in this team'), 'error') return redirect(url_for('team.users', name=team.name )) else: user2team = User2Team(user_id = current_user.id, team_id = team.id ) cached_teams.delete_team_summary() db.session.add(user2team) db.session.commit() flash(gettext('Congratulations! You belong to the Public Invitation Only Team'), 'sucess') return redirect(url_for('team.users', name=team.name ))
def join_private_team(): key = request.args.get('key') if key is None: abort(403) userdict = {} try: userdict = signer.loads(key, max_age=3600, salt='join-private-team') except BadData: abort(403) username = userdict.get('user') teamname = userdict.get('team') if not username or not teamname or current_user.name != username: abort(403) ''' Add to Public with invitation team ''' team = cached_teams.get_team(teamname) if not team: flash(gettext('This team doesn\'t exists'), 'error') return redirect(url_for('team.myteams')) ''' Search relationship ''' user2team = db.session.query(User2Team)\ .filter(User2Team.user_id == current_user.id)\ .filter(User2Team.team_id == team.id )\ .first() if user2team: flash(gettext('This user is already in this team'), 'error') return redirect(url_for('team.users', name=team.name)) else: user2team = User2Team(user_id=current_user.id, team_id=team.id) cached_teams.delete_team_summary() db.session.add(user2team) db.session.commit() flash( gettext( 'Congratulations! You belong to the Public Invitation Only Team' ), 'sucess') return redirect(url_for('team.users', name=team.name))
def update(name): ''' Update the team owner of the current user ''' team = cached_teams.get_team(name) def handle_valid_form(form): new_team = Team( id=form.id.data, name=form.name.data, description=form.description.data, public=form.public.data ) cached_teams.delete_team_summary() db.session.merge(new_team) db.session.commit() flash(gettext('Team updated!'), 'success') return redirect(url_for('.detail',name=new_team.name)) if not require.team.update(team): abort(403) title = gettext('Update Team') if request.method == 'GET': form = TeamForm(obj=team) form.populate_obj(team) if request.method == 'POST': form = TeamForm(request.form) if form.validate(): return handle_valid_form(form) flash(gettext('Please correct the errors'), 'error') return render_template( '/team/update.html', form=form, title=title, team=team)
def users(name): ''' Add new user to a team ''' team = cached_teams.get_team(name) title = gettext('Team Members') if not require.team.read(): abort(403) users = cached_teams.get_users_teams_detail(team.id) # Search users in the team belongs = User2Team.query.filter(User2Team.team_id == team.id)\ .all() template = '/team/users.html' template_args = { "team": team, "users": users, "belongs": belongs, "title": title } return render_template(template, **template_args)
def user_add(name,user=None): ''' Add Current User to a team ''' team = cached_teams.get_team(name) title = gettext('Add User to a Team') if not require.team.read(): abort(403) if request.method == 'GET': return render_template( '/team/user_add.html', title=title, team=team, user=user ) if user: user_search = User.query.filter_by(name=user).first() if not user_search: flash(gettext('This user don\t exists!!!'), 'error') return redirect(url_for('team.myteams', name=team.name )) else: ''' Check to see if the current_user is the owner or admin ''' if current_user.admin is True or team.owner_id == current_user.id: user_id = user_search.id else: flash(gettext('You do not have right to add to this team!!!'), 'error') return redirect(url_for('team.myteams', name=team.name )) else: user_search= current_user '''user_id = current_user.id''' ''' Search relationship ''' user2team = db.session.query(User2Team)\ .filter(User2Team.user_id == user_search.id )\ .filter(User2Team.team_id == team.id )\ .first() if user2team: flash(gettext('This user is already in this team'), 'error') return redirect(url_for('team.search_users', name=team.name )) else: if team.public == True: cached_teams.delete_team_members() user2team = User2Team( user_id = user_search.id, team_id = team.id ) db.session.add(user2team) db.session.commit() flash(gettext('Association to the team created'), 'success') return redirect(url_for('team.myteams' )) else: msg = Message(subject='Invitation to a Team', recipients=[user_search.email_addr]) userdict = {'user': user_search.name, 'team': team.name } key = signer.dumps(userdict, salt='join-private-team') join_url = url_for('.join_private_team', key=key, _external=True) msg.body = render_template( '/team/email/send_invitation.md', user=user_search, team=team, join_url=join_url) msg.html = markdown(msg.body) mail.send(msg) return render_template('./team/message.html')
def user_add(name, user=None): ''' Add Current User to a team ''' team = cached_teams.get_team(name) title = gettext('Add User to a Team') if not require.team.read(): abort(403) if request.method == 'GET': return render_template('/team/user_add.html', title=title, team=team, user=user) if user: user_search = User.query.filter_by(name=user).first() if not user_search: flash(gettext('This user don\t exists!!!'), 'error') return redirect(url_for('team.myteams', name=team.name)) else: ''' Check to see if the current_user is the owner or admin ''' if current_user.admin is True or team.owner_id == current_user.id: user_id = user_search.id else: flash(gettext('You do not have right to add to this team!!!'), 'error') return redirect(url_for('team.myteams', name=team.name)) else: user_search = current_user '''user_id = current_user.id''' ''' Search relationship ''' user2team = db.session.query(User2Team)\ .filter(User2Team.user_id == user_search.id )\ .filter(User2Team.team_id == team.id )\ .first() if user2team: flash(gettext('This user is already in this team'), 'error') return redirect(url_for('team.search_users', name=team.name)) else: if team.public == True: cached_teams.delete_team_members() user2team = User2Team(user_id=user_search.id, team_id=team.id) db.session.add(user2team) db.session.commit() flash(gettext('Association to the team created'), 'success') return redirect(url_for('team.myteams')) else: msg = Message(subject='Invitation to a Team', recipients=[user_search.email_addr]) userdict = {'user': user_search.name, 'team': team.name} key = signer.dumps(userdict, salt='join-private-team') join_url = url_for('.join_private_team', key=key, _external=True) msg.body = render_template('/team/email/send_invitation.md', user=user_search, team=team, join_url=join_url) msg.html = markdown(msg.body) mail.send(msg) return render_template('./team/message.html')