Last active
November 17, 2021 14:07
-
-
Save rg3915/94222f632ad92a485c0267d531578434 to your computer and use it in GitHub Desktop.
Verifica se um usuario pertence a um grupo
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 has_group(user, group_name): | |
| ''' Verifica se este usuario pertence a um grupo. | |
| Uso: | |
| {% load usergroup_tags %} | |
| {% if user|has_group:"Nome do grupo" %} | |
| {{ user|name_group }} | |
| {% endif %} | |
| ''' | |
| groups = user.groups.all().values_list('name', flat=True). | |
| return True if group_name in groups else False | |
| # OU | |
| user.groups.filter(name="admin").exists() | |
| def has_groups(user, group_list): | |
| ''' | |
| Verifica se o usuário pertence a um dos grupos de uma lista. | |
| ''' | |
| if user: | |
| groups = user.groups.all().values_list('name', flat=True) | |
| return any(x in group_list for x in groups) | |
| return False | |
| @register.filter('name_group') | |
| def name_group(user): | |
| ''' | |
| Retorna o nome do grupo do usuário. | |
| Usage: | |
| {% load usergroup_tags %} | |
| {{ user|name_group }} | |
| ''' | |
| _groups = user.groups.first() | |
| if _groups: | |
| return _groups.name | |
| return '' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment