summaryrefslogtreecommitdiff
path: root/stashbox
diff options
context:
space:
mode:
Diffstat (limited to 'stashbox')
-rw-r--r--stashbox/models.py57
-rw-r--r--stashbox/stashbox.dbbin0 -> 7168 bytes
-rw-r--r--stashbox/stashbox.py96
-rw-r--r--stashbox/static/main.css26
-rw-r--r--stashbox/templates/base.html14
-rw-r--r--stashbox/templates/index.html37
-rw-r--r--stashbox/templates/useradd.html25
-rw-r--r--stashbox/templates/usersadmin.html25
8 files changed, 280 insertions, 0 deletions
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
--- /dev/null
+++ b/stashbox/stashbox.db
Binary files 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)
+
+<!DOCTYPE html>
+<html lang="en">
+<head>
+ <title>Shareguard</title>
+ <link rel="stylesheet" type="text/css" href="/static/main.css" />
+</head>
+<body>
+
+$:page
+
+</body>
+</html>
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)
+
+<div class="main">
+ <header>
+ <h1>Existing folders</h1>
+ </header>
+
+ <form action="" method="post">
+
+ <table class="itemlist">
+ <tr>
+ <th>Folder</th>
+ <th>Owner</th>
+ <th>Users</th>
+ </tr>
+ $for box in boxes:
+ <tr>
+ <td><a href="/admin/folder/$box['id']/">$box['name']</a></td>
+ <td>$box['owner']</td>
+ <td>
+ ${", ".join(box['users'])}
+ </td>
+ </tr>
+ </table>
+
+</div>
+
+<div class="right">
+ <header>
+ <h1>Add a folder</h1>
+ </header>
+
+ <form action="" method="post">
+ $:form.render()
+ </form>
+
+</div>
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)
+
+<div class="main">
+
+ <table class="itemlist">
+ <tr>
+ <th>User</th>
+ <th>Password</th>
+ </tr>
+ $for user in users:
+ <tr>
+ <td>$user.username</td>
+ <td>$user.password</td>
+ </tr>
+ </table>
+
+</div>
+
+<div class="right">
+
+ <form action="" method="post">
+ $:form.render()
+ </form>
+
+</div>
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)
+
+<div class="main">
+
+ <table class="itemlist">
+ <tr>
+ <th>User</th>
+ <th>Password</th>
+ </tr>
+ $for user in users:
+ <tr>
+ <td>$user.username</td>
+ <td>$user.password</td>
+ </tr>
+ </table>
+
+</div>
+
+<div class="right">
+
+ <form action="" method="post">
+ $:form.render()
+ </form>
+
+</div>