Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
News_Service_ALLP1/UserInterface.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
207 lines (156 sloc)
6.37 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from Database import DatabaseManager # to manage data in .csv files using Pandas | |
''' | |
Class used to simulate a user interface | |
''' | |
class UserInterface: | |
dm = DatabaseManager() # instance of Database manager used for data operations | |
''' | |
Method: MainMenu | |
At the start of the program It displays an interface that allows the user to choose the actions to perform | |
''' | |
def mainMenu(self): | |
menu = ''' | |
------------------ MAIN MENU ---------------------- | |
Press following numbers to select | |
1 - Admin [ Users' subscriptions and management ] | |
2 - Login | |
3 - Sign up to News Service | |
4 - Send e-mails [ Admin only ] | |
5 - Exit | |
---------------------------------------------------- | |
''' | |
print(menu) # prints the Main menu | |
''' | |
Method: AdminMenu. | |
It displays the system administrator control panel once the administrator is logged in. | |
It allows to read the subscriptions of all users and delete the subscribed users. | |
''' | |
def adminMenu(self): | |
while True: | |
# print the menu | |
menu = ''' | |
------------------- ADMIN MENU --------------------- | |
Press following numbers to select | |
1 - See all users' subscriptions | |
2 - Delete User | |
3 - Return to Main Menu | |
---------------------------------------------------- | |
''' | |
print(menu) | |
_sel = int(input("> ")) # | |
# displays the subscription database | |
if _sel == 1: | |
self.dm.printSubscriptions() | |
# delete user | |
elif _sel == 2: | |
uname = input("Enter username: ") # asks for the username to be deleted | |
self.dm.deleteUser(uname) | |
elif _sel == 3: | |
return | |
else: | |
print("Wrong selection") | |
''' | |
Method: UserMenu. | |
Displays the user's control panel once they are logged in. | |
Allows user to subscribe or unsubscribe from news categories. | |
''' | |
def userMenu(self, uname): | |
categories = self.dm.getSubscriptionsCategories() | |
while True: | |
# prints the menu | |
menu = ''' | |
-------------------- USER MENU -------------------- | |
Press following numbers to select | |
1 - View the news category you subscribed | |
2 - Subscribe to a news category | |
3 - Unsubscribe from a news category | |
4 - Unsubscribe from News Service | |
5 - Return to Main Menu | |
--------------------------------------------------- | |
''' | |
print(menu) | |
_sel = int(input("> ")) | |
# displays news categories subscribed by the user | |
if _sel == 1: | |
userData = self.dm.getSubscriptionsFor(uname).to_dict('records')[0] | |
if 'y' not in userData.values(): # if there aren't active subscriptions | |
print('You have not yet subscribed to any news category \nPlease subscribe to one or more categories ') | |
for k, v in userData.items(): | |
if v == 'y': | |
print(k, ":", "Subcribed") # print the keys and "subscribed" next to it | |
# subscription to a news category | |
elif _sel == 2: | |
print("You can subscribe to any of these categories \n", categories) # gets all columns and print them | |
catsel = input("Please, type a news category: ") | |
if not catsel in categories: # if entered category is not in database columns | |
print("invalid selection") | |
else: | |
self.dm.subscribeUserToCategory(uname,catsel) | |
# unsubscription from a news category | |
elif _sel == 3: | |
print("Please, enter the news category you want to unsubscribe from \n", categories) # show all cats | |
catsel = input("Please, type a news category: ") # | |
if not catsel in categories: # if entered category is not in database columns | |
print("invalid selection") | |
else: | |
self.dm.unsubscribeUserFromCategory(uname, catsel) | |
# unsubscription from all news category | |
elif _sel == 4: | |
self.dm.unsubscribeFromAll(uname) | |
elif _sel == 5: | |
return | |
else: | |
print("Wrong selection") | |
''' | |
Method: RegMenu | |
Displays an interface for user registration. | |
Checks if the username already exists and saves the new data. | |
''' | |
def userSignUp(self): | |
menu = ''' | |
-------------------- USER REGISTRATION ------------------ | |
Please enter your details to sign up to the News Service | |
--------------------------------------------------------- | |
''' | |
print(menu) | |
# asks for user details | |
name = input("Name: ") | |
uname = input("Username (must be unique): ") | |
if self.dm.userExists(uname): # check if entered username exists | |
print("Username already exists, please choose a different one") # if it existas it raises an error | |
return | |
password = input("password: ") | |
email = input("email: ") | |
self.dm.regUser(uname,name,email,password) | |
''' | |
Method: adminLogin. | |
Displays an interface for the administrator login. | |
Checks if entered data is correct and returns True or False. | |
''' | |
def adminLogin(self): | |
_user = input("username: ") | |
_pass = input("Password: ") | |
if (_user == 'admin') and (_pass == 'admin'): | |
return True # true if user pass matchs | |
else: | |
return False | |
''' | |
Method: userLogin | |
Displays an interface for the user login. | |
Checks if entered data is correct and returns True or False. | |
''' | |
def userLogin(self): | |
df = self.dm.getUsersDF() | |
_user = input('username: ') | |
_pass = input('password: ') | |
if self.dm.userExists(_user): # if username exists | |
userData = df[df['username'] == _user] # reads user data {"name,""username","password"} | |
if userData['password'].str.contains(_pass).any() : # if typed password is correct | |
name = userData['name'] # get the name by username from .csv file | |
print(f"Welcome {name.to_string().split()[1]}") # This operation on the variable name is necessary | |
# to take only the second word of the string (in case it has two names) | |
return True, _user # returns true with name | |
else: | |
return False, None | |
else: | |
return False, None | |