# products print('Creating products...') for i in range(1, 25): p = cmod.BulkProduct() p.name = 'unset' # see image adding below p.description = 'unset' p.category = random.choice(categories) p.status = 'A' p.price = random.uniform(1, 1000) p.reorder_trigger = random.randint(5, 15) p.quantity = random.randint(p.reorder_trigger, 2 * p.reorder_trigger) p.reorder_quantity = p.reorder_trigger * 2 p.save() for i in range(1, 25): p = cmod.RentalProduct() p.name = 'unset' # see image adding below p.description = 'unset' p.category = random.choice(categories) p.status = 'A' p.price = random.uniform(1, 1000) p.pid = ''.join([ random.choice( 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789012345678901234567890123456789' ) for j in range(6) ]) p.max_rental_days = random.randint(1, 10) p.save() for i in range(1, 25): p = cmod.IndividualProduct() p.name = 'unset' # see image adding below
def commit(self): '''Process form action''' type = self.cleaned_data.get('type') # if type == 'BulkProduct': # self.product = cmod.BulkProduct() # # unique # self.product.quantity = self.cleaned_data.get('quantity') # self.product.reorder_trigger = self.cleaned_data.get('reorder_trigger') # self.product.reorder_quantity = self.cleaned_data.get('reorder_quantity') # elif type == 'IndividualProduct': # self.product = cmod.IndividualProduct() # # unique # self.product.pid = self.cleaned_data.get('pid') # elif type == 'RentalProduct': # self.product = cmod.RentalProduct() # # unique # self.product.pid = self.cleaned_data.get('pid') # self.product.max_rental_days = self.cleaned_data.get('max_rental_days') # self.product.retire_date = self.cleaned_data.get('retire_date') # # # Generic product information # self.product.create_date = self.cleaned_data.get('created_date') # self.product.last_modified = self.cleaned_data.get('last_modified') # self.product.name = self.cleaned_data.get('name') # self.product.description = self.cleaned_data.get('description') # self.product.status = self.cleaned_data.get('status') # self.product.category = self.cleaned_data.get('category') # self.product.price = self.cleaned_data.get('price') # Save # self.product.save() # Try this way? if type == 'IndividualProduct': self.product = cmod.IndividualProduct() self.product.create_date = self.cleaned_data.get('create_date') self.product.last_modified = self.cleaned_data.get('last_modified') self.product.status = self.cleaned_data.get('status') self.product.name = self.cleaned_data.get('name') self.product.description = self.cleaned_data.get('description') self.product.category = self.cleaned_data.get('category') self.product.price = self.cleaned_data.get('price') # unique fields self.product.pid = self.cleaned_data.get('pid') elif type == 'BulkProduct': self.product = cmod.BulkProduct() self.product.create_date = self.cleaned_data.get('create_date') self.product.last_modified = self.cleaned_data.get('last_modified') self.product.status = self.cleaned_data.get('status') self.product.name = self.cleaned_data.get('name') self.product.description = self.cleaned_data.get('description') self.product.category = self.cleaned_data.get('category') self.product.price = self.cleaned_data.get('price') # unique fields self.product.quantity = self.cleaned_data.get('quantity') self.product.reorder_trigger = self.cleaned_data.get('reorder_trigger') self.product.reorder_quantity = self.cleaned_data.get('reorder_quantity') elif type == 'RentalProduct': self.product = cmod.RentalProduct() self.product.create_date = self.cleaned_data.get('create_date') self.product.last_modified = self.cleaned_data.get('last_modified') self.product.status = self.cleaned_data.get('status') self.product.name = self.cleaned_data.get('name') self.product.description = self.cleaned_data.get('description') self.product.category = self.cleaned_data.get('category') self.product.price = self.cleaned_data.get('price') # unique fields self.product.pid = self.cleaned_data.get('pid') self.product.max_rental_days = self.cleaned_data.get('max_rental_days') self.product.retire_date = self.cleaned_data.get('retire_date') self.product.save()
p6.reorder_trigger = 10 p6.reorder_quantity = 30 dList = [ 'Best dang drumsticks on this earth', 'Never touched by Obama', 'Very conservative, unlike most drumsticks' ] p6.descriptionList = json.dumps(dList) iList = [ '/static/homepage/media/img/drum1.png', '/static/homepage/media/img/drum3.jpg' ] p6.imgList = json.dumps(iList) p6.save() # Create a Rental Product p7 = cmod.RentalProduct() p7.product = p7 p7.serial_number = '1357asdf' p7.name = 'Trumpet' p7.category = cat1 p7.price = Decimal('12.99') dList = [ 'Rent this Brass', 'My Brass is better than your rental brass', 'Trump Trumps the Trumpet' ] p7.descriptionList = json.dumps(dList) iList = [ '/static/homepage/media/img/trump1.jpg', '/static/homepage/media/img/trump2.jpg', '/static/homepage/media/img/trump3.png', '/static/homepage/media/img/trump4.png'
def edit(request): try: p = cmod.Product.objects.get(id=int(request.urlparams[0])) except cmod.Product.DoesNotExist as e: return HttpResponseRedirect('/catalog/products') if request.method != "POST": if request.urlparams[1] == 'indproduct': form = edit_form( initial={ 'name': p.name, 'description': p.description, 'replacementValue': p.replacementValue, 'photoFileName': p.photoFileName, 'size': p.size, 'weight': p.weight, 'creationDate': p.creationDate, 'customizationNotes': p.customizationNotes, }) ptype = 'indproduct' elif request.urlparams[1] == 'bulkproduct': form = edit_form( initial={ 'name': p.name, 'description': p.description, 'replacementValue': p.replacementValue, 'photoFileName': p.photoFileName, 'size': p.size, 'weight': p.weight, 'currentBulkPrice': p.currentBulkPrice, 'quantityAvailable': p.quantityAvailable, }) ptype = 'bulkproduct' elif request.urlparams[1] == 'rentalproduct': form = edit_form( initial={ 'name': p.name, 'description': p.description, 'replacementValue': p.replacementValue, 'photoFileName': p.photoFileName, 'size': p.size, 'weight': p.weight, 'currentRentalRate': p.currentRentalRate, 'available': p.available, }) ptype = 'rentalproduct' if request.method == "POST": form = edit_form(request.POST) if form.is_valid(): if form.cleaned_data.get('productType') == 'indproduct': p = cmod.IndividualProduct() p.creationDate = form.cleaned_data.get('creationDate') p.customizationNotes = form.cleaned_data.get( 'customizationNotes') elif form.cleaned_data.get('productType') == 'bulkproduct': p = cmod.BulkProduct() p.currentBulkPrice = form.cleaned_data.get('currentBulkPrice') p.quantityAvailable = form.cleaned_data.get( 'quantityAvailable') elif form.cleaned_data.get('productType') == 'rentalproduct': p = cmod.RentalProduct() p.currentRentalRate = form.cleaned_data.get( 'currentRentalRate') p.available = form.cleaned_data.get('available') p.name = form.cleaned_data.get('name') p.description = form.cleaned_data.get('description') p.replacementValue = form.cleaned_data.get('replacementValue') p.photoFileName = form.cleaned_data.get('photoFileName') p.size = form.cleaned_data.get('size') p.weight = form.cleaned_data.get('weight') p.save() return HttpResponseRedirect('/catalog/products') # return HttpResponse(''' # <script> # window.location.reload(); # </script> # ''') #products = cmod.Product.objects.all().order_by('last_name','first_name') template_vars = { 'form': form, 'productid': p.id, 'ptype': ptype, } return dmp_render_to_response(request, 'editproduct.html', template_vars)