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

Fix/refactor choices #586

Merged
merged 8 commits into from
Nov 28, 2023
Merged

Fix/refactor choices #586

merged 8 commits into from
Nov 28, 2023

Conversation

EdoStorm96
Copy link
Contributor

Fixes #580. Refactoring all the choices in the models to use Django's special enumeration types. Had to change the code in lots of places, but really nothing major has happened. The most important changes happened in the models. When performing makemigrations --dry-run no changes are detected. A few things to pay attention to:

  • I removed DESIGNS from the Studies model entirely, as these seem to not be used anywhere.
  • I had to slightly adjust the list comprehension on line 105 of reviews/forms.py. It works fine like this, i guess. Although I do wonder if there is a better solution with the new enumeration types ...
  • Also note the changes in get_continuation_display(). Again, this works, but I wonder if there could be a better solution?
  • Lastly, keep in mind the new syntax that this change requires, in the future, eg.:
    • No longer: Review.SUPERVISOR, but: Review.Stages.SUPERVISOR
    • No longer: choices = STAGES, but choices = self.Stages.choices in models or Review.Stages.choices in forms
    • No longer: Import YES, NO, DOUBT, YES_NO_DOUBT, but import YesNoDoubt

@EdoStorm96 EdoStorm96 requested review from tymees and miggol November 23, 2023 12:07
Copy link
Member

@tymees tymees left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, this makes my brain very happy.

Didn't do a full test or anything, but did do a double check if you got everything. I found 4 issues; I provided change-suggestions for you as easy fixes. If you fix them, it's approved :)

The other comments are just suggestions :)

I removed DESIGNS from the Studies model entirely, as these seem to not be used anywhere.

I did try to see if they were ever used, as far as I can tell they never were... At least not for 7 years lol.

@@ -338,7 +338,7 @@ def auto_review(proposal: Proposal):
if study.legally_incapable:
reasons.append(_('De aanvraag bevat het gebruik van wilsonbekwame volwassenen.'))

if study.deception in [YES, DOUBT]:
if study.deception in [YesNoDoubt.YES, DOUBT]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if study.deception in [YesNoDoubt.YES, DOUBT]:
if study.deception in [YesNoDoubt.YES, YesNoDoubt.DOUBT]:

@@ -353,11 +353,11 @@ def auto_review(proposal: Proposal):
for task in Task.objects.filter(session__study=study):
reasons.extend(auto_review_task(study, task))

if study.stressful in [YES, DOUBT]:
if study.stressful in [YesNoDoubt.YES, DOUBT]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if study.stressful in [YesNoDoubt.YES, DOUBT]:
if study.stressful in [YesNoDoubt.YES, YesNoDoubt.DOUBT]:

reasons.append(_('De onderzoeker geeft aan dat (of twijfelt erover of) het onderzoek op onderdelen of \
als geheel zodanig belastend is dat deze ondanks de verkregen informed consent vragen zou kunnen oproepen.'))

if study.risk in [YES, DOUBT]:
if study.risk in [YesNoDoubt.YES, DOUBT]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if study.risk in [YesNoDoubt.YES, DOUBT]:
if study.risk in [YesNoDoubt.YES, YesNoDoubt.DOUBT]:

self.continuation,
self.continuation
)
continuation = dict(self.Continuations.choices)[self.continuation]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
continuation = dict(self.Continuations.choices)[self.continuation]
continuation = self.Continuations(self.continuation).label

This is the proper way to get the label for a choice :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, I knew something like this had to be possible! Glad to know the syntax.

Comment on lines 289 to 292
from main.models import YesNoDoubt

d = dict(YES_NO_DOUBT)
d = dict(YesNoDoubt.choices)
return d[value]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for the dict cast:

        from main.models import YesNoDoubt

        return YesNoDoubt(value).label

reviews/forms.py Outdated
@@ -102,7 +102,7 @@ def __init__(self, *args, **kwargs):
allow_long_route_continuation = kwargs.pop('allow_long_route_continuation', False)
super(ReviewCloseForm, self).__init__(*args, **kwargs)
if not allow_long_route_continuation:
self.fields['continuation'].choices = [x for x in Review.CONTINUATIONS if x[0] != Review.LONG_ROUTE]
self.fields['continuation'].choices = [x for x in Review.Continuations.choices if x != Review.Continuations.LONG_ROUTE]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not work?:

>>> [x for x in Review.Continuations.choices if x != Review.Continuations.LONG_ROUTE]
[(0, 'Goedkeuring door FETC-GW'), (1, 'Revisie noodzakelijk'), (2, 'Afwijzing door FETC-GW'), (3, 'Open review met lange (4-weken) route'), (4, 'Laat opnieuw beoordelen door METC'), (5, 'Positief advies van FETC-GW, post-hoc'), (6, 'Negatief advies van FETC-GW, post-hoc'), (7, 'Niet verder in behandeling genomen')]

I think it should be this:

Suggested change
self.fields['continuation'].choices = [x for x in Review.Continuations.choices if x != Review.Continuations.LONG_ROUTE]
self.fields['continuation'].choices = [x for x in Review.Continuations.choices if x[0] != Review.Continuations.LONG_ROUTE]

As for a better solution: there isn't any really that's clearly better. One alternative is a filter:

filter(lambda x: x[0] != Review.Continuations.LONG_ROUTE, Review.Continuations.choices)

But that's not really that much better.

You could also create a method in the new class to do the list comprehension there, but for this one usage that does not make much sense

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry. I just assumed ... Too bad that does not work. I just used your first suggestion.

Copy link
Contributor

@miggol miggol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I like the new choices. I reviewed as follows:

  • Ran tests
  • Tried to make a pdf
  • Migrated from scratch and loaded all fixtures
  • Finally, pointed out all the places my IDE told me there were undefined variables like DOUBT and NO_WMO

I found a few issues, mainly from the final step.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed a couple of model.wmo.NO_WMO instances on lines 1017 and 1022

reviews/tests.py Outdated
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found some unchanged doubts and no's on lines 18 and 32.

These flagged up in python manage.py test. Usually you would see this in the PR on Github as well, but it seems there's some issue with the runners right now. If this persists/happens again you can always run the tests locally to check.

wmo = Wmo.objects.create(proposal=proposal, metc=YES)
self.assertEqual(proposal.wmo.status, Wmo.WAITING)
wmo = Wmo.objects.create(proposal=proposal, metc=YesNoDoubt.YES)
self.assertEqual(proposal.wmo.status, Wmo.WMOStatuses.WAITING)
wmo.metc = NO
Copy link
Contributor

@miggol miggol Nov 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stubborn metc just can't take NO for an answer.

@@ -279,22 +279,22 @@ def setUp(self):
self.wmo = Wmo.objects.create(proposal=self.p1, metc=NO)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Undefined NO

is_metc = request.POST.get('metc') == YES
is_medical = request.POST.get('medical') == YES
is_metc = request.POST.get('metc') == YesNoDoubt.YES
is_medical = request.POST.get('medical') == YesNoDoubt.YES

doubt = request.POST.get('metc') == DOUBT or request.POST.get('medical') == DOUBT
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Undefined DOUBT

@EdoStorm96 EdoStorm96 merged commit 466eb3a into develop Nov 28, 2023
2 checks passed
@EdoStorm96 EdoStorm96 deleted the fix/refactor_choices branch February 28, 2024 09:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor constant + tuple based choices to models.xChoices
3 participants