-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_utils.py
226 lines (186 loc) · 7.98 KB
/
db_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
from sqlalchemy import create_engine, Column, Integer, String, ForeignKey, Boolean, LargeBinary, DATE, text, func, TIMESTAMP
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, scoped_session, relationship
from werkzeug.security import generate_password_hash
from libs.pfp import make_profile
from base64 import b64encode
from io import BytesIO
from PIL import Image
import sys
from datetime import date
# Import database configuration variables
# from db_connection_config import HOST, USER, PASSWORD, DATABASE
from db_connection_config import HOST, USER, PASSWORD, DATABASE
# Database URL and Engine Setup
DATABASE_URL = f"mysql+mysqlconnector://{USER}:{PASSWORD}@{HOST}/{DATABASE}"
engine = create_engine(DATABASE_URL, echo=False) #ECHO IF YOU WANT TO DEBUG SQL STATEMENTS
# Session and Base Setup
session_factory = sessionmaker(bind=engine)
db_session = scoped_session(session_factory)
Base = declarative_base()
class Friend(Base):
__tablename__ = 'friends'
user_id = Column(Integer, ForeignKey('users_turistapp.id'), primary_key=True)
friend_id = Column(Integer, ForeignKey('users_turistapp.id'), primary_key=True)
class User(Base):
__tablename__ = 'users_turistapp'
id = Column(Integer, primary_key=True)
username = Column(String(50), unique=True, nullable=False)
email = Column(String(100), unique=True, nullable=False)
age = Column(DATE)
full_name = Column(String(63))
bio = Column(String(255))
encrypted_password = Column(String(255), nullable=False)
isAdmin = Column(Boolean)
isDeleted = Column(Boolean, default= False)
xp_collected = Column(Integer, default=0)
_profile_pic = Column("profile_pic" , LargeBinary)
attractions_visted = relationship("Attraction", secondary="user_attractions")
achievements = relationship("Achievement", secondary="user_achievements")
posts = relationship('UserPosts', back_populates='user')
attractions = relationship("Attraction", back_populates="local_admin")
following = relationship("User",
secondary="friends",
primaryjoin=id==Friend.user_id,
secondaryjoin=id==Friend.friend_id)
followers = relationship("User",
secondary="friends",
primaryjoin=id==Friend.friend_id,
secondaryjoin=id==Friend.user_id)
def __init__(self, username, email, password, full_name, data_of_birth):
self.username = username
self.email = email
self.age = data_of_birth
self.encrypted_password = password
self.isAdmin = False
self._profile_pic = make_profile(username, 200, 25).read()
self.full_name = full_name
self.bio = ""
@property
def profile_pic(self):
return b64encode(self._profile_pic).decode('utf-8')
@profile_pic.setter
def profile_pic(self, new_pic):
if new_pic:
buffer = BytesIO()
image = Image.open(new_pic)
image = image.resize((200, 200))
image.save(buffer, format="PNG", quality=20, optimize=True)
buffer.seek(0)
self._profile_pic = buffer.read()
else:
default_picture = make_profile(self.username, 200, 25)
self._profile_pic = default_picture.read()
class Attraction(Base):
__tablename__ = 'attractions'
id = Column(Integer, primary_key=True)
name = Column(String(100), nullable=False)
description = Column(String(255))
category = Column(String(50))
age_recommendation = Column(Integer)
location_coordinates = Column(String(100))
address = Column(String(100))
_image = Column("image", LargeBinary(length=(2**32)-1), nullable=True)
isDeleted = Column(Boolean, default=False)
achievements = relationship('Achievement', back_populates='attraction')
posts = relationship('UserPosts', back_populates='attraction')
local_admin_id = Column(Integer, ForeignKey('users_turistapp.id'))
local_admin = relationship("User", back_populates="attractions")
@property
def image(self):
if self._image is not None:
return b64encode(self._image).decode('utf-8')
else:
return None
@image.setter
def image(self, new_pic):
buffer = BytesIO()
image = Image.open(new_pic)
image = image.resize((500, 500))
image.save(buffer, format="PNG", quality=20, optimize=True)
buffer.seek(0)
self._image = buffer.read()
class Achievement(Base):
__tablename__ = 'achievements'
id = Column(Integer, primary_key=True)
name = Column(String(100), nullable=False)
description = Column(String(255))
pass_code = Column(String(255))
xp_reward = Column(Integer)
attraction_id = Column(Integer, ForeignKey('attractions.id'))
attraction = relationship('Attraction', back_populates='achievements')
age_rating = Column(Integer, default= 0)
is_deleted = Column(Boolean, default=0)
class UserAttraction(Base):
__tablename__ = 'user_attractions'
user_id = Column(Integer, ForeignKey('users_turistapp.id'), primary_key=True)
attraction_id = Column(Integer, ForeignKey('attractions.id'), primary_key=True)
class UserAchievement(Base):
__tablename__ = 'user_achievements'
user_id = Column(Integer, ForeignKey('users_turistapp.id'), primary_key=True)
achievement_id = Column(Integer, ForeignKey('achievements.id'), primary_key=True)
class UserPosts(Base):
__tablename__ = 'user_posts'
post_id = Column(Integer, primary_key=True)
user_id = Column(Integer, ForeignKey('users_turistapp.id'), nullable=False)
user = relationship('User', back_populates="posts")
post = Column(String(255), nullable=False)
_image = Column("image", LargeBinary(length=(2**32)-1), nullable=True)
time = Column(TIMESTAMP, default=func.now(), nullable=False)
attraction_id = Column(Integer, ForeignKey('attractions.id'), nullable=True)
attraction = relationship('Attraction', back_populates=None)
isDeleted = Column(Boolean, default= False)
is_status = Column(Boolean, default= False)
def __init__(self, message, user, attraction = 0, is_status = False):
self.user_id = user
self.post = message
self.is_status = is_status
if int(attraction) > 0:
self.attraction_id = attraction
else:
self.attraction_id = None
@property
def image(self):
if self._image is not None:
return b64encode(self._image).decode('utf-8')
else:
return None
@image.setter
def image(self, new_pic):
if new_pic:
buffer = BytesIO()
image = Image.open(new_pic)
image = image.resize((500, 500))
image.save(buffer, format="PNG", quality=20, optimize=True)
buffer.seek(0)
self._image = buffer.read()
else:
self._image = None
# Create All Tables in the Database
def create_tables():
Base.metadata.create_all(engine)
# Delete All Tables in the Database
def delete_tables():
Base.metadata.drop_all(engine)
def insert_test_data():
with engine.connect() as con:
with open("./testdata.sql", "r", encoding="utf-8") as file:
queries = file.read().split(';')
for query in queries:
if query.strip(): # Skip empty queries
con.execute(text(query))
con.commit()
if __name__ == "__main__":
delete_tables()
create_tables()
if "--release" not in sys.argv:
insert_test_data()
else:
username = input("Username: ")
full_name = input("Full name: ")
email = input("Email: ")
password = input("Password: ")
admin = User(username, email, generate_password_hash(password), full_name, date.today())
admin.isAdmin = True
db_session.add(admin)
db_session.commit()