Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions qiita_pet/handlers/api_proxy/studies.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def study_get_req(study_id, user_id):

samples = study.sample_template
study_info['num_samples'] = 0 if samples is None else len(list(samples))
study_info['owner'] = study.owner.id

return {'status': 'success',
'message': '',
Expand Down
3 changes: 2 additions & 1 deletion qiita_pet/handlers/api_proxy/tests/test_studies.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ def test_study_get_req(self):
'num_samples': 27,
'study_title': 'Identification of the Microbiomes for '
'Cannabis Soils',
'number_samples_collected': 27},
'number_samples_collected': 27,
'owner': 'test@foo.bar'},
'editable': True}

self.assertEqual(obs, exp)
Expand Down
1 change: 1 addition & 0 deletions qiita_pet/handlers/auth_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def get(self, code):
if User.verify_code(email, code, "create"):
msg = "Successfully verified user! You are now free to log in."
color = "black"
r_client.zadd('qiita-usernames', email, 0)
else:
msg = "Code not valid!"
color = "red"
Expand Down
5 changes: 3 additions & 2 deletions qiita_pet/handlers/study_handlers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
# -----------------------------------------------------------------------------

from .listing_handlers import (ListStudiesHandler, StudyApprovalList,
ShareStudyAJAX, SearchStudiesAJAX)
ShareStudyAJAX, SearchStudiesAJAX,
AutocompleteHandler)
from .edit_handlers import StudyEditHandler, CreateStudyAJAX
from .description_handlers import PreprocessingSummaryHandler
from .ebi_handlers import EBISubmitHandler
Expand Down Expand Up @@ -35,4 +36,4 @@
'StudyDeleteAjax', 'ArtifactAJAX', 'NewPrepTemplateAjax',
'DataTypesMenuAJAX', 'StudyFilesAJAX', 'PrepTemplateSummaryAJAX',
'ArtifactSummaryAJAX', 'WorkflowHandler', 'WorkflowRunHandler',
'JobAJAX']
'JobAJAX', 'AutocompleteHandler']
5 changes: 4 additions & 1 deletion qiita_pet/handlers/study_handlers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@ def get(self):
contact = email.format(**study_info['lab_person'])
else:
contact = None
share_access = (self.current_user.id in study_info['shared_with'] or
self.current_user.id == study_info['owner'])

self.render('study_ajax/base_info.html',
study_info=study_info, publications=study_doi, pi=pi,
contact=contact, editable=res['editable'])
contact=contact, editable=res['editable'],
share_access=share_access)


class StudyDeleteAjax(BaseHandler):
Expand Down
23 changes: 12 additions & 11 deletions qiita_pet/handlers/study_handlers/listing_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
from tornado.web import authenticated, HTTPError
from tornado.gen import coroutine, Task
from pyparsing import ParseException
from moi import r_client

from qiita_db.artifact import Artifact
from qiita_db.user import User
from qiita_db.study import Study, StudyPerson
from qiita_db.search import QiitaStudySearch
from qiita_db.metadata_template.sample_template import SampleTemplate
from qiita_db.logger import LogEntry
from qiita_db.exceptions import QiitaDBIncompatibleDatatypeError
from qiita_db.reference import Reference
from qiita_db.util import get_table_cols, get_pubmed_ids_from_dois
from qiita_db.util import get_pubmed_ids_from_dois
from qiita_core.exceptions import IncompetentQiitaDeveloperError
from qiita_core.util import execute_as_transaction
from qiita_pet.handlers.base_handlers import BaseHandler
Expand Down Expand Up @@ -222,15 +222,7 @@ class ListStudiesHandler(BaseHandler):
@coroutine
@execute_as_transaction
def get(self, message="", msg_level=None):
all_emails_except_current = yield Task(self._get_all_emails)
all_emails_except_current.remove(self.current_user.id)
avail_meta = SampleTemplate.metadata_headers() +\
get_table_cols("study")
self.render('list_studies.html',
availmeta=avail_meta,
all_emails_except_current=all_emails_except_current,
message=message,
msg_level=msg_level)
self.render('list_studies.html', message=message, msg_level=msg_level)

