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

Qa #2961

Merged
merged 12 commits into from
Jun 22, 2023
47 changes: 32 additions & 15 deletions myuw/dao/myuw_notice.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
MyuwNotice, start_week_range, duration_range)
from myuw.dao.gws import is_effective_member
from myuw.dao.term import (
get_current_quarter, get_comparison_date,
get_comparison_datetime_with_tz)
get_comparison_date, get_comparison_datetime_with_tz,
get_current_and_next_quarters)

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -125,31 +125,41 @@ def get_notices_by_date(request):
def get_notices_by_term(request):
# MUWM-5265
selected_notices = []
cur_term = get_current_quarter(request)
cmp_date = get_comparison_date(request)
if cur_term.is_summer_quarter():
term, cmp_date = get_notice_term(request)

if term.is_summer_quarter():
fetched_term_notices = MyuwNotice.objects.filter(
Q(is_summer_a=True) | Q(is_summer_b=True))
else:
fltr = {"is_{}".format(cur_term.quarter.lower()): True}
fltr = {"is_{}".format(term.quarter.lower()): True}
fetched_term_notices = MyuwNotice.objects.filter(**fltr)

for notice in fetched_term_notices:
if (notice.start_week in start_week_range and
notice.duration in duration_range):
start_sunday = get_prev_sunday(
get_first_day_quarter(cur_term, notice))
start_date = get_start_date(start_sunday, notice.start_week)
first_day_quarter = get_first_day_quarter(request, notice)
start_date = get_start_date(first_day_quarter, notice.start_week)
end_date = start_date + timedelta(weeks=notice.duration)
if start_date <= cmp_date < end_date:
selected_notices.append(notice)
return selected_notices


def get_first_day_quarter(cur_term, notice):
if notice.is_summer_b:
return cur_term.bterm_first_date
return cur_term.first_day_quarter
def get_first_day_quarter(request, notice):
term, cmp_date = get_notice_term(request)
if term.is_summer_quarter() and notice.is_summer_b:
if not notice.is_summer_a:
return term.bterm_first_date
# MUWM-5273: is_summer_a and is_summer_b both True, check start_date
start_date = get_start_date(
term.bterm_first_date, notice.start_week)
if start_date <= cmp_date:
return term.bterm_first_date
return term.first_day_quarter


def get_start_date(first_day_qtr, notice_start_week):
return get_prev_sunday(first_day_qtr) + timedelta(weeks=notice_start_week)


def get_prev_sunday(first_day_quarter):
Expand All @@ -158,5 +168,12 @@ def get_prev_sunday(first_day_quarter):
return first_day_quarter - timedelta(days=week_day_idx)


