def create(cls, **params): con = Connection() admin = True if params.get('admin') else False params['admin'] = admin query = "INSERT INTO users (first_name, last_name, email, password, phone, admin) VALUES (:first_name, :last_name, :email, :password, :phone, :admin)" success = asyncio.run(con.commit(query, **params)) return success
def insert_token(): try: con = Connection() jti = get_raw_jwt()['jti'] insert_token_query = 'INSERT INTO blacklist_tokens (token) VALUES (:token)' logout = asyncio.run(con.commit(insert_token_query, **dict(token=jti))) return True except: return False
def login(cls, **params): con = Connection() get_hash_query = "SELECT * FROM users WHERE email = :email" user = asyncio.run(con.select(get_hash_query, **params)) if pbkdf2_sha256.verify(params['password'], user[0]['password']): del user[0]['password'] return user[0] return None
def delete(cls, property_id): try: con = Connection() delete_property(property_id) delete_property_query = 'DELETE FROM properties WHERE property_id = :property_id' is_deleted = asyncio.run(con.commit( delete_property_query, **dict(property_id=property_id))) return is_deleted except: return False
def create(cls, files=None, **params): try: con = Connection() new_id = uuid4() params['objectID'] = new_id create_or_update_property(**params) images_path = upload_images(files) create_property_query = 'INSERT INTO properties (property_id, title, description, kind, price, state, sale, bathrooms, rooms, address, square_meters, heating, community_fees, orientation, furnished, equipped_kitchen, floor_number, common_zones, pets, contract_time, bond) VALUES (:objectID, :title, :description, :kind, :price, :state, :sale, :bathrooms, :rooms, :address, :square_meters, :heating, :community_fees, :orientation, :furnished, :equipped_kitchen, :floor_number, :common_zones, :pets, :contract_time, :bond) RETURNING *' property_data = asyncio.run( con.commit(create_property_query, **params)) create_images_query = 'INSERT INTO images(property_id, path) VALUES (:property_id, :path)' for image in images_path: image_data = dict( property_id=property_data["property_id"], path=image['url']) asyncio.run(con.commit(create_images_query, **image_data)) return True except BaseException as e: print(e) return False
def images(cls, property_id): con = Connection() query = 'SELECT images.path FROM properties INNER JOIN images ON images.property_id = properties.property_id WHERE properties.property_id = :id' images = asyncio.run(con.select(query, **dict(id=property_id))) return images
def update(cls, files=None, **params): try: con = Connection() select_property_query = 'SELECT * FROM properties WHERE property_id = :property_id' property_attributes = asyncio.run( con.select(select_property_query, **params))[0] new_attributes = dict() title = property_attributes['title'] description = property_attributes['description'] kind = property_attributes['kind'] price = property_attributes['price'] state = property_attributes['state'] sale = property_attributes['sale'] rooms = property_attributes['rooms'] bathrooms = property_attributes['bathrooms'] address = property_attributes['address'] square_meters = property_attributes['square_meters'] heating = property_attributes['heating'] community_fees = property_attributes['community_fees'] orientation = property_attributes['orientation'] furnished = property_attributes['furnished'] equipped_kitchen = property_attributes['equipped_kitchen'] floor_number = property_attributes['floor_number'] common_zones = property_attributes['common_zones'] contract_time = property_attributes['contract_time'] bond = property_attributes['bond'] pets = property_attributes['pets'] new_attributes['title'] = title if params['title'] == title or params['title'] == None else params['title'] new_attributes['description'] = description if params['description'] == description or params['description'] == None else params['description'] new_attributes['kind'] = kind if params['kind'] == kind or params['kind'] == None else params['kind'] new_attributes['price'] = price if params['price'] == price or params['price'] == None else params['price'] new_attributes['state'] = state if params['state'] == state or params['state'] == None else params['state'] new_attributes['sale'] = sale if params['sale'] == sale or params['sale'] == None else params['sale'] new_attributes['rooms'] = rooms if params['rooms'] == rooms or params['rooms'] == None else params['rooms'] new_attributes['bathrooms'] = bathrooms if params['bathrooms'] == bathrooms or params['bathrooms'] == None else params['bathrooms'] new_attributes['address'] = address if params['address'] == address or params['address'] == None else params['address'] new_attributes['square_meters'] = square_meters if params['square_meters'] == square_meters or params['square_meters'] == None else params['square_meters'] new_attributes['heating'] = heating if params['heating'] == heating or params['heating'] == None else params['heating'] new_attributes['community_fees'] = community_fees if params[ 'community_fees'] == community_fees or params['community_fees'] == None else params['community_fees'] new_attributes['orientation'] = orientation if params['orientation'] == orientation or params['orientation'] == None else params['orientation'] new_attributes['furnished'] = furnished if params['furnished'] == furnished or params['furnished'] == None else params['furnished'] new_attributes['equipped_kitchen'] = equipped_kitchen if params[ 'equipped_kitchen'] == equipped_kitchen or params['equipped_kitchen'] == None else params['equipped_kitchen'] new_attributes['floor_number'] = floor_number if params['floor_number'] == floor_number or params['floor_number'] == None else params['floor_number'] new_attributes['common_zones'] = common_zones if params['common_zones'] == common_zones or params['common_zones'] == None else params['common_zones'] new_attributes['pets'] = pets if params['pets'] == pets or params['pets'] == None else params['pets'] new_attributes['contract_time'] = contract_time if params['contract_time'] == contract_time or params['contract_time'] == None else params['contract_time'] new_attributes['bond'] = bond if params['bond'] == bond or params['bond'] == None else params['bond'] new_attributes['objectID'] = params['property_id'] create_or_update_property(**new_attributes) update_property_query = 'UPDATE properties SET title = :title, description = :description, kind = :kind, price = :price, state = :state, sale = :sale, bathrooms = :bathrooms, rooms = :rooms, address = :address, square_meters = :square_meters, heating = :heating, community_fees = :community_fees, orientation = :orientation, furnished = :furnished, equipped_kitchen = :equipped_kitchen, floor_number = :floor_number, common_zones = :common_zones, pets = :pets, contract_time = :contract_time, bond = :bond WHERE property_id = :objectID' updated = asyncio.run(con.commit( update_property_query, **new_attributes)) return updated except Exception as e: print(e) return False
def check_if_token_in_blacklist(decrypted_token): jti = decrypted_token['jti'] con = Connection() check_token_query = 'SELECT * FROM blacklist_tokens WHERE token=:token' token_blacklisted = asyncio.run(con.select(check_token_query, **dict(token=jti))) return len(token_blacklisted) > 0
def find(cls, email): con = Connection() query = "SELECT * FROM users WHERE email = :email" params = dict(email=email) user = asyncio.run(con.select(query, **params)) return user