text stringlengths 18 773k |
|---|
def delete_resourcegroupitems(scenario_id, item_ids, **kwargs):
"""
Delete specified items in a group, in a scenario.
"""
user_id = int(kwargs.get('user_id'))
#check the scenario exists
_get_scenario(scenario_id, user_id)
for item_id in item_ids:
rgi = db.DBSession.query(Resource... |
def empty_group(group_id, scenario_id, **kwargs):
"""
Delete all itemas in a group, in a scenario.
"""
user_id = int(kwargs.get('user_id'))
#check the scenario exists
_get_scenario(scenario_id, user_id)
rgi = db.DBSession.query(ResourceGroupItem).\
filter(ResourceGroupItem.g... |
def add_resourcegroupitems(scenario_id, items, scenario=None, **kwargs):
"""
Get all the items in a group, in a scenario.
"""
user_id = int(kwargs.get('user_id'))
if scenario is None:
scenario = _get_scenario(scenario_id, user_id)
_check_network_ownership(scenario.network_id, user... |
def _add_resourcegroupitem(group_item, scenario_id):
"""
Add a single resource group item (no DB flush, as it's an internal function)
"""
if group_item.id and group_item.id > 0:
try:
group_item_i = db.DBSession.query(ResourceGroupItem).filter(ResourceGroupItem.id == group_item.id... |
def update_value_from_mapping(source_resource_attr_id, target_resource_attr_id, source_scenario_id, target_scenario_id, **kwargs):
"""
Using a resource attribute mapping, take the value from the source and apply
it to the target. Both source and target scenarios must be specified (and therefor
... |
def get_plugins(**kwargs):
"""
Get all available plugins
"""
plugins = []
plugin_paths = []
#Look in directory or set of directories for
#plugins
base_plugin_dir = config.get('plugin', 'default_directory')
plugin_xsd_path = config.get('plugin', 'plugin_xsd_path')
... |
def run_plugin(plugin,**kwargs):
"""
Run a plugin
"""
args = [sys.executable]
#Get plugin executable
home = os.path.expanduser('~')
path_to_plugin = os.path.join(home, 'svn/HYDRA/HydraPlugins', plugin.location)
args.append(path_to_plugin)
#Parse plugin arguments into a ... |
def load_config():
"""Load a config file. This function looks for a config (*.ini) file in the
following order::
(1) ./*.ini
(2) ~/.config/hydra/
(3) /etc/hydra
(4) /path/to/hydra_base/*.ini
(1) will override (2) will override (3) will override (4). Parameters not
defin... |
def create_mysql_db(db_url):
"""
To simplify deployment, create the mysql DB if it's not there.
Accepts a URL with or without a DB name stated, and returns a db url
containing the db name for use in the main sqlalchemy engine.
THe formats can take the following form:
mysql+... |
def add_project(project,**kwargs):
"""
Add a new project
returns a project complexmodel
"""
user_id = kwargs.get('user_id')
existing_proj = get_project_by_name(project.name,user_id=user_id)
if len(existing_proj) > 0:
raise HydraError("A Project with the name \"%s\" already ... |
def update_project(project,**kwargs):
"""
Update a project
returns a project complexmodel
"""
user_id = kwargs.get('user_id')
#check_perm(user_id, 'update_project')
proj_i = _get_project(project.id)
proj_i.check_write_permission(user_id)
proj_i.name = project.name
... |
def get_project(project_id, include_deleted_networks=False, **kwargs):
"""
get a project complexmodel
"""
user_id = kwargs.get('user_id')
proj_i = _get_project(project_id)
#lazy load owners
proj_i.owners
proj_i.check_read_permission(user_id)
proj_j = JSONObject(proj_i)
pr... |
def get_project_by_network_id(network_id,**kwargs):
"""
get a project complexmodel by a network_id
"""
user_id = kwargs.get('user_id')
projects_i = db.DBSession.query(Project).join(ProjectOwner).join(Network, Project.id==Network.project_id).filter(
... |
def get_project_by_name(project_name,**kwargs):
"""
get a project complexmodel
"""
user_id = kwargs.get('user_id')
projects_i = db.DBSession.query(Project).join(ProjectOwner).filter(
Project.name==project_name,
... |
def to_named_tuple(obj, visited_children=None, back_relationships=None, levels=None, ignore=[], extras={}):
"""
Altered from an example found on stackoverflow
http://stackoverflow.com/questions/23554119/convert-sqlalchemy-orm-result-to-dict
"""
if visited_children is None:
visited_c... |
def get_projects(uid, include_shared_projects=True, projects_ids_list_filter=None, **kwargs):
"""
Get all the projects owned by the specified user.
These include projects created by the user, but also ones shared with the user.
For shared projects, only include networks in those projects whi... |
def set_project_status(project_id, status, **kwargs):
"""
Set the status of a project to 'X'
"""
user_id = kwargs.get('user_id')
#check_perm(user_id, 'delete_project')
project = _get_project(project_id)
project.check_write_permission(user_id)
project.status = status
db.DBSession.... |
def delete_project(project_id,**kwargs):
"""
Set the status of a project to 'X'
"""
user_id = kwargs.get('user_id')
#check_perm(user_id, 'delete_project')
project = _get_project(project_id)
project.check_write_permission(user_id)
db.DBSession.delete(project)
db.DBSession.flush()
... |
def get_networks(project_id, include_data='N', **kwargs):
"""
Get all networks in a project
Returns an array of network objects.
"""
log.info("Getting networks for project %s", project_id)
user_id = kwargs.get('user_id')
project = _get_project(project_id)
project.check_read_permi... |
def get_network_project(network_id, **kwargs):
"""
get the project that a network is in
"""
net_proj = db.DBSession.query(Project).join(Network, and_(Project.id==Network.id, Network.id==network_id)).first()
if net_proj is None:
raise HydraError("Network %s not found"% network_id)
... |
def add_resource_types(resource_i, types):
"""
Save a reference to the types used for this resource.
@returns a list of type_ids representing the type ids
on the resource.
"""
if types is None:
return []
existing_type_ids = []
if resource_i.types:
for t in resource_i.t... |
def create_default_users_and_perms():
"""
Adds the roles and perm to the DB. It adds only roles, perms and links between them that are not inside the db
It is possible adding new role or perm and connecting them just modifiying the following lists
"""
# perms = db.DBSession.query(Perm).all(... |
def create_default_units_and_dimensions():
"""
Adds the units and the dimensions reading a json file. It adds only dimensions and units that are not inside the db
It is possible adding new dimensions and units to the DB just modifiyin the json file
"""
default_units_file_location = os.path.r... |
def get_dimension_from_db_by_name(dimension_name):
"""
Gets a dimension from the DB table.
"""
try:
dimension = db.DBSession.query(Dimension).filter(Dimension.name==dimension_name).one()
return JSONObject(dimension)
except NoResultFound:
raise ResourceNotFoundError("Dimen... |
def get_rules(scenario_id, **kwargs):
"""
Get all the rules for a given scenario.
"""
rules = db.DBSession.query(Rule).filter(Rule.scenario_id==scenario_id, Rule.status=='A').all()
return rules |
def get_attribute_by_id(attr_id, **kwargs):
"""
Get a specific attribute by its ID.
"""
try:
attr_i = db.DBSession.query(Attr).filter(Attr.id==attr_id).one()
return attr_i
except NoResultFound:
return None |
def get_template_attributes(template_id, **kwargs):
"""
Get a specific attribute by its ID.
"""
try:
attrs_i = db.DBSession.query(Attr).filter(TemplateType.template_id==template_id).filter(TypeAttr.type_id==TemplateType.id).filter(Attr.id==TypeAttr.id).all()
log.debug(attrs_i)
... |
def get_attribute_by_name_and_dimension(name, dimension_id=None,**kwargs):
"""
Get a specific attribute by its name.
dimension_id can be None, because in attribute the dimension_id is not anymore mandatory
"""
try:
attr_i = db.DBSession.query(Attr).filter(and_(Attr.name==name, Attr.d... |
def add_attribute(attr,**kwargs):
"""
Add a generic attribute, which can then be used in creating
a resource attribute, and put into a type.
.. code-block:: python
(Attr){
id = 1020
name = "Test Attr"
dimension_id = 123
}
"""
log.debug("Addi... |
def update_attribute(attr,**kwargs):
"""
Add a generic attribute, which can then be used in creating
a resource attribute, and put into a type.
.. code-block:: python
(Attr){
id = 1020
name = "Test Attr"
dimension_id = 123
}
"""
log.debug("... |
def add_attributes(attrs,**kwargs):
"""
Add a list of generic attributes, which can then be used in creating
a resource attribute, and put into a type.
.. code-block:: python
(Attr){
id = 1020
name = "Test Attr"
dimen = "very big"
}
"""
#Ch... |
def get_attributes(**kwargs):
"""
Get all attributes
"""
attrs = db.DBSession.query(Attr).order_by(Attr.name).all()
return attrs |
def update_resource_attribute(resource_attr_id, is_var, **kwargs):
"""
Deletes a resource attribute and all associated data.
"""
user_id = kwargs.get('user_id')
try:
ra = db.DBSession.query(ResourceAttr).filter(ResourceAttr.id == resource_attr_id).one()
except NoResultFound:
... |
def delete_resource_attribute(resource_attr_id, **kwargs):
"""
Deletes a resource attribute and all associated data.
"""
user_id = kwargs.get('user_id')
try:
ra = db.DBSession.query(ResourceAttr).filter(ResourceAttr.id == resource_attr_id).one()
except NoResultFound:
raise Re... |
def add_resource_attribute(resource_type, resource_id, attr_id, is_var, error_on_duplicate=True, **kwargs):
"""
Add a resource attribute attribute to a resource.
attr_is_var indicates whether the attribute is a variable or not --
this is used in simulation to indicate that this value is exp... |
def add_resource_attrs_from_type(type_id, resource_type, resource_id,**kwargs):
"""
adds all the attributes defined by a type to a node.
"""
type_i = _get_templatetype(type_id)
resource_i = _get_resource(resource_type, resource_id)
resourceattr_qry = db.DBSession.query(ResourceAttr).filter... |
def get_all_resource_attributes(ref_key, network_id, template_id=None, **kwargs):
"""
Get all the resource attributes for a given resource type in the network.
That includes all the resource attributes for a given type within the network.
For example, if the ref_key is 'NODE', then it will r... |
def get_resource_attributes(ref_key, ref_id, type_id=None, **kwargs):
"""
Get all the resource attributes for a given resource.
If type_id is specified, only
return the resource attributes within the type.
"""
user_id = kwargs.get('user_id')
resource_attr_qry = db.DBSession.que... |
def check_attr_dimension(attr_id, **kwargs):
"""
Check that the dimension of the resource attribute data is consistent
with the definition of the attribute.
If the attribute says 'volume', make sure every dataset connected
with this attribute via a resource attribute also has a dimen... |
def get_resource_attribute(resource_attr_id, **kwargs):
"""
Get a specific resource attribte, by ID
If type_id is Gspecified, only
return the resource attributes within the type.
"""
resource_attr_qry = db.DBSession.query(ResourceAttr).filter(
ResourceAttr.id == resource_att... |
def set_attribute_mapping(resource_attr_a, resource_attr_b, **kwargs):
"""
Define one resource attribute from one network as being the same as
that from another network.
"""
user_id = kwargs.get('user_id')
ra_1 = get_resource_attribute(resource_attr_a)
ra_2 = get_resource_attribute(r... |
def delete_attribute_mapping(resource_attr_a, resource_attr_b, **kwargs):
"""
Define one resource attribute from one network as being the same as
that from another network.
"""
user_id = kwargs.get('user_id')
rm = aliased(ResourceAttrMap, name='rm')
log.info("Trying to delete attri... |
def delete_mappings_in_network(network_id, network_2_id=None, **kwargs):
"""
Delete all the resource attribute mappings in a network. If another network
is specified, only delete the mappings between the two networks.
"""
qry = db.DBSession.query(ResourceAttrMap).filter(or_(ResourceAttrMap.n... |
def get_mappings_in_network(network_id, network_2_id=None, **kwargs):
"""
Get all the resource attribute mappings in a network. If another network
is specified, only return the mappings between the two networks.
"""
qry = db.DBSession.query(ResourceAttrMap).filter(or_(ResourceAttrMap.network... |
def get_node_mappings(node_id, node_2_id=None, **kwargs):
"""
Get all the resource attribute mappings in a network. If another network
is specified, only return the mappings between the two networks.
"""
qry = db.DBSession.query(ResourceAttrMap).filter(
or_(
and_(
... |
def get_link_mappings(link_id, link_2_id=None, **kwargs):
"""
Get all the resource attribute mappings in a network. If another network
is specified, only return the mappings between the two networks.
"""
qry = db.DBSession.query(ResourceAttrMap).filter(
or_(
and_(
... |
def get_network_mappings(network_id, network_2_id=None, **kwargs):
"""
Get all the mappings of network resource attributes, NOT ALL THE MAPPINGS
WITHIN A NETWORK. For that, ``use get_mappings_in_network``. If another network
is specified, only return the mappings between the two networks.
... |
def check_attribute_mapping_exists(resource_attr_id_source, resource_attr_id_target, **kwargs):
"""
Check whether an attribute mapping exists between a source and target resource attribute.
returns 'Y' if a mapping exists. Returns 'N' in all other cases.
"""
qry = db.DBSession.query(Resource... |
def get_attribute_group(group_id, **kwargs):
"""
Get a specific attribute group
"""
user_id=kwargs.get('user_id')
try:
group_i = db.DBSession.query(AttrGroup).filter(
AttrGroup.id==group_id).one()
group_i.project.check_read_permission... |
def add_attribute_group(attributegroup, **kwargs):
"""
Add a new attribute group.
An attribute group is a container for attributes which need to be grouped
in some logical way. For example, if the 'attr_is_var' flag isn't expressive
enough to delineate different groupings.
... |
def update_attribute_group(attributegroup, **kwargs):
"""
Add a new attribute group.
An attribute group is a container for attributes which need to be grouped
in some logical way. For example, if the 'attr_is_var' flag isn't expressive
enough to delineate different groupings.
... |
def delete_attribute_group(group_id, **kwargs):
"""
Delete an attribute group.
"""
user_id = kwargs['user_id']
try:
group_i = db.DBSession.query(AttrGroup).filter(AttrGroup.id==group_id).one()
group_i.project.check_write_permission(user_id)
db.DBSession.delete(group_i... |
def get_network_attributegroup_items(network_id, **kwargs):
"""
Get all the group items in a network
"""
user_id=kwargs.get('user_id')
net_i = _get_network(network_id)
net_i.check_read_permission(user_id)
group_items_i = db.DBSession.query(AttrGroupItem).filter(
... |
def get_group_attributegroup_items(network_id, group_id, **kwargs):
"""
Get all the items in a specified group, within a network
"""
user_id=kwargs.get('user_id')
network_i = _get_network(network_id)
network_i.check_read_permission(user_id)
group_items_i = db.DBSession.query(AttrGroup... |
def get_attribute_item_groups(network_id, attr_id, **kwargs):
"""
Get all the group items in a network with a given attribute_id
"""
user_id=kwargs.get('user_id')
network_i = _get_network(network_id)
network_i.check_read_permission(user_id)
group_items_i = db.DBSession.query(AttrGroup... |
def add_attribute_group_items(attributegroupitems, **kwargs):
"""
Populate attribute groups with items.
** attributegroupitems : a list of items, of the form:
```{
'attr_id' : X,
'group_id' : Y,
'network_id' : Z,
... |
def delete_attribute_group_items(attributegroupitems, **kwargs):
"""
remove attribute groups items .
** attributegroupitems : a list of items, of the form:
```{
'attr_id' : X,
'group_id' : Y,
'network_id' : Z,
... |
def share_network(network_id, usernames, read_only, share,**kwargs):
"""
Share a network with a list of users, identified by their usernames.
The read_only flag ('Y' or 'N') must be set
to 'Y' to allow write access or sharing.
The share flat ('Y' or 'N') must be set to 'Y' to allow... |
def unshare_network(network_id, usernames,**kwargs):
"""
Un-Share a network with a list of users, identified by their usernames.
"""
user_id = kwargs.get('user_id')
net_i = _get_network(network_id)
net_i.check_share_permission(user_id)
for username in usernames:
user_i = _get_u... |
def share_project(project_id, usernames, read_only, share,**kwargs):
"""
Share an entire project with a list of users, identifed by
their usernames.
The read_only flag ('Y' or 'N') must be set
to 'Y' to allow write access or sharing.
The share flat ('Y' or 'N') must be set ... |
def unshare_project(project_id, usernames,**kwargs):
"""
Un-share a project with a list of users, identified by their usernames.
"""
user_id = kwargs.get('user_id')
proj_i = _get_project(project_id)
proj_i.check_share_permission(user_id)
for username in usernames:
user_i = _get... |
def set_project_permission(project_id, usernames, read, write, share,**kwargs):
"""
Set permissions on a project to a list of users, identifed by
their usernames.
The read flag ('Y' or 'N') sets read access, the write
flag sets write access. If the read flag is 'N', then there is
... |
def set_network_permission(network_id, usernames, read, write, share,**kwargs):
"""
Set permissions on a network to a list of users, identifed by
their usernames. The read flag ('Y' or 'N') sets read access, the write
flag sets write access. If the read flag is 'N', then there is
aut... |
def hide_dataset(dataset_id, exceptions, read, write, share,**kwargs):
"""
Hide a particular piece of data so it can only be seen by its owner.
Only an owner can hide (and unhide) data.
Data with no owner cannot be hidden.
The exceptions paramater lists the usernames of those with p... |
def unhide_dataset(dataset_id,**kwargs):
"""
Hide a particular piece of data so it can only be seen by its owner.
Only an owner can hide (and unhide) data.
Data with no owner cannot be hidden.
The exceptions paramater lists the usernames of those with permission to view the data
... |
def get_all_project_owners(project_ids=None, **kwargs):
"""
Get the project owner entries for all the requested projects.
If the project_ids argument is None, return all the owner entries
for ALL projects
"""
projowner_qry = db.DBSession.query(ProjectOwner)
if project_ids is n... |
def get_all_network_owners(network_ids=None, **kwargs):
"""
Get the network owner entries for all the requested networks.
If the network_ids argument is None, return all the owner entries
for ALL networks
"""
networkowner_qry = db.DBSession.query(NetworkOwner)
if network_ids i... |
def bulk_set_project_owners(project_owners, **kwargs):
"""
Set the project owner of multiple projects at once.
Accepts a list of JSONObjects which look like:
{
'project_id': XX,
'user_id' : YY,
'view' : 'Y'/ 'N'
'edit' : 'Y'... |
def bulk_set_network_owners(network_owners, **kwargs):
"""
Set the network owner of multiple networks at once.
Accepts a list of JSONObjects which look like:
{
'network_id': XX,
'user_id' : YY,
'view' : 'Y'/ 'N'
'edit' : 'Y'... |
def get_dataset(dataset_id,**kwargs):
"""
Get a single dataset, by ID
"""
user_id = int(kwargs.get('user_id'))
if dataset_id is None:
return None
try:
dataset_rs = db.DBSession.query(Dataset.id,
Dataset.type,
Dataset.unit_id,
... |
def clone_dataset(dataset_id,**kwargs):
"""
Get a single dataset, by ID
"""
user_id = int(kwargs.get('user_id'))
if dataset_id is None:
return None
dataset = db.DBSession.query(Dataset).filter(
Dataset.id==dataset_id).options(joinedload_all('metadata')).first()
if... |
def get_datasets(dataset_ids,**kwargs):
"""
Get a single dataset, by ID
"""
user_id = int(kwargs.get('user_id'))
datasets = []
if len(dataset_ids) == 0:
return []
try:
dataset_rs = db.DBSession.query(Dataset.id,
Dataset.type,
Dataset.unit_... |
def search_datasets(dataset_id=None,
dataset_name=None,
collection_name=None,
data_type=None,
unit_id=None,
scenario_id=None,
metadata_key=None,
metadata_val=None,
attr_id = None,
... |
def update_dataset(dataset_id, name, data_type, val, unit_id, metadata={}, flush=True, **kwargs):
"""
Update an existing dataset
"""
if dataset_id is None:
raise HydraError("Dataset must have an ID to be updated.")
user_id = kwargs.get('user_id')
dataset = db.DBSession.query(Datas... |
def add_dataset(data_type, val, unit_id=None, metadata={}, name="", user_id=None, flush=False):
"""
Data can exist without scenarios. This is the mechanism whereby
single pieces of data can be added without doing it through a scenario.
A typical use of this would be for setting default valu... |
def _bulk_insert_data(bulk_data, user_id=None, source=None):
"""
Insert lots of datasets at once to reduce the number of DB interactions.
user_id indicates the user adding the data
source indicates the name of the app adding the data
both user_id and source are added as metadata
... |
def _get_metadata(dataset_ids):
"""
Get all the metadata for a given list of datasets
"""
metadata = []
if len(dataset_ids) == 0:
return []
if len(dataset_ids) > qry_in_threshold:
idx = 0
extent = qry_in_threshold
while idx < len(dataset_ids):
log.... |
def _get_datasets(dataset_ids):
"""
Get all the datasets in a list of dataset IDS. This must be done in chunks of 999,
as sqlite can only handle 'in' with < 1000 elements.
"""
dataset_dict = {}
datasets = []
if len(dataset_ids) > qry_in_threshold:
idx = 0
extent =qr... |
def _get_collection(collection_id):
"""
Get a dataset collection by ID
:param collection ID
"""
try:
collection = db.DBSession.query(DatasetCollection).filter(DatasetCollection.id==collection_id).one()
return collection
except NoResultFound:
raise ResourceNotFound... |
def _get_collection_item(collection_id, dataset_id):
"""
Get a single dataset collection entry by collection ID and dataset ID
:param collection ID
:param dataset ID
"""
collection_item = db.DBSession.query(DatasetCollectionItem).\
filter(DatasetCollectionItem.collection_... |
def add_dataset_to_collection(dataset_id, collection_id, **kwargs):
"""
Add a single dataset to a dataset collection.
"""
collection_i = _get_collection(collection_id)
collection_item = _get_collection_item(collection_id, dataset_id)
if collection_item is not None:
raise HydraError("... |
def remove_dataset_from_collection(dataset_id, collection_id, **kwargs):
"""
Add a single dataset to a dataset collection.
"""
collection_i = _get_collection(collection_id)
collection_item = _get_collection_item(collection_id, dataset_id)
if collection_item is None:
raise HydraError(... |
def check_dataset_in_collection(dataset_id, collection_id, **kwargs):
"""
Check whether a dataset is contained inside a collection
:param dataset ID
:param collection ID
:returns 'Y' or 'N'
"""
_get_collection(collection_id)
collection_item = _get_collection_item(collect... |
def get_collections_like_name(collection_name,**kwargs):
"""
Get all the datasets from the collection with the specified name
"""
try:
collections = db.DBSession.query(DatasetCollection).filter(DatasetCollection.name.like("%%%s%%"%collection_name.lower())).all()
except NoResultFound:
... |
def get_collection_datasets(collection_id,**kwargs):
"""
Get all the datasets from the collection with the specified name
"""
collection_datasets = db.DBSession.query(Dataset).filter(Dataset.id==DatasetCollectionItem.dataset_id,
DatasetCollectionItem.collectio... |
def get_val_at_time(dataset_id, timestamps,**kwargs):
"""
Given a timestamp (or list of timestamps) and some timeseries data,
return the values appropriate to the requested times.
If the timestamp is before the start of the timeseries data, return
None If the timestamp is after the end of the times... |
def get_multiple_vals_at_time(dataset_ids, timestamps,**kwargs):
"""
Given a timestamp (or list of timestamps) and a list of timeseries datasets,
return the values appropriate to the requested times.
If the timestamp is before the start of the timeseries data, return
None If the timestamp is after ... |
def get_vals_between_times(dataset_id, start_time, end_time, timestep,increment,**kwargs):
"""
Retrive data between two specified times within a timeseries. The times
need not be specified in the timeseries. This function will 'fill in the blanks'.
Two types of data retrieval can be done.
... |
def delete_dataset(dataset_id,**kwargs):
"""
Removes a piece of data from the DB.
CAUTION! Use with care, as this cannot be undone easily.
"""
try:
d = db.DBSession.query(Dataset).filter(Dataset.id==dataset_id).one()
except NoResultFound:
raise HydraError("Dataset %s does... |
def get_notes(ref_key, ref_id, **kwargs):
"""
Get all the notes for a resource, identifed by ref_key and ref_ido.
Returns [] if no notes are found or if the resource doesn't exist.
"""
notes = db.DBSession.query(Note).filter(Note.ref_key==ref_key)
if ref_key == 'NETWORK':
notes =... |
def add_note(note, **kwargs):
"""
Add a new note
"""
note_i = Note()
note_i.ref_key = note.ref_key
note_i.set_ref(note.ref_key, note.ref_id)
note_i.value = note.value
note_i.created_by = kwargs.get('user_id')
db.DBSession.add(note_i)
db.DBSession.flush()
return note_i |
def update_note(note, **kwargs):
"""
Update a note
"""
note_i = _get_note(note.id)
if note.ref_key != note_i.ref_key:
raise HydraError("Cannot convert a %s note to a %s note. Please create a new note instead."%(note_i.ref_key, note.ref_key))
note_i.set_ref(note.ref_key, note.ref_id)
... |
def purge_note(note_id, **kwargs):
"""
Remove a note from the DB permenantly
"""
note_i = _get_note(note_id)
db.DBSession.delete(note_i)
db.DBSession.flush() |
def login(username, password, **kwargs):
"""
Login a user, returning a dict containing their user_id and session_id
This does the DB login to check the credentials, and then creates a session
so that requests from apps do not need to perform a login
args:
username (stri... |
def logout(session_id, **kwargs):
"""
Logout a user, removing their cookie if it exists and returning 'OK'
args:
session_id (string): The session ID to identify the cookie to remove
returns:
'OK'
raises:
HydraError if the logout fails
"""
... |
def get_session_user(session_id, **kwargs):
"""
Given a session ID, get the user ID that it is associated with
args:
session_id (string): The user's ID to identify the cookie to remove
returns:
user_id (string) or None if the session does not exist
"""
hydr... |
def array_dim(arr):
"""Return the size of a multidimansional array.
"""
dim = []
while True:
try:
dim.append(len(arr))
arr = arr[0]
except TypeError:
return dim |
def check_array_struct(array):
"""
Check to ensure arrays are symmetrical, for example:
[[1, 2, 3], [1, 2]] is invalid
"""
#If a list is transformed into a numpy array and the sub elements
#of this array are still lists, then numpy failed to fully convert
#the list, meaning it is no... |
def arr_to_vector(arr):
"""Reshape a multidimensional array to a vector.
"""
dim = array_dim(arr)
tmp_arr = []
for n in range(len(dim) - 1):
for inner in arr:
for i in inner:
tmp_arr.append(i)
arr = tmp_arr
tmp_arr = []
return arr |
def vector_to_arr(vec, dim):
"""Reshape a vector to a multidimensional array with dimensions 'dim'.
"""
if len(dim) <= 1:
return vec
array = vec
while len(dim) > 1:
i = 0
outer_array = []
for m in range(reduce(mul, dim[0:-1])):
inner_array = []
... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.