diff options
-rw-r--r-- | mustash/models.py | 71 |
1 files changed, 47 insertions, 24 deletions
diff --git a/mustash/models.py b/mustash/models.py index 9d10fcb..f85d497 100644 --- a/mustash/models.py +++ b/mustash/models.py @@ -16,8 +16,12 @@ import web -db = web.database(dbn='sqlite', db='stashbox.db') +db = web.database(dbn='sqlite', db='mustash.db') + +### Folder methods + +# Get all folders and authorized users (this is kind of an ugly hack) def get_folders(): folder_result = db.query(""" SELECT DISTINCT d.id, d.name, u.username as owner @@ -50,6 +54,7 @@ SELECT DISTINCT d.id, d.name, u.username as owner return folders +# Add a new folder def add_folder(name, owner): where_vars = {'name': name} folders = db.select('directory', where=web.db.sqlwhere(where_vars)) @@ -60,11 +65,24 @@ def add_folder(name, owner): # Can't add non-unique folders return False +# Get folder by ID def get_folder(id): where_vars = {'id': id} directories = db.select('directory', where=web.db.sqlwhere(where_vars)) return get_single(directories) +# Get all users authorized on a folder +def get_folder_users(folder_id): + where_vars = {'directory_id': folder_id} + result = db.select('directory_user', where=web.db.sqlwhere(where_vars)) + users = [] + + for item in result: + users.append(item.user_id) + + return users + +# Update folder def update_folder(update_values): db.update('directory', where="id=$id", vars={'id':update_values['id']}, name=update_values['name'], owner_id=update_values['owner_id']) @@ -76,25 +94,25 @@ def update_folder(update_values): db.insert('directory_user', directory_id=update_values['id'], user_id=id) -def get_folder_users(folder_id): - where_vars = {'directory_id': folder_id} - result = db.select('directory_user', where=web.db.sqlwhere(where_vars)) - users = [] - - for item in result: - users.append(item.user_id) - - return users - +# Delete folder and associated authorizations def delete_folder(id): db.delete('directory', where="id=$id", vars=locals()) db.delete('directory_user', where="directory_id=$id", vars=locals()) -def delete_user(id): - db.delete('user', where="id=$id", vars=locals()) - db.delete('directory_user', where="user_id=$id", vars=locals()) - db.delete('directory', where="owner_id=$id", vars=locals()) +### User methods + +# Add a new user (or retrieve existing user with name supplied) +def add_user(username, password): + users = db.select('user', where="username=$username", vars=locals()) + + if not users: + return db.insert('user', username=username, password=password) + else: + user = users.list().pop() + return user["id"] + +# Get all users (as List if specified; web.py IterBetter otherwise) def get_users(as_list=False): result = db.select('user') @@ -108,18 +126,31 @@ def get_users(as_list=False): else: return result +# Get a single user by ID def get_user(id): where_vars = {'id': id} return get_single(db.select('user', where=web.db.sqlwhere(where_vars))) +# Update user's username & password def update_user(id, username, password): db.update('user', where="id=$id", vars=locals(), username=username, password=password) +# Authorize a given user on a folder def authorize_user(user_id, folder_id): if not is_authorized(user_id, folder_id): db.insert('directory_user', directory_id=folder_id, user_id=user_id) +# Delete user with 'id', every associated authorization, and every +# owned folder +def delete_user(id): + db.delete('user', where="id=$id", vars=locals()) + db.delete('directory_user', where="user_id=$id", vars=locals()) + db.delete('directory', where="owner_id=$id", vars=locals()) + +### Utility methods + +# Is this user authorized on this folder? def is_authorized(user_id, folder_id): where_vars = {'directory_id': folder_id, 'user_id': user_id} auths = db.select('directory_user', where=web.db.sqlwhere(where_vars)) @@ -129,14 +160,6 @@ def is_authorized(user_id, folder_id): else: return True -def add_user(username, password): - users = db.select('user', where="username=$username", vars=locals()) - - if not users: - return db.insert('user', username=username, password=password) - else: - user = users.list().pop() - return user["id"] - +# Return the first row in a IterBetter result def get_single(obj): return obj[0] |