Skip to content
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
56 changes: 29 additions & 27 deletions accounts/templates/account/guardian/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,16 @@

{% block contained_content %}

<section class="margin-top-3">
<h2 class="title text-primary">Your Profile</h2>
<p>Joined on {{ guardian.user.date_joined|naturalday|capfirst }}</p>

<form id="profile_form" enctype="multipart/form-data" action="" method="post">
{% csrf_token %}
<div class="grid-x grid-margin-x">
<div class="cell medium-6">{% bootstrap_field user_form.first_name %}</div>
<div class="cell medium-6">{% bootstrap_field user_form.last_name %}</div>
<div class="cell medium-6">{% bootstrap_field form.phone %}</div>
<div class="cell medium-6">{% bootstrap_field form.zip %}</div>
<div class="cell medium-6">{% bootstrap_field form.birthday %}</div>
<div class="cell medium-6">{% bootstrap_field form.gender %}</div>
<div class="cell medium-6">{% bootstrap_field form.race_ethnicity %}</div>
</div>
{% buttons %}
<button type="submit" class="button">Save</button>
{% endbuttons %}
</form>
</section>

<section class="margin-top-3">
<h2 class="title text-tertiary">Students</h2>
{% if students %}
<table class="students table table-striped">
<table class="students">
<thead>
<tr class="text-muted">
<th>Name</th>
<th class="hidden-xs">Age</th>
<th class="hidden-xs">Medical Conditions</th>
<th class="hidden-xs">Medications</th>
<!-- <th class="hidden-xs">Medical Conditions</th> -->
<!-- <th class="hidden-xs">Medications</th> -->
<th></th>
</tr>
</thead>
Expand All @@ -44,9 +23,9 @@ <h2 class="title text-tertiary">Students</h2>
<tr>
<td>{{ student.full_name }}</td>
<td class="hidden-xs">{{ student.get_age }}</td>
<td class="hidden-xs">{% if student.medical_conditions %}Yes{% else %}No{% endif %}</td>
<td class="hidden-xs">{% if student.medications %}Yes{% else %}No{% endif %}</td>
<td><a href="{% url 'student-detail' student.id %}" class="button small">Edit Info</a></td>
<!-- <td class="hidden-xs">{% if student.medical_conditions %}Yes{% else %}No{% endif %}</td> -->
<!-- <td class="hidden-xs">{% if student.medications %}Yes{% else %}No{% endif %}</td> -->
<td class="text-right"><a href="{% url 'student-detail' student.id %}" class="button ">Edit Info</a></td>
</tr>
{% endfor %}
</tbody>
Expand All @@ -57,6 +36,29 @@ <h2 class="title text-tertiary">Students</h2>
{% endif %}
</section>

<section class="margin-top-3">
<h2 class="title text-primary">Your Profile</h2>
<p>Joined on {{ guardian.user.date_joined|naturalday|capfirst }}</p>

<form id="profile_form" enctype="multipart/form-data" action="" method="post">
{% csrf_token %}
<div class="grid-x grid-margin-x">
<div class="cell medium-6">{% bootstrap_field user_form.first_name %}</div>
<div class="cell medium-6">{% bootstrap_field user_form.last_name %}</div>
<div class="cell medium-6">{% bootstrap_field form.phone %}</div>
<div class="cell medium-6">{% bootstrap_field form.zip %}</div>
<div class="cell medium-6">{% bootstrap_field form.birthday %}</div>
<div class="cell medium-6">{% bootstrap_field form.gender %}</div>
<div class="cell medium-6">{% bootstrap_field form.race_ethnicity %}</div>
</div>
{% buttons %}
<button type="submit" class="button">Save</button>
{% endbuttons %}
</form>
</section>



<section class="margin-top-3">
<h2 class="title text-secondary">Upcoming Classes</h2>

Expand Down
246 changes: 152 additions & 94 deletions accounts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,24 @@ class LoginView(MetadataMixin, AllAuthLoginView):


