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

Staff #15

Merged
merged 16 commits into from
Jul 15, 2019
Binary file added .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ ENV/
env.bak/
venv.bak/
db/
*.idea

# Spyder project settings
.spyderproject
Expand Down
1 change: 1 addition & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file added attendance/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions attendance/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
5 changes: 5 additions & 0 deletions attendance/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class AttendanceConfig(AppConfig):
name = "attendance"
Empty file.
3 changes: 3 additions & 0 deletions attendance/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
133 changes: 133 additions & 0 deletions attendance/templates/attendance.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
{% extends "home/home.html" %}

{% block content %}

<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 75%;
}

td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}

tr:nth-child(even) {
background-color: cadetblue;
}

.button {
font-size: 10px;
font-family: "American Typewriter";
border-radius: 14px;
background-color: white;
color: #4CAF50;
border: 2px solid #000080;
}
.buttonSub {
font-size: 14px;
font-family: "American Typewriter";
border-radius: 18px;
background-color: white;
color: #4CAF50;
border: 2px solid #000080;
}
</style>

<div class="sidenav">
<li><a href="{% url 'home-home' %}">Home</a></li>
</div>

<div class="main-container">
{% if course %}
<h1 style="padding-bottom: 30px"> Attendance for {{ course }} </h1>
<table>
<tr>
<th>Date</th>
<th>Daily Attendance</th>
<th>Take Attendance</th>
</tr>
{% for date in dates %}
<tr>
<td>{{ date }}</td>
<td>{{ daily_attendance|get_item:date }}%</td>
<td>
<form method="post">
{% csrf_token %}
<input type="hidden" name="date" value="{{ date }}" />
<input type="hidden" name="course_id" value={{ course_id }} />
<input type="hidden" name="date_id" value={{ id }} />
<button class="button" type="submit">Take Attendance</button>
</form>
</td>
</tr>
{% endfor %}
</table>
{% elif attendance_objects %}
<h1 style="padding-bottom: 30px"> {{ course_name }} </h1>
<form method="POST" style="width: 80%">
{% csrf_token %}
<table>
<tr>
<th>Date</th>
<th>Student</th>
<th>Status</th>
<th>Current Status</th>
</tr>
{% for attendance_object in attendance_objects %}
<tr>
<td>{{ attendance_object.date }}</td>
<td>{{ attendance_object.student.first_name }} {{ attendance_object.student.last_name }}</td>
<td>
<select name="{{ attendance_object.student.first_name }} {{ attendance_object.student.last_name }}">
<option value="{{ attendance_object.presence }}">-----------</option>
<option value="Present">Present</option>
<option value="Absent">Absent</option>
<option value="Late">Late</option>
<option value="Excused">Excused</option>
</select>
</td>
<td>
{{ attendance_object.presence }}
</td>
</tr>
{% endfor %}
</table>
<input type="hidden" name="attendance_taken" value="true" />
<input type="hidden" name="date" value="{{ attendance_objects.first.date }}" />
<input type="hidden" name="course_id" value="{{ course_id }}" />
<div style="padding-top: 20px">
<button class="buttonSub" type="submit"> Submit </button>
</div>
</form>
{% else %}
<h1 style="padding-bottom: 30px"> Attendance Home </h1>
<table>
<tr>
<th>Course</th>
<th>Teacher</th>
<th>Overall Attendance</th>
<th>View Attendance</th>
</tr>
{% for classroom in classrooms %}
<tr>
<td>{{ classroom.course }}</td>
<td>{{ classroom.teacher }}</td>
<td>{{ attendance_averages|get_item:classroom.course }}%</td>
<td>
<form method="GET">
<button class="button" type="submit" name="course_id" value="{{ classroom.id }}">View Class Attendance</button>
</form>
</td>
</tr>
{% endfor %}
</table>
{% endif %}
</div>

{% endblock content %}


3 changes: 3 additions & 0 deletions attendance/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
4 changes: 4 additions & 0 deletions attendance/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from django.urls import path
from . import views

urlpatterns = [path("", views.attendance, name="attendance")]
123 changes: 123 additions & 0 deletions attendance/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required
from django.shortcuts import render, redirect
from home.models.models import Classroom, Attendance
from home.models.salesforce import ClassOffering
from staff.staff_views_helper import class_offering_meeting_dates
from datetime import datetime
from django.template.defaulttags import register


@login_required
def attendance(request):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up notes in #16, this could use input validation and Post/Redirect/Get refactor

