Common App

common.constants

Here are defined values used in other modules. Those values are used in enum database fields, in particular with django menus in forms. A lot of class defined here are derived from the enum.Enum class and common.constants.EnumMixin, which let to get a enum number value from an Enum object, as described in chapter 6.4.8 of Two scoops of django:

from common.constants import STATUSES

WAITING = STATUSES.get_value("waiting")

WAITING in this example is the numerical representation of waiting status in the The Unified Internal Database. Other values stored in this modules are the API endpoints used for BioSamples submission, the different annotation confidences used in the ontology tables or the fields that are listed within the batch update views used for fix data in a submission.

Common statuses

Statuses are applied for a uid.models.Submission and biosample.models.Submission and reflect what happened to submissions and what is possible to do with submission. Here are the main statuses:

  • WAITING: means a processing phase where no user access is intended to data. While in this status data are checked, submitted or loaded and no update/deletion should occur in this stage. Model and views should have their methods to prevent user access during this phase.

  • LOADED: means that data were correctly loaded insided The Unified Internal Database. data could be modified but need to be validated before submission

  • ERROR: error is a status that should never be seen. It mean an issue in InjectTool itself or an error in data import. Entries in such status need to be verified

  • READY: this status means that the validation is successful, and data could be submitted to biosample.

  • NEED_REVISION: this status mean an issue in validation, or an error in biosample submission. Data need changes in order to be correctly subitted to biosample.

  • SUBMITTED: this status mean that data were submitted to biosample and the system is waiting for USI response in order to collect biosample_ids and complete the submission.

  • COMPLETED: this status means that data were correctly submitted to biosample and that they received a biosample_id. After such status, you need to refer to biosample_id to update or change such data.

Status could change as described by this figure:

../_images/submission_statuses.png

A subset of such statuses are applied also to uid.models.Animal and uid.models.Sample objects (LOADED, READY, NEED_REVISION, SUBMITTED, COMPLETED). Their meaning is the same of submission statuses.

Common confidences

This enum.Enum class models annotation confidence in dictionary tables after annotation with zooma. Even if all annotation statuses are possible, there are three mainly chosen statuses:

  • CURATED: means that this ontology term is annotated manually

  • GOOD and HIGH: those are annotation accuracies as provided by zooma

common.constants module contents

Created on Mon Jun 11 16:05:18 2018

@author: Paolo Cozzi <cozzi@ibba.cnr.it>

class common.constants.EnumMixin[source]

Bases: object

Common methods for my Enum classes

classmethod get_value(member)[source]

Get numerical representation of an Enum object

classmethod get_value_by_desc(value)[source]

Get numerical representation of an Enum object by providing a description

classmethod get_value_display(value)[source]

Get the display value from a key

common.fields

common.fields module contents

Created on Tue Oct 30 15:32:58 2018

@author: Paolo Cozzi <cozzi@ibba.cnr.it>

class common.fields.ProtectedFileField(*args, **kwargs)[source]

Bases: django.db.models.fields.files.FileField

__init__(*args, **kwargs)[source]

Initialize self. See help(type(self)) for accurate signature.

class common.fields.ProtectedImageField(*args, **kwargs)[source]

Bases: django.db.models.fields.files.ImageField

__init__(*args, **kwargs)[source]

Initialize self. See help(type(self)) for accurate signature.

common.forms

common.forms module contents

Created on Tue Nov 20 16:56:36 2018

@author: Paolo Cozzi <cozzi@ibba.cnr.it>

class common.forms.RequestFormMixin(*args, **kwargs)[source]

Bases: object

remove self.request from kwargs to avoid errors

__init__(*args, **kwargs)[source]

Initialize self. See help(type(self)) for accurate signature.

common.helpers

common.helpers module contents

Created on Wed Mar 27 12:50:16 2019

@author: Paolo Cozzi <cozzi@ibba.cnr.it>

common.helpers.format_attribute(value, terms=None, library_uri='http://purl.obolibrary.org/obo', units=None)[source]

Format a generic attribute into biosample dictionary

common.helpers.get_admin_emails()[source]

Return admin email from image.settings

