Skip to content

Commit 4d39df8

Browse files
committed
chore(filestore): add docstrings and consistant return types
1 parent 7732cd5 commit 4d39df8

File tree

1 file changed

+81
-9
lines changed

1 file changed

+81
-9
lines changed

users/filestore.py

+81-9
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,36 @@
1111

1212

1313
def get_user_scope(user: User):
14+
""" Helper method to construct single user filebrowser scope.
15+
Args:
16+
user (User): The User model this action is for.
17+
18+
Returns:
19+
path (string): The single user scope path relative to root filebrowser path.
20+
"""
1421
return f"./users/{user.username}"
1522

1623

1724
def get_admin_login():
25+
""" Helper method to construct admin filebrowser login json string.
26+
27+
Returns:
28+
admin_login (string): The admin login json string.
29+
"""
1830
admin_login = {"username": os.environ["STORE_ADMIN_USERNAME"],
1931
"password": os.environ["STORE_ADMIN_PASSWORD"]}
2032
return admin_login
2133

2234

2335
def get_user_login(user: User):
36+
""" Helper method to construct user.username's filebrowser login json string.
37+
38+
Args:
39+
user (User): The User model this action is for.
40+
41+
Returns:
42+
user_login (string): The user login json string.
43+
"""
2444
if user.username == os.environ["STORE_ADMIN_USERNAME"]:
2545
password = os.environ["STORE_ADMIN_PASSWORD"]
2646
else:
@@ -31,6 +51,15 @@ def get_user_login(user: User):
3151

3252

3353
def use_filestore_auth(user: User):
54+
""" Helper method of to login to user.username's filebrowser account.
55+
56+
Args:
57+
user (User): The User model this action is for.
58+
59+
Returns:
60+
fs_user_token (string): Updated filebrowser api jwt for user.username.
61+
http_status (integer): HTTP status code from filebrowser api login.
62+
"""
3463
if not user.is_authenticated:
3564
return None
3665
verify, host = get_rest_host()
@@ -42,6 +71,17 @@ def use_filestore_auth(user: User):
4271

4372

4473
def get_filestore_token(user_login, host, verify):
74+
""" Uses the filebrowser api to login the user.username's filebrowser account and return their auth jwt.
75+
76+
Args:
77+
user_login (string): The user login json string.
78+
host (string): The django runtime hostname.
79+
verify (bool): True to verify the hostname tls certificate.
80+
81+
Returns:
82+
fs_user_token (string): Updated filebrowser api jwt for user.username.
83+
http_status (integer): HTTP status code from filebrowser api login.
84+
"""
4585
try:
4686
r_userlogin = requests.get(f"https://{host}/storemng/api/login",
4787
data=json.dumps(user_login), verify=verify, timeout=FS_API_TIMEOUT)
@@ -53,22 +93,30 @@ def get_filestore_token(user_login, host, verify):
5393

5494

5595
def add_filestore_auth(user: User):
96+
""" Uses the filebrowser api to add the user.username's filebrowser account and return their auth jwt.
97+
98+
Args:
99+
user (User): The User model this action is for.
100+
101+
Returns:
102+
fs_user_token (string): Updated filebrowser api jwt for user.username.
103+
"""
56104
if not user.is_authenticated:
57105
return None
58106
verify, host = get_rest_host()
59107
# get auth for setting new user
60108
admin_login = get_admin_login()
61109
admin_token, status = get_filestore_token(admin_login, host, verify)
62110
if not admin_token:
63-
return False
111+
return None
64112
# get user defaults from global settings
65113
try:
66114
r_gset = requests.get(f"https://{host}/storemng/api/settings",
67115
headers={"X-Auth": admin_token}, verify=verify, timeout=FS_API_TIMEOUT)
68116
r_gset.raise_for_status()
69117
except (requests.exceptions.ConnectionError, requests.exceptions.HTTPError) as err:
70118
print(err)
71-
return False
119+
return None
72120
settings = r_gset.json()
73121
# set new user options
74122
fs_user = {
@@ -100,24 +148,32 @@ def add_filestore_auth(user: User):
100148

101149

102150
def set_filestore_scope(user: User):
151+
""" Uses the filebrowser api to reset the user.username's filebrowser account scope and return their auth jwt.
152+
153+
Args:
154+
user (User): The User model this action is for.
155+
156+
Returns:
157+
fs_user_token (string): Updated filebrowser api jwt for user.username.
158+
"""
103159
verify, host = get_rest_host()
104160
# get auth for setting new user
105161
admin_login = get_admin_login()
106162
admin_token, status = get_filestore_token(admin_login, host, verify)
107163
if not admin_token:
108-
return False
164+
return None
109165
# find user
110166
fs_user_token, status = use_filestore_auth(user)
111167
if not fs_user_token:
112-
return False
168+
return None
113169
payload = jwt.decode(fs_user_token, options={"verify_signature": False})
114170
try:
115171
r_user = requests.get(f"https://{host}/storemng/api/users/{payload['user']['id']}",
116172
headers={"X-Auth": admin_token}, verify=verify, timeout=FS_API_TIMEOUT)
117173
r_user.raise_for_status()
118174
except (requests.exceptions.ConnectionError, requests.exceptions.HTTPError) as err:
119175
print(err)
120-
return False
176+
return None
121177
edit_user = r_user.json()
122178
if user.is_staff: # admin and staff get root scope
123179
scope = "."
@@ -136,23 +192,31 @@ def set_filestore_scope(user: User):
136192
r_useradd.raise_for_status()
137193
except (requests.exceptions.ConnectionError, requests.exceptions.HTTPError) as err:
138194
print(err)
139-
return False
195+
return None
140196

141197
fs_user_token, status = use_filestore_auth(user)
142198
return fs_user_token
143199

144200

145201
def set_filestore_pass(user: User):
202+
""" Uses the filebrowser api to reset the user.username's filebrowser account password and return their auth jwt.
203+
204+
Args:
205+
user (User): The User model this action is for.
206+
207+
Returns:
208+
fs_user_token (string): Updated filebrowser api jwt for user.username.
209+
"""
146210
if not user.is_authenticated:
147-
return False
211+
return None
148212
if user.username == os.environ["STORE_ADMIN_USERNAME"]:
149-
return False # root admin not allowed pass renew
213+
return None # root admin not allowed pass renew
150214
verify, host = get_rest_host()
151215
# get auth for removing user
152216
admin_login = get_admin_login()
153217
admin_token, status = get_filestore_token(admin_login, host, verify)
154218
if not admin_token:
155-
return False
219+
return None
156220
# find user without valid pass, loop through all
157221
edit_user = {}
158222
try:
@@ -188,6 +252,14 @@ def set_filestore_pass(user: User):
188252

189253

190254
def delete_filestore_user(user: User):
255+
""" Uses the filebrowser api to delete the user.username's filebrowser account and files.
256+
257+
Args:
258+
user (User): The User model this action is for.
259+
260+
Returns:
261+
bool: True when user.username's filebrowser account and files are both removed.
262+
"""
191263
if not user.is_authenticated:
192264
return False
193265
if user.username == os.environ["STORE_ADMIN_USERNAME"]:

0 commit comments

Comments
 (0)