Last active
December 18, 2015 03:59
-
-
Save rvause/5722251 to your computer and use it in GitHub Desktop.
Wanted a simple way to measure the completion of a profile (that is stored in a UserProfile model related to auth.User) in a Django app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ... | |
| def _get_profile_completion(self): | |
| fields = filter( | |
| lambda i: i not in ('user', 'id'), | |
| self.__class__._meta.get_all_field_names() | |
| ) | |
| per_field = float(100) / len(fields) | |
| total = 0.0 | |
| for field in fields: | |
| val = getattr(self, field) | |
| if val.__class__.__name__ == 'ManyRelatedManager': | |
| if val.count(): | |
| total += per_field | |
| elif val is False: | |
| total += per_field | |
| elif val is not None: | |
| total += per_field | |
| return int(round(total)) | |
| ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment