Skip to content
This repository has been archived by the owner on Jan 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #207 from mmoomocow/DCS-080
Browse files Browse the repository at this point in the history
DCS-080 Informative site messages
  • Loading branch information
mmoomocow authored Aug 9, 2023
2 parents eb5e9fb + 5cc28df commit a338249
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 2 deletions.
7 changes: 6 additions & 1 deletion commendations/templates/commendations/my_commendations.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@
<p>
You can view more details about a commendation by clicking on the row.
<br>
So far you have {{commendations|length}} commendations.
<br>
{% if commendationsSince|length > 1 %}
You have received {{commendationsSince|length}} commendations since you last logged in.
<br>
{% endif %}
You have received {{commendations|length}} commendations in total.
</p>

<table id="myCommendations">
Expand Down
9 changes: 8 additions & 1 deletion commendations/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,18 @@ def viewMilestones(request):
@get_student()
def myCommendations(request, student: Student = None):
commendations = student.commendation_set.all().order_by("-date_time")
commendationsSinceLastLogin = commendations.filter(
date_time__gte=request.user.previous_login or request.user.last_login
)

return render(
request,
"commendations/my_commendations.html",
{"commendations": commendations, "studentSwitcherEnabled": True},
{
"commendations": commendations,
"studentSwitcherEnabled": True,
"commendationsSince": commendationsSinceLastLogin,
},
)


Expand Down
12 changes: 12 additions & 0 deletions home/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.shortcuts import redirect, render

Expand All @@ -17,6 +18,17 @@ def portals(request) -> render:
if request.user.is_teacher:
return render(request, "home/home_teacher.html")
if request.user.is_student:
# Get all commendations since previous login
commendations = request.user.student.commendation_set.filter(
date_time__gte=request.user.previous_login or request.user.last_login
)
# Message the user if they have new commendations
if commendations:
messages.info(
request,
f"You have {len(commendations)} new commendation(s)! Follow the link below to view them.",
extra_tags="commendations",
)
return render(request, "home/home_student.html")
if request.user.is_caregiver:
return render(request, "home/home_caregiver.html")
Expand Down
7 changes: 7 additions & 0 deletions users/apps.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
from django.apps import AppConfig
from django.contrib.auth.signals import user_logged_in


class UsersConfig(AppConfig):
"""AppConfig for the Users app."""

default_auto_field = "django.db.models.BigAutoField"
name = "users"

def ready(self):
"""Import signals."""
import users.signals

user_logged_in.connect(users.signals.update_last_visit)
19 changes: 19 additions & 0 deletions users/migrations/0004_user_last_visit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 4.2.1 on 2023-07-31 23:37

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("users", "0003_alter_user_managers"),
]

operations = [
migrations.AddField(
model_name="user",
name="last_visit",
field=models.DateTimeField(
blank=True, null=True, verbose_name="Last Visit"
),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Generated by Django 4.2.1 on 2023-08-01 00:06

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("users", "0004_user_last_visit"),
]

operations = [
migrations.RemoveField(
model_name="user",
name="last_visit",
),
migrations.AddField(
model_name="user",
name="previous_login",
field=models.DateTimeField(
blank=True, null=True, verbose_name="Previous Login"
),
),
migrations.AddField(
model_name="user",
name="recent_login",
field=models.DateTimeField(
blank=True, null=True, verbose_name="Most Recent Login"
),
),
]
6 changes: 6 additions & 0 deletions users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ class User(defaultUser, defaultPermissionsMixin):
)
# User's last login time
last_login = models.DateTimeField(null=True, blank=True, verbose_name="Last Login")
recent_login = models.DateTimeField(
null=True, blank=True, verbose_name="Most Recent Login"
)
previous_login = models.DateTimeField(
null=True, blank=True, verbose_name="Previous Login"
)

# ------ USER ACCESS LEVELS ------ #
# Can log in to django's backend administration site, not reflective of
Expand Down
11 changes: 11 additions & 0 deletions users/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.utils import timezone

from .models import User


def update_last_visit(sender, **kwargs): # skipcq: PYL-W0613
"""Update the last viewed time."""
user: User = kwargs["user"]
user.previous_login = user.recent_login
user.recent_login = timezone.now()
user.save()

0 comments on commit a338249

Please sign in to comment.