Spaces:
Sleeping
Sleeping
Update models.py
Browse files
models.py
CHANGED
|
@@ -1,181 +1,181 @@
|
|
| 1 |
-
"""Application Models"""
|
| 2 |
-
import bson, os
|
| 3 |
-
from dotenv import load_dotenv
|
| 4 |
-
from pymongo import MongoClient
|
| 5 |
-
from werkzeug.security import generate_password_hash, check_password_hash
|
| 6 |
-
|
| 7 |
-
load_dotenv()
|
| 8 |
-
|
| 9 |
-
DATABASE_URL=os.environ.get('DATABASE_URL')
|
| 10 |
-
print(DATABASE_URL)
|
| 11 |
-
client = MongoClient(DATABASE_URL)
|
| 12 |
-
db = client.myDatabase
|
| 13 |
-
|
| 14 |
-
class Books:
|
| 15 |
-
"""Books Model"""
|
| 16 |
-
def __init__(self):
|
| 17 |
-
return
|
| 18 |
-
|
| 19 |
-
def create(self, title="", description="", image_url="", category="", user_id=""):
|
| 20 |
-
"""Create a new book"""
|
| 21 |
-
book = self.get_by_user_id_and_title(user_id, title)
|
| 22 |
-
if book:
|
| 23 |
-
return
|
| 24 |
-
new_book = db.books.insert_one(
|
| 25 |
-
{
|
| 26 |
-
"title": title,
|
| 27 |
-
"description": description,
|
| 28 |
-
"image_url": image_url,
|
| 29 |
-
"category": category,
|
| 30 |
-
"user_id": user_id
|
| 31 |
-
}
|
| 32 |
-
)
|
| 33 |
-
return self.get_by_id(new_book.inserted_id)
|
| 34 |
-
|
| 35 |
-
def get_all(self):
|
| 36 |
-
"""Get all books"""
|
| 37 |
-
books = db.books.find()
|
| 38 |
-
return [{**book, "_id": str(book["_id"])} for book in books]
|
| 39 |
-
|
| 40 |
-
def get_by_id(self, book_id):
|
| 41 |
-
"""Get a book by id"""
|
| 42 |
-
book = db.books.find_one({"_id": bson.ObjectId(book_id)})
|
| 43 |
-
if not book:
|
| 44 |
-
return
|
| 45 |
-
book["_id"] = str(book["_id"])
|
| 46 |
-
return book
|
| 47 |
-
|
| 48 |
-
def get_by_user_id(self, user_id):
|
| 49 |
-
"""Get all books created by a user"""
|
| 50 |
-
books = db.books.find({"user_id": user_id})
|
| 51 |
-
return [{**book, "_id": str(book["_id"])} for book in books]
|
| 52 |
-
|
| 53 |
-
def get_by_category(self, category):
|
| 54 |
-
"""Get all books by category"""
|
| 55 |
-
books = db.books.find({"category": category})
|
| 56 |
-
return [book for book in books]
|
| 57 |
-
|
| 58 |
-
def get_by_user_id_and_category(self, user_id, category):
|
| 59 |
-
"""Get all books by category for a particular user"""
|
| 60 |
-
books = db.books.find({"user_id": user_id, "category": category})
|
| 61 |
-
return [{**book, "_id": str(book["_id"])} for book in books]
|
| 62 |
-
|
| 63 |
-
def get_by_user_id_and_title(self, user_id, title):
|
| 64 |
-
"""Get a book given its title and author"""
|
| 65 |
-
book = db.books.find_one({"user_id": user_id, "title": title})
|
| 66 |
-
if not book:
|
| 67 |
-
return
|
| 68 |
-
book["_id"] = str(book["_id"])
|
| 69 |
-
return book
|
| 70 |
-
|
| 71 |
-
def update(self, book_id, title="", description="", image_url="", category="", user_id=""):
|
| 72 |
-
"""Update a book"""
|
| 73 |
-
data={}
|
| 74 |
-
if title: data["title"]=title
|
| 75 |
-
if description: data["description"]=description
|
| 76 |
-
if image_url: data["image_url"]=image_url
|
| 77 |
-
if category: data["category"]=category
|
| 78 |
-
|
| 79 |
-
book = db.books.update_one(
|
| 80 |
-
{"_id": bson.ObjectId(book_id)},
|
| 81 |
-
{
|
| 82 |
-
"$set": data
|
| 83 |
-
}
|
| 84 |
-
)
|
| 85 |
-
book = self.get_by_id(book_id)
|
| 86 |
-
return book
|
| 87 |
-
|
| 88 |
-
def delete(self, book_id):
|
| 89 |
-
"""Delete a book"""
|
| 90 |
-
book = db.books.delete_one({"_id": bson.ObjectId(book_id)})
|
| 91 |
-
return book
|
| 92 |
-
|
| 93 |
-
def delete_by_user_id(self, user_id):
|
| 94 |
-
"""Delete all books created by a user"""
|
| 95 |
-
book = db.books.delete_many({"user_id": bson.ObjectId(user_id)})
|
| 96 |
-
return book
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
class User:
|
| 100 |
-
"""User Model"""
|
| 101 |
-
def __init__(self):
|
| 102 |
-
return
|
| 103 |
-
|
| 104 |
-
def create(self, name="", email="", password=""):
|
| 105 |
-
"""Create a new user"""
|
| 106 |
-
user = self.get_by_email(email)
|
| 107 |
-
if user:
|
| 108 |
-
return
|
| 109 |
-
new_user = db.users.insert_one(
|
| 110 |
-
{
|
| 111 |
-
"name": name,
|
| 112 |
-
"email": email,
|
| 113 |
-
"password": self.encrypt_password(password),
|
| 114 |
-
"active": True
|
| 115 |
-
}
|
| 116 |
-
)
|
| 117 |
-
return self.get_by_id(new_user.inserted_id)
|
| 118 |
-
|
| 119 |
-
def get_all(self):
|
| 120 |
-
"""Get all users"""
|
| 121 |
-
users = db.users.find({"active": True})
|
| 122 |
-
return [{**user, "_id": str(user["_id"])} for user in users]
|
| 123 |
-
|
| 124 |
-
def get_by_id(self, user_id):
|
| 125 |
-
"""Get a user by id"""
|
| 126 |
-
user = db.users.find_one({"_id": bson.ObjectId(user_id), "active": True})
|
| 127 |
-
if not user:
|
| 128 |
-
return
|
| 129 |
-
user["_id"] = str(user["_id"])
|
| 130 |
-
user.pop("password")
|
| 131 |
-
return user
|
| 132 |
-
|
| 133 |
-
def get_by_email(self, email):
|
| 134 |
-
"""Get a user by email"""
|
| 135 |
-
user = db.users.find_one({"email": email, "active": True})
|
| 136 |
-
if not user:
|
| 137 |
-
return
|
| 138 |
-
user["_id"] = str(user["_id"])
|
| 139 |
-
return user
|
| 140 |
-
|
| 141 |
-
def update(self, user_id, name=""):
|
| 142 |
-
"""Update a user"""
|
| 143 |
-
data = {}
|
| 144 |
-
if name:
|
| 145 |
-
data["name"] = name
|
| 146 |
-
user = db.users.update_one(
|
| 147 |
-
{"_id": bson.ObjectId(user_id)},
|
| 148 |
-
{
|
| 149 |
-
"$set": data
|
| 150 |
-
}
|
| 151 |
-
)
|
| 152 |
-
user = self.get_by_id(user_id)
|
| 153 |
-
return user
|
| 154 |
-
|
| 155 |
-
def delete(self, user_id):
|
| 156 |
-
"""Delete a user"""
|
| 157 |
-
Books().delete_by_user_id(user_id)
|
| 158 |
-
user = db.users.delete_one({"_id": bson.ObjectId(user_id)})
|
| 159 |
-
user = self.get_by_id(user_id)
|
| 160 |
-
return user
|
| 161 |
-
|
| 162 |
-
def disable_account(self, user_id):
|
| 163 |
-
"""Disable a user account"""
|
| 164 |
-
user = db.users.update_one(
|
| 165 |
-
{"_id": bson.ObjectId(user_id)},
|
| 166 |
-
{"$set": {"active": False}}
|
| 167 |
-
)
|
| 168 |
-
user = self.get_by_id(user_id)
|
| 169 |
-
return user
|
| 170 |
-
|
| 171 |
-
def encrypt_password(self, password):
|
| 172 |
-
"""Encrypt password"""
|
| 173 |
-
return generate_password_hash(password)
|
| 174 |
-
|
| 175 |
-
def login(self, email, password):
|
| 176 |
-
"""Login a user"""
|
| 177 |
-
user = self.get_by_email(email)
|
| 178 |
-
if not user or not check_password_hash(user["password"], password):
|
| 179 |
-
return
|
| 180 |
-
user.pop("password")
|
| 181 |
-
return user
|
|
|
|
| 1 |
+
"""Application Models"""
|
| 2 |
+
import bson, os
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
from pymongo import MongoClient
|
| 5 |
+
from werkzeug.security import generate_password_hash, check_password_hash
|
| 6 |
+
|
| 7 |
+
load_dotenv()
|
| 8 |
+
|
| 9 |
+
DATABASE_URL=os.environ.get('DATABASE_URL')
|
| 10 |
+
print(DATABASE_URL)
|
| 11 |
+
client = MongoClient(DATABASE_URL)
|
| 12 |
+
db = client.myDatabase
|
| 13 |
+
|
| 14 |
+
class Books:
|
| 15 |
+
"""Books Model"""
|
| 16 |
+
def __init__(self):
|
| 17 |
+
return
|
| 18 |
+
|
| 19 |
+
def create(self, title="", description="", image_url="", category="", user_id=""):
|
| 20 |
+
"""Create a new book"""
|
| 21 |
+
book = self.get_by_user_id_and_title(user_id, title)
|
| 22 |
+
if book:
|
| 23 |
+
return
|
| 24 |
+
new_book = db.books.insert_one(
|
| 25 |
+
{
|
| 26 |
+
"title": title,
|
| 27 |
+
"description": description,
|
| 28 |
+
"image_url": image_url,
|
| 29 |
+
"category": category,
|
| 30 |
+
"user_id": user_id
|
| 31 |
+
}
|
| 32 |
+
)
|
| 33 |
+
return self.get_by_id(new_book.inserted_id)
|
| 34 |
+
|
| 35 |
+
def get_all(self):
|
| 36 |
+
"""Get all books"""
|
| 37 |
+
books = db.books.find()
|
| 38 |
+
return [{**book, "_id": str(book["_id"])} for book in books]
|
| 39 |
+
|
| 40 |
+
def get_by_id(self, book_id):
|
| 41 |
+
"""Get a book by id"""
|
| 42 |
+
book = db.books.find_one({"_id": bson.ObjectId(book_id)})
|
| 43 |
+
if not book:
|
| 44 |
+
return
|
| 45 |
+
book["_id"] = str(book["_id"])
|
| 46 |
+
return book
|
| 47 |
+
|
| 48 |
+
def get_by_user_id(self, user_id):
|
| 49 |
+
"""Get all books created by a user"""
|
| 50 |
+
books = db.books.find({"user_id": user_id})
|
| 51 |
+
return [{**book, "_id": str(book["_id"])} for book in books]
|
| 52 |
+
|
| 53 |
+
def get_by_category(self, category):
|
| 54 |
+
"""Get all books by category"""
|
| 55 |
+
books = db.books.find({"category": category})
|
| 56 |
+
return [book for book in books]
|
| 57 |
+
|
| 58 |
+
def get_by_user_id_and_category(self, user_id, category):
|
| 59 |
+
"""Get all books by category for a particular user"""
|
| 60 |
+
books = db.books.find({"user_id": user_id, "category": category})
|
| 61 |
+
return [{**book, "_id": str(book["_id"])} for book in books]
|
| 62 |
+
|
| 63 |
+
def get_by_user_id_and_title(self, user_id, title):
|
| 64 |
+
"""Get a book given its title and author"""
|
| 65 |
+
book = db.books.find_one({"user_id": user_id, "title": title})
|
| 66 |
+
if not book:
|
| 67 |
+
return
|
| 68 |
+
book["_id"] = str(book["_id"])
|
| 69 |
+
return book
|
| 70 |
+
|
| 71 |
+
def update(self, book_id, title="", description="", image_url="", category="", user_id=""):
|
| 72 |
+
"""Update a book"""
|
| 73 |
+
data={}
|
| 74 |
+
if title: data["title"]=title
|
| 75 |
+
if description: data["description"]=description
|
| 76 |
+
if image_url: data["image_url"]=image_url
|
| 77 |
+
if category: data["category"]=category
|
| 78 |
+
|
| 79 |
+
book = db.books.update_one(
|
| 80 |
+
{"_id": bson.ObjectId(book_id)},
|
| 81 |
+
{
|
| 82 |
+
"$set": data
|
| 83 |
+
}
|
| 84 |
+
)
|
| 85 |
+
book = self.get_by_id(book_id)
|
| 86 |
+
return book
|
| 87 |
+
|
| 88 |
+
def delete(self, book_id):
|
| 89 |
+
"""Delete a book"""
|
| 90 |
+
book = db.books.delete_one({"_id": bson.ObjectId(book_id)})
|
| 91 |
+
return book
|
| 92 |
+
|
| 93 |
+
def delete_by_user_id(self, user_id):
|
| 94 |
+
"""Delete all books created by a user"""
|
| 95 |
+
book = db.books.delete_many({"user_id": bson.ObjectId(user_id)})
|
| 96 |
+
return book
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
class User:
|
| 100 |
+
"""User Model"""
|
| 101 |
+
def __init__(self):
|
| 102 |
+
return
|
| 103 |
+
|
| 104 |
+
def create(self, name="", email="", password=""):
|
| 105 |
+
"""Create a new user"""
|
| 106 |
+
user = self.get_by_email(email)
|
| 107 |
+
if user:
|
| 108 |
+
return
|
| 109 |
+
new_user = db.users.insert_one(
|
| 110 |
+
{
|
| 111 |
+
"name": name,
|
| 112 |
+
"email": email,
|
| 113 |
+
"password": self.encrypt_password(password),
|
| 114 |
+
"active": True
|
| 115 |
+
}
|
| 116 |
+
)
|
| 117 |
+
return self.get_by_id(new_user.inserted_id)
|
| 118 |
+
|
| 119 |
+
def get_all(self):
|
| 120 |
+
"""Get all users"""
|
| 121 |
+
users = db.users.find({"active": True})
|
| 122 |
+
return [{**user, "_id": str(user["_id"])} for user in users]
|
| 123 |
+
|
| 124 |
+
def get_by_id(self, user_id):
|
| 125 |
+
"""Get a user by id"""
|
| 126 |
+
user = db.users.find_one({"_id": bson.ObjectId(user_id), "active": True})
|
| 127 |
+
if not user:
|
| 128 |
+
return
|
| 129 |
+
user["_id"] = str(user["_id"])
|
| 130 |
+
user.pop("password")
|
| 131 |
+
return user
|
| 132 |
+
|
| 133 |
+
def get_by_email(self, email):
|
| 134 |
+
"""Get a user by email"""
|
| 135 |
+
user = db.users.find_one({"email": email, "active": True})
|
| 136 |
+
if not user:
|
| 137 |
+
return
|
| 138 |
+
user["_id"] = str(user["_id"])
|
| 139 |
+
return user
|
| 140 |
+
|
| 141 |
+
def update(self, user_id, name=""):
|
| 142 |
+
"""Update a user"""
|
| 143 |
+
data = {}
|
| 144 |
+
if name:
|
| 145 |
+
data["name"] = name
|
| 146 |
+
user = db.users.update_one(
|
| 147 |
+
{"_id": bson.ObjectId(user_id)},
|
| 148 |
+
{
|
| 149 |
+
"$set": data
|
| 150 |
+
}
|
| 151 |
+
)
|
| 152 |
+
user = self.get_by_id(user_id)
|
| 153 |
+
return user
|
| 154 |
+
|
| 155 |
+
def delete(self, user_id):
|
| 156 |
+
"""Delete a user"""
|
| 157 |
+
Books().delete_by_user_id(user_id)
|
| 158 |
+
user = db.users.delete_one({"_id": bson.ObjectId(user_id)})
|
| 159 |
+
user = self.get_by_id(user_id)
|
| 160 |
+
return user
|
| 161 |
+
|
| 162 |
+
def disable_account(self, user_id):
|
| 163 |
+
"""Disable a user account"""
|
| 164 |
+
user = db.users.update_one(
|
| 165 |
+
{"_id": bson.ObjectId(user_id)},
|
| 166 |
+
{"$set": {"active": False}}
|
| 167 |
+
)
|
| 168 |
+
user = self.get_by_id(user_id)
|
| 169 |
+
return user
|
| 170 |
+
|
| 171 |
+
def encrypt_password(self, password):
|
| 172 |
+
"""Encrypt password"""
|
| 173 |
+
return generate_password_hash(password)
|
| 174 |
+
|
| 175 |
+
def login(self, email, password):
|
| 176 |
+
"""Login a user"""
|
| 177 |
+
user = self.get_by_email(email)
|
| 178 |
+
if not user or not check_password_hash(user["password"], password):
|
| 179 |
+
return
|
| 180 |
+
user.pop("password")
|
| 181 |
+
return user
|