Advanced Usage
The workspace adapter
The app provides an adapter that you can use to provide extra, customized data about a workspace.
By default, the app uses DefaultWorkspaceAdapter.
The default workspace_data_model specified in this adapter has no fields other than those provided by BaseWorkspaceData.
This section describes how to store additional information about a workspace by setting up a custom adapter.
First, you will need to define a new model with the additional fields.
It must inherit from BaseWorkspaceData, which provides a one-to-one field called workspace to the Workspace model.
from django.db import models
from anvil_consortium_manager.models import BaseWorkspaceData
class CustomWorkspaceData(BaseWorkspaceData):
study_name = models.CharField(max_length=255)
consent_code = models.CharField(max_length=16)
You must also define a form containing the additional fields. You must include the workspace field, which will automatically be linked to the new Workspace when creating or importing a workspace.
from django.forms import ModelForm
from models import CustomWorkspaceData
class CustomWorkspaceDataForm(ModelForm):
class Meta:
model = CustomWorkspaceData
fields = ("study_name", "consent_code", workspace")
Optionally, you can define a new django-tables2 table to use in place of the default WorkspaceTable that comes with the app.
This is helpful if you would like to display fields from your custom workspace data model in the Workspace list view.
This table will need to operate on the Workspace model, but it can include fields from your custom workspace data model.
If you do not want to define a custom table, you can use the default table provided by the app: anvil_consortium_manager.tables.WorkspaceTable.
import django_tables2 as tables
from anvil_consortium_manager import models as acm_models
class CustomWorkspaceDataTable(tables.Table):
name = tables.columns.Column(linkify=True)
class Meta:
model = acm_models.Workspace
fields = ("customworkspacedata__study_name", "workspacedata__consent_code", "name")
Next, set up the adapter by subclassing BaseWorkspaceAdapter. You will need to set:
type: a string indicating the workspace type (e.g.,"custom"). This will be stored in theworkspace_typefield of theanvil_consortium_manager.models.Workspacemodel for any workspaces created using the adapter.name: a human-readable name for workspaces created with this adapater (e.g.,"Custom workspace"). This will be used when displaying information about workspaces created with this adapter.workspace_data_model: the model used to store additional data about a workspace, subclassed fromBaseWorkspaceDataworkspace_data_form_class: the form to use to create an instance of theworkspace_data_modellist_table_class: the table to use to display the list of workspacesworkspace_detail_template_name: the template to use to render the detail of the workspace
Here is example of the custom adapter for my_app with the model, form and table defined above.
from anvil_consortium_manager.adapters.workspace import BaseWorkspaceAdapter
from my_app.models import CustomWorkspaceData
from my_app.forms import CustomWorkspaceDataForm
from my_app.tables import CustomWorkspaceTable
class CustomWorkspaceAdapter(BaseWorkspaceAdapter):
type = "custom"
name = "Custom workspace"
workspace_data_model = models.CustomWorkspaceData
workspace_data_form_class = forms.CustomWorkspaceDataForm
list_table_class = tables.CustomWorkspaceTable
workspace_detail_template_name = "my_app/custom_workspace_detail.html"
Finally, to tell the app to use this adapter, set ANVIL_WORKSPACE_ADAPTERS in your settings file, e.g.: ANVIL_WORKSPACE_ADAPTERS = ["my_app.adapters.CustomWorkspaceAdapter"]. You can even define multiple adapters for different types of workspaces, e.g.:
ANVIL_WORKSPACE_ADAPTERS = [
"my_app.adapters.FirstWorkspaceAdapter",
"my_app.adapters.SecondWorkspaceAdapter",
]
as long as you have defined both FirstWorkspaceAdapter and SecondWorkspaceAdapter in your code.
If you define multiple workspaces, the index page and the navbar that comes with the app will show links for each different type of workspace.
If you would like to display information from the custom workspace data model in the WorkspaceDetail view, you can include it in the workspace_data block of the workspace_detail.html template. For example:
{% extends "anvil_consortium_manager/workspace_detail.html" %}
{% block workspace_data %}
<ul>
<li>Study name: {{ object.customworkspacedata.study_name }}</li>
<li>Consent: {{ object.customworkspacedata.consent_code }}</li>
</ul>
{% endblock workspace_data %}
If custom content is not provided for the workspace_data block, a default set of information will be displayed: the billing project, the date added, and the date modified.