1. Roman Ruban
  2. satchmo-ru

Source

satchmo-ru / satchmo / apps / product / forms.py

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
try:
    from cStringIO import StringIO
except ImportError:
    from StringIO import StringIO

from django import forms
from django.conf import settings
from django.contrib.sites.models import Site
from django.core import serializers, urlresolvers
from django.core.management.base import CommandError
from django.core.management.color import no_style
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.db import transaction
from django.http import HttpResponse
from django.utils.translation import ugettext as _
from livesettings import config_value
from product.models import Product, Price, Option, ProductPriceLookup
from satchmo_utils.unique_id import slugify
import logging
import os
import time
import zipfile

log = logging.getLogger('product.forms')

def export_choices():
    fmts = serializers.get_serializer_formats()
    return zip(fmts,fmts)

class InventoryForm(forms.Form):

    def __init__(self, *args, **kwargs):
        products = kwargs.pop('products', None)
        page = kwargs.pop('page', 1)

        super(InventoryForm, self).__init__(*args, **kwargs)

        if not products:
            products = Product.objects.by_site().order_by('slug')

            paginator = Paginator(products, 25)
            try:
                products = paginator.page(page)
            except (EmptyPage, InvalidPage):
                products = paginator.page(paginator.num_pages)

            self.pages = products
            products = products.object_list

        for product in products:
            subtypes = product.get_subtypes()
            qtyclasses = ('text', 'qty') + subtypes
            qtyclasses = " ".join(qtyclasses)

            kw = {
            'label' : product.slug,
            'help_text' : product.name,
            'initial' : product.items_in_stock,
            'widget' : forms.TextInput(attrs={'class': qtyclasses}) }

            qty = forms.DecimalField(**kw)
            self.fields['qty__%s' % product.slug] = qty
            qty.slug = product.slug
            qty.product_id = product.id
            qty.subtypes = " ".join(subtypes)

            if 'CustomProduct' in subtypes:
                initial_price = product.customproduct.full_price
            else:
                initial_price = product.unit_price

            kw['initial'] = initial_price
            kw['required'] = False
            kw['widget'] = forms.TextInput(attrs={'class': "text price"})
            price = forms.DecimalField(**kw)
            price.slug = product.slug
            self.fields['price__%s' % product.slug] = price

            kw['initial'] = product.active
            kw['widget'] = forms.CheckboxInput(attrs={'class': "checkbox active"})
            active = forms.BooleanField(**kw)
            active.slug = product.slug
            self.fields['active__%s' % product.slug] = active

            kw['initial'] = product.featured
            kw['widget'] = forms.CheckboxInput(attrs={'class': "checkbox featured"})
            featured = forms.BooleanField(**kw)
            featured.slug = product.slug
            self.fields['featured__%s' % product.slug] = featured

    def save(self, request):
        self.full_clean()
        for name, value in self.cleaned_data.items():
            opt, key = name.split('__')

            prod = Product.objects.get(slug__exact=key)
            subtypes = prod.get_subtypes()

            if opt=='qty':
                if value != prod.items_in_stock:
                    request.user.message_set.create(message='Updated %s stock to %s' % (key, value))
                    log.debug('Saving new qty=%d for %s' % (value, key))
                    prod.items_in_stock = value
                    prod.save()

            elif opt=='price':
                if 'CustomProduct' in subtypes:
                    full_price = prod.customproduct.full_price
                else:
                    full_price = prod.unit_price

                if value != full_price:
                    request.user.message_set.create(message='Updated %s unit price to %s' % (key, value))
                    log.debug('Saving new price %s for %s' % (value, key))
                    try:
                        price = Price.objects.get(product=prod, quantity='1')
                    except Price.DoesNotExist:
                        price = Price(product=prod, quantity='1')
                    price.price = value
                    price.save()

            elif opt=="active":
                if value != prod.active:
                    if value:
                        note = "Activated %s"
                    else:
                        note = "Deactivated %s"
                    request.user.message_set.create(message=note % (key))

                    prod.active = value
                    prod.save()

            elif opt=="featured":
                if value != prod.featured:
                    if value:
                        note = "%s is now featured"
                    else:
                        note = "%s is no longer featured"
                    request.user.message_set.create(message=note % (key))

                    prod.featured = value
                    prod.save()

