-
-
Notifications
You must be signed in to change notification settings - Fork 483
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
Deprecate changereason #655
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import warnings | ||
|
||
import django | ||
from django.db import transaction | ||
from django.forms.models import model_to_dict | ||
|
@@ -123,3 +125,19 @@ def bulk_update_with_history( | |
default_user=default_user, | ||
default_change_reason=default_change_reason, | ||
) | ||
|
||
|
||
def get_change_reason_from_object(obj): | ||
if hasattr(obj, "_change_reason"): | ||
return getattr(obj, "_change_reason") | ||
|
||
if hasattr(obj, "changeReason"): | ||
warning_msg = ( | ||
"Using the attr changeReason to populate history_change_reason is" | ||
" deprecated in 2.10.0 and will be removed in 3.0.0. Use " | ||
"_change_reason instead. " | ||
) | ||
warnings.warn(warning_msg, DeprecationWarning) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warnings most of the time need a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. didn't know that. thanks for the feedback |
||
return getattr(obj, "changeReason") | ||
|
||
return None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be
return obj._change_reason
!(or if this is called many times in loops, even more efficient would be
try: return obj.change_reason except AttributeError: pass
)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree on the latter - though this will be removed in next version anyway. How could this be
return obj._change_reason
though? If that doesn't exist, it needs to checkchangeReason
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was saying two things at once!
1: not reason to use getattr + string after you’ve already tested that the attribute exists
2: if performance matters, you can avoid getting the attribute multiple times (hasattr under the covers tries to get the attr and catches AttributeError):
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes, good points.