Skip to content

Commit

Permalink
backend for admin page finished
Browse files Browse the repository at this point in the history
  • Loading branch information
costero-e committed Nov 8, 2023
1 parent 62973cd commit c814a40
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 57 deletions.
2 changes: 1 addition & 1 deletion beacon/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
#
# Database connection
#
database_host = '127.0.0.1'
database_host = 'mongo'
database_port = 27017
database_user = 'root'
database_password = 'example'
Expand Down
78 changes: 65 additions & 13 deletions permissions/permissions-ui/bash/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,34 @@
import time
from django.http import HttpResponseRedirect, HttpResponseBadRequest
import logging

import json
import yaml
from pymongo.mongo_client import MongoClient
import imp
from django.urls import resolve

# load a source module from a file
file, pathname, description = imp.find_module('beacon', [''])
my_module = imp.load_module('beacon', file, pathname, description)

from beacon import conf

client = MongoClient(
"mongodb://{}:{}@{}:{}/{}?authSource={}".format(
conf.database_user,
conf.database_password,
conf.database_host,
conf.database_port,
conf.database_name,
conf.database_auth_source,
)
)

LOG = logging.getLogger(__name__)



LOG = logging.getLogger(__name__)

def add_public_datasets(list_datasets):
with open("../beacon/permissions/public_datasets.yml", 'r') as pfile:
public_datasets = yaml.safe_load(pfile)
Expand All @@ -35,6 +56,7 @@ def add_registered_datasets(list_users, list_datasets):
registered_datasets = yaml.safe_load(pfile)
pfile.close
for user in list_users:
print(user)
registered_datasets[user]=[]
for dataset in list_datasets:
if dataset not in registered_datasets[user]:
Expand All @@ -43,6 +65,16 @@ def add_registered_datasets(list_users, list_datasets):
yaml.dump(registered_datasets, pfile)
pfile.close

def load_datasets():
results = client.beacon.get_collection('datasets').find({}, {"id": 1})
results = list(results)
list_of_datasets=[]
for object in results:
for k,v in object.items():
if k == 'id':
list_of_datasets.append(v)
return list_of_datasets

def load_public_datasets():
with open("../beacon/permissions/public_datasets.yml", 'r') as pfile:
public_datasets = yaml.safe_load(pfile)
Expand Down Expand Up @@ -73,12 +105,19 @@ def load_users():

return list_users

def load_registered_datasets():
with open("../beacon/permissions/public_datasets.yml", 'r') as pfile:
def load_registered_datasets(user):
with open("../beacon/permissions/registered_datasets.yml", 'r') as pfile:
registered_datasets = yaml.safe_load(pfile)
pfile.close()

return registered_datasets
list_registered_datasets=[]

for k, v in registered_datasets.items():
if k == user:
list_registered_datasets = v

return list_registered_datasets



def bash_view(request):
Expand All @@ -99,8 +138,9 @@ def bash_view(request):

def public_view(request):
template = "public.html"
datasets=load_datasets()
bash_out=load_public_datasets()
context={'bash_out': bash_out}
context={'bash_out': bash_out, 'datasets': datasets}
if request.method == 'POST':
answer = request.POST.getlist('list', False)
print(answer)
Expand All @@ -113,8 +153,9 @@ def public_view(request):

def controlled_view(request):
template = "controlled.html"
datasets=load_datasets()
bash_out=load_controlled_datasets()
context={'bash_out': bash_out}
context={'bash_out': bash_out, 'datasets': datasets}
if request.method == 'POST':
answer = request.POST.getlist('list', False)
print(answer)
Expand All @@ -128,14 +169,25 @@ def controlled_view(request):
def registered_view(request):
template = "registered.html"
bash_out=load_users()
registered_datasets=load_registered_datasets()
context={'bash_out': bash_out, 'registered_datasets': registered_datasets['public_datasets']}
datasets=load_datasets()
context={}
if request.method == 'GET':
users = request.GET.getlist('userslist', False)
if users != False:
registered_datasets = load_registered_datasets(users[0])
context={'bash_out': bash_out, 'datasets': datasets, 'users': users, 'registered_datasets': registered_datasets}
else:
context={'bash_out': bash_out, 'datasets': datasets, 'users': users}

return render(request, template, context)


if request.method == 'POST':
answer = request.POST.getlist('list', False)
datasets_list = request.POST.getlist('list_datasets', False)
add_registered_datasets(answer, datasets_list)
user = request.POST.getlist('users', False)
datasets_list = request.POST.getlist('list', False)
add_registered_datasets(user, datasets_list)
context = {
'answer': answer, 'datasets_list': datasets_list
'answer': user, 'datasets_list': datasets_list
}
return redirect("bash:index")
return render(request, template, context)
23 changes: 1 addition & 22 deletions permissions/permissions-ui/media/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const btnAdd = document.querySelector('#btnAdd');
const btnRemove = document.querySelector('#btnRemove');
const listbox = document.querySelector('#list');
const listbox = document.querySelector('#userslist');
const framework = document.querySelector('#framework');

btnAdd.onclick = (e) => {
Expand All @@ -22,23 +21,3 @@ btnAdd.onclick = (e) => {
framework.value = '';
framework.focus();
};

// remove selected option
btnRemove.onclick = (e) => {
e.preventDefault();

// save the selected options
let selected = [];

for (let i = 0; i < listbox.options.length; i++) {
selected[i] = listbox.options[i].selected;
}

// remove all selected option
let index = listbox.options.length;
while (index--) {
if (selected[index]) {
listbox.remove(index);
}
}
};
14 changes: 8 additions & 6 deletions permissions/permissions-ui/media/app2.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const btnAdd2 = document.querySelector('#btnAdd2');
const btnRemove = document.querySelector('#btnRemove');
const datasets = document.querySelector('#datasets');
const listbox = document.querySelector('#list');
const listbox2 = document.querySelector('#list');
btnAdd2.onclick = (e) => {
e.preventDefault();

Expand All @@ -18,10 +18,12 @@ btnAdd2.onclick = (e) => {

for (let i = 0; i < selected.length; i++) {
console.log(selected[i])
if (!selected[i] == false){
const option = new Option(selected[i], selected[i]);
option.setAttribute('selected', 'selected');
// add it to the list
listbox.add(option, undefined);
listbox2.add(option, undefined);
}
}
// create a new option

Expand All @@ -36,15 +38,15 @@ btnRemove.onclick = (e) => {
// save the selected options
let selected = [];

for (let i = 0; i < listbox.options.length; i++) {
selected[i] = listbox.options[i].selected;
for (let i = 0; i < listbox2.options.length; i++) {
selected[i] = listbox2.options[i].selected;
}

// remove all selected option
let index = listbox.options.length;
let index = listbox2.options.length;
while (index--) {
if (selected[index]) {
listbox.remove(index);
listbox2.remove(index);
}
}
};
6 changes: 4 additions & 2 deletions permissions/permissions-ui/templates/controlled.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@
<div><h2 style="color: #760509;">Controlled datasets manager</h2></div>
<form method="post">
{% csrf_token %}
<label for="datasets">Dataset:</label>
<label for="datasets">Datasets:</label>

<select id="datasets" name="datasets" multiple>
<option value="dataset1" selected="selected">dataset1</option>
{% for dataset in datasets %}
<option value="{{dataset}}" selected="selected">{{dataset}}</option>
{% endfor %}
</select>
<button id="btnAdd2">Add</button>
<label for="list">Public Datasets List:</label>
Expand Down
6 changes: 4 additions & 2 deletions permissions/permissions-ui/templates/public.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@
<div><h2 style="color: #760509;">Public datasets manager</h2></div>
<form method="post">
{% csrf_token %}
<label for="datasets">Dataset:</label>
<label for="datasets">Datasets:</label>

<select id="datasets" name="datasets" multiple>
<option value="dataset1" selected="selected">dataset1</option>
{% for dataset in datasets %}
<option value="{{dataset}}" selected="selected">{{dataset}}</option>
{% endfor %}
</select>
<button id="btnAdd2">Add</button>
<label for="list">Public Datasets List:</label>
Expand Down
43 changes: 33 additions & 10 deletions permissions/permissions-ui/templates/registered.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,30 +56,52 @@
</div>
<div id="boxdatasets">
<div><h2 style="color: #760509;">Registered datasets manager</h2></div>
<form method="post">
{% if not users %}
<form method="get">
{% csrf_token %}
<label for="framework">Add a new user to the list:</label>
<input type="text" id="framework" placeholder="Enter a new user" autocomplete="off">

<button id="btnAdd">Add</button>

<label for="list">Select one or more users from the list:</label>
<select id="list" name="list" multiple>
<label for="userslist">Select one user from the list:</label>
<select id="userslist" name="userslist" size="3">
{% for item in bash_out %}
<option value="{{item}}" selected="selected">{{item}}</option>
{% endfor %}
</select>
<label for="datasets">Datasets List:</label>
<select id="list_datasets" name="list_datasets" multiple>
{% for dataset in registered_datasets %}
<option value="{{dataset}}" selected="selected">{{dataset}}</option>
{% endfor %}
</select>
<button id="btnRemove">Remove Dataset</button>
<div class="col-5">
<button type="submit" class="btn btn-success mb-3">Add dataset to user(s) permissions</button>
</div>
</form>
{% else %}
{% for user in users %}
<form method="post">
{% csrf_token %}
<label for="users">User:</label>
<select id="users" name="users" multiple>
<option value="{{user}}" selected="selected">{{user}}</option>
</select>
<label for="datasets">Datasets:</label>
<select id="datasets" name="datasets" multiple>
{% for dataset in datasets %}
<option value="{{dataset}}" selected="selected">{{dataset}}</option>
{% endfor %}
</select>
<button id="btnAdd2">Add</button>
<label for="list">Public Datasets List:</label>
<select id="list" name="list" multiple>
{% for rdataset in registered_datasets %}
<option value="{{rdataset}}" selected="selected">{{rdataset}}</option>
{% endfor %}
</select>
<button id="btnRemove">Remove Dataset</button>
<div class="col-5">
<button type="submit" class="btn btn-success mb-3">Add datasets to user {{user}}</button>
</div>
</form>
{% endfor %}
{% endif %}
</div>


Expand Down Expand Up @@ -137,5 +159,6 @@

</script>
<script src="media/app.js"></script>
<script src="media/app2.js"></script>
</body>
</html>
1 change: 0 additions & 1 deletion permissions/public_datasets.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
public_datasets:
- CINECA_synthetic_cohort_EUROPE_UK1
- dataset1
3 changes: 3 additions & 0 deletions permissions/registered_datasets.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
app-user3:
- CINECA_synthetic_cohort_EUROPE_UK1
gemma:
- CINECA_synthetic_cohort_EUROPE_UK1
- AV_Dataset
hola:
- CINECA_synthetic_cohort_EUROPE_UK1

0 comments on commit c814a40

Please sign in to comment.