Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added SessionCancelView to admin, mentors #888

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions coderdojochi/models/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,9 @@ def get_absolute_url(self) -> str:
def get_sign_up_url(self):
return reverse("session-sign-up", args=[str(self.id)])

def get_cancel_url(self):
return reverse("session-cancel", args=[str(self.id)])

def get_calendar_url(self):
return reverse("session-calendar", args=[str(self.id)])

Expand Down
33 changes: 33 additions & 0 deletions coderdojochi/templates/guardian/session_cancel.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{% extends "coderdojochi/_base.html" %}

{% load i18n %}

{% block title %}{{ session.course.title }} Sign Up | {{ block.super }}{% endblock %}

{% block body_class %}page-class-sign-up{% endblock %}

{% block contained_content %}
<div class="container registration min-height">
{% if user_signed_up %}

<h1 class="title light"><strong>{{ student.full_name }}</strong> can no longer make it to the <strong>"{{ session.course.title }}"</strong> class on <strong>{{ session.start_date|date }}</strong> from <strong>{{ session.start_date|time }}</strong> to <strong>{{ session.end_date|time }}</strong>.</h1>

<form class="form" method="POST" action="">
{% csrf_token %}
<button class="button tertiary large">Yes, cancel</button>
<a href="{{ session.get_absolute_url }}" class="button large secondary">Nevermind</a>
</form>

{% else %}

<h1 class="title light">Enroll <strong>{{ student.full_name }}</strong> for the <strong>"{{ session.course.title }}"</strong> class on <strong>{{ session.start_date|date }}</strong> from <strong>{{ session.start_date|time }}</strong> to <strong>{{ session.end_date|time }}</strong>.</h1>

<form class="form" method="POST" action="">
{% csrf_token %}
<button class="button large">Yes, I'm excited!</button>
<a href="{{ session.get_absolute_url }}" class="button large secondary">Nevermind</a>
</form>

{% endif %}
</div>
{% endblock %}
21 changes: 21 additions & 0 deletions coderdojochi/templates/mentor/session_cancel.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{% extends "coderdojochi/_base.html" %}

{% load i18n %}

{% block title %}{{ session.course.title }} Cancel | {{ block.super }}{% endblock %}

{% block body_class %}page-class-sign-up{% endblock %}

{% block contained_content %}
<div class="container registration min-height">

<h1 class="title light">I can no longer volunteer for the <strong>"{{ session.course.title }}"</strong> class on <strong>{{ session.mentor_start_date|date }}</strong> from <strong>{{ session.mentor_start_date|time }}</strong> to <strong>{{ session.mentor_end_date|time }}</strong>.</h1>

<form class="form" method="POST" action="">
{% csrf_token %}
<button class="button tertiary large">Yes, cancel</button>
<a href="{{ session.get_absolute_url }}" class="button large secondary">Nevermind</a>
</form>

</div>
{% endblock %}
2 changes: 1 addition & 1 deletion coderdojochi/templates/mentor/session_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ <h3 class="title text-primary">Class Details & Enrollment</h3>
{% else %}
{% if mentor_signed_up %}
<p>You are signed up to mentor this class.</p>
<p><a href="{{ session.get_sign_up_url }}" class="button large tertiary">I can't make it...</a></p>
<p><a href="{{ session.get_cancel_url }}" class="button large tertiary">I can't make it...</a></p>
{% else %}
{% if spots_remaining %}
<p><a href="{{ session.get_sign_up_url }}" class="button large secondary">Sign up now!</a></p>
Expand Down
24 changes: 6 additions & 18 deletions coderdojochi/templates/mentor/session_sign_up.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,14 @@

{% block contained_content %}
<div class="container registration min-height">
{% if user_signed_up %}

