def addcar(request): token = request.session.get('token') goodid = request.GET.get('goodid') number = request.GET.get('number') print(goodid) if token: user = User.objects.get(token=token) good = Goods.objects.get(pk=goodid) # cart = Car() # cart.user=user # cart.good=good # cart.number=int(number) # cart.save() carts = Car.objects.filter(user=user).filter(good=good) if carts.exists(): # 存在 cart = carts.first() cart.number = cart.number + int(number) cart.save() else: # 不存在 cart = Car() cart.user = user cart.good = good cart.number = int(number) cart.save() return JsonResponse({'status': 1}) else: data = {} data['msg'] = '请登录后操作!' data['status'] = -1 return JsonResponse(data)
def handle(self, *args, **options): with open(os.path.join(settings.BASE_DIR, 'app.json'), encoding='utf-8') as file: reader = json.load(file) for i in reader: if i['model'] == 'app.car': new_car = Car(brand=i['fields']['brand'], model=i['fields']['model']) new_car.save()
def add_car(current_user): json_data = request.get_json() json_data['owner_id'] = current_user.id json_data['created_at'] = datetime.datetime.utcnow() data, error = car_schema.load(json_data) if error: return response('failed', error, 400) car = Car(current_user.id, data) car.save() data = car_schema.dump(car).data return response_for_added_car(car, 201)
def addcart(request): # 拿到token token = request.session.get('token') # 根据token在缓存中找到对应用户 if token: #获取对应用户 userid = cache.get(token) user = User.objects.get(pk=userid) #获取对应商品信息 goodsid = request.GET.get('goodsid') goods = Goods.objects.get(pk=goodsid) # 获取商品数量 numb = request.GET.get('numb') # 获取选择的是购物车还是立即购买 shopway = request.GET.get('shopway') #判断商品是否已经存在购物车中 carts = Car.objects.filter(user=user).filter(goods=goods) if carts.exists(): if shopway: cart = carts.first() cart.number = cart.number + int(numb) cart.isdelete = False cart.save() else: cart = Car() cart.user = user cart.goods = goods cart.number = numb cart.save() response_data = { 'status':1, 'msg':'添加购物车成功' } return JsonResponse(response_data) else: response_data = { 'status': -1, 'msg': '请登录再操作' } return JsonResponse(response_data)
def car_create(): """ Creates a record based on params supplied Endpoint URL: /car/create :return: JSON successful message or exception response """ if request.method == "POST": if request.data is None: return jsonify({ "status_code": 400, "message": "Invalid request" }) request_data = request.data try: # Find and validate required parameters in order to create car record make = helpers.check_missing('list', request_data, 'make') make = helpers.validate_string(make, 'make') model = helpers.check_missing('list', request_data, 'model') model = helpers.validate_string(model, 'model') year = helpers.check_missing('list', request_data, 'year') year = helpers.validate_year(year) assigned_type = helpers.check_missing('list', request_data, 'assigned_type') assigned_id = helpers.check_missing('list', request_data, 'assigned_id') assigned_id = helpers.validate_int(assigned_id, 'assigned_id') # Validate the assigned type and id, more logic for assigning in a helper function assigned_type, assigned_id = helpers.validate_assigning( assigned_type, assigned_id) # Create object and save it in the database car = Car(make, model, year, assigned_type, assigned_id) car.save() return jsonify({"status_code": 201, "message": "Car created"}) except Exception as e: return jsonify({ "status_code": e.args[0]['status_code'], "message": e.args[0]['message'] })
import csv, sys, os project_path = sys.path[0] + '\cars' sys.path.append(project_path) os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' import django import datetime django.setup() from app.models import Car data = csv.reader(open(sys.path[0] + '\csv.csv'), delimiter=';') for row in data: car = Car() car.brand_name = row[0] car.car_model = row[1] car.release_year = row[2] car.name = "{} - {}".format(row[0], row[1]) try: car.last_year = int(row[3]) except ValueError: car.last_year = datetime.datetime.today().year car.save()