summaryrefslogtreecommitdiff
path: root/stashbox/models.py
blob: 41a58d2cfbe6a2aa5c4756042ab1de515e7de58a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import web

db = web.database(dbn='sqlite', db='stashbox.db')

def get_folders():
    folder_result = db.query("""
SELECT DISTINCT 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 DISTINCT 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 add_folder(name, owner):
    where_vars = {'name': name}
    folders = db.select('directory', where=web.db.sqlwhere(where_vars))

    if not folders:
        return db.insert('directory', name=name.replace("/",""), owner_id=owner)
    else:
        # Can't add non-unique folders
        return False

def get_folder(id):
    where_vars = {'id': id}
    directories = db.select('directory', where=web.db.sqlwhere(where_vars))
    return get_single(directories)

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'])

    db.delete('directory_user', where="directory_id=$did", 
              vars={'did':update_values['id']})

    for id in update_values['users']:
        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

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())

def get_users(as_list=False):
    result = db.select('user')

    if as_list:
        users = []

        for user in result:
            users.append((user.id, user.username))

        return users
    else:
        return result

def get_user(id):
    where_vars = {'id': id}
    return get_single(db.select('user', where=web.db.sqlwhere(where_vars)))

def update_user(id, name, password):
    db.update('user', where="id=$id", vars=locals(),
              name=name, password=password)

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)

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))

    if not auths:
        return False
    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"]

def get_single(obj):
    return obj[0]