Added the user's own account management in the Mia core application.

This commit is contained in:
依瑪貓 2020-08-09 23:46:08 +08:00
parent 364c40ed02
commit a229f8a3f6
5 changed files with 276 additions and 172 deletions

View File

@ -7,8 +7,8 @@ msgid ""
msgstr ""
"Project-Id-Version: mia 1.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2020-08-09 22:51+0800\n"
"PO-Revision-Date: 2020-08-09 22:52+0800\n"
"POT-Creation-Date: 2020-08-09 23:28+0800\n"
"PO-Revision-Date: 2020-08-09 23:29+0800\n"
"Last-Translator: imacat <imacat@mail.imacat.idv.tw>\n"
"Language-Team: Traditional Chinese <imacat@mail.imacat.idv.tw>\n"
"Language: Traditional Chinese\n"
@ -78,13 +78,13 @@ msgstr "全部"
#: mia_core/period.py:583 mia_core/period.py:616
#: mia_core/templates/mia_core/include/period-chooser.html:60
#: mia_core/templatetags/mia_core.py:183
#: mia_core/templatetags/mia_core.py:170
msgid "This Month"
msgstr "這個月"
#: mia_core/period.py:624
#: mia_core/templates/mia_core/include/period-chooser.html:63
#: mia_core/templatetags/mia_core.py:190
#: mia_core/templatetags/mia_core.py:177
msgid "Last Month"
msgstr "上個月"
@ -100,13 +100,13 @@ msgstr "去年"
#: mia_core/period.py:656
#: mia_core/templates/mia_core/include/period-chooser.html:95
#: mia_core/templatetags/mia_core.py:163
#: mia_core/templatetags/mia_core.py:150
msgid "Today"
msgstr "今天"
#: mia_core/period.py:658
#: mia_core/templates/mia_core/include/period-chooser.html:98
#: mia_core/templatetags/mia_core.py:165
#: mia_core/templatetags/mia_core.py:152
msgid "Yesterday"
msgstr "昨天"
@ -156,6 +156,115 @@ msgstr "從:"
msgid "To:"
msgstr "到:"
#: mia_core/templates/mia_core/my-account_detail.html:36
#: mia_core/templates/mia_core/user_detail.html:73
msgid "Settings"
msgstr "設定"
#: mia_core/templates/mia_core/my-account_detail.html:41
#: mia_core/templates/mia_core/user_detail.html:99
msgid "Log in ID.:"
msgstr "登入帳號:"
#: mia_core/templates/mia_core/my-account_detail.html:46
#: mia_core/templates/mia_core/my-account_form.html:71
#: mia_core/templates/mia_core/user_detail.html:104
#: mia_core/templates/mia_core/user_form.html:74
msgid "Name:"
msgstr "姓名:"
#: mia_core/templates/mia_core/my-account_detail.html:52
#: mia_core/templates/mia_core/user_detail.html:110
#: mia_core/templates/mia_core/user_form.html:88
msgid "This account is disabled."
msgstr "帳號停用。"
#: mia_core/templates/mia_core/my-account_detail.html:58
#: mia_core/templates/mia_core/user_detail.html:116
msgid "This account is deleted."
msgstr "帳號已刪。"
#: mia_core/templates/mia_core/my-account_detail.html:64
#: mia_core/templates/mia_core/user_detail.html:122
msgid "This user has not logged in yet."
msgstr "使用者還沒登入過。"
#: mia_core/templates/mia_core/my-account_detail.html:68
#: mia_core/templates/mia_core/user_detail.html:126
msgid "# of visits:"
msgstr "登入次數:"
#: mia_core/templates/mia_core/my-account_detail.html:73
#: mia_core/templates/mia_core/user_detail.html:131
msgid "Last visit at:"
msgstr "上次登入時間:"
#: mia_core/templates/mia_core/my-account_detail.html:78
#: mia_core/templates/mia_core/user_detail.html:136
msgid "IP:"
msgstr "IP:"
#: mia_core/templates/mia_core/my-account_detail.html:83
#: mia_core/templates/mia_core/user_detail.html:141
msgid "Host:"
msgstr "主機名稱:"
#: mia_core/templates/mia_core/my-account_detail.html:88
#: mia_core/templates/mia_core/user_detail.html:146
msgid "Country:"
msgstr "國家:"
#: mia_core/templates/mia_core/my-account_detail.html:89
#: mia_core/templates/mia_core/user_detail.html:147
msgid "(Not Available)"
msgstr "(不可考)"
#: mia_core/templates/mia_core/my-account_detail.html:94
#: mia_core/templates/mia_core/user_detail.html:152
msgid "Created at:"
msgstr "建檔時間:"
#: mia_core/templates/mia_core/my-account_detail.html:99
#: mia_core/templates/mia_core/user_detail.html:157
msgid "Created by:"
msgstr "建檔人:"
#: mia_core/templates/mia_core/my-account_detail.html:104
#: mia_core/templates/mia_core/user_detail.html:162
msgid "Updated at:"
msgstr "更新時間:"
#: mia_core/templates/mia_core/my-account_detail.html:109
#: mia_core/templates/mia_core/user_detail.html:167
msgid "Updated by:"
msgstr "更新人:"
#: mia_core/templates/mia_core/my-account_form.html:38
#: mia_core/templates/mia_core/user_detail.html:69
#: mia_core/templates/mia_core/user_form.html:41
msgid "Back"
msgstr "回上頁"
#: mia_core/templates/mia_core/my-account_form.html:47
#: mia_core/templates/mia_core/user_form.html:50
msgid "Log in ID:"
msgstr "登入帳號:"
#: mia_core/templates/mia_core/my-account_form.html:55
#: mia_core/templates/mia_core/user_form.html:58
msgid "Password:"
msgstr "密碼:"
#: mia_core/templates/mia_core/my-account_form.html:63
#: mia_core/templates/mia_core/user_form.html:66
msgid "Confirm password:"
msgstr "確認密碼:"
#: mia_core/templates/mia_core/my-account_form.html:81
#: mia_core/templates/mia_core/user_form.html:97
msgid "Submit"
msgstr "傳送"
#: mia_core/templates/mia_core/user_detail.html:35
msgid "The account is not in use."
msgstr "帳號未使用。"
@ -172,15 +281,6 @@ msgstr "您真的要刪掉這個帳號嗎?"
msgid "Cancel"
msgstr "取消"
#: mia_core/templates/mia_core/user_detail.html:69
#: mia_core/templates/mia_core/user_form.html:41
msgid "Back"
msgstr "回上頁"
#: mia_core/templates/mia_core/user_detail.html:73
msgid "Settings"
msgstr "設定"
#: mia_core/templates/mia_core/user_detail.html:76 mia_core/views.py:163
msgid "You cannot delete your own account."
msgstr "不能刪除自己的帳號。"
@ -200,88 +300,10 @@ msgstr "帳號使用中,不可刪除。"
msgid "This account is already deleted."
msgstr "帳號已刪除。"
#: mia_core/templates/mia_core/user_detail.html:99
msgid "Log in ID.:"
msgstr "登入帳號:"
#: mia_core/templates/mia_core/user_detail.html:104
#: mia_core/templates/mia_core/user_form.html:74
msgid "Name:"
msgstr "姓名:"
#: mia_core/templates/mia_core/user_detail.html:110
#: mia_core/templates/mia_core/user_form.html:88
msgid "This account is disabled."
msgstr "帳號停用。"
#: mia_core/templates/mia_core/user_detail.html:116
msgid "This account is deleted."
msgstr "帳號已刪。"
#: mia_core/templates/mia_core/user_detail.html:122
msgid "This user has not logged in yet."
msgstr "使用者還沒登入過。"
#: mia_core/templates/mia_core/user_detail.html:126
msgid "# of visits:"
msgstr "登入次數:"
#: mia_core/templates/mia_core/user_detail.html:131
msgid "Last visit at:"
msgstr "上次登入時間:"
#: mia_core/templates/mia_core/user_detail.html:136
msgid "IP:"
msgstr "IP:"
#: mia_core/templates/mia_core/user_detail.html:141
msgid "Host:"
msgstr "主機名稱:"
#: mia_core/templates/mia_core/user_detail.html:146
msgid "Country:"
msgstr "國家:"
#: mia_core/templates/mia_core/user_detail.html:147
msgid "(Not Available)"
msgstr "(不可考)"
#: mia_core/templates/mia_core/user_detail.html:152
msgid "Created at:"
msgstr "建檔時間:"
#: mia_core/templates/mia_core/user_detail.html:157
msgid "Created by:"
msgstr "建檔人:"
#: mia_core/templates/mia_core/user_detail.html:162
msgid "Updated at:"
msgstr "更新時間:"
#: mia_core/templates/mia_core/user_detail.html:167
msgid "Updated by:"
msgstr "更新人:"
#: mia_core/templates/mia_core/user_form.html:31
msgid "Add a New Account"
msgstr "建立帳號"
#: mia_core/templates/mia_core/user_form.html:50
msgid "Log in ID:"
msgstr "登入帳號:"
#: mia_core/templates/mia_core/user_form.html:58
msgid "Password:"
msgstr "密碼:"
#: mia_core/templates/mia_core/user_form.html:66
msgid "Confirm password:"
msgstr "確認密碼:"
#: mia_core/templates/mia_core/user_form.html:97
msgid "Submit"
msgstr "傳送"
#: mia_core/templates/mia_core/user_list.html:27
msgid "Account Management"
msgstr "帳號管理"
@ -345,3 +367,11 @@ msgstr "帳號已儲存。"
#: mia_core/views.py:173
msgid "This user account was deleted successfully."
msgstr "帳號已刪除。"
#: mia_core/views.py:229
msgid "Your user account was not changed."
msgstr "你的帳號未異動。"
#: mia_core/views.py:232
msgid "Your user account was saved successfully."
msgstr "你的帳號已儲存。"

