def add_photo(self, image, lot_id): self.create_directory_if_not_exists("data/images/temp") self.create_directory_if_not_exists("data/images/lots") temporary_file_location = f"data/images/temp/{secure_filename(image.filename)}" image.save(temporary_file_location) im = Image.open(temporary_file_location) photo_hash = sha256(str(datetime.now())) file_location = f"data/images/lots/{photo_hash}.jpg" im = im.convert("RGB") im.save(file_location) remove(temporary_file_location) self.cursor.execute( f"SELECT `photos` FROM Lots WHERE `id` = '{lot_id}'") photos = eval(self.cursor.fetchone()[0]) photos.append(photo_hash) stringified_photos = str(photos).replace("'", '"') self.cursor.execute( f"UPDATE Lots SET `photos` = '{stringified_photos}' WHERE `id` = '{lot_id}'" ) self.conn.commit() return "Photo is successfuly added."
def subscribe_user_to_lot(self, user, lot_id, type: SubscriptionTypes, message) -> bool: if type == SubscriptionTypes.PhoneCall and not self.user_has_phone_number( user): raise UserHasNoPhoneNumber(user) id_hash = sha256(f"{user}_{lot_id}") try: self.cursor.execute( f"INSERT INTO SubscriptionRequests (`id`, `user`, `lot`, `type`, `message`) VALUES ('{id_hash}', '{user}', '{lot_id}', '{type.value}', '{message}')" ) self.conn.commit() return True except: return False
def set_user_avatar(self, user, image): self.create_directory_if_not_exists("data/images/temp") self.create_directory_if_not_exists("data/images/user") self.delete_user_avatar(user) temporary_file_location = f"data/images/temp/{secure_filename(image.filename)}" image.save(temporary_file_location) im = Image.open(temporary_file_location) photo_hash = sha256(str(datetime.now())) file_location = f"data/images/user/{photo_hash}.jpg" im = im.convert("RGB") im.save(file_location) self.cursor.execute( f"UPDATE Users SET `avatar` = '{photo_hash}' WHERE `email` = '{user}'" ) self.conn.commit() remove(temporary_file_location)
def unsubscribe_user_from_lot(self, user, lot_id): id_hash = sha256(f"{user}_{lot_id}") self.cursor.execute( f"DELETE FROM SubscriptionRequests WHERE `id` = '{id_hash}'") self.conn.commit()
def subscribe_user_to_lot(self, user, lot_id): id_hash = sha256(f'{user}_{lot_id}') self.cursor.execute( f"INSERT INTO SubscriptionRequests (`id`, `user`, `lot`) VALUES ('{id_hash}', '{user}', '{lot_id}')" ) self.conn.commit()