From efc395dfbec772d09703b4cfbeead17c3f38a2cb Mon Sep 17 00:00:00 2001 From: Aaron Williamson Date: Fri, 1 Jul 2011 14:47:02 -0400 Subject: Added interface code --- stashbox/models.py | 57 ++++++++++++++++++++++ stashbox/stashbox.db | Bin 0 -> 7168 bytes stashbox/stashbox.py | 96 +++++++++++++++++++++++++++++++++++++ stashbox/static/main.css | 26 ++++++++++ stashbox/templates/base.html | 14 ++++++ stashbox/templates/index.html | 37 ++++++++++++++ stashbox/templates/useradd.html | 25 ++++++++++ stashbox/templates/usersadmin.html | 25 ++++++++++ 8 files changed, 280 insertions(+) create mode 100644 stashbox/models.py create mode 100644 stashbox/stashbox.db create mode 100644 stashbox/stashbox.py create mode 100644 stashbox/static/main.css create mode 100644 stashbox/templates/base.html create mode 100644 stashbox/templates/index.html create mode 100644 stashbox/templates/useradd.html create mode 100644 stashbox/templates/usersadmin.html diff --git a/stashbox/models.py b/stashbox/models.py new file mode 100644 index 0000000..47b7ef2 --- /dev/null +++ b/stashbox/models.py @@ -0,0 +1,57 @@ +import web + +db = web.database(dbn='sqlite', db='stashbox.db') + +def get_folders(): + folder_result = db.query(""" +SELECT d.id, d.name, u.username as owner + FROM directory d + JOIN user u ON d.owner_id = u.id + ORDER BY d.name +""") + + folders = [] + + for row in folder_result: + folder = {} + folder['id'] = row.id + folder['name'] = row.name + folder['owner'] = row.owner + folder['users'] = [] + + user_result = db.query(""" + SELECT u.username + FROM user u, directory_user a + WHERE a.user_id = u.id + AND a.directory_id = %s + ORDER BY u.username + """ % row.id) + + for user in user_result: + folder['users'].append(user.username) + + folders.append(folder) + + return folders + +def new_folder(name, owner): + result = db.select('directory', where="name=$name", vars=locals()) + + if not result: + return db.insert('directory', name=name.replace("/",""), owner_id=owner) + +def del_folder(id): + db.delete('directory', where="id=$id", vars=locals()) + db.delete('directory_user', where="directory_id=$id", vars=locals()) + +def get_users(): + return db.select('user') + +def authorize_user(user_id, folder_id): + return db.insert('directory_user', directory_id=folder_id, user_id=user_id) + +def add_user(username, password): + result = db.select('user', where="username=$username", vars=locals()) + + if not result: + return db.insert('user', username=username, password=password) diff --git a/stashbox/stashbox.db b/stashbox/stashbox.db new file mode 100644 index 0000000..7cf98c0 Binary files /dev/null and b/stashbox/stashbox.db differ diff --git a/stashbox/stashbox.py b/stashbox/stashbox.py new file mode 100644 index 0000000..ba2188d --- /dev/null +++ b/stashbox/stashbox.py @@ -0,0 +1,96 @@ +""" Basic todo list using webpy 0.3 """ +import web +import models + +### Url mappings + +urls = ( + '/', 'Index', + '/admin/users/', 'UsersAdmin', + '/del/(\d+)', 'Delete' +) + +### Templates +render = web.template.render('templates', base='base') + +class Index: + + result = models.get_users() + users = [] + + for user in result: + users.append((user.id, user.username)) + + form = web.form.Form( + web.form.Textbox('name', web.form.notnull, description="Name:"), + web.form.Dropdown('owner_id', users, description="Owner:"), + web.form.Dropdown('users', + users, + description="Users:", + **{'multiple': 'multiple', 'size': 5} + ), + web.form.Textbox('new_name', description="New user:"), + web.form.Password('new_password', description="New user password:"), + web.form.Button('Add stash'), + ) + + def GET(self): + """ Show page """ + folders = models.get_folders() + + return render.index(folders, self.form) + + def POST(self): + """ Add new entry """ + form = self.form() + if not form.validates(): + folders = models.get_folders() + return render.index(folders, form) + + folder_id = models.new_folder(form.d.name, form.d.owner_id) + + for user_id in form.d.users: + models.authorize_user(user_id, folder_id) + + if form.d.new_name: + new_user_id = models.add_user(form.d.new_name, form.d.new_password) + models.authorize_user(new_user_id, folder_id) + + scoob + + raise web.seeother('/') + +class Delete: + + def POST(self, id): + """ Delete based on ID """ + id = int(id) + models.del_folder(id) + raise web.seeother('/') + +class UsersAdmin: + form = web.form.Form( + web.form.Textbox('username', web.form.notnull, description="Username:"), + web.form.Password('password', web.form.notnull, description="Password:"), + web.form.Button('Add user'), + ) + + def GET(self): + """ Show page """ + users = models.get_users() + form = self.form() + return render.usersadmin(users, form) + + def POST(self): + """ Add new entry """ + form = self.form() + if not form.validates(): + users = models.get_users() + return render.index(users, form) + models.add_user(form.d.username, form.d.password) + raise web.seeother('/admin/users/') + +app = web.application(urls, globals()) + +if __name__ == '__main__': + app.run() diff --git a/stashbox/static/main.css b/stashbox/static/main.css new file mode 100644 index 0000000..62ff181 --- /dev/null +++ b/stashbox/static/main.css @@ -0,0 +1,26 @@ +h1 { + margin: 5px 0 10px 0; + font-weight: normal; +} + +th { + text-align: left; +} + +td { + padding: 3px 10px; +} + +.main { + float: left; +} + +.right { + float: right; + background-color: #ddd; + padding: 10px; +} + +select { + width: 15em; +} \ No newline at end of file diff --git a/stashbox/templates/base.html b/stashbox/templates/base.html new file mode 100644 index 0000000..7171658 --- /dev/null +++ b/stashbox/templates/base.html @@ -0,0 +1,14 @@ +$def with (page) + + + + + Shareguard + + + + +$:page + + + diff --git a/stashbox/templates/index.html b/stashbox/templates/index.html new file mode 100644 index 0000000..9c59de9 --- /dev/null +++ b/stashbox/templates/index.html @@ -0,0 +1,37 @@ +$def with (boxes, form) + +
+
+

Existing folders

+
+ +
+ + + + + + + + $for box in boxes: + + + + + +
FolderOwnerUsers
$box['name']$box['owner'] + ${", ".join(box['users'])} +
+ +
+ +
+
+

Add a folder

+
+ + + $:form.render() + + +
diff --git a/stashbox/templates/useradd.html b/stashbox/templates/useradd.html new file mode 100644 index 0000000..a916386 --- /dev/null +++ b/stashbox/templates/useradd.html @@ -0,0 +1,25 @@ +$def with (users, form) + +
+ + + + + + + $for user in users: + + + + +
UserPassword
$user.username$user.password
+ +
+ +
+ +
+ $:form.render() +
+ +
diff --git a/stashbox/templates/usersadmin.html b/stashbox/templates/usersadmin.html new file mode 100644 index 0000000..a916386 --- /dev/null +++ b/stashbox/templates/usersadmin.html @@ -0,0 +1,25 @@ +$def with (users, form) + +
+ + + + + + + $for user in users: + + + + +
UserPassword
$user.username$user.password
+ +
+ +
+ +
+ $:form.render() +
+ +
-- cgit v1.2.3