-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #425 from openedx/pwnage101/ENT-8518
feat: add late redemptions support to policies
- Loading branch information
Showing
20 changed files
with
427 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
enterprise_access/apps/subsidy_access_policy/admin/forms.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
""" | ||
Forms to be used for subsidy_access_policy django admin. | ||
""" | ||
from django import forms | ||
from django.utils.translation import gettext as _ | ||
|
||
|
||
class LateRedemptionDaysFromNowChoices: | ||
""" | ||
Enumerate different choices for the type of Subsidy. For example, this can be used to control whether enrollments | ||
associated with this Subsidy should be rev rec'd through our standard commercial process or not. | ||
""" | ||
DISABLE_NOW = "disable_now" | ||
CHOICES = ( | ||
(DISABLE_NOW, _("Disable now")), | ||
("1", _("1")), | ||
("2", _("2")), | ||
("3", _("3")), | ||
("4", _("4")), | ||
("5", _("5")), | ||
("6", _("6")), | ||
("7", _("7")), | ||
) | ||
|
||
|
||
class SetLateRedemptionForm(forms.Form): | ||
""" | ||
Form to set late redemption timeline. | ||
""" | ||
days_from_now = forms.ChoiceField( | ||
label=_("Enable late redemptions until _ days from now"), | ||
choices=LateRedemptionDaysFromNowChoices.CHOICES, | ||
help_text=_("Unless disabled now, late redemptions will be disabled at midnight UTC of the selected day."), | ||
required=True, | ||
) |
10 changes: 10 additions & 0 deletions
10
enterprise_access/apps/subsidy_access_policy/admin/utils.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
""" | ||
Admin utilities. | ||
""" | ||
|
||
|
||
class UrlNames: | ||
""" | ||
Collection on URL names used in admin | ||
""" | ||
SET_LATE_REDEMPTION = "set_late_redemption" |
93 changes: 93 additions & 0 deletions
93
enterprise_access/apps/subsidy_access_policy/admin/views.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
""" | ||
Custom Django Admin views for subsidy_access_policy app. | ||
""" | ||
from datetime import timedelta | ||
|
||
from django.conf import settings | ||
from django.contrib import messages | ||
from django.http import HttpResponseRedirect | ||
from django.shortcuts import render | ||
from django.urls import reverse | ||
from django.utils.translation import gettext as _ | ||
from django.views.generic import View | ||
|
||
from enterprise_access.apps.subsidy_access_policy.admin.forms import ( | ||
LateRedemptionDaysFromNowChoices, | ||
SetLateRedemptionForm | ||
) | ||
from enterprise_access.apps.subsidy_access_policy.admin.utils import UrlNames | ||
from enterprise_access.apps.subsidy_access_policy.models import SubsidyAccessPolicy | ||
from enterprise_access.utils import localized_utcnow | ||
|
||
|
||
class SubsidyAccessPolicySetLateRedemptionView(View): | ||
""" | ||
View which allows admins to set the late redemption timeline for a given policy. | ||
""" | ||
template = "subsidy_access_policy/admin/set_late_redemption.html" | ||
|
||
def get(self, request, policy_uuid): | ||
""" | ||
Handle GET request - render "Set Late Redemption" form. | ||
Args: | ||
request (django.http.request.HttpRequest): Request instance | ||
policy_uuid (str): Subsidy Access Policy UUID | ||
Returns: | ||
django.http.response.HttpResponse: HttpResponse | ||
""" | ||
policy = SubsidyAccessPolicy.objects.get(uuid=policy_uuid) | ||
opts = policy._meta | ||
context = { | ||
'ENTERPRISE_LEARNER_PORTAL_URL': settings.ENTERPRISE_LEARNER_PORTAL_URL, | ||
'set_late_redemption_form': SetLateRedemptionForm(), | ||
'subsidy_access_policy': policy, | ||
'opts': opts, | ||
} | ||
return render(request, self.template, context) | ||
|
||
def post(self, request, policy_uuid): | ||
""" | ||
Handle POST request - handle form submissions. | ||
Arguments: | ||
request (django.http.request.HttpRequest): Request instance | ||
policy_uuid (str): Subsidy Access Policy UUID | ||
Returns: | ||
django.http.response.HttpResponse: HttpResponse | ||
""" | ||
policy = SubsidyAccessPolicy.objects.get(uuid=policy_uuid) | ||
set_late_redemption_form = SetLateRedemptionForm(request.POST) | ||
|
||
if set_late_redemption_form.is_valid(): | ||
days_from_now = set_late_redemption_form.cleaned_data.get('days_from_now') | ||
if days_from_now == LateRedemptionDaysFromNowChoices.DISABLE_NOW: | ||
policy.late_redemption_allowed_until = None | ||
policy.save() | ||
else: | ||
late_redemption_allowed_until = localized_utcnow() + timedelta(days=int(days_from_now)) | ||
# Force time to the end-of-day UTC. This is consistent with the help text in the HTML template. | ||
late_redemption_allowed_until = late_redemption_allowed_until.replace( | ||
hour=23, | ||
minute=59, | ||
second=59, | ||
microsecond=999999, | ||
) | ||
policy.late_redemption_allowed_until = late_redemption_allowed_until | ||
policy.save() | ||
|
||
messages.success(request, _("Successfully set late redemption.")) | ||
|
||
# Redirect to form GET if everything went smooth. | ||
set_late_redemption_url = reverse("admin:" + UrlNames.SET_LATE_REDEMPTION, args=(policy_uuid,)) | ||
return HttpResponseRedirect(set_late_redemption_url) | ||
|
||
# Somehow, form validation failed. Re-render form. | ||
context = { | ||
'set_late_redemption_form': SetLateRedemptionForm(), | ||
'subsidy_access_policy': policy, | ||
'ENTERPRISE_LEARNER_PORTAL_URL': settings.ENTERPRISE_LEARNER_PORTAL_URL | ||
} | ||
return render(request, self.template, context) |
23 changes: 23 additions & 0 deletions
23
...ubsidy_access_policy/migrations/0024_subsidyaccesspolicy_late_redemption_allowed_until.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Generated by Django 4.2.9 on 2024-03-21 16:28 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('subsidy_access_policy', '0023_alter_policygroupassociation_enterprise_group_uuid'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='historicalsubsidyaccesspolicy', | ||
name='late_redemption_allowed_until', | ||
field=models.DateTimeField(blank=True, help_text='Before this date, "late redemptions" will be allowed. If empty, late redemptions are disallowed.', null=True), | ||
), | ||
migrations.AddField( | ||
model_name='subsidyaccesspolicy', | ||
name='late_redemption_allowed_until', | ||
field=models.DateTimeField(blank=True, help_text='Before this date, "late redemptions" will be allowed. If empty, late redemptions are disallowed.', null=True), | ||
), | ||
] |
Oops, something went wrong.