def itemvar_forms(self): se_item_instances = { sei.item_id: sei for sei in SubEventItem.objects.filter(subevent=self.object) } se_var_instances = { sei.variation_id: sei for sei in SubEventItemVariation.objects.filter( subevent=self.object) } if self.copy_from: se_item_instances = { sei.item_id: SubEventItem(item=sei.item, price=sei.price, disabled=sei.disabled) for sei in SubEventItem.objects.filter( subevent=self.copy_from).select_related('item') } se_var_instances = { sei.variation_id: SubEventItemVariation(variation=sei.variation, price=sei.price, disabled=sei.disabled) for sei in SubEventItemVariation.objects.filter( subevent=self.copy_from).select_related('variation') } formlist = [] for i in self.request.event.items.filter( active=True).prefetch_related('variations'): if i.has_variations: for v in i.variations.all(): inst = se_var_instances.get(v.pk) or SubEventItemVariation( subevent=self.object, variation=v) formlist.append( SubEventItemVariationForm( prefix='itemvar-{}'.format(v.pk), item=i, variation=v, instance=inst, data=(self.request.POST if self.request.method == "POST" else None))) else: inst = se_item_instances.get(i.pk) or SubEventItem( subevent=self.object, item=i) formlist.append( SubEventItemForm( prefix='item-{}'.format(i.pk), item=i, instance=inst, data=(self.request.POST if self.request.method == "POST" else None))) return formlist
def update(self, instance, validated_data): item_price_overrides_data = validated_data.pop( 'subeventitem_set' ) if 'subeventitem_set' in validated_data else {} variation_price_overrides_data = validated_data.pop( 'subeventitemvariation_set' ) if 'subeventitemvariation_set' in validated_data else {} meta_data = validated_data.pop('meta_data', None) subevent = super().update(instance, validated_data) existing_item_overrides = { item.item: item.id for item in SubEventItem.objects.filter(subevent=subevent) } for item_price_override_data in item_price_overrides_data: id = existing_item_overrides.pop(item_price_override_data['item'], None) SubEventItem(id=id, subevent=subevent, **item_price_override_data).save() SubEventItem.objects.filter( id__in=existing_item_overrides.values()).delete() existing_variation_overrides = { item.variation: item.id for item in SubEventItemVariation.objects.filter(subevent=subevent) } for variation_price_override_data in variation_price_overrides_data: id = existing_variation_overrides.pop( variation_price_override_data['variation'], None) SubEventItemVariation(id=id, subevent=subevent, **variation_price_override_data).save() SubEventItemVariation.objects.filter( id__in=existing_variation_overrides.values()).delete() # Meta data if meta_data is not None: current = { mv.property: mv for mv in subevent.meta_values.select_related('property') } for key, value in meta_data.items(): prop = self.meta_properties.get(key) if prop in current: current[prop].value = value current[prop].save() else: subevent.meta_values.create( property=self.meta_properties.get(key), value=value) for prop, current_object in current.items(): if prop.name not in meta_data: current_object.delete() return subevent
def update(self, instance, validated_data): item_price_overrides_data = validated_data.pop('subeventitem_set', None) variation_price_overrides_data = validated_data.pop('subeventitemvariation_set', None) meta_data = validated_data.pop('meta_data', None) seat_category_mapping = validated_data.pop('seat_category_mapping', None) subevent = super().update(instance, validated_data) if item_price_overrides_data is not None: existing_item_overrides = {item.item: item.id for item in SubEventItem.objects.filter(subevent=subevent)} for item_price_override_data in item_price_overrides_data: id = existing_item_overrides.pop(item_price_override_data['item'], None) SubEventItem(id=id, subevent=subevent, **item_price_override_data).save() SubEventItem.objects.filter(id__in=existing_item_overrides.values()).delete() if variation_price_overrides_data is not None: existing_variation_overrides = {item.variation: item.id for item in SubEventItemVariation.objects.filter(subevent=subevent)} for variation_price_override_data in variation_price_overrides_data: id = existing_variation_overrides.pop(variation_price_override_data['variation'], None) SubEventItemVariation(id=id, subevent=subevent, **variation_price_override_data).save() SubEventItemVariation.objects.filter(id__in=existing_variation_overrides.values()).delete() # Meta data if meta_data is not None: current = {mv.property: mv for mv in subevent.meta_values.select_related('property')} for key, value in meta_data.items(): if key not in self.ignored_meta_properties: prop = self.meta_properties.get(key) if prop in current: current[prop].value = value current[prop].save() else: subevent.meta_values.create( property=self.meta_properties.get(key), value=value ) for prop, current_object in current.items(): if prop.name not in self.ignored_meta_properties: if prop.name not in meta_data: current_object.delete() # Seats if seat_category_mapping is not None or ('seating_plan' in validated_data and validated_data['seating_plan'] is None): current_mappings = { m.layout_category: m for m in self.context['request'].event.seat_category_mappings.filter(subevent=subevent) } if not subevent.seating_plan: seat_category_mapping = {} for key, value in seat_category_mapping.items(): if key in current_mappings: m = current_mappings.pop(key) m.product = value m.save() else: self.context['request'].event.seat_category_mappings.create( product=value, layout_category=key, subevent=subevent ) for m in current_mappings.values(): m.delete() if 'seating_plan' in validated_data or seat_category_mapping is not None: generate_seats(self.context['request'].event, subevent, subevent.seating_plan, { m.layout_category: m.product for m in self.context['request'].event.seat_category_mappings.select_related('product').filter(subevent=subevent) }) return subevent