if request.method == "POST":
if request.POST.get("attendance_taken") is not None:
store_attendance_data(request)
attendance_averages = compile_attendance_averages_for_all_courses()
context = {
"classrooms": Classroom.objects.all(),
"attendance_averages": attendance_averages,
}
return render(request, "attendance.html", context)
else:
context = take_attendance_context(
request.POST.get("course_id"),
datetime.strptime(request.POST.get("date"), "%B %d, %Y").date(),
)
return render(request, "attendance.html", context)
if request.GET.get("course_id") is not None:
context = get_classroom_attendance(request.GET.get("course_id"))
context.update(
{
"attendance_statistic": get_course_attendance_statistic(
request.GET.get("course_id")
)
}
)
else:
attendance_averages = compile_attendance_averages_for_all_courses()
context = {
"classrooms": Classroom.objects.all(),
"attendance_averages": attendance_averages,
}
return render(request, "attendance.html", context)


def get_classroom_attendance(course_id):
dates = get_classroom_meeting_dates(course_id)
classroom_attendance = Attendance.objects.filter(classroom_id=course_id)
course = Classroom.objects.get(id=course_id).course
daily_attendance = compile_daily_attendance_for_course(course_id)
return {
"classroom_attendance": classroom_attendance,
"dates": dates,
"course": course,
"course_id": course_id,
"daily_attendance": daily_attendance,
}


def take_attendance_context(course_id, date):
course = Classroom.objects.get(id=course_id).course
attendance_objects = Attendance.objects.filter(classroom_id=course_id, date=date)
return {
"course_name": course,
"course_id": course_id,
"attendance_objects": attendance_objects,
}


def store_attendance_data(request):
date = datetime.strptime(request.POST.get("date"), "%B %d, %Y").date()
course_id = request.POST.get("course_id")
attendance_objects = Attendance.objects.filter(classroom_id=course_id, date=date)
for attendance_object in attendance_objects:
attendance_object.presence = request.POST.get(str(attendance_object.student))
attendance_object.save()


def get_course_attendance_statistic(course_id):
class_attendance = Attendance.objects.filter(classroom_id=course_id)
class_attendance = [
daily_attendance
for daily_attendance in class_attendance
if daily_attendance.date < datetime.today().date()
tylerIams marked this conversation as resolved.
Show resolved Hide resolved
]
average = get_average_attendance_from_list(class_attendance)
return round(average * 100, 2)


def compile_attendance_averages_for_all_courses():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Performance follow-up in #16

attendance_averages = {}
for classroom in Classroom.objects.all():
stat = get_course_attendance_statistic(classroom.id)
attendance_averages.update({classroom.course: stat})
return attendance_averages


def compile_daily_attendance_for_course(course_id):
daily_attendance_record = {}
dates = get_classroom_meeting_dates(course_id)
for date in dates:
daily_attendance = Attendance.objects.filter(classroom_id=course_id, date=date)
average = round(get_average_attendance_from_list(daily_attendance) * 100, 2)
daily_attendance_record.update({date: average})
return daily_attendance_record


def get_average_attendance_from_list(list):
return sum(
attendance_object.presence == "Present" or attendance_object.presence == "Late"
for attendance_object in list
) / len(list)


def get_classroom_meeting_dates(course_id):
return class_offering_meeting_dates(
ClassOffering.objects.get(name=Classroom.objects.get(id=course_id).course)
)


@register.filter
def get_item(dictionary, key):
return dictionary.get(key)
2 changes: 2 additions & 0 deletions home/choices.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,3 +628,5 @@
"Above Moderate Income $139,101 or greater (8 persons)",
),
)

CHANGE_STUDENT_CHOICES = (("Add", "Add"), ("Remove", "Remove"), ("None", "None"))
2 changes: 2 additions & 0 deletions home/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ def wrapper(request, *args, **kwargs):
return func(request, *args, **kwargs)
else:
raise PermissionDenied

return wrapper

return user_in_group
40 changes: 39 additions & 1 deletion home/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def __str__(self):


class ChangePwdForm(PasswordChangeForm):
old_password = forms.CharField(widget=forms.PasswordInput, initial="missionbit")
old_password = forms.CharField(widget=forms.PasswordInput)

class Meta:
model = User
Expand Down Expand Up @@ -228,3 +228,41 @@ class Meta:
"recipient_classrooms",
"email_recipients",
]


class ChangeTeacherForm(forms.ModelForm):
teacher = forms.ModelChoiceField(
queryset=DjangoUser.objects.filter(groups__name="teacher"),
required=False,
label="",
)

class Meta:
model = Classroom
fields = ["teacher"]


class AddVolunteersForm(forms.ModelForm):
volunteers = forms.ModelMultipleChoiceField(
widget=forms.CheckboxSelectMultiple,
queryset=DjangoUser.objects.filter(groups__name="volunteer"),
required=False,
label="",
)

class Meta:
model = Classroom
fields = ["volunteers"]


class AddStudentsForm(forms.ModelForm):
students = forms.ModelMultipleChoiceField(
widget=forms.CheckboxSelectMultiple,
queryset=DjangoUser.objects.filter(groups__name="student"),
required=False,
label="",
)

class Meta:
model = Classroom
fields = ["students"]
Loading