jumpserver/apps/assets/utils.py

38 lines
1.4 KiB
Python
Raw Normal View History

2016-08-09 09:27:37 +00:00
# ~*~ coding: utf-8 ~*~
#
2016-09-22 10:31:04 +00:00
from rest_framework import serializers
2016-10-12 10:57:51 +00:00
from models import Tag
from django.views.generic.edit import CreateView
2016-09-04 09:43:03 +00:00
2016-10-12 10:57:51 +00:00
class CreateAssetTagsMiXin(CreateView):
def get_form_kwargs(self):
tags_list = self.request.POST.getlist('tags')
kwargs = {
'initial': self.get_initial(),
'prefix': self.get_prefix(),
}
if self.request.method in ('POST', 'PUT'):
post_data = self.request.POST.copy()
if post_data.has_key('tags'):
post_data.pop('tags')
for t in tags_list:
try:
oTag = Tag.objects.get(pk=int(t))
2016-10-28 13:19:37 +00:00
except (Tag.DoesNotExist, UnicodeEncodeError):
oTag = Tag(name=t, created_by=self.request.user.username or 'Admin')
2016-10-12 10:57:51 +00:00
oTag.save()
post_data.update({'tags':oTag.pk})
else:
post_data.update({'tags':int(t)})
kwargs.update({
'data': post_data,
'files': self.request.FILES,
})
return kwargs
class UpdateAssetTagsMiXin(CreateAssetTagsMiXin):
def get_form_kwargs(self):
kwargs = super(UpdateAssetTagsMiXin, self).get_form_kwargs()
if hasattr(self, 'object'):
kwargs.update({'instance': self.object})
return kwargs