def get_start_date(start_sunday, notice_start_week):
return start_sunday + timedelta(weeks=notice_start_week)
def get_notice_term(request):
# MUWM-5273 myuw notice display term switches after the last inst week
cmp_date = get_comparison_date(request)
terms = get_current_and_next_quarters(request, 1)
cur_term = terms[0]
next_term = terms[1]
if cmp_date > (cur_term.last_day_instruction + timedelta(days=1)):
return next_term, cmp_date
return cur_term, cmp_date
14 changes: 3 additions & 11 deletions myuw/dao/term.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from uw_sws.util import convert_to_begin_of_day, convert_to_end_of_day
from uw_sws.section import is_a_term, is_b_term, is_full_summer_term
from uw_sws.term import (
get_term_by_date, get_specific_term, get_current_term,
get_term_by_date, get_specific_term,
get_term_before, get_term_after, get_next_autumn_term,
get_next_non_summer_term)
from restclients_core.exceptions import DataFailureException
Expand All @@ -36,13 +36,7 @@ def get_default_datetime():
right in the middle of the "current" term.
"""
if is_using_file_dao():
term = get_current_term()
first_day = term.first_day_quarter
default_date = first_day + timedelta(days=14)
return datetime(default_date.year,
default_date.month,
default_date.day,
0, 0, 1)
return datetime(2013, 4, 15, 0, 0, 1)
return sws_now()


Expand Down Expand Up @@ -538,11 +532,9 @@ def add_term_data_to_context(request, context):


def current_terms_prefetch(request):
# This triggers a call to get_current_term when using the file dao.
# That request won't happen on test/production
compare = get_comparison_date(request)
year = compare.year
month = compare.year
month = compare.month

methods = []

Expand Down
6 changes: 4 additions & 2 deletions myuw/templates/admin/notice_edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,11 @@ <h3>Display Window</h3>
</div>
<br />
<div class="form-group {% if duration_error %}has-error{% endif %}">
<label class="" for="notice_duration">Duration in weeks (1-11 weeks) </label>
<label class="" for="notice_duration">Duration in weeks (1-11 weeks): </label>
<input type="number" id="notice_duration" name="duration" min="1" max="11"
class="form-control" value='{% if notice.duration %}{{ notice.duration }}{% endif %}'>
class="form-control" aria-describedby="duration_explanation" value='{% if notice.duration %}{{ notice.duration }}{% endif %}'>
<span class="help-block small" id="duration_explanation"><strong>DURATION NOTE:</strong> Regardless of duration, notice
will not display after the Saturday following the last day of instruction (e.g. last week of classes) for the selected quarter.</span>
</div>
</div>
</fieldset>
Expand Down
92 changes: 60 additions & 32 deletions myuw/test/dao/test_myuw_notice.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from myuw.dao.affiliation import get_all_affiliations
from myuw.dao.myuw_notice import (
get_myuw_notices_for_user, get_prev_sunday, get_start_date,
get_current_quarter, campus_neutral, is_stud_campus_matched,
get_notice_term, campus_neutral, is_stud_campus_matched,
is_employee_campus_matched, get_first_day_quarter,
get_notices_by_date, get_notices_by_term, student_affiliation_matched)
from myuw.dao.notice_mapping import categorize_notices
Expand All @@ -23,6 +23,23 @@ def get_datetime_with_tz(year, month, day, hour):

class TestMyuwNotice(TransactionTestCase):

def test_get_notice_term(self):
request = get_request_with_date("2013-03-16")
term, cmp_date = get_notice_term(request)
self.assertEqual(term.quarter, "winter")
request = get_request_with_date("2013-03-17")
term, cmp_date = get_notice_term(request)
self.assertEqual(term.quarter, "spring")
request = get_request_with_date("2013-06-09")
term, cmp_date = get_notice_term(request)
self.assertEqual(term.quarter, "summer")
request = get_request_with_date("2013-08-25")
term, cmp_date = get_notice_term(request)
self.assertEqual(term.quarter, "autumn")
request = get_request_with_date("2013-12-08")
term, cmp_date = get_notice_term(request)
self.assertEqual(term.quarter, "winter")

def test_get_prev_sunday(self):
start_sun = get_prev_sunday(date(2013, 1, 2))
self.assertEqual(str(start_sun), "2012-12-30")
Expand All @@ -34,90 +51,101 @@ def test_get_prev_sunday(self):
self.assertEqual(str(start_sun), "2013-09-22")

def test_get_start_date(self):
dt = get_start_date(date(2013, 1, 6), -1)
dt = get_start_date(date(2013, 1, 7), -1)
self.assertEqual(str(dt), "2012-12-30")
dt = get_start_date(date(2013, 1, 6), 0)
dt = get_start_date(date(2013, 1, 7), 0)
self.assertEqual(str(dt), "2013-01-06")
dt = get_start_date(date(2013, 1, 6), 1)
dt = get_start_date(date(2013, 1, 7), 1)
self.assertEqual(str(dt), "2013-01-13")
dt = get_start_date(date(2013, 9, 22), -2)
self.assertEqual(str(dt), "2013-09-08")

def test_get_first_day_quarter(self):
# MUWM-5273
notice = MyuwNotice(title="Test",
content="Notice Content Five",
notice_type="Banner",
notice_category="MyUWNotice",
start_week=-2,
duration=1,
start_week=0,
duration=2,
is_spring=True,
is_summer_b=True)
request = get_request_with_date("2013-04-11")
dt = get_first_day_quarter(request, notice)
self.assertEqual(str(dt), "2013-04-01")
# notice.is_summer_a is False
request = get_request_with_date("2013-07-11")
cur_term = get_current_quarter(request)
self.assertEqual(cur_term.quarter, "summer")
dt = get_first_day_quarter(cur_term, notice)
dt = get_first_day_quarter(request, notice)
self.assertEqual(str(dt), "2013-07-25")

notice = MyuwNotice(title="Test",
content="Notice Content Five",
notice_type="Banner",
notice_category="MyUWNotice",
start_week=-2,
start_week=-1,
duration=1,
is_summer_a=True,
is_summer_b=True,
is_autumn=True)
request = get_request_with_date("2013-09-01")
cur_term = get_current_quarter(request)
self.assertEqual(cur_term.quarter, "autumn")
dt = get_first_day_quarter(cur_term, notice)
# summer A-term
request = get_request_with_date("2013-07-11")
dt = get_first_day_quarter(request, notice)
self.assertEqual(str(dt), "2013-06-24")
# summer B-term
request = get_request_with_date("2013-07-14")
dt = get_first_day_quarter(request, notice)
self.assertEqual(str(dt), "2013-07-25")

request = get_request_with_date("2013-08-28")
dt = get_first_day_quarter(request, notice)
self.assertEqual(str(dt), "2013-09-25")

def test_get_notices_by_term(self):
notice = MyuwNotice(title="Test1",
content="Notice Content Five",
notice_type="Banner",
notice_category="MyUWNotice",
start_week=2,
duration=1,
start_week=-2,
duration=4,
is_summer_a=True)
notice.save()
notice = MyuwNotice(title="Test2",
content="Notice Content Five",
notice_type="Banner",
start_week=-2,
duration=1,
duration=4,
is_summer_b=True)
notice.save()
notice = MyuwNotice(title="Test3",
content="Notice Content Five",
notice_type="Banner",
notice_category="MyUWNotice",
start_week=3,
duration=2,
start_week=-2,
duration=4,
is_autumn=True)
notice.save()
request = get_request_with_date("2013-07-08")
request = get_request_with_date("2013-06-09")
notices = get_notices_by_term(request)
self.assertEqual(len(notices), 2)
self.assertEqual(len(notices), 1)
self.assertEqual(notices[0].title, "Test1")
self.assertEqual(notices[1].title, "Test2")

request = get_request_with_date("2013-08-25")
request = get_request_with_date("2013-07-07")
notices = get_notices_by_term(request)
self.assertEqual(len(notices), 0)
self.assertEqual(len(notices), 1)
self.assertEqual(notices[0].title, "Test2")

request = get_request_with_date("2013-10-12")
request = get_request_with_date("2013-08-11")
notices = get_notices_by_term(request)
self.assertEqual(len(notices), 0)

request = get_request_with_date("2013-10-13")
request = get_request_with_date("2013-09-07")
notices = get_notices_by_term(request)
self.assertEqual(len(notices), 1)
self.assertEqual(notices[0].title, "Test3")
self.assertEqual(len(notices), 0)

request = get_request_with_date("2013-10-26")
request = get_request_with_date("2013-09-08")
notices = get_notices_by_term(request)
self.assertEqual(len(notices), 1)
self.assertEqual(notices[0].title, "Test3")

request = get_request_with_date("2013-10-27")
request = get_request_with_date("2013-10-06")
notices = get_notices_by_term(request)
self.assertEqual(len(notices), 0)

Expand Down
17 changes: 15 additions & 2 deletions myuw/test/dao/test_term.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
from datetime import datetime
from django.test import TestCase
from commonconf import override_settings
from uw_sws.models import ClassSchedule, Term, Section, Person
from myuw.dao.term import (
get_specific_term, is_past, is_future, sws_now,
get_specific_term, is_past, is_future, sws_now, current_terms_prefetch,
get_default_date, get_default_datetime, get_comparison_date,
get_current_quarter, get_next_quarter, is_cur_term_before,
get_previous_number_quarters, last_4instruction_weeks,
Expand All @@ -33,6 +32,20 @@ class TestTerm(TestCase):
def setUp(self):
get_request()

def test_current_terms_prefetch(self):
request = get_request_with_date("2013-12-21")
methods = current_terms_prefetch(request)
self.assertEqual(len(methods), 6)
request = get_request_with_date("2013-03-01")
methods = current_terms_prefetch(request)
self.assertEqual(len(methods), 5)
request = get_request_with_date("2013-06-24")
methods = current_terms_prefetch(request)
self.assertEqual(len(methods), 4)
request = get_request_with_date("2013-09-24")
methods = current_terms_prefetch(request)
self.assertEqual(len(methods), 5)

def test_get_term(self):
term = get_specific_term(2013, "summer")
self.assertEqual(term.year, 2013)
Expand Down
18 changes: 13 additions & 5 deletions myuw/test/util/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,33 @@ def test_get_cache_time(self):
"sws", "/student/v5/registration"), FIFTEEN_MINS)
self.assertEquals(cache.get_cache_expiration_time(
"sws", "/student/v5/section"), FIFTEEN_MINS)
self.assertEquals(cache.get_cache_expiration_time(
"sws", "/student/v5/section", status=404), 60 * 7)
self.assertEquals(cache.get_cache_expiration_time(
"sws", "/student/v5/section", status=503), 60 * 15)

self.assertEquals(cache.get_cache_expiration_time(
"gws", "/group_sws/v3", status=200), HALF_HOUR)
"gws", "/group_sws/v3"), HALF_HOUR)
self.assertEquals(cache.get_cache_expiration_time(
"gws", "/group_sws/v3", status=404), 60 * 10)
"gws", "/group_sws/v3", status=404), 60 * 7)
self.assertEquals(cache.get_cache_expiration_time(
"gws", "/group_sws/v3", status=500), 60 * 15)

self.assertEquals(cache.get_cache_expiration_time(
"pws", "/identity/v2/person"), ONE_HOUR)
self.assertEquals(cache.get_cache_expiration_time(
"pws", "/identity/v2/person", status=404), 60 * 5)
"pws", "/identity/v2/person", status=404), 60 * 7)
self.assertEquals(cache.get_cache_expiration_time(
"pws", "/identity/v2/person", status=503), 60 * 15)

self.assertEquals(cache.get_cache_expiration_time(
"uwnetid", "/nws/v1/uwnetid"), FOUR_HOURS)
self.assertEquals(cache.get_cache_expiration_time(
"uwnetid", "/nws/v1/uwnetid", status=404), 60 * 5)
"uwnetid", "/nws/v1/uwnetid", status=404), 60 * 7)
self.assertEquals(cache.get_cache_expiration_time(
"uwnetid", "/nws/v1/uwnetid", status=409), 60 * 7)
self.assertEquals(cache.get_cache_expiration_time(
"uwnetid", "/nws/v1/uwnetid", status=409), 60 * 5)
"uwnetid", "/nws/v1/uwnetid", status=500), 60 * 15)

self.assertEquals(cache.get_cache_expiration_time(
"grad", "/services/students"), FOUR_HOURS)
Expand Down
Loading