site_tps/settings/forms.py

20 lines
660 B
Python
Raw Normal View History

2018-03-02 10:16:42 +00:00
from django import forms
from django.contrib.auth.models import User, Group
from django.shortcuts import get_object_or_404
class SelectUserForm(forms.Form):
pk = forms.ChoiceField(
label="Utilisateur",
help_text="Sera ajouté aux administrateurs du site.",
validators=[],
)
def get_user(self):
pk = self.cleaned_data['pk']
return get_object_or_404(User, pk=pk)
def populate(self):
admins,_ = Group.objects.get_or_create(name='admins')
choices = [(u.pk, u.first_name + ' ' + u.last_name + '(' + u.username + ')') for u in User.objects.all()]
self.fields['pk'].choices = choices