class ProfileSettingsView(TemplateLoginRequiredMixin): template_name = "core/profile/settings.html" def __init__(self, **kwargs): self.form_profile = None self.current_image = None self.form_profile_image = None self.form_profile_password = None super().__init__(**kwargs) def get(self, request, *args, **kwargs): profile = UserProfile.current.get(user__email=request.user.email) user = User.objects.get(pk=profile.user.id) self.form_profile = UserProfileForm(auto_id="id_profile_%s", instance=profile) self.form_profile_image = UserProfileImageForm(auto_id="id_profile_image_%s", instance=profile) self.form_profile_password = UserProfilePasswordForm(auto_id="id_profile_password_%s", instance=user) return super().render_to_response(self.get_context_data()) def post(self, request, *args, **kwargs): current_form_send = request.POST.get("current_form_send") profile = UserProfile.current.get(user__email=request.user.email) user = User.objects.get(pk=profile.user.id) self.form_profile = UserProfileForm(auto_id="id_profile_%s", instance=profile) self.form_profile_image = UserProfileImageForm(auto_id="id_profile_image_%s", instance=profile) self.form_profile_password = UserProfilePasswordForm(auto_id="id_profile_password_%s", instance=user) if current_form_send == "form_profile": self.form_profile = UserProfileForm(request.POST, auto_id="id_profile_%s", instance=profile) if self.form_profile.is_valid(): self.form_profile.save() if current_form_send == "form_upload": self.current_image = profile.profile_image self.form_profile_image = UserProfileImageForm( request.POST, request.FILES, auto_id="id_profile_image_%s", instance=profile ) if self.form_profile_image.is_valid(): form_profile_image = self.form_profile_image.save(current_image=self.current_image, commit=False) if request.FILES: uploads.handle_upload_profile(name_image=form_profile_image.profile_image, resize_height=100) if current_form_send == "form_password": self.form_profile_password = UserProfilePasswordForm( request.POST, auto_id="id_profile_password_%s", instance=user ) if self.form_profile_password.is_valid(): self.form_profile.save(commit=False) return super().render_to_response(self.get_context_data()) def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["form_profile"] = self.form_profile context["form_profile_upload"] = self.form_profile_image context["form_profile_password"] = self.form_profile_password return context
class UserEditView(TemplateLoginRequiredMixin): template_name = "administrator/maintenance/users/user_add.html" def __init__(self, **kwargs): self.form_profile = None self.user = None self.form_user = None super().__init__(**kwargs) def get(self, request, *args, **kwargs): user_email = request.GET["user_email"] self.user = User.objects.get(email=user_email) self.form_user = UserCreationAdminForm(auto_id="id_user_%s", instance=self.user) self.form_profile = UserProfileForm(auto_id="id_profile_%s", instance=self.user) return super().render_to_response(self.get_context_data()) def post(self, request, *args, **kwargs): data = loads(request.body.decode("utf-8")) data_user_pk = data["form_pk"] data_form_user = data["form"] self.user = User.objects.get(pk=data_user_pk) self.form_user = UserCreationAdminForm(data_form_user, auto_id="id_user_%s", instance=self.user) self.form_profile = UserProfileForm(data_form_user, auto_id="id_profile_%s", instance=self.user) if self.form_user.is_valid(): self.form_user.save(new=False, commit=False) if self.form_profile.is_valid(): self.form_profile.save() messages.success(request, core_constants.MESSAGE_TAGS["success"]) else: messages.error(request, core_constants.MESSAGE_TAGS["error"]) return super().render_to_response(self.get_context_data()) def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["form_profile"] = self.form_profile context["form_user"] = self.form_user context["form_pk"] = self.user.id context["btn_edit"] = core_constants.BTN_EDIT return context
class UserView(TemplateLoginRequiredMixin): template_name = "administrator/maintenance/users/user.html" def __init__(self, **kwargs): self.form_profile = None self.form_user = None self.user_all = None super().__init__(**kwargs) def dispatch(self, request, *args, **kwargs): self.user_all = User.objects.all() return super().dispatch(request, *args, **kwargs) def get(self, request, *args, **kwargs): self.form_profile = UserProfileForm(auto_id="id_profile_%s") self.form_user = UserCreationAdminForm(auto_id="id_user_%s") return super().render_to_response(self.get_context_data()) def post(self, request, *args, **kwargs): self.form_user = UserCreationAdminForm(data=request.POST, auto_id="id_user_%s") self.form_profile = UserProfileForm(data=request.POST, auto_id="id_profile_%s") if self.form_user.is_valid(): user = self.form_user.save(new=True, commit=False) profile = UserProfile.objects.get(user=user) self.form_profile = UserProfileForm(request.POST, auto_id="id_profile_%s", instance=profile) if self.form_profile.is_valid(): self.form_profile.save() messages.success(request, core_constants.MESSAGE_TAGS["success"]) else: messages.warning(request, core_constants.MESSAGE_TAGS["warning"]) return super().render_to_response(self.get_context_data()) def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["form_profile"] = self.form_profile context["form_user"] = self.form_user context["list_users"] = self.user_all return context