Commit e9705889 authored by ibuler's avatar ibuler

[Update] 支持生成api key

parent 367ebebf
...@@ -12,6 +12,7 @@ class AccessKeyViewSet(ModelViewSet): ...@@ -12,6 +12,7 @@ class AccessKeyViewSet(ModelViewSet):
permission_classes = (IsValidUser,) permission_classes = (IsValidUser,)
serializer_class = serializers.AccessKeySerializer serializer_class = serializers.AccessKeySerializer
pagination_class = LimitOffsetPagination pagination_class = LimitOffsetPagination
search_fields = ['^id', '^secret']
def get_queryset(self): def get_queryset(self):
return self.request.user.access_keys.all() return self.request.user.access_keys.all()
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
{% load i18n %} {% load i18n %}
{% load static %} {% load static %}
{% block modal_id %}access_key_modal{% endblock %} {% block modal_id %}access_key_modal{% endblock %}
{% block modal_class %}modal-lg{% endblock %}
{% block modal_title%}{% trans "API key list" %}{% endblock %} {% block modal_title%}{% trans "API key list" %}{% endblock %}
{% block modal_body %} {% block modal_body %}
<style> <style>
...@@ -21,6 +22,7 @@ ...@@ -21,6 +22,7 @@
</th> </th>
<th class="text-center">{% trans 'ID' %}</th> <th class="text-center">{% trans 'ID' %}</th>
<th class="text-center">{% trans 'Secret' %}</th> <th class="text-center">{% trans 'Secret' %}</th>
<th class="text-center">{% trans 'Active' %}</th>
<th class="text-center">{% trans 'Date' %}</th> <th class="text-center">{% trans 'Date' %}</th>
<th class="text-center">{% trans 'Action' %}</th> <th class="text-center">{% trans 'Action' %}</th>
</tr> </tr>
...@@ -39,11 +41,36 @@ function initTable() { ...@@ -39,11 +41,36 @@ function initTable() {
var options = { var options = {
ele: $('#access_key_list_table'), ele: $('#access_key_list_table'),
columnDefs: [ columnDefs: [
{targets: 2, createdCell: function (td, cellData, rowData) { {targets: 2, createdCell: function (td, cellData) {
$(td).html(detail_btn.replace('{{ DEFAULT_PK }}', cellData.id)); var btn = '<button class="btn btn-primary btn-xs btn-secret" data-secret="SECRET">{% trans 'Show' %}</button>';
btn = btn.replace("SECRET", cellData);
$(td).html(btn)
}}, }},
{targets: 4, createdCell: function (td, cellData, rowData) { {targets: 3, createdCell: function (td, cellData) {
$(td).html(update_btn + del_btn) if (cellData) {
$(td).html('<i class="fa fa-check text-navy"></i>')
} else {
$(td).html('<i class="fa fa-times text-danger"></i>')
}
}},
{targets: 4, createdCell: function (td, cellData) {
var date = toSafeLocalDateStr(cellData);
$(td).html(date)
}},
{targets: 5, createdCell: function (td, cellData, rowData) {
var btn = '';
var btn_del = '<a class="btn btn-xs btn-danger m-l-xs btn-del" data-id="ID">{% trans "Delete" %}</a>';
var btn_inactive = '<a class="btn btn-xs btn-info m-l-xs btn-inactive" data-id="ID">{% trans "Disable" %}</a>';
var btn_active = '<a class="btn btn-xs btn-primary m-l-xs btn-active" data-id="ID">{% trans "Enable" %}</a>';
btn += btn_del;
if (rowData.is_active) {
btn += btn_inactive
} else {
btn += btn_active
}
btn = btn.replaceAll("ID", cellData);
$(td).html(btn);
}} }}
], ],
ajax_url: '{% url "api-auth:access-key-list" %}', ajax_url: '{% url "api-auth:access-key-list" %}',
...@@ -51,6 +78,7 @@ function initTable() { ...@@ -51,6 +78,7 @@ function initTable() {
{data: "id"}, {data: "id"},
{data: "id"}, {data: "id"},
{data: "secret"}, {data: "secret"},
{data: "is_active"},
{data: "date_created"}, {data: "date_created"},
{data: "id", orderable: false} {data: "id", orderable: false}
], ],
...@@ -61,17 +89,50 @@ function initTable() { ...@@ -61,17 +89,50 @@ function initTable() {
$(document).ready(function () { $(document).ready(function () {
}).on("show.bs.modal", "#access_key_modal", function () { }).on("show.bs.modal", "#access_key_modal", function () {
initTable() if (!table) {
initTable()
}
}).on("click", "#create-btn", function () { }).on("click", "#create-btn", function () {
var url = "{% url "api-auth:access-key-list" %}"; var url = "{% url "api-auth:access-key-list" %}";
var body = { var data = {
url: url, url: url,
method: 'POST', method: 'POST',
success: function () { success: function () {
table.ajax.reload(); table.ajax.reload();
} }
}; };
requestApi(body) requestApi(data)
}).on("click", ".btn-secret", function () {
var $this = $(this);
$this.parent().html($this.data("secret"))
}).on("click", ".btn-del", function () {
var url = "{% url "api-auth:access-key-detail" pk=DEFAULT_PK %}";
url = url.replace("{{ DEFAULT_PK }}", $(this).data("id")) ;
objectDelete($(this), $(this).data("id"), url);
}).on("click", ".btn-active", function () {
var url = "{% url "api-auth:access-key-detail" pk=DEFAULT_PK %}";
url = url.replace("{{ DEFAULT_PK }}", $(this).data("id")) ;
var data = {
url: url,
body: JSON.stringify({"is_active": true}),
method: "PATCH",
success: function () {
table.ajax.reload();
}
};
requestApi(data)
}).on("click", ".btn-inactive", function () {
var url = "{% url "api-auth:access-key-detail" pk=DEFAULT_PK %}";
url = url.replace("{{ DEFAULT_PK }}", $(this).data("id")) ;
var data = {
url: url,
body: JSON.stringify({"is_active": false}),
method: "PATCH",
success: function () {
table.ajax.reload();
}
};
requestApi(data)
}) })
</script> </script>
{% endblock %} {% endblock %}
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment