Skip to content

Commit 0c0108d

Browse files
committed
Updated config file to .secrets
also updated accounts/describe Changes to be committed: modified: .gitignore new file: .secrets.example modified: authentication/selectors.py modified: biocompute/apis.py modified: biocompute/models.py modified: biocompute/selectors.py modified: config/settings.py deleted: server.conf deleted: sever.conf.example
1 parent 992b281 commit 0c0108d

File tree

9 files changed

+60
-255
lines changed

9 files changed

+60
-255
lines changed

.gitignore

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,8 @@ dmypy.json
130130

131131
# --- USER-ADDED IGNORES --- #
132132

133-
# The settings file.
134-
config/settings.py
135-
136133
# The server configuration file.
137-
server.conf
134+
.secrets
138135

139136
# The migrations folder.
140137
# bco_api/api/migrations/

.secrets.example

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[DJANGO_KEYS]
2+
SECRET_KEY=
3+
ANON_KEY=
4+
5+
[SERVER]
6+
PRODUCTION=
7+
SERVER_VERSION=
8+
HOSTNAME=
9+
HUMAN_READABLE_HOSTNAME=
10+
PUBLIC_HOSTNAME=
11+
SERVER_URL=
12+
#DATABASE=
13+
DATABASE=
14+
EMAIL_BACKEND=

authentication/selectors.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
from django.contrib.auth.models import User, Permission
66
from authentication.models import Authentication, NewUser
77
from rest_framework.authtoken.models import Token
8-
8+
from prefix.selectors import get_user_prefixes
9+
from biocompute.selectors import get_authorized_bcos
910

1011
def get_anon()-> User:
1112
"""Get AnonymosUser
@@ -73,27 +74,15 @@ def get_user_info(user: User) -> dict:
7374
other_info = {
7475
"permissions": {},
7576
"account_creation": "",
76-
"account_expiration": "",
7777
}
78-
user_perms = {"user": [], "groups": []}
79-
80-
for permission in user.user_permissions.all():
81-
if permission.name not in user_perms["user"]:
82-
user_perms["user"].append(permission.name)
83-
84-
for group in user.groups.all():
85-
if group.name not in user_perms["groups"]:
86-
user_perms["groups"].append(group.name)
87-
for permission in Permission.objects.filter(group=group):
88-
if permission.name not in user_perms["user"]:
89-
user_perms["user"].append(permission.name)
78+
user_perms = {"prefixes": get_user_prefixes(user), "BCOs": get_authorized_bcos(user)}
9079

9180
other_info["permissions"] = user_perms
9281

9382
other_info["account_creation"] = user.date_joined
9483

9584
return {
96-
"hostname": settings.ALLOWED_HOSTS[0],
85+
"hostname": settings.HOSTNAME,
9786
"human_readable_hostname": settings.HUMAN_READABLE_HOSTNAME,
9887
"public_hostname": settings.PUBLIC_HOSTNAME,
9988
"token": token.key,

biocompute/apis.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
),
5454
"authorized_users": openapi.Schema(
5555
type=openapi.TYPE_ARRAY,
56-
description="Users which can access the BCO draft.",
56+
description="Users that can access the BCO draft.",
5757
items=openapi.Schema(type=openapi.TYPE_STRING, example="tester")
5858
),
5959
"contents": openapi.Schema(

biocompute/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Bco(models.Model):
3030
owner = ForeignKey(User)
3131
String representing the django.contrib.auth.models.User that 'owns' the object
3232
authorized_users: ManyToManyField(User)
33-
String representing the User that has access to the object
33+
String representing the Users that have access to the object
3434
prefix: str
3535
Prefix for the BCO
3636
state:str

biocompute/selectors.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from datetime import datetime
1111
from django.conf import settings
1212
from django.contrib.auth. models import User
13+
from django.db.models import Q
1314
from prefix.selectors import (
1415
user_can_view_prefix,
1516
user_can_modify_prefix,
@@ -225,6 +226,27 @@ def retrieve_bco(bco_accession:str, user:User, bco_version:str=None) -> bool:
225226

226227
return bco_instance
227228

229+
def get_authorized_bcos(user: User):
230+
"""
231+
Retrieve all BioCompute Objects (BCOs) that a specific user is authorized
232+
to access, excluding those in 'DELETE' state.
233+
234+
Parameters:
235+
- user (User):
236+
The Django User instance for whom to retrieve authorized BCOs.
237+
238+
Returns:
239+
- QuerySet:
240+
A Django QuerySet containing the BCOs the user is authorized to access.
241+
"""
242+
243+
bcos = Bco.objects.filter(
244+
Q(owner=user) | Q(authorized_users=user)
245+
).exclude(state='DELETE').values_list('object_id', flat=True).distinct()
246+
247+
248+
return bcos
249+
228250
def object_id_deconstructor(object_id=str) -> list:
229251
"""
230252
Deconstructs a BioCompute Object (BCO) identifier into its constituent

config/settings.py

Lines changed: 17 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -9,80 +9,30 @@
99

1010
# --- SECURITY SETTINGS --- #
1111
# Load the server config file.
12-
server_config = configparser.ConfigParser()
13-
server_config.read(BASE_DIR + "/server.conf")
14-
15-
# Quick-start development settings - unsuitable for production
16-
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
17-
18-
# Is this a production server?
19-
PRODUCTION = server_config["PRODUCTION"]["production"]
12+
secrets = configparser.ConfigParser()
13+
secrets.read(BASE_DIR + "/.secrets")
14+
PRODUCTION = secrets["SERVER"]["PRODUCTION"]
15+
DEBUG = PRODUCTION
2016

2117
# Set the anonymous user's key.
22-
ANON_KEY = server_config["KEYS"]["anon"]
18+
ANON_KEY = secrets["DJANGO_KEYS"]["ANON_KEY"]
2319

2420
# SECURITY WARNING: keep the secret key used in production secret!
25-
SECRET_KEY = "$vz@#@^q(od&$rf&*6^z!m5nh6qw2*cq*j6fha#^h9(r7$xqy4"
21+
SECRET_KEY = secrets["DJANGO_KEYS"]["SECRET_KEY"]
2622

2723
# SECURITY WARNING: don't run with debug turned on in production!
28-
DEBUG = PRODUCTION
2924

25+
# The publicly accessible hostname.
26+
HOSTNAME = secrets["SERVER"]["HOSTNAME"]
3027
# The human-readable hostname.
31-
HUMAN_READABLE_HOSTNAME = server_config["HRHOSTNAME"]["hrnames"]
32-
33-
if server_config["GROUP_PREFIX"]["allow_all_creation"] == "True":
34-
GROUP = True
35-
PREFIX = True
36-
elif server_config["GROUP_PREFIX"]["allow_group_creation"] == "True":
37-
GROUP = True
38-
elif server_config["GROUP_PREFIX"]["allow_prefix_creation"] == "True":
39-
PREFIX = True
40-
28+
HUMAN_READABLE_HOSTNAME = secrets["SERVER"]["HUMAN_READABLE_HOSTNAME"]
4129
# The publicly accessible hostname.
42-
if server_config["PRODUCTION"]["production"] == "True":
43-
PUBLIC_HOSTNAME = server_config["PUBLICHOSTNAME"]["prod_name"]
44-
elif server_config["PRODUCTION"]["production"] == "False":
45-
PUBLIC_HOSTNAME = server_config["PUBLICHOSTNAME"]["name"]
30+
PUBLIC_HOSTNAME = secrets["SERVER"]["PUBLIC_HOSTNAME"]
31+
# import pdb; pdb.set_trace()
4632

47-
# Source: https://dzone.com/articles/how-to-fix-django-cors-error
4833

49-
# Check for open (public) access to the API.
50-
if server_config["REQUESTS_FROM"]["public"].strip() == "false":
51-
52-
# Process the requester groups.
53-
54-
# configparser automatically strips white space off the
55-
# ends of arguments.
56-
requesters = [
57-
server_config["REQUESTS_FROM"][i].strip()
58-
for i in server_config["REQUESTS_FROM"]
59-
]
60-
requesters.remove("false")
61-
requesters = [i.split(",") for i in requesters]
62-
63-
# Flatten the list.
64-
# Source: https://stackabuse.com/python-how-to-flatten-list-of-lists/
65-
flattened = [item.strip() for sublist in requesters for item in sublist]
66-
67-
if server_config["PRODUCTION"]["production"] == "True":
68-
ALLOWED_HOSTS = [
69-
i.strip() for i in server_config["HOSTNAMES"]["prod_names"].split(",")
70-
]
71-
elif server_config["PRODUCTION"]["production"] == "False":
72-
ALLOWED_HOSTS = [
73-
i.strip() for i in server_config["HOSTNAMES"]["names"].split(",")
74-
]
75-
76-
CORS_ORIGIN_ALLOW_ALL = False
77-
CORS_ORIGIN_WHITELIST = tuple(flattened)
78-
79-
elif server_config["REQUESTS_FROM"]["public"].strip() == "true":
80-
if server_config["PRODUCTION"]["production"] == "True":
81-
ALLOWED_HOSTS = [server_config["HOSTNAMES"]["prod_names"].split(",")[0], "*"]
82-
CORS_ORIGIN_ALLOW_ALL = True
83-
elif server_config["PRODUCTION"]["production"] == "False":
84-
ALLOWED_HOSTS = [server_config["HOSTNAMES"]["names"].split(",")[0], "*"]
85-
CORS_ORIGIN_ALLOW_ALL = True
34+
CORS_ORIGIN_ALLOW_ALL = True
35+
CORS_ORIGIN_WHITELIST = ["*"]
8636

8737
# Use the REST framework
8838
REST_FRAMEWORK = {
@@ -94,8 +44,6 @@
9444
],
9545
"DEFAULT_PERMISSION_CLASSES": ["rest_framework.permissions.IsAuthenticated"],
9646
"DEFAULT_SCHEMA_CLASS": "rest_framework.schemas.coreapi.AutoSchema",
97-
98-
9947
}
10048

10149
JWT_AUTH = {
@@ -197,7 +145,7 @@
197145
DATABASES = {
198146
"default": {
199147
"ENGINE": "django.db.backends.sqlite3",
200-
"NAME": server_config["DATABASES"]["path"],
148+
"NAME": secrets["SERVER"]["DATABASE"],
201149
}
202150
}
203151

@@ -219,8 +167,8 @@
219167
# https://docs.djangoproject.com/en/3.0/howto/static-files/
220168

221169
STATIC_URL = "/api/static/"
222-
# STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
223-
STATIC_ROOT = "/var/www/bcoeditor/bco_api/bco_api/static/"
170+
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
171+
# STATIC_ROOT = "/var/www/bcoeditor/bco_api/bco_api/static/"
224172

225173
# ----- CUSTOM VARIABLES AND METHODS ----- #
226174
# Load request and validation templates (definitions).
@@ -229,26 +177,9 @@
229177
# First, the request definitions.
230178

231179
# Make the object naming accessible as a dictionary.
232-
OBJECT_NAMING = {}
233-
234-
if server_config["PRODUCTION"]["production"] == "True":
235-
236-
for i in server_config["OBJECT_NAMING"]:
237-
if i.split("_")[0] == "prod":
238-
239-
# Strip out the production flag.
240-
STRIPPED = "_".join(i.split("_")[1:])
241-
242-
OBJECT_NAMING[STRIPPED] = server_config["OBJECT_NAMING"][i]
243-
244-
elif server_config["PRODUCTION"]["production"] == "False":
245-
246-
for i in server_config["OBJECT_NAMING"]:
247-
if i.split("_")[0] != "prod":
248-
OBJECT_NAMING[i] = server_config["OBJECT_NAMING"][i]
249180

250181
# emailing notifications
251-
EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
182+
EMAIL_BACKEND = secrets["SERVER"]["EMAIL_BACKEND"]
252183
EMAIL_HOST = "localhost"
253184
EMAIL_PORT = 25
254185
DEFAULT_AUTO_FIELD = "django.db.models.AutoField"

server.conf

Lines changed: 0 additions & 74 deletions
This file was deleted.

0 commit comments

Comments
 (0)