def _get_all_emails(self, callback):
callback(list(User.iter()))
Expand All @@ -254,6 +246,15 @@ def get(self):
study_info=parsed_studies)


class AutocompleteHandler(BaseHandler):
@authenticated
def get(self):
text = self.get_argument('text')
vals = r_client.execute_command('zrangebylex', 'qiita-usernames',
u'[%s' % text, u'[%s\xff' % text)
self.write({'results': [{'id': s, 'text': s} for s in vals]})


class ShareStudyAJAX(BaseHandler):
@execute_as_transaction
def _get_shared_for_study(self, study, callback):
Expand Down
29 changes: 29 additions & 0 deletions qiita_pet/handlers/study_handlers/tests/test_listing_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from json import loads

from mock import Mock
from moi import r_client

from qiita_core.exceptions import IncompetentQiitaDeveloperError
from qiita_pet.test.tornado_test_base import TestHandlerBase
Expand Down Expand Up @@ -201,6 +202,34 @@ def test_get(self):
self.assertIn("test@foo.bar", response.body)


class TestAutocompleteHandler(TestHandlerBase):
database = False

base_url = '/study/sharing/autocomplete/?text=%s'

def test_get(self):
# Create the usernames key so we can do autocomplete
r_client.zadd('qiita-usernames', **{u: 0 for u in User.iter()})
response = self.get(self.base_url % 't')
self.assertEqual(response.code, 200)
self.assertEqual(loads(response.body),
{'results': [{"id": "test@foo.bar",
"text": "test@foo.bar"}]})

response = self.get(self.base_url % 'admi')
self.assertEqual(response.code, 200)
self.assertEqual(loads(response.body),
{'results': [{"id": "admin@foo.bar",
"text": "admin@foo.bar"}]})

response = self.get(self.base_url % 'tesq')
self.assertEqual(response.code, 200)
self.assertEqual(loads(response.body),
{'results': []})

r_client.delete('qiita-usernames')


class TestShareStudyAjax(TestHandlerBase):
database = True

Expand Down
55 changes: 55 additions & 0 deletions qiita_pet/static/js/sharing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
var current_study = null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way to not use this global variable? Global variables in JavaScript are a slippery slope, and it tends to be really hard to recover from its (ab)use.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, seems like users and users_links are all global 😭

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was a forgotten var. Fixed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From offline conversation, @ElDeveloper and I agree it's not feasible to remove this global variable at this time.


$(document).ready(function () {
$('#shares-select').select2({
ajax: {
url: "/study/sharing/autocomplete/",
dataType: 'json',
delay: 250,
data: function (params) {
return {text: params.term};
},
cache: true
},
minimumInputLength: 1,
formatResult: function (data, term) {
return data;
}
});

$('#shares-select').on("select2:select", function (e) {
update_share({selected: e.params.data.text});
});

$('#shares-select').on("select2:unselect", function (e) {
update_share({deselected: e.params.data.text});
});
});

function modify_sharing(study_id) {
var shared_list;
current_study = study_id;
$.get('/study/sharing/', {study_id: study_id})
.done(function(data) {
var users_links = JSON.parse(data);
var users = users_links.users;
//empty dropdown and repopulate with new study shared values
$('#shares-select').html('');
for(var i=0;i<users.length;i++) {
var shared = new Option(users[i], users[i], true, true);
$("#shares-select").append(shared).trigger('change');
}
$("#shares-select").trigger("change");
});
}

function update_share(params) {
data = params || {};
data.study_id = current_study;
$.get('/study/sharing/', data)
.done(function(data) {
users_links = JSON.parse(data);
links = users_links.links;
$("#shared_html_"+current_study).html(links);
});
}
Loading