common.helpers.get_deleted_objects(objs, db_alias='default')[source]
common.helpers.image_timedelta(t1, t2)[source]

A function to deal with image time intervals. Returns a number and time unit

common.helpers.parse_image_timedelta(interval)[source]

A function to parse from a image_timdelta string

common.helpers.send_mail_to_admins(email_subject, email_message, default_from='example@mail.com', addresses=['bioinfo.ibba@gmail.com', 'paolo.cozzi@ibba.cnr.it'])[source]

Send a mail to all admins

Parameters
  • email_subject (str) – the email subject

  • email_message (str) – the body of the email

  • default_from (str) – the from field of the email

  • addresses (list) – a list of email addresses

async common.helpers.send_message_to_websocket(message, pk)[source]

Function will create websocket object and send message to django-channels

Parameters
  • message (dict) – message to send to websocket

  • pk (str) – primary key of submission

common.helpers.uid2biosample(value)[source]

Convert human-readable name to model field

common.storage

common.storage module contents

Created on Tue Oct 30 15:28:14 2018

@author: Paolo Cozzi <cozzi@ibba.cnr.it>

class common.storage.ProtectedFileSystemStorage(*args, **kwargs)[source]

Bases: django.core.files.storage.FileSystemStorage

base_location
base_url

common.tasks

common.tasks module contents

Created on Tue Jan 15 16:42:24 2019

@author: Paolo Cozzi <cozzi@ibba.cnr.it>

class common.tasks.BaseTask(*args, **kwargs)[source]

Bases: celery.app.task.Task

Base class to celery tasks. Define logs for on_failure and debug_task

__init__(*args, **kwargs)[source]

Initialize self. See help(type(self)) for accurate signature.

action = None
debug_task()[source]
description = None
ignore_result = False
name = None
on_failure(exc, task_id, args, kwargs, einfo)[source]

Error handler.

This is run by the worker when the task fails.

Parameters
  • exc (Exception) – The exception raised by the task.

  • task_id (str) – Unique id of the failed task.

  • args (Tuple) – Original arguments for the task that failed.

  • kwargs (Dict) – Original keyword arguments for the task that failed.

  • einfo (ExceptionInfo) – Exception information.

Returns

The return value of this handler is ignored.

Return type

None

priority = None
rate_limit = None
reject_on_worker_lost = None
request_stack = <celery.utils.threads._LocalStack object>
serializer = 'json'
store_errors_even_if_ignored = False
track_started = False
typing = True
class common.tasks.NotifyAdminTaskMixin[source]

Bases: object

A mixin to send error message to admins

action = None
on_failure(exc, task_id, args, kwargs, einfo)[source]

Override the default on_failure method

(task)common.tasks.cleanupregistration()

Cleanup expired registration keys by using Django management command.

(task)common.tasks.clearsessions()

Cleanup expired sessions by using Django management command.

class common.tasks.exclusive_task(task_name, lock_id, blocking=False, block_expire=False)[source]

Bases: object

A class decorator to execute an exclusive task by decorating celery.tasks.Task.run (run this task once, others task calls will return already running message without calling task or will wait until other tasks of this type are completed)

Parameters
  • task_name (str) – task name used for debug

  • lock_id (str) – the task lock id

  • blocking (bool) – set task as blocking (wait until no other tasks are running. def. False)

  • lock_expire (bool) – define if lock will expire or not after a certain time (def. False)

__init__(task_name, lock_id, blocking=False, block_expire=False)[source]

If there are decorator arguments, the function to be decorated is not passed to the constructor!

common.tasks.redis_lock(lock_id, blocking=False, expire=True)[source]

This function get a lock relying on a lock name and other status. You can describe more process using the same lock name and give exclusive access to one of them.

Parameters
  • lock_id (str) – the name of the lock to take

  • blocking (bool) – if True, we wait until we have the block, if False we returns immediately False

  • expire (bool) – if True, lock will expire after LOCK_EXPIRE timeout, if False, it will persist until lock is released

Returns

True if lock acquired, False otherwise

Return type

bool

common.tests

common.tests module contents

Created on Wed Mar 27 14:40:44 2019

@author: Paolo Cozzi <cozzi@ibba.cnr.it>