<h1 class="title light">I can no longer volunteer for the <strong>"{{ session.course.title }}"</strong> class on <strong>{{ session.mentor_start_date|date }}</strong> from <strong>{{ session.mentor_start_date|time }}</strong> to <strong>{{ session.mentor_end_date|time }}</strong>.</h1>
<h1 class="title light">Sign up to mentor for the <strong>"{{ session.course.title }}"</strong> class on <strong>{{ session.mentor_start_date|date }}</strong> from <strong>{{ session.mentor_start_date|time }}</strong> to <strong>{{ session.mentor_end_date|time }}</strong>.</h1>

<form class="form" method="POST" action="">
{% csrf_token %}
<button class="button tertiary large">Yes, cancel</button>
<a href="{{ session.get_absolute_url }}" class="button large secondary">Nevermind</a>
</form>
<form class="form" method="POST" action="">
{% csrf_token %}
<button class="button large">Yes, I'm excited!</button>
<a href="{{ session.get_absolute_url }}" class="button large secondary">Nevermind</a>
</form>

{% else %}

<h1 class="title light">Sign up to mentor for the <strong>"{{ session.course.title }}"</strong> class on <strong>{{ session.mentor_start_date|date }}</strong> from <strong>{{ session.mentor_start_date|time }}</strong> to <strong>{{ session.mentor_end_date|time }}</strong>.</h1>

<form class="form" method="POST" action="">
{% csrf_token %}
<button class="button large">Yes, I'm excited!</button>
<a href="{{ session.get_absolute_url }}" class="button large secondary">Nevermind</a>
</form>

{% endif %}
</div>
{% endblock %}
7 changes: 7 additions & 0 deletions coderdojochi/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
SessionCalendarView,
SessionDetailView,
SessionSignUpView,
SessionCancelView,
WelcomeView,
meeting_announce,
meeting_sign_up,
Expand Down Expand Up @@ -156,6 +157,12 @@
path("<int:pk>/sign-up/", SessionSignUpView.as_view(), name="session-sign-up"),
# /classes/ID/sign-up/STUDENT-ID/
path("<int:pk>/sign-up/<int:student_id>/", SessionSignUpView.as_view(), name="session-sign-up"),

# Cancel session
# /classes/ID/cancel/
path("<int:pk>/cancel/", SessionCancelView.as_view(), name="session-cancel"),
# /classes/ID/cancel/STUDENT-ID/
path("<int:pk>/cancel/<int:student_id>/", SessionCancelView.as_view(), name="session-cancel"),
]
),
),
Expand Down
3 changes: 3 additions & 0 deletions coderdojochi/views/guardian/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ def get_context_data(self, **kwargs):
)

return context

class SessionCancelView(DetailView):
pass
52 changes: 49 additions & 3 deletions coderdojochi/views/mentor/sessions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.shortcuts import get_object_or_404
from django.core.exceptions import ObjectDoesNotExist
from django.shortcuts import get_object_or_404, redirect
from django.views.generic import DetailView

from ...models import Mentor, MentorOrder, Session
Expand All @@ -14,12 +15,11 @@ def get_context_data(self, **kwargs):

session_orders = MentorOrder.objects.filter(
session=session,
mentor=mentor,
is_active=True,
)

context = super().get_context_data(**kwargs)
context["mentor_signed_up"] = session_orders.exists()
context["mentor_signed_up"] = session_orders.filter(mentor=mentor).exists()
context["spots_remaining"] = session.mentor_capacity - session_orders.count()
context["account"] = mentor

Expand All @@ -31,3 +31,49 @@ def get_context_data(self, **kwargs):
)

return context


class SessionCancelView(DetailView):
model = Session
template_name = "mentor/session_cancel.html"

# redirect if order not found
def get(self, request, *args, **kwargs):
try:
self.object = self.get_object()
session = self.object
mentor = get_object_or_404(Mentor, user=self.request.user)
mentor_order = MentorOrder.objects.get(
session=session,
mentor=mentor,
is_active=True,
)
context = self.get_context_data(object=self.object)
return self.render_to_response(context)
except ObjectDoesNotExist:
return redirect(session.get_absolute_url())


