def testSlugify(self): # 3x A acute + 2x S caron val = slugify('ÁÁÁŠŠ') self.assertEqual(val, 'aaass') # the same with disabled all "&" conversions (is not nice) val = slugify('ÁÁÁŠŠ', entities=False, decimal=False, hexadecimal=False) self.assertEqual(val, 'aacute-193-xc1-352-x160') # A acute + S caron val = slugify(u'\xc1\u0160') self.assertEqual(val, 'as') # Greek alpha beta gamma can not be converted to ascii val = slugify('αβγ') self.assertEqual(val, '')
def testSlugify(self): # 3x A acute + 2x S caron val = slugify(b'ÁÁÁŠŠ') self.assertEqual(val, 'aaass') # the same with disabled all "&" conversions (is not nice) val = slugify(b'ÁÁÁŠŠ', entities=False, decimal=False, hexadecimal=False) self.assertEqual(val, 'aacute-193-xc1-352-x160') # A acute + S caron val = slugify(u'\xc1\u0160') self.assertEqual(val, 'as') # Greek alpha beta gamma can not be converted to ascii val = slugify(b'αβγ') self.assertEqual(val, '')
def create_all_variations(self): myslug = self.product.slug myname=self.product.name site = self.product.site to_insert = {'variants':[]} for date in self.dates.all(): for group in self.hallscheme.groups.all(): for seat in group.seats.all(): slug = slugify(cyr2lat('%s_%s_%s' % (myslug, date.__unicode__(), seat.__unicode__()))) name=u"%s :: %s :: %s" % (myname, date.__unicode__(), seat.__unicode__()) to_insert['variants'].append({'name':name, 'slug':slug, 'date':date, 'seat':seat}) products = Product.objects.bulk_create([ Product( site=site, name=u"%s :: %s :: %s" % (myname, variant['date'].__unicode__(), variant['seat'].__unicode__()), items_in_stock=1, active=True, slug="%s" % slugify(cyr2lat('%s_%s_%s' % (myslug, variant['date'].__unicode__(), variant['seat'].__unicode__()))) ) for variant in to_insert['variants'] ]) to_insert['products'] = {p.slug: Product.objects.get(slug=p.slug) for p in products} Ticket.objects.bulk_create([ Ticket( product=to_insert['products'][variant['slug']], event=self, datetime=variant['date'], seat=variant['seat'] ) for variant in to_insert['variants'] ]) Price.objects.bulk_create([ Price( product=to_insert['products'][variant['slug']], quantity='1', price=self.prices.filter(group=variant['seat'].group).values('price')[0]['price'], ) for variant in to_insert['variants'] ]) for date in self.dates.all(): date.update_statuses()
def __init__(self, *args, **kwargs): self.optionkeys = [] self.variationkeys = [] self.existing = {} self.optiondict = {} self.edit_urls = {} self.namedict = {} self.skudict = {} self.slugdict = {} self.product = kwargs.pop('product', None) super(VariationManagerForm, self).__init__(*args, **kwargs) if self.product: configurableproduct = self.product.configurableproduct; for grp in configurableproduct.option_group.all(): optchoices = [("%i_%i" % (opt.option_group.id, opt.id), opt.name) for opt in grp.option_set.all()] kw = { 'label' : grp.name, 'widget' : forms.CheckboxSelectMultiple(), 'required' : False, 'choices' : optchoices, } fld = forms.MultipleChoiceField(**kw) key = 'optiongroup__%s' % grp.id self.fields[key] = fld self.optionkeys.append(key) self.optiondict[grp.id] = [] configurableproduct.setup_variation_cache() for opts in configurableproduct.get_all_options(): variation = configurableproduct.get_product_from_options(opts) optnames = [opt.value for opt in opts] kw = { 'initial' : None, 'label' : " ".join(optnames), 'required' : False } opt_str = '__'.join(["%i_%i" % (opt.option_group.id, opt.id) for opt in opts]) key = "pv__%s" % opt_str if variation: basename = variation.name sku = variation.sku slug = variation.slug kw['initial'] = 'add' self.existing[key] = True self.edit_urls[key] = urlresolvers.reverse('admin:product_product_change', args=(variation.id,)) else: basename = u'%s (%s)' % (self.product.name, u'/'.join(optnames)) slug = slugify(u'%s_%s' % (self.product.slug, u'_'.join(optnames))) sku = "" pv = forms.BooleanField(**kw) self.fields[key] = pv for opt in opts: self.optiondict[opt.option_group.id].append(key) self.variationkeys.append(key) # Name Field nv = forms.CharField(initial=basename) namekey = "name__%s" % opt_str self.fields[namekey] = nv self.namedict[key] = namekey # SKU Field sv = forms.CharField(initial=sku, required=False) skukey = "sku__%s" % opt_str self.fields[skukey] = sv self.skudict[key] = skukey # Slug Field sf = forms.CharField(initial=slug) slugkey = "slug__%s" % opt_str self.fields[slugkey] = sf self.slugdict[key] = slugkey
def save(self, **kwargs): if not self.slug: self.slug = slugify(self.name, instance=self) super(CustomTextField, self).save(**kwargs)
def create_variation(self, options, name="", sku="", slug=""): """Create a productvariation with the specified options. Will not create a duplicate.""" log.debug("Create variation: %s", options) variations = self.get_variations_for_options(options) if not variations: # There isn't an existing ProductVariation. if self.product: sites = self.product.site.all() else: sites = self.site.all() variant = Product(items_in_stock=0, name=name) optnames = [opt.value for opt in options] if not slug: slug = slugify('%s_%s' % (self.product.slug, '_'.join(optnames))) while Product.objects.filter(slug=slug).count(): slug = '_'.join((slug, six.text_type(self.product.id))) variant.slug = slug log.info("Creating variation for [%s] %s", self.product.slug, variant.slug) variant.save() variant.site.add(*sites) pv = ProductVariation(product=variant, parent=self) pv.save() for option in options: pv.options.add(option) pv.name = name pv.sku = sku pv.save() else: variant = variations[0].product log.debug("Existing variant: %s", variant) dirty = False if name and name != variant.name: log.debug("Updating name: %s --> %s", self, name) variant.name = name dirty = True if sku and sku != variant.sku: variant.sku = sku log.debug("Updating sku: %s --> %s", self, sku) dirty = True if slug: # just in case slug = slugify(slug) if slug and slug != variant.slug: variant.slug = slug log.debug("Updating slug: %s --> %s", self, slug) dirty = True if dirty: log.debug("Changed existing variant, saving: %s", variant) variant.save() else: log.debug("No change to variant, skipping save: %s", variant) return variant
def wizard_event(request, step='step0', template='localsite/wizard_event.html'): wizard = request.session.get('wizard') output = {} formsets = [] form = None if step == 'step0': if not wizard: wizard = {} product = Product() if request.method == 'POST': form = ProductForm(request.POST, instance=product) formsets.append(EventFormInline(request.POST, instance=product)) if form.is_valid() and formsets[0].is_valid(): product = form.save(commit=False) product.site = Site.objects.get(id=1) product.slug = slugify(cyr2lat(product.name)) product.save() form.save_m2m() formsets[0].save() event = formsets[0].instance.event wizard['event'] = event wizard['step'] = 1 request.session['wizard'] = wizard for group in event.hallscheme.groups.all(): price = SeatGroupPrice(event=event, group=group) price.save() return HttpResponseRedirect('/wizards/event/step1/') else: form = ProductForm(instance=product) formsets.append(EventFormInline(instance=product)) elif step == 'step1': if not wizard: return HttpResponseRedirect('/wizards/event/') event = wizard['event'] step = wizard['step'] if step != 1: return HttpResponseRedirect('/wizards/event/') if request.method == 'POST': form = SeatGroupPriceFormset(request.POST, queryset=event.prices.all()) if form.is_valid(): form.save() wizard['step'] = 2 request.session['wizard'] = wizard return HttpResponseRedirect('/wizards/event/step2/') else: form = SeatGroupPriceFormset(queryset=event.prices.all()) elif step == 'step2': template='localsite/wizard_event_dates.html' if not wizard: return HttpResponseRedirect('/wizards/event/') event = wizard['event'] step = wizard['step'] if step != 2: return HttpResponseRedirect('/wizards/event/') if request.method == 'POST': formsets.append(EventDateFormInline(request.POST, instance=event)) if formsets[0].is_valid(): formsets[0].save() wizard['step'] = 3 request.session['wizard'] = wizard return HttpResponseRedirect('/wizards/event/step3/') else: formsets.append(EventDateFormInline(instance=event)) elif step == 'step3': template='localsite/wizard_product_images.html' if not wizard: return HttpResponseRedirect('/wizards/event/') event = wizard['event'] step = wizard['step'] if step != 3: return HttpResponseRedirect('/wizards/event/') if request.method == 'POST': formsets.append(ProductImageFormInline(request.POST, request.FILES, instance=event.product)) if formsets[0].is_valid(): formsets[0].save() wizard['step'] = 4 request.session['wizard'] = wizard return HttpResponseRedirect('/wizards/event/step4/') else: formsets.append(ProductImageFormInline(instance=event.product)) elif step == 'step4': if not wizard: return HttpResponseRedirect('/wizards/event/') event = wizard['event'] step = wizard['step'] if step != 4: return HttpResponseRedirect('/wizards/event/') if request.method == 'POST': formsets.append(AnnouncementFormInline(request.POST, request.FILES, instance=event)) if formsets[0].is_valid(): formsets[0].save() wizard['step'] = 5 request.session['wizard'] = wizard return HttpResponseRedirect('/wizards/event/done/') else: formsets.append(AnnouncementFormInline(instance=event)) elif step == 'done': template='localsite/wizard_event_done.html' if not wizard: return HttpResponseRedirect('/wizards/event/') event = wizard['event'] output['event'] = event step = wizard['step'] if step != 5: return HttpResponseRedirect('/wizards/event/') event.create_all_variations() del request.session['wizard'] return HttpResponseRedirect(event.get_absolute_url()) if form: output['form'] = form if formsets: output['formsets'] = formsets ctx = RequestContext(request, output) return render_to_response(template, context_instance=ctx)
def create_variation(self, options, name=u"", sku=u"", slug=u""): """Create a productvariation with the specified options. Will not create a duplicate.""" log.debug("Create variation: %s", options) variations = self.get_variations_for_options(options) if not variations: # There isn't an existing ProductVariation. if self.product: site = self.product.site else: site = self.site variant = Product(site=site, items_in_stock=0, name=name) optnames = [opt.value for opt in options] if not slug: slug = slugify(u'%s_%s' % (self.product.slug, u'_'.join(optnames))) while Product.objects.filter(slug=slug).count(): slug = u'_'.join((slug, unicode(self.product.id))) variant.slug = slug log.info("Creating variation for [%s] %s", self.product.slug, variant.slug) variant.save() pv = ProductVariation(product=variant, parent=self) pv.save() for option in options: pv.options.add(option) pv.name = name pv.sku = sku pv.save() else: variant = variations[0].product log.debug("Existing variant: %s", variant) dirty = False if name and name != variant.name: log.debug("Updating name: %s --> %s", self, name) variant.name = name dirty = True if sku and sku != variant.sku: variant.sku = sku log.debug("Updating sku: %s --> %s", self, sku) dirty = True if slug: # just in case slug = slugify(slug) if slug and slug != variant.slug: variant.slug = slug log.debug("Updating slug: %s --> %s", self, slug) dirty = True if dirty: log.debug("Changed existing variant, saving: %s", variant) variant.save() else: log.debug("No change to variant, skipping save: %s", variant) return variant