2018-02-08 03:34:21 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
from django import forms
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
|
|
|
from ..models import Label, Asset
|
|
|
|
|
|
|
|
__all__ = ['LabelForm']
|
|
|
|
|
|
|
|
|
|
|
|
class LabelForm(forms.ModelForm):
|
|
|
|
assets = forms.ModelMultipleChoiceField(
|
2018-07-14 16:55:05 +00:00
|
|
|
queryset=Asset.objects.all(), label=_('Asset'), required=False,
|
2018-02-08 03:34:21 +00:00
|
|
|
widget=forms.SelectMultiple(
|
|
|
|
attrs={'class': 'select2', 'data-placeholder': _('Select assets')}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
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']
|
|
|
|
label.assets.set(assets)
|
|
|
|
return label
|