Buying Many Products At Once From A Webshop
Solution 1:
Man, this is a really strange code. If you will want to add new items in you shop you must rewrite you shop's script. At the first unlink your items from interface, you must send POST request to controller with your items ids and quantity, i don know how work gae request object, but it must be like that: from your order page make POST request with dict of items which really need {"item_id":"qnt"}. When in the controller you can fetch all objects like:
foritem, qnt in request.POST:
{do something with each item, forexamplewhere you can sum total}
and etc Don't link controllers with your interfaces directly. You must write more abstraction code, if you want make really flexible app.
Solution 2:
I'm going to try to focus on one very obvious problem with your code, but there are lots of problems with it that I'm not going to get into. My advice is to stop right now. You're implementing a web-based payment system. You really should leave that to people with more skills and experience. "Web-based" is a pretty difficult thing to get right whilst ensuring security, but an online payment system is the sort of thing that well-paid consultants with decades of experience are well-paid for, and they still manage to get it wrong pretty often. You're opening yourself up to a lot of legal liability.
If you're still dead set on it, please read The Python Tutorial cover to cover, possibly several times. Python is a very different language to whatever classical OOP language you're mentally cramming into it. After that, at least leaf through the other documentation. If you're having trouble with these, pick up an O'Reilly book on Python; approaching it from another angle should help. After you done all this (and maybe at the same time), write as much code as you can that is not going to get you sued into oblivion if you do it wrong. Then maybe you can write an order/payment system.
I'm sorry if this sounds harsh, but the world doesn't need any more shoddy web stores; 1999 took care of that for us.
Anyway, on to your code :D When you write something repetitive and copy-pasted like this:
items = [ self.request.get('items[1]'),self.request.get('items[2]'),self.request.get('items[3]'),self.request.get('items[4]'),self.request.get('items[5]'),self.request.get('items[6]'),self.request.get('items[7]'),self.request.get('items[8]') ]
You should be thinking to yourself, "Wait a second! Repetitive task are exactly what computers are designed to do." You could get your text editor to do it (see Vim Macros), but concise (but not too concise ;) code is always better than long code, since you make it faster to maintain, less prone to programmer error, and easier to debug, not to mention the amount of time you save not copying and pasting, so let's improve the code.
Here's how I would revise this in Python (advanced programmers do this in their heads, or just skip to the end):
#1. with a for loop
MAX_ITEMS = 8
items = []
for i inrange(MAX_ITEMS):
items.append(self.request.get('items[{}]'.format(i + 1))
#2. with a list comprehension
MAX_ITEMS = 8
items = [self.request.get('items[{}]'.format(i + 1)) for i inrange(MAX_ITEMS)]
Actually, having a limit to the number of items is rather amateurish and will only frustrate your users. You can fix it like this:
items = []
i = 0whileTrue:
try:
items.append(self.request[i + 1]) #attempt to get the next itemexcept IndexError as exc: #but if it fails...break#we must be at the last one
i += 1
I think this is the way you should leave it for now because it's clear but not repetitive. However, you could shorten it even further using functions from the itertools
module.
A few quick tips:
- Avoid string concatenation, especially where user-supplied strings and especially especially when user-supplied string from over the web are concerned. Use str.format and
"%d" % (5,)
modulus string formatting. BONUS: You don't have to convert everything to strings! - Get those constants (e.g.,
ExtraCost = 2
) out of the middle and put them somewhere safe (at the top of the module, or in a special file in the package) - You trust the user way too much: At
for item in self.request.POST:
, you're assuming everything in the request is going to be an item, and you do zero validation. - Please, please, please. Never turn off autocomplete. I really don't know why that attribute exists, except to annoy.
Post a Comment for "Buying Many Products At Once From A Webshop"