toolkit.helpers.utils – miscellaneous helpers

toolkit.helpers.utils.generate_username_from_name(first_name, last_name)

Method generates a valid username based off the given first and last names. It ensures that the username is unique by querying the user model. Usernames are generated by combining the first letter of the first name and the full last name (fbar). If this combination already exists, a number is appended to the username (fbar1, fbar2) and retested until a unique username is found.

Parameters:
  • first_name (string) – user first name
  • last_name (string) – user last name
Returns:

unique, valid username based off the given first and last names

Raises:

IndexError – if first_name is empty or contains no characters valid for use in a username.

Note

The method will not create the user object, it will only return a valid username that can be used in creating a user object outside this method

Usage:
1
2
3
4
5
6
>>> generate_username_from_name('Foo', 'Bar')
fbar
>>> generate_username_from_name('Foo', 'Bar')
fbar1
>>> generate_username_from_name('Foo', 'Bar')
fbar2
toolkit.helpers.utils.get_object_or_none(klass, *args, **kwargs)

Method returns the queried object or None if the object does not exist

Usage:
1
2
3
4
5
6
>>> get_object_or_none(Poll, pk=1)
None
>>> get_object_or_none(Poll, {'pk': 1, 'user': 1})
None
>>> get_object_or_none(Poll, {'pk': 2})
Poll object
toolkit.helpers.utils.get_subclass_instance(obj)

Returns the utilized child class instance of a superclass instance.

Parameters:obj (Model) – Django Model instance
Returns:Subclass instance or None
toolkit.helpers.utils.hasfield(model, field_name)
Returns whether the specified field_name string is a valid field on
model or its related models
Parameters:
  • model (Model) – Django Model object
  • field_name (string) – attribute string, dotted or dunderscored. example: ‘user.first_name’ or ‘user__first_name’
Returns:

Field object or False

Usage:
1
2
3
4
5
6
7
8
>>> hasfield(Poll, 'question')
Django Model
>>> hasfield(Poll, 'user__name')
Django Model
>>> hasfield(Poll, 'user.username')
Django Model
>>> hasfield(Poll, 'user.full_name')
False # full_name is a property method not a field
toolkit.helpers.utils.snakify(*args, **kwargs)

Converts to ASCII. Converts spaces to underscores. Removes characters that aren’t alphanumerics, underscores, or hyphens. Converts to lowercase. Also strips leading and trailing whitespace.

Parameters:value (string) – unsanitized value
Returns:snakified value
Usage:
1
2
>>> snakify('polls-report May 1, 2016')
u'polls_report_may_1_2016'
toolkit.helpers.utils.usernamify(username, special_chars='@.+-_')

Remove characters invalid for use in a username and convert to lowercase.

Parameters:
  • username (string) – unsanitized string
  • special_chars (string) – special characters that need to be removed from the provided username. Default value: ‘@.+-_’
Returns:

sanitized string to be used as username

Note

If username contains no valid characters, the returned value will be the empty string, which is not a valid username on its own.

Author:
Fredrick Wagner