def patch(self, account_id, account): if account_id != account['id']: return '', 403 with db.connection as con: service = UsersService(con) request_json = request.json user = service.patch_account(request_json, account['id']) return jsonify(user)
def get(self, account_id, account): with db.connection as con: service = UsersService(con) try: user = service.get_account(account_id) except UserDoesNotExistError: return '', 404 else: return jsonify(user)
def get(self, account_id): if not session.get('user_id', ): return '', 401 with db.connection as con: service = UsersService(con) is_seller = service.account_is_seller(account_id) account = service.get_user(account_id, is_seller) if not account: return '', 404 return jsonify(account), 200
def get(self, user_id): account_id = session.get('user_id') if not account_id: return '', 403 service = UsersService() try: user = service.getuser(user_id) except UserNotFound: return '', 404 else: return jsonify(user), 200
def post(self): with db.connection as con: service = UsersService(con) request_json = request.json is_seller = request_json.get('is_seller') if not is_seller: user = dict(service.reqistration_account(request_json)) else: user = service.reqistration_account(request_json) account_id = user['id'] user = service.registration_seller(request_json, account_id) return jsonify(user)
def users(): """ Обработка регистрации нового пользователя """ try: user_data = UsersSchema().load(request.json) except ValidationError as error: return error.messages, 400 else: with db.connection as con: service = UsersService(con) try: user = service.add_user(user_data) except ConflictError: return '', 409 return jsonify(user), 201
def wrapper(*args, **kwargs): user_id = session.get("user_id") if not user_id: return '', 403 with db.connection as connection: user_service = UsersService(connection) try: user = user_service.read(user_id) except UserDoesNotExistsError: return '', 403 return func(*args, **kwargs, user=user)
def post(self): request_json = request.json email = request_json.get('email') password = request_json.get('password') first_name = request_json.get('first_name') last_name = request_json.get('last_name') is_seller = request_json.get('is_seller') if is_seller: phone = request_json.get('phone') zip_code = int(request_json.get('zip_code')) city_id = int(request_json.get('city_id')) street = request_json.get('street') home = request_json.get('home') with db.connection as con: service = UsersService(con) account_id = service.create_account(first_name, last_name, email, password) if not account_id: return '', 400 if is_seller: CitiesService(con).create_zip_code(zip_code=zip_code, city_id=city_id) service.create_seller(account_id, phone, zip_code, street, home) response = service.get_user(account_id, is_seller) return jsonify(response)
def post(self): request_json = request.json email = request_json.get('email') password = request_json.get('password') first_name = request_json.get('first_name') last_name = request_json.get('last_name') is_seller = request_json.get('is_seller') if is_seller: phone = request_json.get('phone') zip_code = int(request_json.get('zip_code')) city_id = int(request_json.get('city_id')) street = request_json.get('street') home = request_json.get('home') with db.connection as con: service = UsersService(con) acc_id = service.create_account(first_name, last_name, email, password) try: if is_seller: CitiesService(con).create_zip_code(zip_code=zip_code, city_id=city_id) service.create_seller(acc_id, phone, zip_code, street, home) con.commit() except sqlite3.IntegrityError: return '', 409 resp = service.get_user(acc_id, is_seller) return jsonify(resp), 200
def post(self): account_id = session.get('user_id') if not account_id: return '', 401 request_json = request.json name = request_json['name'] hex = request_json['hex'] if not name or not hex: return '', 400 with db.connection as con: user_service = UsersService(con) is_seller = user_service.account_is_seller(account_id) if not is_seller: return '', 403 service = ColorsService(con) try: service.add_color(name, hex) except: color = service.get_color(name) return jsonify(color), 208 color = service.get_color(name) return jsonify(color), 200
def post(self): account_id = session.get('user_id') if not account_id: return '', 401 with db.connection as con: is_seller = UsersService(con).account_is_seller(account_id) if is_seller is False: return '', 403 file = request.files.get('file') filename = f'{uuid.uuid4()}{os.path.splitext(file.filename)[1]}' file.save(os.path.join(self.UPLOAD_FOLDER, filename)) result = { 'url': url_for('images.image', image_name=filename, _external=True) } return result, 200
def patch(self, account_id): def update_data(data_dict, key, req_json): if req_json.get(key): data_dict.update({key: req_json[key]}) return data_dict if not session.get('user_id'): return '', 401 if account_id != session['user_id']: return '', 403 request_json = request.json with db.connection as con: service = UsersService(con) account_update = dict() update_data(account_update, 'first_name', request_json) update_data(account_update, 'last_name', request_json) if len(account_update): service.update_account(account_id, account_update) is_seller_update = request_json.get('is_seller') is_seller = service.account_is_seller(account_id) if is_seller_update is not None: if is_seller_update is True: seller_update = dict() zipcode_update = dict() update_data(seller_update, 'phone', request_json) update_data(seller_update, "zip_code", request_json) update_data(seller_update, "street", request_json) update_data(seller_update, "home", request_json) update_data(zipcode_update, "zip_code", request_json) update_data(zipcode_update, "city_id", request_json) CitiesService(con).create_zip_code( zip_code=zipcode_update['zip_code'], city_id=zipcode_update['city_id']) if is_seller is False: service.create_seller( account_id=account_id, zip_code=zipcode_update['zip_code'], home=seller_update['home'], phone=seller_update['phone'], street=seller_update['street']) user = service.get_user(account_id, is_seller_update) return jsonify(user), 200 else: print(seller_update) service.update_seller(account_id, seller_update) return jsonify( UsersService(con).get_user(account_id, is_seller_update)), 200 else: if is_seller is True: with db.connection as con: cur = con.execute( f'SELECT * ' f'FROM ad ' f'WHERE seller_id = {account_id} ') ads = [dict(row) for row in cur.fetchall()] for ad in ads: car_id = ad['car_id'] con.execute(f'DELETE FROM carcolor ' f'WHERE car_id = {car_id}') con.execute(f'DELETE FROM car ' f'WHERE id = {car_id}') con.execute(f'DELETE FROM image ' f'WHERE car_id = {car_id}') con.execute(f'DELETE FROM ad ' f'WHERE seller_id = {account_id} ') print(account_id) con.execute(f'DELETE FROM seller ' f'WHERE account_id = {account_id}') return jsonify( UsersService(con).get_user( account_id, is_seller_update)), 200 is_seller_update = is_seller return jsonify( UsersService(con).get_user(account_id, is_seller_update)), 200