-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
153 lines (112 loc) · 3.66 KB
/
main.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
import bcrypt
import csv
import os
# User Works
def existingUser():
e_username = input("Enter username : ")
e_password = input("Enter password : ").encode('utf-8')
userFound = False
incorrectPassword = False
with open('database.txt','r') as csv_file:
csv_reader = csv.DictReader(csv_file)
for line in csv_reader:
if e_username == line['username']:
userFound = True
if bcrypt.checkpw(e_password,line['password'].encode('utf-8')):
print("\nLogin successful :)\n")
loginUser(e_username)
incorrectPassword =True
break
if not userFound:
print("\nInvalid Username\n")
elif not incorrectPassword:
print("\nIncorrect Password !")
print("Try Again\n")
def registerUser():
r_username = input("Enter username : ")
r_password = input("Enter password : ").encode('utf-8')
r_hashed = bcrypt.hashpw(r_password, bcrypt.gensalt())
with open('database.txt', 'a') as f:
f.write(f"{r_username},{r_hashed.decode('utf-8')}\n")
print("\nREGISTRATION PROCESS COMPLETED !")
print("Now you can login with this username and password\n")
# Blog Works
blogFile = 'blogs.csv'
# create
def createPost(username):
title = input("Enter blog title: ")
content = input("Enter blog content: ")
with open(blogFile, mode='a', newline='') as file:
writer = csv.writer(file)
writer.writerow([username, title, content])
print("Blog post created successfully.")
# modify
def modifyPost(username):
title = input("Enter the title of the blog post to modify: ")
rows = []
found = False
with open(blogFile, mode='r', newline='') as file:
reader = csv.reader(file)
for row in reader:
if row and row[0] == username and row[1] == title:
found = True
new_content = input(f"Enter new content for '{title}': ")
rows.append([username, title, new_content])
else:
rows.append(row)
if not found:
print(f"No blog post with title '{title}' found.")
else:
with open(blogFile, mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerows(rows)
print("Blog post modified successfully.")
# modify
def deletePost(username):
title = input("Enter the title of the blog post to delete: ")
rows = []
found = False
with open(blogFile, mode='r', newline='') as file:
reader = csv.reader(file)
for row in reader:
if row and row[0] == username and row[1] == title:
found = True
else:
rows.append(row)
if not found:
print(f"No blog post with title '{title}' found.")
else:
with open(blogFile, mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerows(rows)
print("Blog post deleted successfully.")
def loginUser(username):
print(f"\nWelcome {username}!")
print("1. Create a new post")
print("2. Delete a post")
print("3. Modify a post")
choice = input("Enter your choice: ")
match choice:
case "1":
createPost(username)
case "2":
deletePost(username)
case "3":
modifyPost(username)
case _:
print("Invalid Option")
while True:
print("Choose an option:")
print("1) Existing User, Login ")
print("2) New User, Register Now ")
print("3) Exit ")
choice = input("Enter the Choice : ")
match choice:
case "1":
existingUser()
case "2":
registerUser()
case "3":
break
case _:
print("Invalid Option")