@method_decorator(login_required, name="dispatch")
class AccountHomeView(MetadataMixin, TemplateView):
class GuardianAccountHomeView(MetadataMixin, TemplateView):
template_name = "account/guardian/home.html"
title = "My Account | We All Code"

def dispatch(self, *args, **kwargs):
if not self.request.user.role:
guardian = get_object_or_404(Guardian, user=self.request.user)
students = Student.objects.filter(
is_active=True,
guardian=guardian,
).exists()

if students == False:
if "next" in self.request.GET:
return redirect(f"{reverse('welcome')}?next={self.request.GET['next']}")
else:
messages.warning(
messages.error(
self.request,
"Tell us a little about yourself before going on account.",
"Please add at least one student to your account.",
)
return redirect("welcome")

Expand All @@ -67,62 +74,11 @@ def get_context_data(self, **kwargs):

context["user"] = self.request.user

if self.request.user.role == "mentor":
return {**context, **self.get_context_data_for_mentor()}

if self.request.user.role == "guardian":
return {**context, **self.get_context_data_for_guardian()}

return context

def get_context_data_for_mentor(self):
mentor = get_object_or_404(Mentor, user=self.request.user)

orders = MentorOrder.objects.select_related().filter(
is_active=True,
mentor=mentor,
guardian = get_object_or_404(
Guardian,
user=self.request.user,
)

upcoming_sessions = orders.filter(is_active=True, session__start_date__gte=timezone.now()).order_by(
"session__start_date"
)

past_sessions = orders.filter(is_active=True, session__start_date__lte=timezone.now()).order_by(
"session__start_date"
)

meeting_orders = MeetingOrder.objects.select_related().filter(mentor=mentor)

upcoming_meetings = meeting_orders.filter(
is_active=True,
meeting__is_public=True,
meeting__end_date__gte=timezone.now(),
).order_by("meeting__start_date")

account_complete = False

if (
mentor.first_name
and mentor.last_name
and mentor.avatar
and mentor.background_check
and past_sessions.count() > 0
):
account_complete = True

return {
"mentor": mentor,
"orders": orders,
"upcoming_sessions": upcoming_sessions,
"past_sessions": past_sessions,
"meeting_orders": meeting_orders,
"upcoming_meetings": upcoming_meetings,
"account_complete": account_complete,
}

def get_context_data_for_guardian(self):
guardian = get_object_or_404(Guardian, user=self.request.user)

students = Student.objects.filter(
is_active=True,
guardian=guardian,
Expand All @@ -142,30 +98,20 @@ def get_context_data_for_guardian(self):
session__start_date__lte=timezone.now(),
).order_by("session__start_date")

return {
"guardian": guardian,
"students": students,
"student_orders": student_orders,
"upcoming_orders": upcoming_orders,
"past_orders": past_orders,
context = {
**context,
**{
"guardian": guardian,
"students": students,
"student_orders": student_orders,
"upcoming_orders": upcoming_orders,
"past_orders": past_orders,
},
}

def get(self, *args, **kwargs):
if self.request.user.role == "mentor":
return self.get_for_mentor(**kwargs)

if self.request.user.role == "guardian":
return self.get_for_guardian(**kwargs)

def get_for_mentor(self, **kwargs):
context = self.get_context_data(**kwargs)

context["form"] = MentorForm(instance=context["mentor"])
context["user_form"] = CDCModelForm(instance=context["mentor"].user)

return render(self.request, "account/mentor/home.html", context)
return context

def get_for_guardian(self, **kwargs):
def get(self, *args, **kwargs):
context = self.get_context_data(**kwargs)

context["form"] = GuardianForm(instance=context["guardian"])
Expand All @@ -174,20 +120,19 @@ def get_for_guardian(self, **kwargs):
return render(self.request, "account/guardian/home.html", context)

def post(self, *args, **kwargs):
if self.request.user.role == "mentor":
return self.post_for_mentor(**kwargs)

if self.request.user.role == "guardian":
return self.post_for_guardian(**kwargs)

def post_for_mentor(self, **kwargs):
context = self.get_context_data(**kwargs)

mentor = context["mentor"]
guardian = context["guardian"]

form = MentorForm(self.request.POST, self.request.FILES, instance=mentor)
form = GuardianForm(
self.request.POST,
instance=guardian,
)

user_form = CDCModelForm(self.request.POST, self.request.FILES, instance=mentor.user)
user_form = CDCModelForm(
self.request.POST,
instance=guardian.user,
)

if form.is_valid() and user_form.is_valid():
form.save()
Expand All @@ -202,16 +147,105 @@ def post_for_mentor(self, **kwargs):
context["form"] = form
context["user_form"] = user_form

return render(self.request, "account/guardian/home.html", context)


@method_decorator(login_required, name="dispatch")
class MentorAccountHomeView(MetadataMixin, TemplateView):
template_name = "account/mentor/home.html"
title = "My Account | We All Code"

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)

if "highlight" in self.request.GET:
context["highlight"] = self.request.GET["highlight"]
else:
context["highlight"] = False

context["user"] = self.request.user

mentor = get_object_or_404(
Mentor,
user=self.request.user,
)

orders = MentorOrder.objects.select_related().filter(
is_active=True,
mentor=mentor,
)

upcoming_sessions = orders.filter(
is_active=True,
session__start_date__gte=timezone.now(),
).order_by("session__start_date")

past_sessions = orders.filter(
is_active=True,
session__start_date__lte=timezone.now(),
).order_by("session__start_date")

meeting_orders = MeetingOrder.objects.select_related().filter(
mentor=mentor,
)

upcoming_meetings = meeting_orders.filter(
is_active=True,
meeting__is_public=True,
meeting__end_date__gte=timezone.now(),
).order_by(
"meeting__start_date",
)

account_complete = False

if (
mentor.first_name
and mentor.last_name
and mentor.avatar
and mentor.background_check
and past_sessions.count() > 0
):
account_complete = True

context = {
**context,
**{
"mentor": mentor,
"orders": orders,
"upcoming_sessions": upcoming_sessions,
"past_sessions": past_sessions,
"meeting_orders": meeting_orders,
"upcoming_meetings": upcoming_meetings,
"account_complete": account_complete,
},
}
return context

def get(self, *args, **kwargs):
context = self.get_context_data(**kwargs)

context["form"] = MentorForm(instance=context["mentor"])
context["user_form"] = CDCModelForm(instance=context["mentor"].user)

return render(self.request, "account/mentor/home.html", context)

def post_for_guardian(self, **kwargs):
def post(self, *args, **kwargs):
context = self.get_context_data(**kwargs)

guardian = context["guardian"]
mentor = context["mentor"]

form = GuardianForm(self.request.POST, instance=guardian)
form = MentorForm(
self.request.POST,
self.request.FILES,
instance=mentor,
)

user_form = CDCModelForm(self.request.POST, instance=guardian.user)
user_form = CDCModelForm(
self.request.POST,
self.request.FILES,
instance=mentor.user,
)

if form.is_valid() and user_form.is_valid():
form.save()
Expand All @@ -226,4 +260,28 @@ def post_for_guardian(self, **kwargs):
context["form"] = form
context["user_form"] = user_form

return render(self.request, "account/guardian/home.html", context)
return render(self.request, "account/mentor/home.html", context)


@method_decorator(login_required, name="dispatch")
class AccountHomeView(MetadataMixin, TemplateView):
title = "My Account | We All Code"

def dispatch(self, *args, **kwargs):
if not self.request.user.role:
if "next" in self.request.GET:
return redirect(f"{reverse('welcome')}?next={self.request.GET['next']}")

Check warning

Code scanning / CodeQL

URL redirection from remote source

Untrusted URL redirection depends on a [user-provided value](1).
else:
messages.warning(
self.request,
"Tell us a little about yourself before going on account.",
)
return redirect("welcome")

if self.request.user.role == "guardian":
return GuardianAccountHomeView.as_view()(self.request)

if self.request.user.role == "mentor":
return MentorAccountHomeView.as_view()(self.request)

return super().dispatch(*args, **kwargs)
Loading