View File

@ -22,6 +22,9 @@ First written: 2020/8/9
{% endcomment %}
{% load i18n %}
{% load mia_core %}
{% if request|is_in_section:"mia_core:my-account" %}
{% setvar "user" request.user %}
{% endif %}
{% block settings %}
{% setvar "title" user.name %}
@ -29,71 +32,80 @@ First written: 2020/8/9
{% block content %}
{% if not user.is_in_use %}
<div class="alert alert-info alert-dismissible fade show">
<button type="button" class="close" data-dismiss="alert">&times;</button>
{{ _("The account is not in use.")|force_escape }}
{% if request|is_in_section:"mia_core:my-account" %}
<div class="btn-group btn-actions">
<a class="btn btn-primary" role="button" href="{% url "mia_core:my-account.edit" %}">
<i class="fas fa-user-cog"></i>
{{ _("Settings")|force_escape }}
</a>
</div>
{% endif %}
{% else %}
{% if not user.is_in_use %}
<div class="alert alert-info alert-dismissible fade show">
<button type="button" class="close" data-dismiss="alert">&times;</button>
{{ _("The account is not in use.")|force_escape }}
</div>
{% endif %}
<!-- the delete confirmation dialog -->
<form action="{% url "mia_core:users.delete" user %}" method="POST">
{% csrf_token %}
<!-- The Modal -->
<div class="modal" id="del-modal">
<div class="modal-dialog">
<div class="modal-content">
<!-- the delete confirmation dialog -->
<form action="{% url "mia_core:users.delete" user %}" method="POST">
{% csrf_token %}
<!-- The Modal -->
<div class="modal" id="del-modal">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">{{ _("User Deletion Confirmation")|force_escape }}</h4>
<button type="button" class="close" data-dismiss="modal">&times;</button>
</div>
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">{{ _("User Deletion Confirmation")|force_escape }}</h4>
<button type="button" class="close" data-dismiss="modal">&times;</button>
</div>
<!-- Modal body -->
<div class="modal-body">{{ _("Do you really want to delete this user?")|force_escape }}</div>
<!-- Modal body -->
<div class="modal-body">{{ _("Do you really want to delete this user?")|force_escape }}</div>
<!-- Modal footer -->
<div class="modal-footer">
<button class="btn btn-danger" type="submit" name="del-confirm">{{ _("Confirm")|force_escape }}</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">{{ _("Cancel")|force_escape }}</button>
<!-- Modal footer -->
<div class="modal-footer">
<button class="btn btn-danger" type="submit" name="del-confirm">{{ _("Confirm")|force_escape }}</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">{{ _("Cancel")|force_escape }}</button>
</div>
</div>
</div>
</div>
</div>
</form>
</form>
<div class="btn-group btn-actions">
<a class="btn btn-primary" role="button" href="{% url "mia_core:users" %}">
<i class="fas fa-chevron-circle-left"></i>
{{ _("Back")|force_escape }}
</a>
<a class="btn btn-primary" role="button" href="{% url "mia_core:users.edit" user %}">
<i class="fas fa-user-cog"></i>
{{ _("Settings")|force_escape }}
</a>
{% if user.login_id == request.user.login_id %}
<button class="btn btn-secondary" type="button" disabled="disabled" title="{{ _("You cannot delete your own account.")|force_escape }}">
<i class="fas fa-trash"></i>
{{ _("Delete")|force_escape }}
</button>
{% elif user.is_in_use %}
<button class="btn btn-secondary" type="button" disabled="disabled" title="{{ _("You cannot delete this account because it is in use.")|force_escape }}">
<i class="fas fa-trash"></i>
{{ _("Delete")|force_escape }}
</button>
{% elif user.is_deleted %}
<button class="btn btn-secondary" type="button" disabled="disabled" title="{{ _("This account is already deleted.")|force_escape }}>">
<i class="fas fa-trash"></i>
{{ _("Delete")|force_escape }}
</button>
{% else %}
<button class="btn btn-danger" type="button" data-toggle="modal" data-target="#del-modal">
<i class="fas fa-trash"></i>
{{ _("Delete")|force_escape }}
</button>
{% endif %}
</div>
<div class="btn-group btn-actions">
<a class="btn btn-primary" role="button" href="{% url "mia_core:users" %}">
<i class="fas fa-chevron-circle-left"></i>
{{ _("Back")|force_escape }}
</a>
<a class="btn btn-primary" role="button" href="{% url "mia_core:users.edit" user %}">
<i class="fas fa-user-cog"></i>
{{ _("Settings")|force_escape }}
</a>
{% if user.login_id == request.user.login_id %}
<button class="btn btn-secondary" type="button" disabled="disabled" title="{{ _("You cannot delete your own account.")|force_escape }}">
<i class="fas fa-trash"></i>
{{ _("Delete")|force_escape }}
</button>
{% elif user.is_in_use %}
<button class="btn btn-secondary" type="button" disabled="disabled" title="{{ _("You cannot delete this account because it is in use.")|force_escape }}">
<i class="fas fa-trash"></i>
{{ _("Delete")|force_escape }}
</button>
{% elif user.is_deleted %}
<button class="btn btn-secondary" type="button" disabled="disabled" title="{{ _("This account is already deleted.")|force_escape }}>">
<i class="fas fa-trash"></i>
{{ _("Delete")|force_escape }}
</button>
{% else %}
<button class="btn btn-danger" type="button" data-toggle="modal" data-target="#del-modal">
<i class="fas fa-trash"></i>
{{ _("Delete")|force_escape }}
</button>
{% endif %}
</div>
{% endif %}
<div class="row form-group">
<div class="col-sm-2">{{ _("Log in ID.:")|force_escape }}</div>
@ -150,22 +162,22 @@ First written: 2020/8/9
<div class="row form-group">
<div class="col-sm-2">{{ _("Created at:")|force_escape }}</div>
<div class="col-sm-10">{{ account.created_at }}</div>
<div class="col-sm-10">{{ user.created_at }}</div>
</div>
<div class="row form-group">
<div class="col-sm-2">{{ _("Created by:")|force_escape }}</div>
<div class="col-sm-10">{{ account.created_by }}</div>
<div class="col-sm-10">{{ user.created_by }}</div>
</div>
<div class="row form-group">
<div class="col-sm-2">{{ _("Updated at:")|force_escape }}</div>
<div class="col-sm-10">{{ account.updated_at }}</div>
<div class="col-sm-10">{{ user.updated_at }}</div>
</div>
<div class="row form-group">
<div class="col-sm-2">{{ _("Updated by:")|force_escape }}</div>
<div class="col-sm-10">{{ account.updated_by }}</div>
<div class="col-sm-10">{{ user.updated_by }}</div>
</div>
{% endblock %}

