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

Most recent with excluded fields #477

Merged
merged 8 commits into from Dec 24, 2018

Conversation

Alig1493
Copy link
Contributor

@Alig1493 Alig1493 commented Nov 7, 2018

Description

Added changes in manager to compensate for excluded fields while feetching most recent. Updated test_model_with_excluded_fields test to verify.

Related Issue

Steps to reproduce:

Motivation and Context

How Has This Been Tested?

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@@ -65,6 +65,9 @@ def __init__(self, verbose_name=None, bases=(models.Model,),
except TypeError:
raise TypeError("The `bases` option must be a list or a tuple.")

def _get_excluded_fields(self):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this being used? Can't see it being called anywhere else

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's not being used anywhere. My mistake since I hadn't spot the meta attribute containing the excluded fields in the first place.

self.assertIn('question', all_fields_names)
self.assertNotIn('pub_date', all_fields_names)
self.assertEqual(most_recent.__class__, PollWithExcludeFields)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we also get a test case that ensures that the excluded fields are indeed excluded?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kseever the test here is already checking whether the field is present in the fields of the history model so I think it's already being covered. I have added another statement checking if the field is present in the excluded fields of the history model. It might be that I may have failed to understand your requirement so please feel free to elaborate. =)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah, I just noticed that the assertion I was looking for already exists on line 403 - confirming that pub_date isn't in the list of returned fields. Nevermind me!

@rossmechanic
Copy link
Collaborator

@Alig1493 not sure why this isn't building. Want to try fixing conflicts and pushing?

@Alig1493
Copy link
Contributor Author

I'll have the conflicts resolved. SOrry for the delayed response

@rossmechanic
Copy link
Collaborator

Want to run make format and fix the build? @Alig1493

@Alig1493
Copy link
Contributor Author

Alig1493 commented Dec 9, 2018

@rossmechanic sure I'll do it right away. sorry for the late reply.

@codecov-io
Copy link

codecov-io commented Dec 9, 2018

Codecov Report

Merging #477 into master will increase coverage by 0.5%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master     #477     +/-   ##
=========================================
+ Coverage   97.23%   97.74%   +0.5%     
=========================================
  Files          16       17      +1     
  Lines         724      799     +75     
  Branches       95      111     +16     
=========================================
+ Hits          704      781     +77     
+ Misses         10        8      -2     
  Partials       10       10
Impacted Files Coverage Δ
simple_history/manager.py 94.11% <100%> (+0.46%) ⬆️
...ory/management/commands/clean_duplicate_history.py 100% <0%> (ø)
...le_history/management/commands/populate_history.py 96.03% <0%> (+2.22%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cbb0ab4...aa63579. Read the comment docs.

@Alig1493
Copy link
Contributor Author

Alig1493 commented Dec 9, 2018

@rossmechanic any advice regarding solving cyclomatic complexity def most_recent(self)?

@rossmechanic
Copy link
Collaborator

@Alig1493 refactoring your new code into a get_excluded_fields function should do the trick

@Alig1493
Copy link
Contributor Author

@rossmechanic Is my latest change what you had in mind? Maybe I missed something?

@rossmechanic
Copy link
Collaborator

@Alig1493 yes! just rebase and the codeclimate issue should go away. However, I would like to see the code coverage increase

@Alig1493
Copy link
Contributor Author

I'll have it rebased soon. Sorry was on leave.

@Alig1493
Copy link
Contributor Author

@rossmechanic I have rebased and pushed but it seems the initial problem still persists. Anything I might have missed?

@@ -34,6 +34,11 @@ def get_queryset(self):
key_name = self.instance._meta.pk.name
return self.get_super_queryset().filter(**{key_name: self.instance.pk})

def get_excluded_fields(self):
if isinstance(self.instance._meta.model.history, self.__class__):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Don't want to hardcode history here. Instead, use utils.get_history_manager_for_model

Copy link
Collaborator

Choose a reason for hiding this comment

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

What is this conditional checking for?

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 was using this conditional check because I thought all the instances might not have a model history class.

Copy link
Collaborator

Choose a reason for hiding this comment

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

They should all have a model history class. However, they might not have _history_excluded_fields so you should do:
excluded_fields = getattr(self.model, '_history_excluded_fields', [])

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh shoot you're right. Now that I'm looking at the print statements for debugging it's painfully obvious. Damn!

@@ -34,6 +34,11 @@ def get_queryset(self):
key_name = self.instance._meta.pk.name
return self.get_super_queryset().filter(**{key_name: self.instance.pk})

def get_excluded_fields(self):
if isinstance(self.instance._meta.model.history, self.__class__):
return self.instance._meta.model.history.model._history_excluded_fields
Copy link
Collaborator

Choose a reason for hiding this comment

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

could do self.model._history_excluded_fields

@rossmechanic
Copy link
Collaborator

@Alig1493 don't worry about the codeclimate error, but I left a few other comments. Also want to get the codecov/patch up to 100%

@Alig1493
Copy link
Contributor Author

@rossmechanic the codecov patch is upto a 100% just like you mentioned.

@rossmechanic
Copy link
Collaborator

@Alig1493 Great! Just left one more comment

@Alig1493
Copy link
Contributor Author

@rossmechanic I've made your requested changes.

tmp.append(field.name + "_id")
elif field.name not in excluded_fields:
tmp.append(field.name)
if field.name not in excluded_fields:
Copy link
Collaborator

Choose a reason for hiding this comment

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

I might just change this so you don't nest the if statement.

Try:

if field.name in excluded_fields:
    continue
elif isinstance(field, models.ForeignKey):
    tmp.append(field.name + "_id")
else:
    tmp.append(field.name)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Have you made the changes? If not maybe I can just do them and push it for you and it stays there?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yea can you do that? Then I’ll merge

@Alig1493
Copy link
Contributor Author

@rossmechanic please see if the changes are in order.

Copy link
Collaborator

@rossmechanic rossmechanic left a comment

Choose a reason for hiding this comment

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

Looks good to me! Thanks!

@rossmechanic rossmechanic merged commit 65785a0 into jazzband:master Dec 24, 2018
@Alig1493 Alig1493 deleted the most_recent_with_excluded branch December 27, 2018 10:58
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.

None yet

4 participants