added set admin function

This commit is contained in:
Linus E 2022-04-25 19:56:41 +02:00
parent 2ed419713b
commit 03c02aaabc
2 changed files with 64 additions and 4 deletions

View File

@ -37,6 +37,7 @@ class API_Handler:
set_portfolio(user_id, portfolio): sets the portfolio of the user set_portfolio(user_id, portfolio): sets the portfolio of the user
delete_portfolio(user_id, portfolio): deletes the portfolio of the user delete_portfolio(user_id, portfolio): deletes the portfolio of the user
set_cron_interval(user_id, interval): sets the cron interval of the user set_cron_interval(user_id, interval): sets the cron interval of the user
set_admin(email, is_admin): sets the admin status of the user with the given email
""" """
@ -334,6 +335,22 @@ class API_Handler:
return req.status_code return req.status_code
def set_admin(self, email, is_admin):
"""sets the admin of the user
Args:
email (string): email of the user
is_admin (String): "true" if user should be Admin, "false" if not
Returns:
int: status code
"""
with r.Session() as s:
headers = {'Authorization': 'Bearer ' + self.token} # only bot token is needed, user is chosen by email
req = s.put(self.db_adress + "/user/setAdmin", json={"admin": str(is_admin),"email": str(email)})
return req.status_code
if __name__ == "__main__": # editable, just for basic on the go testing of new functions if __name__ == "__main__": # editable, just for basic on the go testing of new functions
print("This is a module for the telegram bot. It is not intended to be run directly.") print("This is a module for the telegram bot. It is not intended to be run directly.")
@ -346,6 +363,8 @@ if __name__ == "__main__": # editable, just for basic on the go testing of new f
user = handler.get_user(user_id = 1709356058) user = handler.get_user(user_id = 1709356058)
print(user) print(user)
all_users = handler.get_all_users() all_users = handler.get_all_users()
admin_status = handler.set_admin("test@test.com", "true")
print(admin_status)
print(all_users) print(all_users)
print(shares) print(shares)
sys.exit(1) sys.exit(1)

View File

@ -97,7 +97,7 @@ def send_version(message):
@bot.message_handler(commands=['help', 'Help']) # /help -> sending all functions @bot.message_handler(commands=['help', 'Help']) # /help -> sending all functions
def send_welcome(message): def send_help(message):
""" Send all functions """ Send all functions
:type message: message object bot :type message: message object bot
@ -136,11 +136,52 @@ def send_all_users(message):
username = user['username'] username = user['username']
email = user['email'] email = user['email']
user_id = user['telegram_user_id'] id = user['telegram_user_id']
cron = user['cron'] cron = user['cron']
admin = user['admin'] admin = user['admin']
bot.send_message(chat_id=user_id, text=f'Username: {username}\nEmail: {email}\nID: {user_id}\nCron: {cron}\nAdmin: {admin}') # format user data into readable message text bot.send_message(chat_id=user_id, text=f'Username: {username}\nEmail: {email}\nID: {id}\nCron: {cron}\nAdmin: {admin}') # format user data into readable message text
@bot.message_handler(commands=['setAdmin', 'SetAdmin']) # set admin rights to user
def set_admin(message):
""" Set admin rights to user
:type message: message object bot
:param message: message that was reacted to, in this case always containing '/setAdmin'
:raises: none
:rtype: none
"""
user_id = int(message.from_user.id)
user_data = api_handler.get_user(user_id)
if(user_data["admin"] == False): # check if user has admin rights
bot.reply_to(message, "You have to be an admin to use this command")
return
bot.send_message(chat_id=user_id, text='send email and "true" if this account should have admin rights, else "false"\n in format: <email>,<is_admin>') # ask for email and admin rights to set
bot.register_next_step_handler(message, set_admin_step)
def set_admin_step(message):
str_message = str(message.text)
args_message = str_message.split(',') # split message into email and admin rights
if len(args_message) != 2: # make sure 2 args (email,is_admin) are given
bot.reply_to(message, "exactly 2 arguments (<email>,<is_admin>) required, try again")
return
email = args_message[0]
is_admin = args_message[1]
status = api_handler.set_admin(email, is_admin) # set admin in db
if(status == 200):
bot.reply_to(message, "Admin rights set")
else:
bot.reply_to(message, f"Admin rights could not be set ({status})")
@bot.message_handler(commands=['me', 'Me']) # /me -> sending user info @bot.message_handler(commands=['me', 'Me']) # /me -> sending user info