Skip to content

Commit

Permalink
Adding google sheets deferrals library (#1604)
Browse files Browse the repository at this point in the history
  • Loading branch information
annagav authored May 25, 2023
1 parent f0b1009 commit 75791c9
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 17 deletions.
30 changes: 29 additions & 1 deletion app.json
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,34 @@
"description": "The name of the timezone that should be assumed for date/time values in spreadsheets. Choose from a value in the TZ database (https://en.wikipedia.org/wiki/List_of_tz_database_time_zones).",
"required": false
},
"MITOL_GOOGLE_SHEETS_DEFERRALS_COMPLETED_DATE_COL": {
"description": "The zero-based index of the enrollment change sheet column that contains the row completion date",
"required": false
},
"MITOL_GOOGLE_SHEETS_DEFERRALS_ERROR_COL": {
"description": "The zero-based index of the enrollment change sheet column that contains row processing error messages",
"required": false
},
"MITOL_GOOGLE_SHEETS_DEFERRALS_FIRST_ROW": {
"description": "The first row (as it appears in the spreadsheet) of data that our scripts should consider processing in the deferral request spreadsheet",
"required": false
},
"MITOL_GOOGLE_SHEETS_DEFERRALS_PLUGINS": {
"description": "The path to your deferral plugin, example: app.plugins.DeferralPlugin",
"required": false
},
"MITOL_GOOGLE_SHEETS_DEFERRALS_PROCESSOR_COL": {
"description": "The zero-based index of the enrollment change sheet column that contains the user that processed the row",
"required": false
},
"MITOL_GOOGLE_SHEETS_DEFERRALS_REQUEST_WORKSHEET_ID": {
"description": "ID of the worksheet within the enrollment change request spreadsheet that contains enrollment deferral requests",
"required": false
},
"MITOL_GOOGLE_SHEETS_DEFERRALS_SKIP_ROW_COL": {
"description": "The zero-based index of the enrollment change sheet column that indicates whether the row should be skipped",
"required": false
},
"MITOL_GOOGLE_SHEETS_DRIVE_API_PROJECT_ID": {
"description": "ID for the Google API project where the credentials were created",
"required": false
Expand Down Expand Up @@ -551,4 +579,4 @@
},
"success_url": "/",
"website": "https://github.com/mitodl/mitx-online"
}
}
7 changes: 5 additions & 2 deletions main/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
)
from mitol.google_sheets.settings.google_sheets import *
from mitol.google_sheets_refunds.settings.google_sheets_refunds import *
from mitol.google_sheets_deferrals.settings.google_sheets_deferrals import *
from redbeat import RedBeatScheduler

