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
| import time,uuid,datetime from django.db import transaction from django.http import JsonResponse, HttpResponse from django.shortcuts import render, redirect from user.models import TCar, TBook, TUser, TOrder, TAddress, TOrderItem
class Book: def __init__(self, id, count): book = TBook.objects.get(pk=id) self.id = id self.count = count self.new_price = book.new_price self.picture = book.picture self.book_name = book.book_name def totalprice(self): return round(float(self.new_price) * float(self.count), 2) class Car: def __init__(self): self.book_list = [] self.index = 0 def add_book(self, id, count=1): book = self.get_book(id) if book: book.count = int(book.count) + int(count) else: book = Book(id=id, count=count) self.book_list.append(book) def get_book(self, id): for book in self.book_list: if book.id == id: return book def remove_book(self, id): book = self.get_book(id) self.book_list.remove(book) def __iter__(self): self.index = 0 return self def __next__(self): if self.index < len(self.book_list): item = self.book_list[self.index] self.index += 1 return item else: raise StopIteration def __len__(self): return len(self.book_list) def rem_book(request): book_id = request.GET.get("id") car = request.session.get('car') user_name = request.COOKIES.get('txtUsername') user_pwd = request.COOKIES.get('txtPassword') req = TUser.objects.filter(user_name=user_name, user_pwd=user_pwd) if req: id = TUser.objects.get(user_name=user_name) try: with transaction.atomic(): car = TCar.objects.filter(user_id=id) book_item = TCar.objects.get(book_id=book_id) print(book_item) book_item.delete() sum_price = 0 for i in car: sum_price += round(float(i.book.new_price) * float(i.count), 2) return JsonResponse({"error": 0, "msg": "删除成功", "sum_price": sum_price}) return JsonResponse({"error": 1, "msg": "删除失败"}) except Exception as e: print(e) return JsonResponse({"error": 1, "msg": "删除失败"}) else: if car: totalprice = 0 sum_price = 0 for i in car: totalprice = round(float(i.new_price) * float(i.count), 2) sum_price += round(float(i.new_price) * float(i.count), 2) with transaction.atomic(): car.remove_book(book_id) request.session['car'] = car return JsonResponse({"error": 0, "msg": "删除成功", "sum_price": sum_price, "totalprice": totalprice}) return JsonResponse({"error": 1, "msg": "删除失败"})
def add_car(request): book_id = request.GET.get("bookid") book_num = request.GET.get("booknum") book_num1 = request.GET.get("booknum1") user_name = request.COOKIES.get('txtUsername') user_pwd = request.COOKIES.get('txtPassword') car = request.session.get('car') req = TUser.objects.filter(user_name=user_name, user_pwd=user_pwd) sum_price = 0 totalprice = 0 if req: id = TUser.objects.get(user_name=user_name) car = TCar.objects.filter(user_id=id) if car: book_item = TCar.objects.filter(book_id=book_id) if book_item: if book_num1: book_item[0].count = int(book_num1) book_item[0].save() totalprice = book_item[0].totalprice() else: book_item[0].count += int(book_num) book_item[0].save() else: with transaction.atomic(): TCar.objects.create(book_id=book_id, count=book_num, user_id=id.id) else: with transaction.atomic(): TCar.objects.create(book_id=book_id, count=book_num, user_id=id.id) for i in car: sum_price += round(float(i.book.new_price) * float(i.count), 2) return JsonResponse({"msg": "添加购物车成功!快去看看吧!", "error": 0, "totalprice": totalprice, "sum_price": sum_price}) else: if car: pass else: car = Car() car.add_book(book_id, int(book_num)) request.session['car'] = car sum_price = 0 totalprice = 0 for book in car.book_list: if int(book.id) == int(book_id): totalprice = round(float(book.new_price) * float(book.count), 2) sum_price += round(float(book.new_price) * float(book.count), 2) return JsonResponse({"msg": "添加购物车成功!快去看看吧!", "error": 0, "sum_price": sum_price, "totalprice": totalprice})
def car(request): user_name = request.COOKIES.get('txtUsername') user_pwd = request.COOKIES.get('txtPassword') req = TUser.objects.filter(user_name=user_name, user_pwd=user_pwd) car = request.session.get('car') if req: request.session['login'] = 'ok' try: with transaction.atomic(): id = TUser.objects.get(user_name=user_name) if car: for i in car: book_item = TCar.objects.filter(book_id=i.id) if book_item: book_item[0].count += int(i.count) book_item[0].save() print(i.id, i.count) else: with transaction.atomic(): TCar.objects.create(book_id=i.id, count=i.count, user_id=id.id) request.session.flush() else: with transaction.atomic(): car = TCar.objects.filter(user_id=id) sum_price = 0 for i in car: sum_price += round(float(i.book.new_price) * float(i.count), 2) return render(request, 'car/car.html', {"car": car, "sum_price": sum_price, "user_name": user_name}) except Exception as e: print(e) else: if car: totalprice = 0 sum_price = 0 for i in car: totalprice = round(float(i.new_price) * float(i.count), 2) sum_price += round(float(i.new_price) * float(i.count), 2) return render(request, 'car/car.html', {"car": car, "totalprice": totalprice, "sum_price": sum_price, }) return render(request, 'car/car.html')
def order(request): user_name = request.COOKIES.get('txtUsername') user_pwd = request.COOKIES.get('txtPassword') req = TUser.objects.filter(user_name=user_name, user_pwd=user_pwd) sum_price = 0 if req: id = TUser.objects.get(user_name=user_name) car = TCar.objects.filter(user_id=id) with transaction.atomic(): address = TAddress.objects.filter(user_id=id) for i in car: sum_price += round(float(i.book.new_price) * float(i.count), 2) return render(request, "car/indent.html", {"address": address, "car": car, "sum_price": sum_price, "user_name": user_name}) return redirect("user:login")
def order_ok(request): user_name = request.COOKIES.get('txtUsername') id = TUser.objects.get(user_name=user_name) car = TCar.objects.filter(user_id=id) sum_price = 0 address = request.GET.get("address") address_id = TAddress.objects.filter(address=address)[0] order_id = str(time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))) + str(time.time()).replace('.', '')[-7:] create_time = datetime.datetime.now().strftime('%Y-%m-%d') for i in car: sum_price += round(float(i.book.new_price) * float(i.count), 2) with transaction.atomic(): order = TOrder.objects.create(order_id=order_id, create_time=create_time, price=sum_price, address_id=address_id.id, user_id=id.id)
count = 0 for i in car: with transaction.atomic(): order_item = TOrderItem.objects.create(count=i.count, book_id=i.id, order_id=order.id) count += i.count i.delete() return render(request, 'car/indent ok.html', {"user_name": user_name, "order": order, "count": count, "order_item": order_item, "address": address_id})
def order_submit(request): user_name = request.COOKIES.get('txtUsername') id = TUser.objects.get(user_name=user_name) t_address = TAddress.objects.filter(user_id=id)
ship_man = request.POST.get("ship_man") address = request.POST.get("address") addr_mobile = request.POST.get("addr_mobile") cellphone = request.POST.get("cellphone") post = request.POST.get("post")
print(ship_man, address, addr_mobile, cellphone, post) if t_address[0].address == address: return JsonResponse({"msg": "地址已经存在", "error": 1}) else: TAddress.objects.create(address=address, name=ship_man, post_code=post, cellphone=cellphone, addr_mobile=addr_mobile, user_id=id.id) return JsonResponse({"msg": "订单生成", "error": 0})
|