From b2d6645f4645c7761d22e73d97e36b2b5faa9282 Mon Sep 17 00:00:00 2001 From: ibuler Date: Fri, 26 Jan 2018 15:32:23 +0800 Subject: [PATCH] =?UTF-8?q?[Feature]=20=E6=B7=BB=E5=8A=A0=E8=B5=84?= =?UTF-8?q?=E4=BA=A7=E8=AF=A6=E6=83=85=E6=A0=87=E7=AD=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/assets/forms.py | 6 +++++ .../assets/templates/assets/asset_detail.html | 27 +++++++++++++++++++ apps/assets/templates/assets/label_list.html | 2 +- apps/assets/urls/views_urls.py | 1 + apps/assets/views/label.py | 18 +++++++++++-- 5 files changed, 51 insertions(+), 3 deletions(-) diff --git a/apps/assets/forms.py b/apps/assets/forms.py index b3ee468c6..38e1e65ef 100644 --- a/apps/assets/forms.py +++ b/apps/assets/forms.py @@ -418,6 +418,12 @@ class LabelForm(forms.ModelForm): model = Label fields = ['name', 'value', 'assets'] + def __init__(self, *args, **kwargs): + if kwargs.get('instance', None): + initial = kwargs.get('initial', {}) + initial['assets'] = kwargs['instance'].assets.all() + super().__init__(*args, **kwargs) + def save(self, commit=True): label = super().save(commit=commit) assets = self.cleaned_data['assets'] diff --git a/apps/assets/templates/assets/asset_detail.html b/apps/assets/templates/assets/asset_detail.html index fc2320420..346a52723 100644 --- a/apps/assets/templates/assets/asset_detail.html +++ b/apps/assets/templates/assets/asset_detail.html @@ -244,6 +244,33 @@ + +
+
+ {% trans 'Labels' %} +
+
+{# #} +{# #} +{# {% for label in asset.labels.all %}#} +{# #} +{# #} +{# #} +{# #} +{# {% endfor %}#} +{# #} +{#
{{ label.name }}#} +{# #} +{# {{ label.value }}#} +{# #} +{#
#} + +
+
{% endif %} diff --git a/apps/assets/templates/assets/label_list.html b/apps/assets/templates/assets/label_list.html index bbe6ae5c1..b9430bb97 100644 --- a/apps/assets/templates/assets/label_list.html +++ b/apps/assets/templates/assets/label_list.html @@ -35,7 +35,7 @@ function initTable() { }}, {targets: 4, createdCell: function (td, cellData, rowData) { - var update_btn = '{% trans "Update" %}'.replace('{{ DEFAULT_PK }}', cellData); + var update_btn = '{% trans "Update" %}'.replace('{{ DEFAULT_PK }}', cellData); var del_btn = '{% trans "Delete" %}'.replace('{{ DEFAULT_PK }}', cellData); $(td).html(update_btn + del_btn) }}], diff --git a/apps/assets/urls/views_urls.py b/apps/assets/urls/views_urls.py index a63c065cf..6207ae0c5 100644 --- a/apps/assets/urls/views_urls.py +++ b/apps/assets/urls/views_urls.py @@ -55,5 +55,6 @@ urlpatterns = [ url(r'^label/$', views.LabelListView.as_view(), name='label-list'), url(r'^label/create/$', views.LabelCreateView.as_view(), name='label-create'), + url(r'^label/(?P[0-9a-zA-Z\-]{36})/update/$', views.LabelUpdateView.as_view(), name='label-update'), ] diff --git a/apps/assets/views/label.py b/apps/assets/views/label.py index e0b489323..a6e1d3aa3 100644 --- a/apps/assets/views/label.py +++ b/apps/assets/views/label.py @@ -7,7 +7,8 @@ from django.utils.translation import ugettext_lazy as _ from django.urls import reverse_lazy from common.mixins import AdminUserRequiredMixin -from common.const import create_success_msg +from common.const import create_success_msg, update_success_msg +from ..models import Label from ..forms import LabelForm @@ -30,6 +31,7 @@ class LabelListView(AdminUserRequiredMixin, TemplateView): class LabelCreateView(AdminUserRequiredMixin, CreateView): + model = Label template_name = 'assets/label_create_update.html' form_class = LabelForm success_url = reverse_lazy('assets:label-list') @@ -45,7 +47,19 @@ class LabelCreateView(AdminUserRequiredMixin, CreateView): class LabelUpdateView(AdminUserRequiredMixin, UpdateView): - pass + model = Label + template_name = 'assets/label_create_update.html' + form_class = LabelForm + success_url = reverse_lazy('assets:label-list') + success_message = update_success_msg + + def get_context_data(self, **kwargs): + context = { + 'app': _('Assets'), + 'action': _('Update label'), + } + kwargs.update(context) + return super().get_context_data(**kwargs) class LabelDetailView(AdminUserRequiredMixin, DetailView):