-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase_operations.py
310 lines (245 loc) · 11.1 KB
/
database_operations.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
from asyncio.log import logger
from cmath import log
from gettext import find
from json.tool import main
import logging
from tabnanny import process_tokens
from unicodedata import name
from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider
from flask import flash, session
from application_logging.logger import App_Logger
from DB_connect.db_connect import connect_to_DB
from cassandra.query import tuple_factory
from cassandra.query import dict_factory
# logging.basicConfig(filename="newfile.log",
# format='%(asctime)s %(message)s',
# filemode='w')
# logger = logging.getLogger(__name__)
# logger.setLevel(logging.INFO)
class dbOperation:
"""
This will be used for all the DB related operations
Written By: Sunny Tamang
Version: 1.0
Revisions: None
"""
def __init__(self):
self.logger = App_Logger()
self.connect_to_database = connect_to_DB()
def dataBaseConnection(self):
"""
Method Name: dataBaseConnection
Description: This method creates the connection with the database
Output: Connection to the DB
On Failure: Raise ConnectionError
Written By: Sunny Tamang
Version: 1.0
Revisions: None
"""
try:
cloud_config= {
'secure_connect_bundle': 'secure-connect-onlineeda.zip'
}
auth_provider = PlainTextAuthProvider('JpuZaXAKUbcvezUPigAofrwp', 'iU50rwbZ+fJQqFRjB9H.8wXFl3X54o0C1A.1kEEofB1PXvISBZ15Z8Q43Q3ASWcC7I.9SETYr,b,7CQiwKn7zdzWdiq6ZmfiQpCO+ikf.WbyZ2wS135joqFA_r14uPQN')
cluster = Cluster(cloud=cloud_config, auth_provider=auth_provider)
session = cluster.connect()
# logger.info("Database connected")
row = session.execute("select release_version from system.local;").one()
if row:
print(row)
file = open("Logs/DatabaseConnectionLog.txt",'a+')
self.logger.log(file,"DB connection established successfully")
file.close()
except ConnectionError:
file = open("Logs/DatabaseConnectionLog.txt",'a+')
self.logger.log(file,"Error while connecting to the database")
file.close()
# return row
def registerUser(self, email, first_name, last_name, password):
"""
Method Name: registerUser
Description: This method register the user and save it in DB
Output: Data insertion
On Failure: Raise DB errors
Written By: Sunny Tamang
Version: 1.0
Revisions: None
"""
# cloud_config= {
# 'secure_connect_bundle': 'secure-connect-onlineeda.zip'
# }
# auth_provider = PlainTextAuthProvider('JpuZaXAKUbcvezUPigAofrwp', 'iU50rwbZ+fJQqFRjB9H.8wXFl3X54o0C1A.1kEEofB1PXvISBZ15Z8Q43Q3ASWcC7I.9SETYr,b,7CQiwKn7zdzWdiq6ZmfiQpCO+ikf.WbyZ2wS135joqFA_r14uPQN')
# cluster = Cluster(cloud=cloud_config, auth_provider=auth_provider)
# session = cluster.connect()
session = self.connect_to_database.db_connection()
self.email = email
self.first_name = first_name
self.last_name = last_name
self.password = password
try:
CQLString = f"INSERT INTO users.t_sec_user_mst (id, email, firstname, lastname, password) VALUES (uuid(),'{self.email}', '{self.first_name}', '{self.last_name}', '{self.password}');"
session.execute(CQLString)
file = open("Logs/ApplicationLog.txt",'a+')
self.logger.log(file,"User registration successfull")
file.close()
# if (data_inserted != null):
# flash('You were successfully logged in')
except Exception as e:
file = open("Logs/ApplicationLog.txt",'a+')
self.logger.log(file,e)
file.close()
return False
finally:
return True
def validateUser(self, email, password):
"""
It takes in email and password as parameters and checks if the user exists in the database. If
the user exists, it returns True else it returns False
:param email: The email address of the user
:param password: The password for the user
:return: A tuple of tuples.
"""
# cloud_config= {
# 'secure_connect_bundle': 'secure-connect-onlineeda.zip'
# }
# auth_provider = PlainTextAuthProvider('JpuZaXAKUbcvezUPigAofrwp', 'iU50rwbZ+fJQqFRjB9H.8wXFl3X54o0C1A.1kEEofB1PXvISBZ15Z8Q43Q3ASWcC7I.9SETYr,b,7CQiwKn7zdzWdiq6ZmfiQpCO+ikf.WbyZ2wS135joqFA_r14uPQN')
# cluster = Cluster(cloud=cloud_config, auth_provider=auth_provider)
# session = cluster.connect()
session = self.connect_to_database.db_connection()
self.email = email
self.password = password
try:
CQLString = f"SELECT EMAIL, PASSWORD from users.t_sec_user_mst where email = '{self.email}' and password = '{self.password}' allow filtering;"
session.row_factory = tuple_factory
rows = session.execute(CQLString).one()
if rows:
file = open("Logs/ApplicationLog.txt",'a+')
self.logger.log(file,"User Found. Login Successful")
file.close()
return True
else:
file = open("Logs/ApplicationLog.txt",'a+')
self.logger.log(file,"User not found. Login Failed")
file.close()
return False
except Exception as e:
file = open("Logs/ApplicationLog.txt",'a+')
self.logger.log(file,e)
file.close()
return False
def getUserName(self, email, password):
"""
It takes an email and password as input and returns the firstname and lastname of the user
:param email: [email protected]
:param password: 'b.ZQ.ZQ.ZQ.ZQ.ZQ.ZQ.ZQ.ZQ.ZQ.ZQ.ZQ.ZQ.ZQ.ZQ.ZQ.ZQ.ZQ
:return: The name of the user.
"""
session = self.connect_to_database.db_connection()
self.email = email
self.password = password
name = ''
try:
CQLString = f"SELECT firstname, lastname from users.t_sec_user_mst where email = '{self.email}' and password = '{self.password}' allow filtering;"
session.row_factory = tuple_factory
rows = session.execute(CQLString).one()
name = rows[0].capitalize() +","+ rows[1].capitalize()
if rows:
file = open("Logs/ApplicationLog.txt",'a+')
self.logger.log(file,"User Found.")
file.close()
return name
else:
file = open("Logs/ApplicationLog.txt",'a+')
self.logger.log(file,"User not found.")
file.close()
return ""
except Exception as e:
file = open("Logs/ApplicationLog.txt",'a+')
self.logger.log(file,e)
file.close()
return name
def initialProjectCheckpoint(self, first_name, last_name, user_email, project_name, created_on, last_updated_on, project_status, filename):
"""
It inserts a row into a table
:param first_name: String
:param last_name: "Doe"
:param user_email: [email protected]
:param project_name: "Test Project"
:param created_on: 2020-02-20
:param last_updated_on: 2020-02-20
:param project_status: This is a string that can be either "active" or "inactive"
:return: The return value is a boolean value.
"""
session = self.connect_to_database.db_connection()
self.first_name = first_name
self.last_name = last_name
self.user_email = user_email
self.project_name = project_name
# self.project_id = project_id
self.created_on = created_on
self.last_updated_on = last_updated_on
self.project_status = project_status
self.filename = filename
try:
CQLString = f"INSERT INTO users.user_project_names (first_name, last_name, user_email, project_name, proj_id, created_on,filename, last_updated_on, project_status) VALUES ('{self.first_name}', '{self.last_name}', '{self.user_email}', '{self.project_name}', uuid(), '{self.created_on}','{self.filename}', '{self.last_updated_on}', '{self.project_status}');"
print(CQLString)
session.execute(CQLString)
file = open("Logs/ApplicationLog.txt",'a+')
self.logger.log(file,"Project details added")
file.close()
return True
except Exception as e:
file = open("Logs/ApplicationLog.txt",'a+')
self.logger.log(file,e)
file.close()
return False
def getProjectDetails(self, first_name, last_name, user_email):
"""
It takes in a first name, last name, and email address and returns a list of project names that
the user has access to.
:param first_name: "John"
:param last_name: 'Smith'
:param user_email: '[email protected]'
:return: A list of tuples.
"""
session = self.connect_to_database.db_connection()
self.first_name = first_name
self.last_name = last_name
self.user_email = user_email
# self.project_name = project_name
try:
project_names=[]
project_file_name=[]
CQLString = f"SELECT project_name, filename from users.user_project_names where first_name = '{self.first_name}' and last_name = '{self.last_name}' and user_email = '{self.user_email}' allow filtering;"
print(CQLString)
session.row_factory = tuple_factory
rows = session.execute(CQLString)
for row in rows:
project_names.append(row[0])
project_file_name.append(row[1])
# print(rows)
if rows:
file = open("Logs/ApplicationLog.txt",'a+')
self.logger.log(file,"Project Found: " +len(rows))
file.close()
return tuple(rows)
else:
file = open("Logs/ApplicationLog.txt",'a+')
self.logger.log(file,"No project found")
file.close()
return project_names, project_file_name
except Exception as e:
file = open("Logs/ApplicationLog.txt",'a+')
self.logger.log(file,e)
file.close()
return False
# finally:
# return True
def add_csv_data_to_DB():
"""
This function adds data from a csv file to a database.
"""
pass
# if __name__ == '__main__':
# logging.info('Database connected')