working code for form submission

This commit is contained in:
David Kennedy 2024-05-15 08:13:19 -04:00
parent f34da8029a
commit b77b5ae689
No known key found for this signature in database
GPG key ID: 6528A5386E66B96B
3 changed files with 52 additions and 9 deletions

View file

@ -4,6 +4,7 @@ from registrar.models.contact import Contact
from django.core.validators import MaxLengthValidator from django.core.validators import MaxLengthValidator
from phonenumber_field.widgets import RegionalPhoneNumberWidget from phonenumber_field.widgets import RegionalPhoneNumberWidget
from registrar.models.utility.domain_helper import DomainHelper
class UserProfileForm(forms.ModelForm): class UserProfileForm(forms.ModelForm):
"""Form for updating user profile.""" """Form for updating user profile."""
@ -51,4 +52,6 @@ class UserProfileForm(forms.ModelForm):
"required": "Enter your email address in the required format, like name@example.com." "required": "Enter your email address in the required format, like name@example.com."
} }
self.fields["phone"].error_messages["required"] = "Enter your phone number." self.fields["phone"].error_messages["required"] = "Enter your phone number."
self.domainInfo = None self.domainInfo = None
DomainHelper.disable_field(self.fields["email"], disable_required=True)

View file

@ -19,6 +19,17 @@ Edit your User Profile |
Back to manage your domains Back to manage your domains
</p> </p>
</a> </a>
{# messages block is under the back breadcrumb link #}
{% if messages %}
{% for message in messages %}
<div class="usa-alert usa-alert--{{ message.tags }} usa-alert--slim margin-bottom-3">
<div class="usa-alert__body">
{{ message }}
</div>
</div>
{% endfor %}
{% endif %}
{% include "includes/form_errors.html" with form=form %}
<h1>Your profile</h1> <h1>Your profile</h1>
<p>We require that you maintain accurate contact information. The details you provide will only be used to support the administration of .gov and wont be made public.</p> <p>We require that you maintain accurate contact information. The details you provide will only be used to support the administration of .gov and wont be made public.</p>
<h2>Contact information</h2> <h2>Contact information</h2>

View file

@ -4,7 +4,10 @@
import logging import logging
from django.contrib import messages
from django.views.generic.edit import FormMixin
from registrar.forms.user_profile import UserProfileForm from registrar.forms.user_profile import UserProfileForm
from django.urls import reverse
from registrar.models import ( from registrar.models import (
User, User,
Contact, Contact,
@ -15,7 +18,7 @@ from registrar.views.utility.permission_views import UserProfilePermissionView
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class UserProfileView(UserProfilePermissionView): class UserProfileView(UserProfilePermissionView, FormMixin):
""" """
Base View for the Domain. Handles getting and setting the domain Base View for the Domain. Handles getting and setting the domain
in session cache on GETs. Also provides methods for getting in session cache on GETs. Also provides methods for getting
@ -38,13 +41,39 @@ class UserProfileView(UserProfilePermissionView):
logger.info(context) logger.info(context)
return self.render_to_response(context) return self.render_to_response(context)
# def get_context_data(self, **kwargs): def get_success_url(self):
# logger.info("in get_context_data") """Redirect to the overview page for the domain."""
# kwargs.setdefault("view", self) return reverse("user-profile")
# if self.extra_context is not None:
# kwargs.update(self.extra_context) # def post(self, request, *args, **kwargs):
# return kwargs # # Handle POST request logic here
# form = self.get_form()
# if form.is_valid():
# # Save form data or perform other actions
# return HttpResponseRedirect(reverse('profile_success')) # Redirect to a success page
# else:
# # Form is not valid, re-render the page with errors
# return self.render_to_response(self.get_context_data(form=form))
def post(self, request, *args, **kwargs):
self.object = self.get_object()
form = self.get_form()
form.instance.id = self.object.id
form.instance.created_at = self.object.created_at
form.instance.user = self.request.user
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid(form)
def form_valid(self, form):
form.save()
messages.success(self.request, "Your profile has been updated.")
# superclass has the redirect
return super().form_valid(form)
# Override get_object to return the logged-in user # Override get_object to return the logged-in user
def get_object(self, queryset=None): def get_object(self, queryset=None):
logger.info("in get_object") logger.info("in get_object")