View File

@ -36,13 +36,13 @@ First written: 2020/8/9
{% block content %}
<div class="btn-group btn-actions">
<a class="btn btn-primary" role="button" href="{% if form.user %}{% url "mia_core:users.detail" form.user %}{% else %}{% url "mia_core:users" %}{% endif %}">
<a class="btn btn-primary" role="button" href="{% if request|is_in_section:"mia_core:my-account" %}{% url "mia_core:my-account" %}{% elif form.user %}{% url "mia_core:users.detail" form.user %}{% else %}{% url "mia_core:users" %}{% endif %}">
<i class="fas fa-chevron-circle-left"></i>
{{ _("Back")|force_escape }}
</a>
</div>
<form id="user-form" action="{% if form.user %}{% url "mia_core:users.update" form.user %}{% else %}{% url "mia_core:users.store" %}{% endif %}" method="POST">
<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.store" %}{% endif %}" method="POST">
{% csrf_token %}
<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 }}" />
@ -78,18 +78,20 @@ First written: 2020/8/9
</div>
</div>
<div class="row form-group form-check">
<div class="col-sm-12">
{% if form.user and form.user.pk == request.user.pk %}
{{ _("You cannot disable your own account.")|force_escape }}
{% else %}
<input id="user-is-disabled" class="form-check-input" type="checkbox" name="is_disabled" value="1" {% if form.is_disabled.value %} checked="checked" {% endif %} />
<label class="form-check-label" for="user-is-disabled">
{{ _("This account is disabled.")|force_escape }}
</label>
{% endif %}
{% if not request|is_in_section:"mia_core:my-account" %}
<div class="row form-group form-check">
<div class="col-sm-12">
{% if form.user and form.user.pk == request.user.pk %}
{{ _("You cannot disable your own account.")|force_escape }}
{% else %}
<input id="user-is-disabled" class="form-check-input" type="checkbox" name="is_disabled" value="1" {% if form.is_disabled.value %} checked="checked" {% endif %} />
<label class="form-check-label" for="user-is-disabled">
{{ _("This account is disabled.")|force_escape }}
</label>
{% endif %}
</div>
</div>
</div>
{% endif %}
<div class="row form-group">
<div class="col-sm-12">

