Skip to content

Commit 011bb06

Browse files
committed
Codes 4 W11
1 parent 5c3ae7a commit 011bb06

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

Week11/db.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import firebase_admin # pip install firebase-admin
2+
from firebase_admin import credentials, db
3+
import pyrebase # pip install pyrebase4
4+
5+
6+
class Firebase:
7+
def __init__(self):
8+
#####################################################################
9+
# Use your own information for your Firebase project below. #
10+
#####################################################################
11+
# You need to download the credentials file from your Firebase #
12+
# project settings. Click on the "Generate new private key" button #
13+
# and save the file as "credentials.json" in the same directory. #
14+
CREDENTIALS_FILE = "credentials.json"
15+
# You can find your database URL in your Firebase project settings. #
16+
# The format is "https://<project_id>.firebasedatabase.app/". #
17+
DATABASE_URL = ""
18+
# You can find your API key in your Firebase project settings. #
19+
# The format is "AIza...". #
20+
# If you can't find your API key, create a new web app in your #
21+
# Firebase project, then you will see the API key. #
22+
API_KEY = ""
23+
# You can find your project ID in your Firebase project settings. #
24+
PROJECT_ID = ""
25+
#####################################################################
26+
# You can find these information in your Firebase project settings. #
27+
# You don't need to change the following code. #
28+
#####################################################################
29+
self.cred = credentials.Certificate(CREDENTIALS_FILE)
30+
firebase_admin.initialize_app(
31+
self.cred,
32+
{
33+
"databaseURL": DATABASE_URL,
34+
},
35+
)
36+
self.ref = db.reference("/")
37+
self.config = {
38+
"apiKey": API_KEY,
39+
"authDomain": f"{PROJECT_ID}.firebaseapp.com",
40+
"databaseURL": DATABASE_URL,
41+
"storageBucket": f"{PROJECT_ID}.appspot.com",
42+
}
43+
self.firebase = pyrebase.initialize_app(self.config)
44+
self.auth = self.firebase.auth()
45+
46+
def login(self, email, password):
47+
user = self.auth.sign_in_with_email_and_password(email, password)
48+
return user
49+
50+
def register(self, email, password):
51+
user = self.auth.create_user_with_email_and_password(email, password)
52+
return user
53+
54+
def get_current_user(self):
55+
return self.auth.current_user
56+
57+
58+
if __name__ == "__main__":
59+
f = Firebase()
60+
print(f.ref.get())
61+
f.ref.set({"name": "Bora Canbula"})
62+
print(f.ref.get())
63+
try:
64+
f.login("prof@university.edu", "password")
65+
except Exception as e:
66+
f.register("prof@university.edu", "password")
67+
print(f.get_current_user())

Week11/logo.png

71.7 KB
Loading

Week11/pycat.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
from db import Firebase
2+
import wx # pip install wxPython
3+
4+
5+
class LoginFrame(wx.Frame):
6+
def __init__(self, parent, title, f: Firebase):
7+
super(LoginFrame, self).__init__(parent, title=title, size=(400, 550))
8+
9+
self.f = f
10+
self.user = None
11+
12+
self.init_ui()
13+
self.Centre()
14+
self.Show()
15+
16+
def init_ui(self):
17+
panel = wx.Panel(self)
18+
sizer = wx.BoxSizer(wx.VERTICAL)
19+
20+
logo = wx.StaticBitmap(panel, bitmap=wx.Bitmap("logo.png"))
21+
22+
email_label = wx.StaticText(
23+
panel, label="Email", style=wx.ALIGN_CENTER, size=(200, 20)
24+
)
25+
self.email_entry = wx.TextCtrl(
26+
panel, size=(200, 20), style=wx.TE_CENTER, value="prof@university.edu"
27+
)
28+
29+
password_label = wx.StaticText(
30+
panel, label="Password", style=wx.ALIGN_CENTER, size=(200, 20)
31+
)
32+
self.password_entry = wx.TextCtrl(
33+
panel, size=(200, 20), style=wx.TE_PASSWORD | wx.TE_CENTER, value="password"
34+
)
35+
36+
login_button = wx.Button(
37+
panel, label="Login", size=(200, 20), style=wx.ALIGN_CENTER
38+
)
39+
login_button.Bind(wx.EVT_BUTTON, self.on_login)
40+
41+
sizer.Add(-1, 10)
42+
sizer.Add(logo, 0, wx.ALIGN_CENTER)
43+
sizer.Add(-1, 10)
44+
sizer.Add(email_label, 0, wx.ALIGN_CENTER)
45+
sizer.Add(-1, 10)
46+
sizer.Add(self.email_entry, 0, wx.ALIGN_CENTER)
47+
sizer.Add(-1, 10)
48+
sizer.Add(password_label, 0, wx.ALIGN_CENTER)
49+
sizer.Add(-1, 10)
50+
sizer.Add(self.password_entry, 0, wx.ALIGN_CENTER)
51+
sizer.Add(-1, 20)
52+
sizer.Add(login_button, 0, wx.ALIGN_CENTER)
53+
54+
panel.SetSizer(sizer)
55+
56+
def on_login(self, event):
57+
email = self.email_entry.GetValue()
58+
password = self.password_entry.GetValue()
59+
try:
60+
self.user = self.f.login(email, password)
61+
print("Login successful as ", self.user)
62+
except Exception as e:
63+
self.user = self.f.register(email, password)
64+
print("Registration successful as ", self.user)
65+
self.Close()
66+
67+
68+
if __name__ == "__main__":
69+
app = wx.App()
70+
f = Firebase()
71+
login_frame = LoginFrame(None, title="Login to CAT", f=f)
72+
app.MainLoop()

0 commit comments

Comments
 (0)