def address(request): """ On GET- returns form to create an address. On POST- creates new address for current user. """ try: if request.method == 'GET': return HttpResponse(render(request, "account/address/address.html", {'form': NewAddressForm}), status=200) elif request.method == 'POST': addressData = request.POST newAddress = Address(addressLine1=addressData['addressLine1'], addressLine2=addressData['addressLine2'], city=addressData['city'], state=addressData['state'], postalCode=addressData['postalCode'], country=addressData['country']) newAddress.save() currCustomer = Customer.objects.get(user=request.user) currCustomer.customerAddress.add(newAddress) return HttpResponse('Address created successfully', status=status.HTTP_200_OK) except DatabaseError: return HttpResponse(DatabaseErrorMessage, status=status.HTTP_400_BAD_REQUEST) except Exception: return HttpResponse(BadRequestMessage, status=status.HTTP_400_BAD_REQUEST)
def post(self,request,format=None): serializer=self.serializer_class(data=request.data,) if serializer.is_valid(): shipping_address = serializer.validated_data['shipping_address'] state = serializer.validated_data['state'] country = serializer.validated_data['country'] zip_code = serializer.validated_data['zip_code'] user_phone_number=serializer.validated_data['user_phone_number'] user=User.objects.filter(phone_number=user_phone_number)[0] newadd=shipping_address+'\n'+state+'\n'+country+'\n'+zip_code obj=Address.objects.filter(user=user,address=newadd) if obj.count()<=0: address=Address(user=user,address=newadd) address.save() return Response({'status':'address saved','success':"True"}) else: return Response({'status':'address already exists','success':'False'}) else: return Response({'status':'Invalid Format','success':'False'})
def add_address(request): response = {'response': '2'} r_platform = 'android' error_message = '' user = None buyer = None history_id = '' order_id = '' print(request.method) if request.method == 'POST': req = json.loads(request.body) print req r_platform = req['platform'] a_name = req['name'] a_phone = req['phone'] a_province = req['province'] a_city = req['city'] a_county = req['county'] a_zip = req['zip'] a_detail = req['detail'] buyer = Buyer.objects.filter(token=req['token']) if len(buyer) == 1: user = buyer[0] lenA = len(Address.objects.filter(buyer=user)) address = Address(name=a_name, phone=a_phone, province=a_province, city=a_city, county=a_county, zip=a_zip, detail=a_detail, is_del=0, is_default=(1 if lenA == 0 else 0), buyer=user) address.save() response['response'] = 1 print response else: response['response'] = 2 error_message = 'no this user' response['error_msg'] = error_message if r_platform == 'web': return HttpResponseRedirect('/info') elif r_platform == 'android': json.dumps(response) j = json.dumps(response) return HttpResponse(j)
def checkout(): if session['role'] == 'customer': form = AddressForm() cart = Cart.query.filter_by(user_id=current_user.id).all() address = Address.query.filter_by(user_id=current_user.id).first() total = 0 for item in cart: total += item.product.price * item.quantity if form.validate_on_submit(): if address is None: address = Address(addressLine1=form.addressLine1.data, addressLine2=form.addressLine2.data, pincode=form.pincode.data, city=form.city.data, state=form.state.data, mobile=form.mobile.data, customer=current_user) db.session.add(address) db.session.commit() for item in cart: order = Order(product=item.product, user_id=current_user.id, seller_id=item.product.seller_id, quantity=item.quantity, total=total) db.session.add(order) db.session.commit() for item in cart: product = Product.query.filter_by(id=item.product.id).first() product.quantity -= item.quantity db.session.commit() for item in cart: Cart.query.filter_by(id=item.id).delete() db.session.commit() flash('Order Placed successfully', 'check') return redirect(url_for('orders')) if request.method == 'GET' and address is not None: form.addressLine1.data = address.addressLine1 form.addressLine2.data = address.addressLine2 form.pincode.data = address.pincode form.city.data = address.city form.state.data = address.state form.mobile.data = address.mobile return render_template('checkout.html', form=form, total=total, cart_items=cart) else: flash('Only customer can access this page', 'warning') return redirect(url_for('seller_dashboard'))
def addClient(request): if not request.user.is_authenticated(): return HttpResponseRedirect("/") if request.method == 'POST': # If the form has been submitted... form = ClientForm(request.POST) # A form bound to the POST data if form.is_valid(): # All validation rules pass # Process the data in form.cleaned_data firstName = form.cleaned_data['firstName'] middleName = form.cleaned_data['middleName'] lastName = form.cleaned_data['lastName'] dateOfBirth = form.cleaned_data['dateOfBirth'] street = form.cleaned_data['street'] city = form.cleaned_data['city'] stateID = form.cleaned_data['state'] zipCode = form.cleaned_data['zipCode'] phone = form.cleaned_data['phone'] email = form.cleaned_data['email'] clientAddress = Address() clientAddress.street = street clientAddress.city = city theState = State.objects.filter(id=stateID) if len(theState)==1: theState = theState[0] clientAddress.state = theState clientAddress.zipCode = zipCode clientAddress.save() aClient = Client() aClient.firstName = firstName aClient.middleName = middleName aClient.lastName = lastName aClient.dateOfBirth = dateOfBirth aClient.address = clientAddress aClient.phone = phone aClient.email = email aClient.save() return HttpResponseRedirect('/clients/') # Redirect after POST else: form = ClientForm() # An unbound form context = { 'form': form, } return render(request, 'addClient.html', context)
def create(self, validated_data): address = Address(**validated_data) address.save() return address
def handle(self, *args, **options): restaurant_count = options["count"] # keep track of used names, streets etc. in the variables below, # so we can prevent duplicates names = [] streets = [] cities = [] # XXX factorize into smaller functions for item in range(restaurant_count): print "generate restaurant %d" % (item + 1) r = Restaurant() r.name = gen_unique_name(names, restaurant_names) r.save() print "generate address" a = Address(country=random.choice(_countries)) a.street = gen_unique_name(streets, street_names) a.street_no = random.randint(1, 100) a.zip = random.randint(1000, 100000) a.city = gen_unique_name(cities, city_names) a.restaurant = r a.save() count = random.randint(5, 25) print "generate %d tables" % count for i in range(1, 1 + count): t = Table() t.restaurant = r t.no = i t.save() print "add supported payment methods" for _ in range(2, len(_payment_methods)): method = random.choice(_payment_methods) if method not in r.payment_method.all(): r.payment_method.add(method) r.save() print "generate menu" m = Menu() m.currency = random.choice(_currencies) m.language = _languages[2] m.restaurant = r m.save() m.translations.add(_languages[0]) m.translations.add(_languages[1]) m.save() items = [] _cat_names = [] count = random.randint(3, 12) itemid = 1 print "generate %d categories" % count for j in range(1, 1 + count): c = Category() c.menu = m c.ordering = j c.view = random.choice(_views) c.save() count = random.randint(4, 20) # i18n stuff name = gen_unique_name(_cat_names, category_names) for lang in _languages: c18n = CategoryI18n() c18n.category = c c18n.language = lang c18n.name = name if lang.abbr != "en": c18n.name += "(%s)" % lang.abbr c18n.save() print "generate %d items" % count for _ in range(1, count): item = Item() item.no = itemid itemid += 1 item.category = c item.price = "%.2f" % random.uniform( 1, 110) # 2 decimal points item.save() item.image.save("item%d.jpg" % item.id, random.choice(_pictures), save=True) count = random.randint(0, 8) # "assign %d allergens" % count for k in range(count): a = random.choice(_allergens).allergen if a in ItemAllergen.objects.filter(item=item, allergen=a): continue traces = bool(random.randint(0, 1)) ItemAllergen.objects.get_or_create( item=item, allergen=a, traces=traces)[0].save() item.save() # i18n stuff name = gen_unique_name(items, food_names) for lang in _languages: i18n = ItemI18n() i18n.item = item i18n.language = lang i18n.name = name if lang.abbr != "en": i18n.name += "(%s)" % lang.abbr i18n.description = "Very delicious %s. It is made with love and all the care it is needed to make a great %s." % ( name, name) i18n.save() print self.stdout.write('Successfully added %d restaurants' % restaurant_count)
def post(self, request, format=None): serializer = self.serializer_class( data=request.data, ) if serializer.is_valid(): user_phone_number = serializer.validated_data['user_phone_number'] restaurant_name=serializer.validated_data['restaurant'] phone_number=serializer.validated_data['phone_number'] item_jsons=serializer.validated_data['item_jsons'] name = serializer.validated_data['name'] email = serializer.validated_data['email'] shipping_address = serializer.validated_data['shipping_address'] billing_address = serializer.validated_data['billing_address'] state = serializer.validated_data['state'] country = serializer.validated_data['country'] zip_code = serializer.validated_data['zip_code'] total_price=serializer.validated_data['total_price'] paymentStatus=serializer.validated_data['paymentStatus'] how=serializer.validated_data['how'] special_instruction=serializer.validated_data['special_instruction'] schedule_time=serializer.validated_data['schedule_time'] schedule_date=serializer.validated_data['schedule_date'] try: user=User.objects.filter(phone_number=user_phone_number)[0] except: user=None response_data={} if user: try: restaurant=Restaurant.objects.filter(name=restaurant_name)[0] except: restaurant=None if restaurant: if how=='d': how='delivery' elif how=='t': how='takeaway' elif how=='sd': how='schedule-delivery' elif how=='st': how='schedule-takeaway' if how=="delivery" or how=="takeaway": today=date.today() schedule_date=today.strftime("%Y-%m-%d") now = datetime.now() schedule_time=now.strftime("%H:%M:%S") if how=='delivery' or how=='schedule-delivery': order=Order(user=user,restaurant=restaurant,item_jsons=item_jsons,name=name,phone_number=phone_number,email=email, shipping_address=shipping_address,billing_address=billing_address,state=state,country=country,zip_code=zip_code, total_price=total_price,payment_mode=paymentStatus,how=how,special_instruction=special_instruction,schedule_time=schedule_time, schedule_date=schedule_date) else: order=Order(user=user,restaurant=restaurant,item_jsons=item_jsons,name=name,phone_number=phone_number,email=email, total_price=total_price,payment_mode=paymentStatus,how=how,special_instruction=special_instruction,schedule_time=schedule_time, schedule_date=schedule_date) order.save() id=order.order_id if paymentStatus=="COD": order.order_status='pending' order.payment_mode='COD' order.payment_status=False order.save() if how=='delivery' or how=='schedule-delivery': newadd=shipping_address+'\n'+state+'\n'+country+'\n'+zip_code obj=Address.objects.filter(user=user,address=newadd) if obj.count()<=0: address=Address(user=user,address=newadd) address.save() response={'order_id':id,'status':'success','checkSum':""} return Response(response) else: order.save() if how=='delivery' or how=='schedule-delivery': newadd=shipping_address+'\n'+state+'\n'+country+'\n'+zip_code obj=Address.objects.filter(user=user,address=newadd) if obj.count()<=0: address=Address(user=user,address=newadd) address.save() id=order.order_id data_dict = { 'MID':'aVrRqW70498541104158', 'ORDER_ID':str(order.order_id), 'TXN_AMOUNT':str(order.total_price), 'CUST_ID':str(email), 'INDUSTRY_TYPE_ID':'Retail', 'WEBSITE':'WEBSTAGING', 'CHANNEL_ID':'WAP', 'CALLBACK_URL':'https://securegw-stage.paytm.in/theia/paytmCallback?ORDER_ID='+str(order.order_id), } data_dict['CHECKSUMHASH']=Checksum.generate_checksum(data_dict,MERCHANT_KEY) print(data_dict['CHECKSUMHASH']) response={'order_id':id,'status':'success','checkSum':data_dict['CHECKSUMHASH']} return Response(response) else: response_data={'status':'restaurant not found'} return Response(response_data) else: response_data={'status':'user not found'} return Response(response_data) else: return Response({'status':"Fail"})