Here I will set all the tests.mixins I want to export outside common.tests

class common.tests.AsyncIOMixin[source]

Bases: object

A common mixing for async objects

setUp()[source]
tearDown()[source]
class common.tests.DataSourceMixinTestCase[source]

Bases: object

Place file in data source directory

base_dir = '/home/docs/checkouts/readthedocs.org/user_builds/image-injecttool/checkouts/devel/django-data/image/common/tests'
check_errors(my_check, message)[source]

Common stuff for error in file loading

dst_path = None
classmethod setUpClass()[source]

Place test data in data source if needed

submission_model = None
classmethod tearDownClass()[source]

Remove test data from data source if needed

upload_datasource(message)[source]

Testing uploading and importing data from crbanim to UID

upload_method = None
uploaded_file = False
class common.tests.FormMixinTestCase[source]

Bases: common.tests.mixins.GeneralMixinTestCase

form_class = None
test_contains_form()[source]
test_csrf()[source]
class common.tests.GeneralMixinTestCase[source]

Bases: common.tests.mixins.LoginMixinTestCase, common.tests.mixins.StatusMixinTestCase, common.tests.mixins.MessageMixinTestCase

class common.tests.InvalidFormMixinTestCase[source]

Bases: common.tests.mixins.StatusMixinTestCase, common.tests.mixins.MessageMixinTestCase

test_form_errors()[source]
class common.tests.LoginMixinTestCase[source]

Bases: object

test_unathenticated()[source]

Non Authenticated user are directed to login page

url = None
class common.tests.MessageMixinTestCase[source]

Bases: object

check_messages(response, tag, message_text)[source]

Check that a response has a message of a certain type

class common.tests.OwnerMixinTestCase[source]

Bases: common.tests.mixins.LoginMixinTestCase

test_ownership()[source]

Test a non-owner having a 404 response

class common.tests.StatusMixinTestCase[source]

Bases: object

response = None
test_status_code()[source]
class common.tests.WebSocketMixin[source]

Bases: common.tests.mixins.AsyncIOMixin

Override setUp to mock websocket objects

check_message(message, notification_message, validation_message=None, pk=1)[source]

assert message to websocket called with parameters

check_message_not_called()[source]

Check django channels async messages not called

setUp()[source]
tearDown()[source]

common.views

common.views module contents

Created on Mon Oct 29 15:33:34 2018

@author: Paolo Cozzi <cozzi@ibba.cnr.it>

class common.views.AjaxTemplateView(**kwargs)[source]

Bases: django.views.generic.base.TemplateView

dispatch(*args, **kwargs)[source]
class common.views.DeleteMaterialMixin[source]

Bases: common.views.OwnerMixin

A common DeleteMixin for Material classes (Sample/Animal)

dispatch(request, *args, **kwargs)[source]
get_success_url()[source]
class common.views.DetailMaterialMixin[source]

Bases: common.views.OwnerMixin

A common DetailMixin for Material classes (Sample/Animal)

get_context_data(**kwargs)[source]
class common.views.FormInvalidMixin[source]

Bases: object

form_invalid(form)[source]
get_form_kwargs()[source]
class common.views.ListMaterialMixin[source]

Bases: common.views.OwnerMixin

A common ListMixin for Material classes (Sample/Animal)

class common.views.OwnerMixin[source]

Bases: django.contrib.auth.mixins.LoginRequiredMixin

get_queryset()[source]

Filter base queryset relying on django authenticated sessions:

from common.views import OwnerMixin
from django.views.generic import DetailView

class MyDetailView(OwnerMixin, DetailView):
    def get_queryset(self):
        # call OwnerMixin and DetailView super methods
        qs = super(MyDetailView, self).get_queryset()

        # add custom filter to queryset

        # remeber to return the updated queryset to the caller
        return qs
class common.views.UpdateMaterialMixin[source]

Bases: common.views.OwnerMixin

A common UpdateMixin for Material classes (Sample/Animal)

dispatch(request, *args, **kwargs)[source]
form_valid(form)[source]
get_form()[source]
get_form_kwargs()[source]
validationresult_class = None
common.views.ajax_required(f)[source]