Exemplo n.º 1
0
 def get_form(self, form_class):
     form = super(AdminUserUpdateView, self).get_form(form_class)
     try:
         user_profile = UserProfile.objects.get(user_id=self.kwargs.get('pk'))
     except:
         user_profile = UserProfile()
         user_profile.user = self.object
         user_profile.save()
     form.fields['cod_emp'].initial = user_profile.cod_emp
     return form
Exemplo n.º 2
0
    def form_valid(self, form):

        user = form.save(commit=False)
        password = form.cleaned_data['password']
        user.set_password(password)
        user.save()

        user_profile = UserProfile()
        user_profile.user = user
        user_profile.cod_emp = form.cleaned_data['cod_emp']
        user_profile.save()

        for group in form.cleaned_data['groups']:
            group.user_set.add(user)
            permissions = group.permissions.all()
            for permission in permissions:
                user.user_permissions.add(permission)
        return HttpResponseRedirect(self.get_success_url())