Skip to content

Commit

Permalink
Redirect to volunteer sign-up if appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
marksteward committed May 15, 2024
1 parent d0a6601 commit 3b382be
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
28 changes: 23 additions & 5 deletions apps/volunteer/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
from flask import Blueprint, request
from decorator import decorator

from ..common import require_permission
from flask import Blueprint, request, current_app as app, redirect, url_for, abort
from flask_login import current_user

from models.volunteer import Volunteer

volunteer = Blueprint("volunteer", __name__)


# This is like require_permission but redirects to volunteer sign-up
def require_volunteer_permission(permission):
def call(f, *args, **kwargs):
if current_user.is_authenticated:
if not Volunteer.get_for_user(current_user):
return redirect(url_for("volunteer.sign_up", next=request.path))
if current_user.has_permission(permission):
return f(*args, **kwargs)
abort(404)
return app.login_manager.unauthorized()

return decorator(call)


# User basically means they have signed up
# These aren't needed for the admin views as they have their auth baked in.
v_user_required = require_permission("volunteer:user")
v_admin_required = require_permission("volunteer:admin")
v_manager_required = require_permission("volunteer:manager")
v_user_required = require_volunteer_permission("volunteer:user")
v_admin_required = require_volunteer_permission("volunteer:admin")
v_manager_required = require_volunteer_permission("volunteer:manager")


@volunteer.context_processor
Expand Down
4 changes: 3 additions & 1 deletion apps/volunteer/sign_up.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from ..common.forms import Form
from ..common.fields import TelField
from ..common import create_current_user, feature_flag
from apps.users import get_next_url


class VolunteerSignUpForm(Form):
Expand Down Expand Up @@ -82,7 +83,8 @@ def sign_up():
db.session.commit()
app.logger.info("Add volunteer: %s", new_volunteer)
flash("Thank you for signing up!", "message")
return redirect(url_for(".choose_role"))

return redirect(get_next_url(default=url_for(".choose_role")))

return render_template("volunteer/sign-up.html", user=current_user, form=form)

Expand Down

0 comments on commit 3b382be

Please sign in to comment.