def update_user(request, user_id, template_name="custom_user/register_users.html"): user = get_object_or_404(User, pk=user_id) if user and user.is_active: form = UserFormUpdate(request.POST or None, instance=user) if request.method == "POST": if request.POST['action'] == "save": if form.is_valid(): form.save() messages.success(request, _('User updated successfully.')) return redirect('user_list') if request.POST['action'] == "remove": user = get_object_or_404(User, id=user_id) user.is_active = False user.save() messages.success(request, _('User deleted successfully.')) return redirect('user_list') context = { 'form': form, 'editing': True, } return render(request, template_name, context)
def user_update(request, user_id, template_name="custom_user/register_users.html"): user = get_object_or_404(User, pk=user_id) if user and user.is_active: form = UserFormUpdate(request.POST or None, instance=user) # form = UserFormUpdate(request.POST or None, instance=User.objects.get(id=user_id)) user_groups = User.objects.get(id=user_id).groups.all() group_permissions = [] groups = Group.objects.all() for group in groups: if group in user_groups: group_permissions.append( {'group': group, 'checked': True} ) else: group_permissions.append( {'group': group, 'checked': False} ) if request.method == "POST": if request.POST['action'] == "save": if form.is_valid(): form.save() if request.POST['password']: user = get_object_or_404(User, id=user_id) profile, created = UserProfile.objects.get_or_create(user=user) profile.force_password_change = True profile.save() messages.success(request, 'Usuário atualizado com sucesso.') return redirect('user_list') else: if request.POST['action'] == "remove": user = get_object_or_404(User, id=user_id) user.is_active = False user.save() messages.success(request, 'Usuário removido com sucesso.') return redirect('user_list') context = { 'form': form, 'editing': True, 'group_permissions': group_permissions, 'creating': False } return render(request, template_name, context)
def user_update(request, user_id, template_name="custom_user/register_users.html"): user = get_object_or_404(User, pk=user_id) if user and user.is_active: form = UserFormUpdate(request.POST or None, instance=user) user_groups = User.objects.get(id=user_id).groups.all() group_permissions = [] groups = Group.objects.all() for group in groups: if group in user_groups: group_permissions.append({'group': group, 'checked': True}) else: group_permissions.append({'group': group, 'checked': False}) if request.method == "POST": if request.POST['action'] == "save": if form.is_valid(): form.save() if 'password_flag' in request.POST: if request.POST['password']: user = get_object_or_404(User, id=user_id) profile, created = UserProfile.objects.get_or_create( user=user) profile.force_password_change = True profile.save() messages.success(request, _('User updated successfully.')) return redirect('user_list') else: if request.POST['action'] == "remove": user = get_object_or_404(User, id=user_id) user.is_active = False user.save() messages.success(request, _('User deleted successfully.')) return redirect('user_list') context = { 'form': form, 'editing': True, 'group_permissions': group_permissions, 'creating': False } return render(request, template_name, context)
def user_view(request, user_id, template_name="custom_user/register_users.html"): user = get_object_or_404(User, pk=user_id) form = UserFormUpdate(request.POST or None, instance=user) profile_form = UserProfileForm(request.POST or None, instance=user.user_profile) group_permissions = get_group_permissions(user) for field in form.fields: form.fields[field].widget.attrs['disabled'] = True for field in profile_form.fields: profile_form.fields[field].widget.attrs['disabled'] = True if request.method == "POST": if request.POST['action'] == "remove": user.is_active = False user.save() messages.success(request, _('Researcher removed successfully.')) return redirect('user_list') context = { "user": user.id, "profile_form": profile_form, "form": form, "group_permissions": group_permissions, "viewing": True, "editing": False, "creating": False, "can_change": True, "current_user_id": request.user.id } return render(request, template_name, context)
def user_update(request, user_id, template_name="custom_user/register_users.html"): user = get_object_or_404(User, pk=user_id) if user and user.is_active: form = UserFormUpdate(request.POST or None, instance=user) profile_form = UserProfileForm(request.POST or None, instance=user.user_profile) group_permissions = get_group_permissions(user) if request.method == "POST": if request.POST['action'] == "save" and request.POST['login_enabled'] == 'True': if form.is_valid() and profile_form.is_valid(): form.save() profile_form.save() if 'password_flag' in request.POST: if request.POST['password']: user = get_object_or_404(User, id=user_id) profile, created = UserProfile.objects.get_or_create(user=user) profile.force_password_change = True profile.save() messages.success(request, _('Researcher updated successfully.')) redirect_url = reverse("user_view", args=(user_id,)) return HttpResponseRedirect(redirect_url) if request.POST['action'] == "save" and request.POST['login_enabled'] == 'False': researcher_form = ResearcherForm(request.POST or None, instance=user) if researcher_form.is_valid(): if researcher_form.has_changed() or profile_form.has_changed(): if 'email' in researcher_form.changed_data and \ User.objects.filter(email=request.POST['email']).exists(): messages.error(request, _('E-mail already registered')) redirect_url = reverse("user_view", args=(user_id,)) return HttpResponseRedirect(redirect_url) else: researcher_form.save() profile_form.save() messages.success(request, _('Researcher updated successfully.')) redirect_url = reverse("user_view", args=(user_id,)) return HttpResponseRedirect(redirect_url) else: messages.info(request, _('There is no changes to save.')) redirect_url = reverse("user_view", args=(user_id,)) return HttpResponseRedirect(redirect_url) if request.POST['action'] == "remove": user = get_object_or_404(User, id=user_id) user.is_active = False user.save() messages.success(request, _('Researcher deleted successfully.')) return redirect('user_list') if request.POST['action'] == "deactivate": user = get_object_or_404(User, id=user_id) user.set_unusable_password() user.save() if profile_form.is_valid(): profile_form.save() messages.success(request, _('Researcher disabled successfully.')) redirect_url = reverse("user_view", args=(user_id,)) return HttpResponseRedirect(redirect_url) context = { "user": user_id, "form": form, "profile_form": profile_form, "editing": True, "group_permissions": group_permissions, "creating": False, "current_user_id": request.user.id } return render(request, template_name, context)
def user_update(request, user_id, template_name="custom_user/register_users.html"): user = get_object_or_404(User, pk=user_id) if user and user.is_active: form = UserFormUpdate(request.POST or None, instance=user) profile_form = UserProfileForm(request.POST or None, instance=user.user_profile) group_permissions = get_group_permissions(user) if request.method == "POST": if request.POST['action'] == "save" and request.POST[ 'login_enabled'] == 'True': if form.is_valid() and profile_form.is_valid(): form.save() profile_form.save() if 'password_flag' in request.POST: if request.POST['password']: user = get_object_or_404(User, id=user_id) profile, created = UserProfile.objects.get_or_create( user=user) profile.force_password_change = True profile.save() messages.success(request, _('Researcher updated successfully.')) redirect_url = reverse("user_view", args=(user_id, )) return HttpResponseRedirect(redirect_url) if request.POST['action'] == "save" and request.POST[ 'login_enabled'] == 'False': researcher_form = ResearcherForm(request.POST or None, instance=user) if researcher_form.is_valid(): if researcher_form.has_changed( ) or profile_form.has_changed(): if 'email' in researcher_form.changed_data and \ User.objects.filter(email=request.POST['email']).exists(): messages.error(request, _('E-mail already registered')) redirect_url = reverse("user_view", args=(user_id, )) return HttpResponseRedirect(redirect_url) else: researcher_form.save() profile_form.save() messages.success( request, _('Researcher updated successfully.')) redirect_url = reverse("user_view", args=(user_id, )) return HttpResponseRedirect(redirect_url) else: messages.info(request, _('There is no changes to save.')) redirect_url = reverse("user_view", args=(user_id, )) return HttpResponseRedirect(redirect_url) if request.POST['action'] == "remove": user = get_object_or_404(User, id=user_id) user.is_active = False user.save() messages.success(request, _('Researcher deleted successfully.')) return redirect('user_list') if request.POST['action'] == "deactivate": user = get_object_or_404(User, id=user_id) user.set_unusable_password() user.save() if profile_form.is_valid(): profile_form.save() messages.success(request, _('Researcher disabled successfully.')) redirect_url = reverse("user_view", args=(user_id, )) return HttpResponseRedirect(redirect_url) context = { "user": user_id, "form": form, "profile_form": profile_form, "editing": True, "group_permissions": group_permissions, "creating": False, "current_user_id": request.user.id } return render(request, template_name, context)