from main.celery_utils import OffsettingSchedule
Expand Down Expand Up @@ -207,6 +208,7 @@
"mitol.common.apps.CommonApp",
"mitol.google_sheets.apps.GoogleSheetsApp",
"mitol.google_sheets_refunds.apps.GoogleSheetsRefundsApp",
"mitol.google_sheets_deferrals.apps.GoogleSheetsDeferralsApp",
# "mitol.digitalcredentials.apps.DigitalCredentialsApp",
"mitol.hubspot_api",
"mitol.mail.apps.MailApp",
Expand Down Expand Up @@ -843,8 +845,8 @@
month_of_year="*",
),
},
"process-refund-requests": {
"task": "sheets.tasks.process_refund_requests",
"process-google-sheets-requests": {
"task": "sheets.tasks.process_google_sheets_requests",
"schedule": crontab(minute=CRON_PROCESS_REFUND_REQUESTS_MINUTES),
},
"generate-course-certificate": {
Expand Down Expand Up @@ -1090,6 +1092,7 @@
)

MITOL_GOOGLE_SHEETS_REFUNDS_PLUGINS = ["sheets.plugins.RefundPlugin"]
MITOL_GOOGLE_SHEETS_DEFERRALS_PLUGINS = ["sheets.plugins.DeferralPlugin"]


# Fastly configuration
Expand Down
1 change: 1 addition & 0 deletions requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ mitol-django-openedx>=1.3.0
mitol-django-payment-gateway>=1.9.1
mitol-django-google-sheets>=2.6.0
mitol-django-google-sheets-refunds>=0.8.0
mitol-django-google-sheets-deferrals~=2023.5.25
newrelic
pyOpenSSL>=23.1.1
psycopg2
Expand Down
6 changes: 6 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ django==3.2.18
# mitol-django-authentication
# mitol-django-common
# mitol-django-google-sheets
# mitol-django-google-sheets-deferrals
# mitol-django-google-sheets-refunds
# mitol-django-hubspot-api
# mitol-django-mail
Expand Down Expand Up @@ -284,14 +285,18 @@ mitol-django-common==2.7.0
# -r requirements.in
# mitol-django-authentication
# mitol-django-google-sheets
# mitol-django-google-sheets-deferrals
# mitol-django-google-sheets-refunds
# mitol-django-hubspot-api
# mitol-django-mail
# mitol-django-payment-gateway
mitol-django-google-sheets==2.6.0
# via
# -r requirements.in
# mitol-django-google-sheets-deferrals
# mitol-django-google-sheets-refunds
mitol-django-google-sheets-deferrals==2023.5.25
# via -r requirements.in
mitol-django-google-sheets-refunds==0.8.0
# via -r requirements.in
mitol-django-hubspot-api==2023.5.22
Expand Down Expand Up @@ -327,6 +332,7 @@ pillow==9.3.0
# via wagtail
pluggy==0.13.1
# via
# mitol-django-google-sheets-deferrals
# mitol-django-google-sheets-refunds
# pytest
premailer==3.9.0
Expand Down
24 changes: 24 additions & 0 deletions sheets/plugins.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from mitol.google_sheets_refunds.hooks import hookimpl, RefundResult
from mitol.google_sheets_refunds.utils import RefundRequestRow
from mitol.google_sheets_deferrals.hooks import hookimpl, DeferralResult
from mitol.google_sheets_deferrals.utils import DeferralRequestRow
from mitol.google_sheets.utils import ResultType

from ecommerce.api import refund_order
from courses.api import defer_enrollment
from users.api import fetch_user


class RefundPlugin:
Expand All @@ -18,3 +22,23 @@ def refunds_process_request(
return RefundResult(ResultType.PROCESSED)

return RefundResult(ResultType.FAILED)


class DeferralPlugin:
@hookimpl
def deferrals_process_request(
self, deferral_request_row: DeferralRequestRow
) -> DeferralResult:
user = fetch_user(deferral_request_row.learner_email)
from_courseware_id = deferral_request_row.from_courseware_id
to_courseware_id = deferral_request_row.to_courseware_id

deferral_api_success = defer_enrollment(
user,
from_courseware_id,
to_courseware_id,
)
if deferral_api_success:
return DeferralResult(ResultType.PROCESSED)

return DeferralResult(ResultType.FAILED)
15 changes: 9 additions & 6 deletions sheets/tasks.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import logging
from mitol.google_sheets_refunds.api import RefundRequestHandler
from mitol.google_sheets_deferrals.api import DeferralRequestHandler

from main.celery import app

log = logging.getLogger(__name__)


@app.task
def process_refund_requests():
def process_google_sheets_requests():
"""
Task to process refund requests from Google sheets
Task to process refund and deferral requests from Google sheets
"""
refund_request_handler = RefundRequestHandler()
if not refund_request_handler.is_configured():
log.warning("MITOL_GOOGLE_SHEETS_* are not set")
return
refund_request_handler.process_sheet()
deferral_request_handler = DeferralRequestHandler()
if refund_request_handler.is_configured():
refund_request_handler.process_sheet()

if deferral_request_handler.is_configured():
deferral_request_handler.process_sheet()
34 changes: 26 additions & 8 deletions sheets/tasks_test.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,52 @@
import pytest

from mitol.google_sheets_refunds.api import RefundRequestHandler
from mitol.google_sheets_deferrals.api import DeferralRequestHandler

from sheets.tasks import process_refund_requests
from sheets.tasks import process_google_sheets_requests


@pytest.mark.django_db
@pytest.mark.parametrize(
"is_configured",
[True, False],
"refunds_is_configured, deferrals_is_configured",
[(True, True), (False, False)],
)
def test_process_refund_requests(mocker, user, is_configured):
def test_process_google_sheets_requests(
mocker, user, refunds_is_configured, deferrals_is_configured
):
"""
Test that process_refund_requests calls process_sheet only if it is properly configured
Test that process_google_sheets_requests calls process_sheet only if it is properly configured
"""
Mock = mocker.Mock
MagicMock = mocker.MagicMock
refund_req_handler = MagicMock(
spec=RefundRequestHandler, process_sheet=Mock(), is_configured=Mock()
)
deferral_req_handler = MagicMock(
spec=DeferralRequestHandler, process_sheet=Mock(), is_configured=Mock()
)
refund_req_handler_mock = mocker.patch(
"sheets.tasks.RefundRequestHandler",
return_value=refund_req_handler,
)
refund_req_handler.is_configured.return_value = is_configured
deferral_req_handler_mock = mocker.patch(
"sheets.tasks.DeferralRequestHandler",
return_value=deferral_req_handler,
)
refund_req_handler.is_configured.return_value = refunds_is_configured
deferral_req_handler.is_configured.return_value = deferrals_is_configured

process_refund_requests.delay()
process_google_sheets_requests.delay()
refund_req_handler_mock.assert_called()
refund_req_handler.is_configured.assert_called()
if is_configured:
deferral_req_handler_mock.assert_called()
deferral_req_handler.is_configured.assert_called()
if refunds_is_configured:
refund_req_handler.process_sheet.assert_called()
else:
assert not refund_req_handler.process_sheet.called

if deferrals_is_configured:
deferral_req_handler.process_sheet.assert_called()
else:
assert not deferral_req_handler.process_sheet.called

0 comments on commit 75791c9

Please sign in to comment.