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): def save(self, *args, **kwargs):
if self.cleaned_data.get('copy_from'): if self.cleaned_data.get('copy_from'):
self.instance.description = self.cleaned_data['copy_from'].description fields = (
self.instance.active = self.cleaned_data['copy_from'].active 'description',
self.instance.available_from = self.cleaned_data['copy_from'].available_from 'active',
self.instance.available_until = self.cleaned_data['copy_from'].available_until 'available_from',
self.instance.require_voucher = self.cleaned_data['copy_from'].require_voucher 'available_until',
self.instance.hide_without_voucher = self.cleaned_data['copy_from'].hide_without_voucher 'require_voucher',
self.instance.allow_cancel = self.cleaned_data['copy_from'].allow_cancel 'hide_without_voucher',
self.instance.min_per_order = self.cleaned_data['copy_from'].min_per_order 'allow_cancel',
self.instance.max_per_order = self.cleaned_data['copy_from'].max_per_order 'min_per_order',
self.instance.generate_tickets = self.cleaned_data['copy_from'].generate_tickets 'max_per_order',
self.instance.checkin_attention = self.cleaned_data['copy_from'].checkin_attention 'generate_tickets',
self.instance.free_price = self.cleaned_data['copy_from'].free_price 'checkin_attention',
self.instance.original_price = self.cleaned_data['copy_from'].original_price 'free_price',
self.instance.sales_channels = self.cleaned_data['copy_from'].sales_channels '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: else:
# Add to all sales channels by default # Add to all sales channels by default
self.instance.sales_channels = [k for k in get_all_sales_channels().keys()] self.instance.sales_channels = [k for k in get_all_sales_channels().keys()]

View File

@@ -95,6 +95,7 @@
</td> </td>
<td class="text-right flip"> <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.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> <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> </td>
</tr> </tr>

View File

@@ -1064,11 +1064,27 @@ class ItemCreate(EventPermissionRequiredMixin, CreateView):
'item': self.object.id, '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): def get_initial(self):
initial = super().get_initial() initial = super().get_initial()
trs = list(self.request.event.tax_rules.all()) trs = list(self.request.event.tax_rules.all())
if len(trs) == 1: if len(trs) == 1:
initial['tax_rule'] = trs[0] 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 return initial
@transaction.atomic @transaction.atomic