class ProductExportForm(forms.Form):

    def __init__(self, *args, **kwargs):
        products = kwargs.pop('products', None)

        super(ProductExportForm, self).__init__(*args, **kwargs)

        self.fields['format'] = forms.ChoiceField(label=_('export format'), choices=export_choices(), required=True)
        self.fields['include_images'] = forms.BooleanField(label=_('Include Images'), initial=True, required=False)
        self.fields['include_categories'] = forms.BooleanField(label=_('Include Categories'), initial=True, required=False)

        if not products:
            products = Product.objects.by_site().order_by('slug')

        for product in products:
            subtypes = product.get_subtypes()
            expclasses = ('export', ) + subtypes
            extclasses = " ".join(expclasses)

            kw = {
            'label' : product.slug,
            'help_text' : product.name,
            'initial' : False,
            'required' : False,
            'widget' : forms.CheckboxInput(attrs={'class': extclasses}) }

            chk = forms.BooleanField(**kw)
            chk.slug = product.slug
            chk.product_id = product.id
            chk.subtypes = " ".join(subtypes)
            self.fields['export__%s' % product.slug] = chk

    def export(self, request):
        self.full_clean()
        format = 'yaml'
        selected = []
        include_images = False
        include_categories = False

        for name, value in self.cleaned_data.items():
            if name == 'format':
                format = value
                continue

            if name == 'include_images':
                include_images = value
                continue

            if name == 'include_categories':
                include_categories = value
                continue

            opt, key = name.split('__')

            if opt=='export':
                if value:
                    selected.append(key)

        try:
            serializers.get_serializer(format)
        except KeyError:
            raise CommandError("Unknown serialization format: %s" % format)

        objects = []
        images = []
        categories = {}
        for slug in selected:
            product = Product.objects.get(slug=slug)
            objects.append(product)
            for subtype in product.get_subtypes():
                objects.append(getattr(product,subtype.lower()))
            objects.extend(list(product.price_set.all()))
            objects.extend(list(product.productimage_set.all()))
            if include_images:
                for image in product.productimage_set.all():
                    images.append(image.picture)
            if include_categories:
                for category in product.category.all():
                    if category not in categories:
                        categories[category] = 1

        # Export all categories, translations.  Export images,translations if
        # desired.
        if include_categories:
            for category in categories.keys():
                objects.append(category)
                objects.extend(list(category.translations.all()))
                if include_images:
                    for image in category.images.all():
                        objects.append(image)
                        objects.extend(list(image.translations.all()))

        try:
            raw = serializers.serialize(format, objects, indent=False)
        except Exception, e:
            raise CommandError("Unable to serialize database: %s" % e)

        if include_images:
            filedir = settings.MEDIA_ROOT
            buf = StringIO()
            zf = zipfile.ZipFile(buf, 'a', zipfile.ZIP_STORED)

            export_file = 'products.%s' % format
            zf.writestr(str(export_file), raw)

            zinfo = zf.getinfo(str(export_file))
            # Caution, highly magic number, chmods the file to 644
            zinfo.external_attr = 2175008768L

            image_dir = config_value('PRODUCT', 'IMAGE_DIR')
            config = "PRODUCT.IMAGE_DIR=%s\nEXPORT_FILE=%s" % (image_dir, export_file)
            zf.writestr('VARS', config)

            zinfo = zf.getinfo('VARS')
            # Caution, highly magic number, chmods the file to 644
            zinfo.external_attr = 2175008768L

            for image in images:
                f = os.path.join(filedir, image)
                if os.path.exists(f):
                    zf.write(f, str(image))

            zf.close()

            raw = buf.getvalue()
            mimetype = "application/zip"
            format = "zip"
        else:
            mimetype = "text/" + format

        response = HttpResponse(mimetype=mimetype, content=raw)
        response['Content-Disposition'] = 'attachment; filename="products-%s.%s"' % (time.strftime('%Y%m%d-%H%M'), format)

        return response


