Skip to content

Instantly share code, notes, and snippets.

@rvause
Last active December 18, 2015 03:59
Show Gist options
  • Select an option

  • Save rvause/5722251 to your computer and use it in GitHub Desktop.

Select an option

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
...
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