def get_context_data(self, **kwargs):
session = self.object
mentor = get_object_or_404(Mentor, user=self.request.user)

mentor_order = MentorOrder.objects.get(
session=session,
mentor=mentor,
is_active=True,
)

context = super().get_context_data(**kwargs)
# context["mentor_signed_up"] = session_orders.filter(mentor=mentor).exists()
# context["spots_remaining"] = session.mentor_capacity - session_orders.count()
# context["account"] = mentor

# context["active_mentors"] = Mentor.objects.filter(
# id__in=MentorOrder.objects.filter(
# session=self.object,
# is_active=True,
# ).values("mentor__id")
# )

return context
133 changes: 30 additions & 103 deletions coderdojochi/views/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,109 +136,6 @@ def validate_partner_session_access(self, request, pk):
return False


# class SessionDetailView(RoleRedirectMixin, RoleTemplateMixin, TemplateView):
# template_name = "session_detail.html"

# def dispatch(self, request, *args, **kwargs):
# session_obj = get_object_or_404(Session, id=kwargs["pk"])

# if request.method == "GET":
# if session_obj.password and not self.validate_partner_session_access(self.request, kwargs["pk"]):
# return redirect(reverse("session-password", kwargs=kwargs))

# if request.user.is_authenticated and request.user.role:
# if "enroll" in request.GET or "enroll" in kwargs:
# return self.enroll_redirect(request, session_obj)

# # kwargs["session_obj"] = session_obj
# return super(SessionDetailView, self).dispatch(request, *args, **kwargs)

# def enroll_redirect(self, request, session_obj):
# if request.user.role == "mentor":
# return redirect("session-sign-up", pk=session_obj.id)

# guardian = get_object_or_404(Guardian, user=request.user)
# student = get_object_or_404(Student, guardian=guardian, id=(int(request.GET["student"])))

# if student:
# return redirect("session-sign-up", pk=session_obj.id, student_id=student.id)

# return redirect(f"{reverse('welcome')}?next={session_obj.get_absolute_url()}&enroll=True")

# def validate_partner_session_access(self, request, pk):
# authed_sessions = request.session.get("authed_partner_sessions")

# if authed_sessions and pk in authed_sessions:
# if request.user.is_authenticated:
# PartnerPasswordAccess.objects.get_or_create(session_id=pk, user=request.user)
# return True

# if request.user.is_authenticated:
# try:
# PartnerPasswordAccess.objects.get(session_id=pk, user_id=request.user.id)
# except PartnerPasswordAccess.DoesNotExist:
# return False
# else:
# return True

# else:
# return False

# def get_context_data(self, **kwargs):
# print(kwargs["session_obj"].__dict__)
# context = super(SessionDetailView, self).get_context_data(**kwargs)
# session_obj = kwargs["session_obj"]
# context["session"] = session_obj

# upcoming_classes = Session.objects.filter(is_active=True, start_date__gte=timezone.now()).order_by("start_date")
# context["upcoming_classes"] = upcoming_classes

# active_mentors = Mentor.objects.filter(
# id__in=MentorOrder.objects.filter(session=session_obj, is_active=True).values("mentor__id")
# )
# context["active_mentors"] = active_mentors

# if self.request.user.is_authenticated:
# if self.request.user.role == "mentor":
# account = get_object_or_404(Mentor, user=self.request.user)
# session_orders = MentorOrder.objects.filter(session=session_obj, is_active=True,)
# context["mentor_signed_up"] = session_orders.filter(mentor=account).exists()

# context["spots_remaining"] = session_obj.get_mentor_capacity() - session_orders.count()
# else:
# account = get_object_or_404(Guardian, user=self.request.user)
# context["students"] = account.get_students()
# context["spots_remaining"] = session_obj.capacity - session_obj.get_active_student_count()
# context["account"] = account
# else:
# context["upcoming_classes"] = upcoming_classes.filter(is_public=True)
# context["spots_remaining"] = session_obj.capacity - session_obj.objects.get_active_student_count()

