Replaced the function-based my_account_form and my_account_store views with the class-based MyAccountFormView view in the Mia core application.
This commit is contained in:
parent
283da62a3f
commit
a4d92a6a30
@ -34,7 +34,7 @@ First written: 2020/8/9
|
|||||||
|
|
||||||
{% if request|is_in_section:"mia_core:my-account" %}
|
{% if request|is_in_section:"mia_core:my-account" %}
|
||||||
<div class="btn-group btn-actions">
|
<div class="btn-group btn-actions">
|
||||||
<a class="btn btn-primary" role="button" href="{% url "mia_core:my-account.edit" %}">
|
<a class="btn btn-primary" role="button" href="{% url "mia_core:my-account.update" %}">
|
||||||
<i class="fas fa-user-cog"></i>
|
<i class="fas fa-user-cog"></i>
|
||||||
{{ _("Settings")|force_escape }}
|
{{ _("Settings")|force_escape }}
|
||||||
</a>
|
</a>
|
||||||
|
@ -42,7 +42,7 @@ First written: 2020/8/9
|
|||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<form id="user-form" action="{% if request|is_in_section:"mia_core:my-account" %}{% url "mia_core:my-account.update" %}{% elif form.user %}{% url "mia_core:users.update" form.user %}{% else %}{% url "mia_core:users.create" %}{% endif %}" method="POST">
|
<form id="user-form" action="{{ request.get_full_path }}" method="POST">
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
<input id="exists-url" type="hidden" value="{% url "mia_core:api.users.exists" "ID" %}" />
|
<input id="exists-url" type="hidden" value="{% url "mia_core:api.users.exists" "ID" %}" />
|
||||||
<input id="user-login-id-original" type="hidden" value="{{ form.user.login_id }}" />
|
<input id="user-login-id-original" type="hidden" value="{{ form.user.login_id }}" />
|
||||||
|
@ -39,7 +39,6 @@ urlpatterns = [
|
|||||||
name="api.users.exists"),
|
name="api.users.exists"),
|
||||||
path("my-account", require_GET(login_required(TemplateView.as_view(
|
path("my-account", require_GET(login_required(TemplateView.as_view(
|
||||||
template_name="mia_core/user_detail.html"))), name="my-account"),
|
template_name="mia_core/user_detail.html"))), name="my-account"),
|
||||||
path("my-account/edit", views.my_account_form, name="my-account.edit"),
|
path("my-account/update", views.MyAccountFormView.as_view(),
|
||||||
path("my-account/update", views.my_account_store,
|
|
||||||
name="my-account.update"),
|
name="my-account.update"),
|
||||||
]
|
]
|
||||||
|
@ -18,7 +18,7 @@
|
|||||||
"""The views of the Mia core application.
|
"""The views of the Mia core application.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from typing import Dict, Type, Optional, Union
|
from typing import Dict, Type, Optional
|
||||||
|
|
||||||
from dirtyfields import DirtyFieldsMixin
|
from dirtyfields import DirtyFieldsMixin
|
||||||
from django import forms
|
from django import forms
|
||||||
@ -41,7 +41,7 @@ from . import stored_post, utils
|
|||||||
from .digest_auth import login_required
|
from .digest_auth import login_required
|
||||||
from .forms import UserForm
|
from .forms import UserForm
|
||||||
from .models import User
|
from .models import User
|
||||||
from .utils import strip_post, UrlBuilder
|
from .utils import UrlBuilder
|
||||||
|
|
||||||
|
|
||||||
class FormView(View):
|
class FormView(View):
|
||||||
@ -246,7 +246,7 @@ class UserFormView(FormView):
|
|||||||
form.current_user = self.request.user
|
form.current_user = self.request.user
|
||||||
return form
|
return form
|
||||||
|
|
||||||
def make_form_from_model(self, obj: User) -> forms.Form:
|
def make_form_from_model(self, obj: User) -> UserForm:
|
||||||
"""Creates and returns the form from a data model."""
|
"""Creates and returns the form from a data model."""
|
||||||
form = UserForm({
|
form = UserForm({
|
||||||
"login_id": obj.login_id,
|
"login_id": obj.login_id,
|
||||||
@ -303,61 +303,28 @@ def user_delete(request: HttpRequest, user: User) -> HttpResponseRedirect:
|
|||||||
return redirect("mia_core:users")
|
return redirect("mia_core:users")
|
||||||
|
|
||||||
|
|
||||||
@require_GET
|
@method_decorator(login_required, name="dispatch")
|
||||||
@login_required
|
class MyAccountFormView(UserFormView):
|
||||||
def my_account_form(request: HttpRequest) -> HttpResponse:
|
"""The form of the user's own account."""
|
||||||
"""The view to edit my account.
|
not_modified_message = gettext_noop("Your user account was not changed.")
|
||||||
|
success_message = gettext_noop("Your user account was saved successfully.")
|
||||||
|
|
||||||
Args:
|
def fill_model_from_form(self, obj: User, form: UserForm) -> None:
|
||||||
request: The request.
|
"""Fills in the data model from the form."""
|
||||||
|
obj.login_id = form["login_id"].value()
|
||||||
|
if form["password"].value() is not None:
|
||||||
|
obj.set_digest_password(
|
||||||
|
form["login_id"].value(), form["password"].value())
|
||||||
|
obj.name = form["name"].value()
|
||||||
|
obj.current_user = self.request.user
|
||||||
|
|
||||||
Returns:
|
def get_success_url(self) -> str:
|
||||||
The response.
|
"""Returns the URL on success."""
|
||||||
"""
|
return reverse("mia_core:my-account")
|
||||||
previous_post = stored_post.get_previous_post(request)
|
|
||||||
if previous_post is not None:
|
|
||||||
form = UserForm(previous_post)
|
|
||||||
else:
|
|
||||||
form = UserForm({
|
|
||||||
"login_id": request.user.login_id,
|
|
||||||
"name": request.user.name,
|
|
||||||
})
|
|
||||||
form.user = request.user
|
|
||||||
form.current_user = request.user
|
|
||||||
return render(request, "mia_core/user_form.html", {
|
|
||||||
"form": form,
|
|
||||||
})
|
|
||||||
|
|
||||||
|
def get_object(self) -> Optional[Model]:
|
||||||
def my_account_store(request: HttpRequest) -> HttpResponseRedirect:
|
"""Finds and returns the current object, or None on a create form."""
|
||||||
"""The view to store my account.
|
return self.request.user
|
||||||
|
|
||||||
Args:
|
|
||||||
request: The request.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
The response.
|
|
||||||
"""
|
|
||||||
post = request.POST.dict()
|
|
||||||
strip_post(post)
|
|
||||||
form = UserForm(post)
|
|
||||||
form.user = request.user
|
|
||||||
form.current_user = request.user
|
|
||||||
if not form.is_valid():
|
|
||||||
url = reverse("mia_core:my-account.edit")
|
|
||||||
return stored_post.error_redirect(request, url, post)
|
|
||||||
request.user.login_id = form["login_id"].value()
|
|
||||||
if form["password"].value() is not None:
|
|
||||||
request.user.set_digest_password(
|
|
||||||
form["login_id"].value(), form["password"].value())
|
|
||||||
request.user.name = form["name"].value()
|
|
||||||
if not request.user.is_dirty():
|
|
||||||
message = gettext_noop("Your user account was not changed.")
|
|
||||||
else:
|
|
||||||
request.user.save(current_user=request.user)
|
|
||||||
message = gettext_noop("Your user account was saved successfully.")
|
|
||||||
messages.success(request, message)
|
|
||||||
return redirect("mia_core:my-account")
|
|
||||||
|
|
||||||
|
|
||||||
def api_users_exists(request: HttpRequest, login_id: str) -> JsonResponse:
|
def api_users_exists(request: HttpRequest, login_id: str) -> JsonResponse:
|
||||||
|
Loading…
Reference in New Issue
Block a user