class ProductImportForm(forms.Form):

    def __init__(self, *args, **kwargs):
        super(ProductImportForm, self).__init__(*args, **kwargs)

        self.fields['upload'] = forms.Field(label=_("File to import"), widget=forms.FileInput, required=False)

    def import_from(self, infile, maxsize=10000000):
        errors = []
        results = []

        filetype = infile.content_type
        filename = infile.name
        raw = infile.read()

        # filelen = len(raw)
        # if filelen > maxsize:
        #     errors.append(_('Import too large, must be smaller than %i bytes.' % maxsize ))

        format = os.path.splitext(filename)[1]
        if format and format.startswith('.'):
            format = format[1:]
        if not format:
            errors.append(_('Could not parse format from filename: %s') % filename)

        if format == 'zip':
            zf = zipfile.ZipFile(StringIO(raw), 'r')
            files = zf.namelist()
            image_dir = config_value('PRODUCT', 'IMAGE_DIR')
            other_image_dir = None
            export_file = None
            if 'VARS' in files:
                config = zf.read('VARS')
                lines = [line.split('=') for line in config.split('\n')]
                for key, val in lines:
                    if key == 'PRODUCT.IMAGE_DIR':
                        other_image_dir = val
                    elif key == 'EXPORT_FILE':
                        export_file = val

                if other_image_dir is None or export_file is None:
                    errors.append(_('Bad VARS file in import zipfile.'))

                else:
                    # save out all the files which start with other_image_dr
                    rename = image_dir == other_image_dir
                    for f in files:
                        if f.startswith(other_image_dir):
                            buf = zf.read(f)
                            if rename:
                                f = f[len(other_image_dir):]
                                if f[0] in ('/', '\\'):
                                    f = f[1:]
                                f = os.path.join(settings.MEDIA_ROOT, image_dir, f)
                            outf = open(f, 'w')
                            outf.write(buf)
                            outf.close()
                            results.append('Imported image: %s' % f)

                    infile = zf.read(export_file)
                    zf.close()

                    format = os.path.splitext(export_file)[1]
                    if format and format.startswith('.'):
                        format = format[1:]
                    if not format:
                        errors.append(_('Could not parse format from filename: %s') % filename)
                    else:
                        raw = infile

            else:
                errors.append(_('Missing VARS in import zipfile.'))

        else:
            raw = StringIO(str(raw))

        if not format in serializers.get_serializer_formats():
            errors.append(_('Unknown file format: %s') % format)

        if not errors:

            from django.db import connection, transaction

            transaction.commit_unless_managed()
            transaction.enter_transaction_management()
            transaction.managed(True)

            try:

                ct = 0
                models = set()

                for obj in serializers.deserialize(format, raw):
                    obj.save()
                    models.add(obj.object.__class__)
                    ct += 1
                if ct>0:
                    style=no_style()
                    sequence_sql = connection.ops.sequence_reset_sql(style, models)
                    if sequence_sql:
                        cursor = connection.cursor()
                        for line in sequence_sql:
                            cursor.execute(line)

                results.append(_('Added %(count)i objects from %(filename)s') % {'count': ct, 'filename': filename})
                transaction.commit()
                #label_found = True
            except Exception, e:
                #fixture.close()
                errors.append(_("Problem installing fixture '%(filename)s': %(error_msg)s\n") % {'filename': filename, 'error_msg': str(e)})
                errors.append("Raw: %s" % raw)
                transaction.rollback()
                transaction.leave_transaction_management()
            else:
                transaction.leave_transaction_management()

        return results, errors


class VariationManagerForm(forms.Form):
    dirty = forms.CharField(widget=forms.HiddenInput(), required=False)

    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, request):
        self.full_clean()
        data = self.cleaned_data
        optiondict = _get_optiondict()

        #dirty is a comma delimited list of groupid__optionid strings
        dirty = self.cleaned_data['dirty'].split(',')
        if dirty:
            for key in dirty:
                # this is the keep/create checkbox field
                try:
                    keep = data["pv__" + key]
                    opts = _get_options_for_key(key, optiondict)
                    if opts:
                        if keep:
                            self._create_variation(opts, key, data, request)
                        else:
                            self._delete_variation(opts, request)
                except KeyError:
                    pass
            ProductPriceLookup.objects.smart_create_for_product(self.product)

    save = transaction.commit_on_success(_save)

    def _create_variation(self, opts, key, data, request):
        namekey = "name__" + key
        nameval = data[namekey]
        skukey = "sku__" + key
        skuval = data[skukey]
        slugkey = "slug__" + key
        slugval = data[slugkey]
        log.debug("Got name=%s, sku=%s, slug=%s", nameval, skuval, slugval)
        v = self.product.configurableproduct.create_variation(
            opts,
            name=nameval,
            sku=skuval,
            slug=slugval)
        log.info('Updated variation %s', v)
        request.user.message_set.create(message='Created %s' % v)
        return v

    def _delete_variation(self, opts, request):
        variation = self.product.configurableproduct.get_product_from_options(opts)
        if variation:
#            log.info("Deleting variation for [%s] %s", self.product.slug, opts)
            request.user.message_set.create(message='Deleted %s' % variation)
            variation.delete()

def _get_optiondict():
    site = Site.objects.get_current()
    opts = Option.objects.filter(option_group__site__id = site.id)
    d = {}
    for opt in opts:
        d.setdefault(opt.option_group.id, {})[opt.id] = opt
    return d

def _get_options_for_key(key, optiondict):
    ids = key.split('__')
    opts = []
    for work in ids:
        grpid, optid = work.split('_')
        try:
            opts.append(optiondict[int(grpid)][int(optid)])
        except KeyError:
            log.warn('Could not find option for group id=%s, option id=%s', grpid, optid)
    return opts