# return context

# def post(self, request, *args, **kwargs):
# session_obj = kwargs["session_obj"]
# if "waitlist" not in request.POST:
# messages.error(request, "Invalid request, please try again.")
# return redirect(session_obj.get_absolute_url())

# if request.POST["waitlist"] == "student":
# account = Student.objects.get(id=request.POST["account_id"])
# waitlist_attr = "waitlist_students"
# else:
# account = Guardian.objects.get(id=request.POST["account_id"])
# waitlist_attr = "waitlist_guardians"

# if request.POST["remove"] == "true":
# getattr(session_obj, waitlist_attr).remove(account)
# session_obj.save()
# messages.success(request, "You have been removed from the waitlist. Thanks for letting us know.")
# else:
# getattr(session_obj, waitlist_attr).add(account)
# session_obj.save()
# messages.success(request, "Added to waitlist successfully.")
# return redirect(session_obj.get_absolute_url())


class SessionSignUpView(RoleRedirectMixin, RoleTemplateMixin, TemplateView):
template_name = "session_sign_up.html"
Expand All @@ -254,6 +151,8 @@ def dispatch(self, request, *args, **kwargs):
kwargs["mentor"] = get_object_or_404(Mentor, user=request.user)
kwargs["user_signed_up"] = session_orders.filter(mentor=kwargs["mentor"]).exists()

kwargs["spots_remaining"] = session_obj.mentor_capacity - session_orders.count()

elif request.user.role == "guardian":
kwargs["guardian"] = get_object_or_404(Guardian, user=request.user)
kwargs["student"] = get_object_or_404(Student, id=kwargs["student_id"])
Expand Down Expand Up @@ -286,6 +185,15 @@ def check_access(self, request, *args, **kwargs):
),
"redirect": request.META.get("HTTP_REFERER", "/dojo"),
}

# Check to make sure there is a spot open to sign up
if kwargs["spots_remaining"] <= 0:
access_dict = {
"message": (
"There are no slots open to mentor. Please check another class."
),
"redirect": request.META.get("HTTP_REFERER", kwargs["session_obj"].get_absolute_url()),
}

if kwargs.get("student"):
limits = self.student_limitations(kwargs["student"], kwargs["session_obj"], kwargs["user_signed_up"])
Expand Down Expand Up @@ -365,6 +273,25 @@ def post(self, request, *args, **kwargs):

return redirect(session_obj.get_absolute_url())

class SessionCancelView(View):
def get(self, request, *args, **kwargs):
pk = kwargs["pk"]
session = get_object_or_404(Session, id=pk)

# TODO: Do we need to check for this?
# if session.password and not self.validate_partner_session_access(request, pk):
# return redirect(reverse("session-password", kwargs=kwargs))

if request.user.is_authenticated:
if request.user.role == "mentor":
return mentor.SessionCancelView.as_view()(request, *args, **kwargs)
else:
return guardian.SessionCancelView.as_view()(request, *args, **kwargs)

return redirect(session.get_absolute_url())




class PasswordSessionView(TemplateView):
template_name = "session_partner_password.html"
Expand Down
12 changes: 6 additions & 6 deletions fixtures/12-coderdojochi.mentor.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@
"user": 3,
"bio": "",
"is_active": true,
"background_check": false,
"is_public": false,
"background_check": true,
"is_public": true,
"avatar": "",
"avatar_approved": false,
"birthday": null,
"gender": null,
"avatar_approved": true,
"birthday": "1988-01-01",
"gender": "male",
"work_place": null,
"phone": null,
"home_address": null,
"race_ethnicity": []
"race_ethnicity": [2]
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion fixtures/14-coderdojochi.session.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"start_date": "2022-01-01T16:00:00Z",
"location": 3,
"capacity": 20,
"mentor_capacity": 10,
"mentor_capacity": 1,
"instructor": 1,
"cost": "0.00",
"minimum_cost": null,
Expand Down
Loading