Skip to content

Commit

Permalink
Taking care of @adamrp suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
squirrelo committed Jun 12, 2014
1 parent 18c2edf commit c4a2234
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 33 deletions.
55 changes: 27 additions & 28 deletions qiita_db/study.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@

from __future__ import division
from future.builtins import zip
from future.utils import viewitems
from datetime import date
from copy import deepcopy

Expand Down Expand Up @@ -157,15 +158,15 @@ def create(cls, owner, title, efo, info, investigation=None):
----------
owner : User object
the study' owner
title: str
title : str
Title of the study
efo: int or list
efo : int or list
Experimental Factor Ontology or -ies for the study
info: dict
info : dict
the information attached to the study. All "*_id" keys must pass
the objects associated with them.
investigation: Investigation object
if the study is part of an investigation, the id to associate with
investigation : Investigation object, optional
If passed, the investigation to associate with. Defaults to None.
Raises
------
Expand All @@ -182,13 +183,13 @@ def create(cls, owner, title, efo, info, investigation=None):
"""
# make sure not passing non-info columns in the info dict
if cls._non_info.intersection(info):
raise QiitaDBColumnError("non info keys passed: %s" %
cls._non_info.intersection(info))
raise QiitaDBColumnError("non info keys passed: %s" %
cls._non_info.intersection(info))

# add default values to info
insertdict = deepcopy(info)
if "first_contact" not in insertdict:
insertdict['first_contact'] = date.today().strftime("%B %d, %Y")
insertdict['first_contact'] = date.today().isoformat()
insertdict['email'] = owner.id
insertdict['study_title'] = title
if "reprocess" not in insertdict:
Expand All @@ -203,13 +204,12 @@ def create(cls, owner, title, efo, info, investigation=None):
check_required_columns(conn_handler, insertdict, cls._table)

# Insert study into database
keys = insertdict.keys()
sql = ("INSERT INTO qiita.{0} ({1}) VALUES ({2}) RETURNING "
"study_id".format(cls._table, ','.join(keys),
"study_id".format(cls._table, ','.join(insertdict),
','.join(['%s'] * len(insertdict))))
# make sure data in same order as sql column names, and ids are used
data = []
for col in keys:
for col in insertdict:
if isinstance(insertdict[col], QiitaObject):
data.append(insertdict[col].id)
else:
Expand All @@ -218,7 +218,7 @@ def create(cls, owner, title, efo, info, investigation=None):

# insert efo information into database
if isinstance(efo, int):
efo = [efo]
efo = [efo]
sql = ("INSERT INTO qiita.{0}_experimental_factor (study_id, "
"efo_id) VALUES (%s, %s)".format(cls._table))
conn_handler.executemany(sql, list(zip([study_id] * len(efo), efo)))
Expand Down Expand Up @@ -309,9 +309,8 @@ def info(self, info):

sql_vals = []
data = []
# items() used for py3 compatability
# build query with data values in correct order for SQL statement
for key, val in info.items():
for key, val in viewitems(info):
sql_vals.append("{0} = %s".format(key))
if isinstance(val, QiitaObject):
data.append(val.id)
Expand All @@ -336,13 +335,13 @@ def efo(self, efo_vals):
Parameters
----------
status_id: int
status_id : int
ID for the new status
"""
conn_handler = SQLConnectionHandler()
self._lock_public(conn_handler)
if isinstance(efo_vals, int):
efo_vals = [efo_vals]
efo_vals = [efo_vals]
# wipe out any EFOs currently attached to study
sql = ("DELETE FROM qiita.{0}_experimental_factor WHERE "
"study_id = %s".format(self._table))
Expand All @@ -351,7 +350,7 @@ def efo(self, efo_vals):
sql = ("INSERT INTO qiita.{0}_experimental_factor (study_id, "
"efo_id) VALUES (%s, %s)".format(self._table))
conn_handler.executemany(sql, list(zip([self._id] * len(efo_vals),
efo_vals)))
efo_vals)))

@property
def shared_with(self):
Expand Down Expand Up @@ -451,7 +450,7 @@ def add_pmid(self, pmid):
Parameters
----------
pmid: str
pmid : str
pmid to associate with study
"""
conn_handler = SQLConnectionHandler()
Expand All @@ -465,13 +464,13 @@ class StudyPerson(QiitaObject):
Attributes
----------
name: str
name : str
name of the person
email: str
email : str
email of the person
address: str or None
address : str or None
address of the person
phone: str or None
phone : str or None
phone number of the person
"""
_table = "study_person"
Expand Down Expand Up @@ -503,13 +502,13 @@ def create(cls, name, email, address=None, phone=None):
Parameters
----------
name: str
name : str
name of person
email: str
email : str
email of person
address: str, optional
address : str, optional
address of person
phone: str, optional
phone : str, optional
phone number of person
Returns
Expand Down Expand Up @@ -582,7 +581,7 @@ def address(self, value):
Parameters
----------
value: str
value : str
New address for person
"""
conn_handler = SQLConnectionHandler()
Expand Down Expand Up @@ -610,7 +609,7 @@ def phone(self, value):
Parameters
----------
value: str
value : str
New phone number for person
"""
conn_handler = SQLConnectionHandler()
Expand Down
6 changes: 4 additions & 2 deletions qiita_db/test/test_study.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from unittest import TestCase, main
from datetime import date

from future.utils import viewitems

from qiita_core.exceptions import IncompetentQiitaDeveloperError
from qiita_core.util import qiita_test_checker
from qiita_db.base import QiitaObject
Expand Down Expand Up @@ -154,7 +156,7 @@ def test_create_study_min_data(self):
'reprocess': False, 'study_status_id': 1,
'number_samples_promised': 28, 'emp_person_id': 2,
'funding': None, 'vamps_id': None,
'first_contact': date.today().strftime("%B %d, %Y"),
'first_contact': date.today().isoformat(),
'principal_investigator_id': 3,
'timeseries_type_id': 1,
'study_abstract': 'Exploring how a high fat diet changes the '
Expand Down Expand Up @@ -291,7 +293,7 @@ def test_set_efo_public(self):
self.study.efo = 6

def test_retrieve_info(self):
for key, val in self.existingexp.items():
for key, val in viewitems(self.existingexp):
if isinstance(val, QiitaObject):
self.existingexp[key] = val.id
self.assertEqual(self.study.info, self.existingexp)
Expand Down
6 changes: 3 additions & 3 deletions qiita_db/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class User(QiitaObject):
"pass_reset_code", "pass_reset_timestamp"}

def _check_id(self, id_, conn_handler=None):
r"""Check that the provided ID actually exists on the database
r"""Check that the provided ID actually exists in the database
Parameters
----------
Expand All @@ -84,8 +84,8 @@ def _check_id(self, id_, conn_handler=None):
Notes
-----
This functionoverwrites the base function, as sql layout doesn't follow
the same conventions done in the other classes.
This function overwrites the base function, as sql layout doesn't
follow the same conventions done in the other classes.
"""
self._check_subclass()

Expand Down

0 comments on commit c4a2234

Please sign in to comment.