Skip to content

Commit 5f55d14

Browse files
author
david allen
committed
Implemented db as a class.
1 parent 54e521d commit 5f55d14

File tree

3 files changed

+126
-80
lines changed

3 files changed

+126
-80
lines changed

capstone_kv_impl.py renamed to db.py

Lines changed: 47 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import string
2-
from kivy.app import App
3-
from kivy.lang import Builder
4-
from kivy.uix.screenmanager import ScreenManager, Screen
2+
# from kivy.app import App
3+
# from kivy.lang import Builder
4+
# from kivy.uix.screenmanager import ScreenManager, Screen
55

66
# ---------------------
77
# database handling
88
# ---------------------
99
import sqlite3
1010
# source tutorial: https://www.youtube.com/watch?v=byHcYRpMgI4
11-
def db_init(db_name = 'profile.db', table_name = "users"):
11+
def init(db_name = 'profile.db', table_name = "users"):
1212
# connect to database
1313
# database file is created if it doesn't exist
1414
conn = sqlite3.connect(db_name)
@@ -36,7 +36,7 @@ def db_init(db_name = 'profile.db', table_name = "users"):
3636

3737
return
3838

39-
def db_un_exists(un, db_name = 'profile.db'):
39+
def un_exists(un, db_name = 'profile.db'):
4040
conn = sqlite3.connect(db_name)
4141
c = conn.cursor()
4242
c.execute(f"""SELECT un
@@ -47,13 +47,13 @@ def db_un_exists(un, db_name = 'profile.db'):
4747

4848
result = True if len(un_result) else False
4949

50-
print(f'db_un_exists("{un}") result = "{result}"')
50+
print(f'db.un_exists("{un}") result = "{result}"')
5151

5252
conn.close()
5353

5454
return result
5555

56-
def db_profile_new(
56+
def profile_new(
5757
un,
5858
pw,
5959
fn,
@@ -65,7 +65,7 @@ def db_profile_new(
6565
conn = sqlite3.connect(db_name)
6666
c = conn.cursor()
6767

68-
if not db_un_exists(un):
68+
if not un_exists(un):
6969
c.execute(f"""INSERT INTO users VALUES (
7070
'{un}',
7171
'{pw}',
@@ -87,7 +87,7 @@ def db_profile_new(
8787

8888
return
8989

90-
def db_profile_print(*args, **kwargs):
90+
def profile_print(*args, **kwargs):
9191
uns = []
9292
print_all = False
9393
profiles = []
@@ -134,15 +134,15 @@ def db_profile_print(*args, **kwargs):
134134

135135
result = True if len(profiles) else False
136136

137-
print(f'db_profile_print("*{args}, **{kwargs}") profiles =')
137+
print(f'db.profile_print("*{args}, **{kwargs}") profiles =')
138138
for profile in profiles:
139139
print(profile)
140140

141141
conn.close()
142142

143143
return result
144144

145-
def db_un_login(un, pw, db_name = 'profile.db'):
145+
def un_login(un, pw, db_name = 'profile.db'):
146146
conn = sqlite3.connect(db_name)
147147
c = conn.cursor()
148148
c.execute(f"""SELECT rowid, first_name
@@ -156,13 +156,13 @@ def db_un_login(un, pw, db_name = 'profile.db'):
156156

157157
result = True if len(first_name) else False
158158

159-
print(f'db_un_login("{un}", "{pw}") result = "{result}"')
159+
print(f'db.un_login("{un}", "{pw}") result = "{result}"')
160160

161161
conn.close()
162162

163163
return result
164164

165-
def db_profile_update(un, **kwargs):
165+
def profile_update(un, **kwargs):
166166
# handle default kwargs
167167
if 'db_name' not in kwargs.keys():
168168
db_name = 'profile.db'
@@ -179,14 +179,14 @@ def db_profile_update(un, **kwargs):
179179
WHERE un = '{un}'
180180
""")
181181

182-
print(f'db_profile_update({un}, {kwargs}) executed.')
182+
print(f'db.profile_update({un}, {kwargs}) executed.')
183183

184184
conn.commit()
185185
conn.close()
186186

187187
return
188188

189-
def db_profile_delete(un, **kwargs):
189+
def profile_delete(un, **kwargs):
190190
# handle default kwargs
191191
if 'db_name' not in kwargs.keys():
192192
db_name = 'profile.db'
@@ -202,78 +202,46 @@ def db_profile_delete(un, **kwargs):
202202
WHERE un = '{un}'
203203
""")
204204

205-
print(f'db_profile_delete({un}) profile deleted, if it existed in the first place.')
205+
print(f'db.profile_delete({un}) profile deleted, if it existed in the first place.')
206206

207207
conn.commit()
208208
conn.close()
209209

210210
return
211211

212212
# ----------------------
213-
# Kivy Window management
213+
# Main application function
214214
# ----------------------
215-
class SplashWindow(Screen):
216-
pass
217-
218-
class LoginWindow(Screen):
219-
220-
def login_released(self, usnm_input, passw_input):
221-
usnm_input.text = ""
222-
passw_input.text = ""
223-
224-
return
225-
226-
pass
227-
228-
class ProfileWindow(Screen):
229-
pass
230-
231-
class WindowManager(ScreenManager):
232-
pass
233-
234-
kv = Builder.load_file("my.kv")
215+
def main():
216+
# Database initialization and tests
217+
# init()
218+
# un_exists('userNameThatDoesNotExist')
219+
# profile_delete('userNameThatDoesNotExist')
220+
# profile_delete('davidDelSol')
221+
# profile_new(
222+
# 'davidDelSol',
223+
# 'encrypted?',
224+
# 'david',
225+
# 'aloka',
226+
# 'test@preform.io',
227+
# 'salsa,extended intelligence,marathon running in a full suit'
228+
# )
229+
# profile_new(
230+
# 'Python733t',
231+
# 'encrypted?',
232+
# 'Doroteo ',
233+
# 'Bonilla',
234+
# 'doabonilla@yahoo.com',
235+
# 'work,school,sleep,repeat'
236+
# )
237+
# un_login('davidDelSol', 'encrypted?')
238+
# profile_update('davidDelSol', pw = 'definitelyNotEncripted!')
239+
# un_login('davidDelSol', 'definitelyNotEncripted!')
240+
# un_exists('davidDelSol')
241+
# profile_print(all = True)
242+
# profile_print(['Python733t'])
243+
# profile_print('Python733t')
235244

236-
# ----------------------
237-
# Main Kivy app
238-
# ----------------------
239-
class MyMainApp(App):
240-
def build(self):
241-
return kv
242245
pass
243246

244-
245-
# ----------------------
246-
# Main application function
247-
# ----------------------
248-
if __name__ == "__main__":
249-
# Database initialization and tests
250-
db_init()
251-
db_un_exists('userNameThatDoesNotExist')
252-
db_profile_delete('userNameThatDoesNotExist')
253-
db_profile_delete('davidDelSol')
254-
db_profile_new(
255-
'davidDelSol',
256-
'encrypted?',
257-
'david',
258-
'aloka',
259-
'test@preform.io',
260-
'salsa,extended intelligence,marathon running in a full suit'
261-
)
262-
db_profile_new(
263-
'Python733t',
264-
'encrypted?',
265-
'Doroteo ',
266-
'Bonilla',
267-
'doabonilla@yahoo.com',
268-
'work,school,sleep,repeat'
269-
)
270-
db_un_login('davidDelSol', 'encrypted?')
271-
db_profile_update('davidDelSol', pw = 'definitelyNotEncripted!')
272-
db_un_login('davidDelSol', 'definitelyNotEncripted!')
273-
db_un_exists('davidDelSol')
274-
db_profile_print(all = True)
275-
db_profile_print(['Python733t'])
276-
db_profile_print('Python733t')
277-
278-
# Run Kivy app
279-
MyMainApp().run()
247+
main()

db_gui_impl.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import string
2+
from xml.etree.ElementInclude import include
3+
from kivy.app import App
4+
from kivy.lang import Builder
5+
from kivy.uix.screenmanager import ScreenManager, Screen
6+
7+
import db # Teo: use this line to import the db class
8+
9+
# ----------------------
10+
# Kivy Window management
11+
# ----------------------
12+
class SplashWindow(Screen):
13+
14+
pass
15+
16+
class LoginWindow(Screen):
17+
18+
def login_released(self, usnm_input, passw_input):
19+
usnm_input.text = ""
20+
passw_input.text = ""
21+
22+
return
23+
24+
pass
25+
26+
class ProfileWindow(Screen):
27+
pass
28+
29+
class WindowManager(ScreenManager):
30+
pass
31+
32+
kv = Builder.load_file("my.kv")
33+
34+
# ----------------------
35+
# Main Kivy app
36+
# ----------------------
37+
class MyMainApp(App):
38+
def build(self):
39+
return kv
40+
pass
41+
42+
43+
# ----------------------
44+
# Main application function
45+
# ----------------------
46+
if __name__ == "__main__":
47+
# # Teo: use these lines to manipulate database
48+
# Database initialization and tests
49+
db.init()
50+
db.un_exists('userNameThatDoesNotExist')
51+
db.profile_delete('userNameThatDoesNotExist')
52+
db.profile_delete('davidDelSol')
53+
db.profile_new(
54+
'davidDelSol',
55+
'encrypted?',
56+
'david',
57+
'aloka',
58+
'test@preform.io',
59+
'salsa,extended intelligence,marathon running in a full suit'
60+
)
61+
db.profile_new(
62+
'Python733t',
63+
'encrypted?',
64+
'Doroteo ',
65+
'Bonilla',
66+
'doabonilla@yahoo.com',
67+
'work,school,sleep,repeat'
68+
)
69+
db.un_login('davidDelSol', 'encrypted?')
70+
db.profile_update('davidDelSol', pw = 'definitelyNotEncripted!')
71+
db.un_login('davidDelSol', 'definitelyNotEncripted!')
72+
db.un_exists('davidDelSol')
73+
db.profile_print(all = True)
74+
db.profile_print(['Python733t'])
75+
db.profile_print('Python733t')
76+
77+
# Run Kivy app
78+
MyMainApp().run()

my.kv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ WindowManager:
4646
Button:
4747
text: "Submit"
4848
on_release:
49-
root.login_released(usnm, passw)
49+
# root.login_released(usnm, passw)
5050
app.root.current = "profile"
5151
root.manager.transition.direction = "left"
5252

0 commit comments

Comments
 (0)