def patch(self, user_id, user): """Частичное редактирование пользователя""" if not user_id == user["id"]: return '', 403 request_json = request.json is_seller = request_json["is_seller"] with db.connection as connection: user_service = UsersService(connection) seller_service = SellersService(connection) zipcode_service = ZipcodesService(connection) car_service = CarsService(connection) user_service.update(user_id=user_id, data=request_json) zipcode_service.update(data=request_json) if is_seller: try: seller_service.update(user_id=user_id, data=request_json) except SellerDoesNotExistsError: seller_service.create(user_id=user_id, seller_data=request_json) else: seller_service.delete(user_id) try: car_id = car_service.get_id(user_id=user_id) car_service.delete(car_id) except CarDoesNotExists: pass return self.get(user_id)
def login(): """Авторизация пользователя""" request_json = request.json email = request_json.get('email') password = request_json.get('password') if not email or not password: return '', 400 with db.connection as connection: user_service = UsersService(connection) try: user_data = user_service.auth(email=email) except UserDoesNotExistsError: return '', 404 else: user_email = user_data["email"] user_password_hash = user_data["password"] user_id = user_data["id"] if not (email == user_email and check_password_hash(user_password_hash, password)): return '', 403 session['user_id'] = user_id return '', 200
def post(self): """Регистрация нового пользователя""" request_json = request.json if not user_request_check(request_json): return '', 400 request_json["password"] = generate_password_hash(request_json["password"]) with db.connection as connection: user_service = UsersService(connection) try: user_id = user_service.create(user_data=request_json) except UserCreationError: return '', 409 user = user_service.read(user_id=user_id) user.update({ "is_seller": request_json["is_seller"], }) if user["is_seller"]: seller_service = SellersService(connection) zipcode_service = ZipcodesService(connection) try: seller_id = seller_service.create(seller_data=request_json, user_id=user_id) zipcode_service.create(Zipcode_data=request_json) except (SellerCreationError, ZipcodesCreationError): return '', 409 seller = seller_service.read(seller_id=seller_id) user.update(seller) return jsonify(user), 201
def get(self, account_id): if not session.get('user_id', ): return '', 403 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 post(self, user_id=None): if not session.get('user_id', None): return '', 403 if user_id is not None and user_id != session['user_id']: return '', 403 account_id = session['user_id'] with db.connection as con: service = UsersService(con) service_ads = AdsService(con) is_seller = service.account_is_seller(account_id) if not is_seller: return '', 403 request_json = request.json response = dict() title = request_json['title'] tags = request_json['tags'] car = request_json['car'] colors_id = car['colors'] images = car['images'] car.setdefault('num_owners', 1) response.update({ 'title': title, 'tags': tags, 'seller_id': account_id, 'is_seller': is_seller }) car_id = service_ads.create_car(car) service_ads.add_images(images, car_id) date = datetime.datetime.now().strftime('%Y-%m-%d') response.update({'date': date}) service_ads.add_tag(tags) tags_id = service_ads.get_tags_id(tags=tags) colors = [] service_ads.set_car_color(colors_id, car_id) service_color = ColorsService(con) for color_id in colors_id: color = service_color.get_color(id=color_id) colors += color car['colors'] = colors response.update({'car': car}) ad_id = service_ads.create_ad(title=title, date=date, account_id=account_id, car_id=car_id) response.update({'id': ad_id}) service_ads.set_ad_tag(ad_id, tags_id) return jsonify(response), 201
def get(self, user_id, user): """Получение информации о пользователе по id""" with db.connection as connection: user_service = UsersService(connection) seller_service = SellersService(connection) try: user_data = user_service.read(user_id=user_id) seller_data = seller_service.read(user_id=user_id) except UserDoesNotExistsError: return '', 404 except SellerDoesNotExistsError: return jsonify(user_data) else: user_data.update(seller_data) user_data.update({"is_seller": True}) return jsonify(user_data)
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): 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 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 '', 403 if account_id != session['user_id']: return '', 401 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