1
+ import string
1
2
from kivy .app import App
2
3
from kivy .lang import Builder
3
4
from kivy .uix .screenmanager import ScreenManager , Screen
@@ -35,7 +36,24 @@ def db_init(db_name = 'profile.db', table_name = "users"):
35
36
36
37
return
37
38
38
- def db_newprofile (
39
+ def db_un_exists (un , db_name = 'profile.db' ):
40
+ conn = sqlite3 .connect (db_name )
41
+ c = conn .cursor ()
42
+ c .execute (f"""SELECT un
43
+ FROM users
44
+ WHERE un = '{ un } '
45
+ """ )
46
+ un_result = c .fetchall ()
47
+
48
+ result = True if len (un_result ) else False
49
+
50
+ print (f'db_un_exists("{ un } ") result = "{ result } "' )
51
+
52
+ conn .close ()
53
+
54
+ return result
55
+
56
+ def db_profile_new (
39
57
un ,
40
58
pw ,
41
59
fn ,
@@ -69,7 +87,62 @@ def db_newprofile(
69
87
70
88
return
71
89
72
- def db_login (un , pw , db_name = 'profile.db' ):
90
+ def db_profile_print (* args , ** kwargs ):
91
+ uns = []
92
+ print_all = False
93
+ profiles = []
94
+ if len (args ):
95
+ for arg in args :
96
+ if type (arg ) == str :
97
+ uns .append (arg )
98
+ if type (arg ) == list and len (arg ) and type (arg [0 ]) == str :
99
+ for un in arg :
100
+ uns .append (un )
101
+
102
+ # print(f'uns = {uns}')
103
+
104
+ # handle default kwargs
105
+ if 'db_name' not in kwargs .keys ():
106
+ db_name = 'profile.db'
107
+ pass
108
+ else :
109
+ db_name = kwargs ['db_name' ]
110
+ pass
111
+ if 'all' in kwargs .keys ():
112
+ # print all profiles
113
+ print_all = kwargs ['all' ]
114
+ pass
115
+
116
+ conn = sqlite3 .connect (db_name )
117
+ c = conn .cursor ()
118
+ if print_all :
119
+ c .execute (f"""SELECT rowid, *
120
+ FROM users
121
+ ORDER BY un
122
+ """ )
123
+ profiles = c .fetchall ()
124
+ pass
125
+ else :
126
+ for un in uns :
127
+ print (f'un = { un } ' )
128
+ c .execute (f"""SELECT rowid, *
129
+ FROM users
130
+ WHERE un = '{ un } '
131
+ """ )
132
+ profiles .append (c .fetchone ())
133
+
134
+
135
+ result = True if len (profiles ) else False
136
+
137
+ print (f'db_profile_print("*{ args } , **{ kwargs } ") profiles =' )
138
+ for profile in profiles :
139
+ print (profile )
140
+
141
+ conn .close ()
142
+
143
+ return result
144
+
145
+ def db_un_login (un , pw , db_name = 'profile.db' ):
73
146
conn = sqlite3 .connect (db_name )
74
147
c = conn .cursor ()
75
148
c .execute (f"""SELECT rowid, first_name
@@ -83,16 +156,20 @@ def db_login(un, pw, db_name = 'profile.db'):
83
156
84
157
result = True if len (first_name ) else False
85
158
86
- print (f'db_login ("{ un } ", "{ pw } ") result = "{ result } "' )
159
+ print (f'db_un_login ("{ un } ", "{ pw } ") result = "{ result } "' )
87
160
88
161
conn .close ()
89
162
90
163
return result
91
164
92
- def db_update_profile (un , ** kwargs ):
165
+ def db_profile_update (un , ** kwargs ):
93
166
# handle default kwargs
94
- if " db_name" not in kwargs .keys ():
167
+ if ' db_name' not in kwargs .keys ():
95
168
db_name = 'profile.db'
169
+ pass
170
+ else :
171
+ db_name = kwargs ['db_name' ]
172
+ pass
96
173
97
174
conn = sqlite3 .connect (db_name )
98
175
c = conn .cursor ()
@@ -102,29 +179,35 @@ def db_update_profile(un, **kwargs):
102
179
WHERE un = '{ un } '
103
180
""" )
104
181
105
- print (f'db_update_profile ({ un } , { kwargs } ) executed.' )
182
+ print (f'db_profile_update ({ un } , { kwargs } ) executed.' )
106
183
107
184
conn .commit ()
108
185
conn .close ()
109
186
110
187
return
111
188
112
- def db_un_exists (un , db_name = 'profile.db' ):
189
+ def db_profile_delete (un , ** kwargs ):
190
+ # handle default kwargs
191
+ if 'db_name' not in kwargs .keys ():
192
+ db_name = 'profile.db'
193
+ pass
194
+ else :
195
+ db_name = kwargs ['db_name' ]
196
+ pass
197
+
113
198
conn = sqlite3 .connect (db_name )
114
199
c = conn .cursor ()
115
- c . execute ( f"""SELECT un
116
- FROM users
200
+
201
+ c . execute ( f"""DELETE from users
117
202
WHERE un = '{ un } '
118
203
""" )
119
- un_result = c .fetchall ()
120
-
121
- result = True if len (un_result ) else False
122
204
123
- print (f'db_un_exists(" { un } ") result = " { result } " ' )
205
+ print (f'db_profile_delete( { un } ) profile deleted, if it existed in the first place. ' )
124
206
207
+ conn .commit ()
125
208
conn .close ()
126
209
127
- return result
210
+ return
128
211
129
212
# ----------------------
130
213
# Kivy Window management
@@ -165,26 +248,32 @@ def build(self):
165
248
if __name__ == "__main__" :
166
249
# Database initialization and tests
167
250
db_init ()
168
- db_newprofile (
251
+ db_un_exists ('userNameThatDoesNotExist' )
252
+ db_profile_delete ('userNameThatDoesNotExist' )
253
+ db_profile_delete ('davidDelSol' )
254
+ db_profile_new (
169
255
'davidDelSol' ,
170
256
'encrypted?' ,
171
257
'david' ,
172
258
'aloka' ,
173
259
'test@preform.io' ,
174
260
'salsa,extended intelligence,marathon running in a full suit'
175
261
)
176
- db_newprofile (
262
+ db_profile_new (
177
263
'Python733t' ,
178
264
'encrypted?' ,
179
265
'Doroteo ' ,
180
266
'Bonilla' ,
181
267
'doabonilla@yahoo.com' ,
182
268
'work,school,sleep,repeat'
183
269
)
184
- db_login ('davidDelSol' , 'encrypted?' )
185
- db_update_profile ('davidDelSol' , pw = 'definitelyNotEncripted!' )
186
- db_login ('davidDelSol' , 'definitelyNotEncripted!' )
270
+ db_un_login ('davidDelSol' , 'encrypted?' )
271
+ db_profile_update ('davidDelSol' , pw = 'definitelyNotEncripted!' )
272
+ db_un_login ('davidDelSol' , 'definitelyNotEncripted!' )
187
273
db_un_exists ('davidDelSol' )
274
+ db_profile_print (all = True )
275
+ db_profile_print (['Python733t' ])
276
+ db_profile_print ('Python733t' )
188
277
189
278
# Run Kivy app
190
279
MyMainApp ().run ()
0 commit comments