Skip to content

Commit

Permalink
Refactor clean_extended_due_Date
Browse files Browse the repository at this point in the history
  • Loading branch information
MattyMay committed Sep 13, 2024
1 parent f218b37 commit 09ec823
Showing 1 changed file with 24 additions and 26 deletions.
50 changes: 24 additions & 26 deletions autograder/rest_api/views/group_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import shutil
import tempfile
from typing import Mapping

from django.contrib.auth.models import User
from django.db import transaction
Expand Down Expand Up @@ -217,7 +218,7 @@ def get(self, *args, **kwargs):
def patch(self, request, *args, **kwargs):
group = self.get_object()

update_data = clean_extended_due_dates(dict(request.data), group)
update_data = clean_extended_due_dates(request.data, group)
if 'member_names' in update_data:
users = [
User.objects.get_or_create(
Expand Down Expand Up @@ -387,7 +388,7 @@ def _get_merged_extended_due_date(self, group1, group2):
return max(group1.extended_due_date, group2.extended_due_date)


def clean_extended_due_dates(update_data: dict, old_group: ag_models.Group) -> dict:
def clean_extended_due_dates(update_data: Mapping, old_group: ag_models.Group) -> dict:
"""
Return a new dict without 'soft_extended_due_date' and without 'hard_extended_due_date'
if `update_data` contains a changed value for 'extended_due_date'. If it contains just
Expand All @@ -398,52 +399,49 @@ def clean_extended_due_dates(update_data: dict, old_group: ag_models.Group) -> d
both deprecated 'extended_due_date' and one of 'soft_extended_due_date' or
'hard_extended_due_date', or if any datetime strings in `update_data` are invalid.
"""
legacy_changed = current_changed = False
update_data = dict(copy.deepcopy(update_data))

try:
if ('extended_due_date' in update_data and not core_ut.datetimes_are_equal(
update_data['extended_due_date'], old_group.extended_due_date)):
legacy_changed = True
legacy_changed = 'extended_due_date' in update_data \
and not core_ut.datetimes_are_equal(update_data['extended_due_date'],
old_group.extended_due_date)
except(ValueError) as e:
raise exceptions.ValidationError({
'extended_due_date': str(e)
})

try:
if ('soft_extended_due_date' in update_data and not core_ut.datetimes_are_equal(
update_data['soft_extended_due_date'], old_group.soft_extended_due_date)):
current_changed = True
soft_extension_changed = 'soft_extended_due_date' in update_data \
and not core_ut.datetimes_are_equal(update_data['soft_extended_due_date'],
old_group.soft_extended_due_date)
except(ValueError) as e:
raise exceptions.ValidationError({
'soft_extended_due_date': str(e)
})

try:
if ('hard_extended_due_date' in update_data and not core_ut.datetimes_are_equal(
update_data['hard_extended_due_date'], old_group.hard_extended_due_date)):
current_changed = True
hard_extension_changed = 'hard_extended_due_date' in update_data \
and not core_ut.datetimes_are_equal(update_data['hard_extended_due_date'],
old_group.hard_extended_due_date)
except(ValueError) as e:
raise exceptions.ValidationError({
'hard_extended_due_date': str(e)
})

if legacy_changed and current_changed:
if legacy_changed and (soft_extension_changed or hard_extension_changed):
raise exceptions.ValidationError({
'extended_due_date': (
'Extended due date is deprecated and may not be used along with soft and'
+ ' hard extended due dates')
})

elif legacy_changed:
return _deepcopy_dict_without_fields(update_data, 'soft_extended_due_date',
'hard_extended_due_date')
elif current_changed:
return _deepcopy_dict_without_fields(update_data, 'extended_due_date')
else:
return _deepcopy_dict_without_fields(update_data)


def _deepcopy_dict_without_fields(dict_to_copy: dict, *keys: str) -> dict:
return {
key: copy.deepcopy(dict_to_copy[key]) for key in dict_to_copy
if key not in keys
}
if 'soft_extended_due_date' in update_data:
update_data.pop('soft_extended_due_date')
if 'hard_extended_due_date' in update_data:
update_data.pop('hard_extended_due_date')
elif soft_extension_changed or hard_extension_changed:
if 'extended_due_date' in update_data:
update_data.pop('extended_due_date')

return update_data

0 comments on commit 09ec823

Please sign in to comment.