View File

@ -19,8 +19,11 @@
"""
from django.urls import path, register_converter
from django.views.decorators.http import require_GET
from django.views.generic import TemplateView
from . import views, converters
from .digest_auth import login_required
register_converter(converters.UserConverter, "user")
@ -35,10 +38,9 @@ urlpatterns = [
path("users/<user:user>/delete", views.user_delete, name="users.delete"),
path("api/users/<str:login_id>/exists", views.api_users_exists,
name="api.users.exists"),
# TODO: To be done.
path("my-account", views.todo, name="my-account"),
# TODO: To be done.
path("my-account/edit", views.todo, name="my-account.edit"),
# TODO: To be done.
path("my-account/update", views.todo, name="my-account.update"),
path("my-account", require_GET(login_required(TemplateView.as_view(
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.my_account_store,
name="my-account.update"),
]

View File

@ -175,6 +175,64 @@ def user_delete(request, user):
return redirect("mia_core:users")
@require_GET
@login_required
def my_account_form(request):
"""The view to edit my account.
Args:
request (HttpRequest): The request.
user (User): The account.
Returns:
HttpResponse: The response.
"""
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 my_account_store(request):
"""The view to store my account.
Args:
request (HttpRequest): The request.
user (Account): The user.
Returns:
HttpResponseRedirect: 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, login_id):
"""The view to check whether a user with a log in ID exists.