Clone button for products

This commit is contained in:
Raphael Michel
2020-07-16 08:53:18 +02:00
parent f179a220bc
commit a685af6433
3 changed files with 42 additions and 14 deletions

View File

@@ -295,20 +295,31 @@ class ItemCreateForm(I18nModelForm):
def save(self, *args, **kwargs):
if self.cleaned_data.get('copy_from'):
self.instance.description = self.cleaned_data['copy_from'].description
self.instance.active = self.cleaned_data['copy_from'].active
self.instance.available_from = self.cleaned_data['copy_from'].available_from
self.instance.available_until = self.cleaned_data['copy_from'].available_until
self.instance.require_voucher = self.cleaned_data['copy_from'].require_voucher
self.instance.hide_without_voucher = self.cleaned_data['copy_from'].hide_without_voucher
self.instance.allow_cancel = self.cleaned_data['copy_from'].allow_cancel
self.instance.min_per_order = self.cleaned_data['copy_from'].min_per_order
self.instance.max_per_order = self.cleaned_data['copy_from'].max_per_order
self.instance.generate_tickets = self.cleaned_data['copy_from'].generate_tickets
self.instance.checkin_attention = self.cleaned_data['copy_from'].checkin_attention
self.instance.free_price = self.cleaned_data['copy_from'].free_price
self.instance.original_price = self.cleaned_data['copy_from'].original_price
self.instance.sales_channels = self.cleaned_data['copy_from'].sales_channels
fields = (
'description',
'active',
'available_from',
'available_until',
'require_voucher',
'hide_without_voucher',
'allow_cancel',
'min_per_order',
'max_per_order',
'generate_tickets',
'checkin_attention',
'free_price',
'original_price',
'sales_channels',
'issue_giftcard',
'require_approval',
'allow_waitinglist',
'show_quota_left',
'hidden_if_available',
'require_bundling',
'checkin_attention',
)
for f in fields:
setattr(self.instance, f, getattr(self.cleaned_data['copy_from'], f))
else:
# Add to all sales channels by default
self.instance.sales_channels = [k for k in get_all_sales_channels().keys()]

View File

@@ -95,6 +95,7 @@
</td>
<td class="text-right flip">
<a href="{% url "control:event.item" organizer=request.event.organizer.slug event=request.event.slug item=i.id %}" class="btn btn-default btn-sm"><i class="fa fa-edit"></i></a>
<a href="{% url "control:event.items.add" organizer=request.event.organizer.slug event=request.event.slug %}?copy_from={{ i.id }}" class="btn btn-default btn-sm" title="{% trans "Clone" %}" data-toggle="tooltip"><i class="fa fa-copy"></i></a>
<a href="{% url "control:event.items.delete" organizer=request.event.organizer.slug event=request.event.slug item=i.id %}" class="btn btn-danger btn-sm"><i class="fa fa-trash"></i></a>
</td>
</tr>

View File

@@ -1064,11 +1064,27 @@ class ItemCreate(EventPermissionRequiredMixin, CreateView):
'item': self.object.id,
})
@cached_property
def copy_from(self):
if self.request.GET.get("copy_from") and not getattr(self, 'object', None):
try:
return self.request.event.items.get(pk=self.request.GET.get("copy_from"))
except Item.DoesNotExist:
pass
def get_initial(self):
initial = super().get_initial()
trs = list(self.request.event.tax_rules.all())
if len(trs) == 1:
initial['tax_rule'] = trs[0]
if self.copy_from:
fields = ('name', 'internal_name', 'category', 'admission', 'default_price', 'tax_rule')
for f in fields:
initial[f] = getattr(self.copy_from, f)
initial['copy_from'] = self.copy_from
initial['has_variations'] = self.copy_from.variations.exists()
return initial
@transaction.atomic