Skip to content

Commit

Permalink
Merge pull request #34 from openhealthcare/remove-multisave
Browse files Browse the repository at this point in the history
Remove multisave
  • Loading branch information
davidmiller committed Jul 15, 2016
2 parents 9ea554e + e61ae3d commit fc50ba2
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 97 deletions.
53 changes: 43 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class SimplePathway(pathways.Pathway):

You could access this pathway from e.g. `http:\\localhost:8000\pathway\#\simples\`.

### Steps with multiple isntances of records

Sometimes we may want to add multiple instances of a subrecord at the same time, for example when we're recording multiple allergies. To add a multiple step simply use a MultiSaveStep, for example:

Expand All @@ -74,6 +75,48 @@ class SimplePathway(pathways.Pathway):
)
```

### Complex steps - more than one subrecord type

If we want to save multiple types of subrecords at the same step, we can do that by simply including the
relevant form templates in a custom step template.

```python
from pathway import pathways
from myapp import models

class SimplePathway(pathways.Pathway):
display_name = 'A simple pathway'
slug = 'simples'
steps = (
pathways.Step(
title='Demographics and Diagnosis',
icon='fa fa-clock',
template='pathways/demographics_and_diagnosis_step.html'
),
)
```

The title and icon are rendered in the header for this step in your pathway, which exist outside the scope of the step template itself. Then all we would need is the template itself:

```html
<!-- pathways/demographics_and_diagnosis_step.html -->
{% include models.Demographics.get_form_template %}
{% include models.Diagnosis.get_form_template %}
```

#### Complex steps with multiple instances per subrecord

If we need to also save multiple types of the same subrecord e.g. `Treatment` in this step,
we simply use the `multisave` template tag.

```html
{% load pathways %}

{% include models.Demographics.get_form_template %}
{% include models.Diagnosis.get_form_template %}
{% multisave models.Treatment %}
```

### Success redirects

Often, after successfully saving a pathway, we want to redirect the user to a different
Expand Down Expand Up @@ -160,13 +203,3 @@ Redirect to the patient detail page for this patient.
## pathways.RedirectsToEpisodeMixin

Redirect to the patient detail page, viewing the last episode for this patient.

## Template tags

Pathways allow you to do things that aren't usually trivial including saving multiple models
at the same time, to make this easy we have a template tag that you can add, this will add
forms for each of your existing models and allow the user to add new models dynamically

```html
{% multisave models.Treatment %}
```
1 change: 0 additions & 1 deletion pathway/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class PathwayPlugin(plugins.OpalPlugin):
'js/pathway/controllers/find_patient.js',
'js/pathway/controllers/line_controller.js',
'js/pathway/controllers/multi_stage_default.js',
'js/pathway/controllers/multi_save.js',
'js/pathway/services/multi_stage_form.js',
'js/pathway/services/pathway_loader.js',
'js/pathway/directives.js',
Expand Down
24 changes: 3 additions & 21 deletions pathway/pathways.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import inspect
from copy import copy
from functools import wraps

from django.core.urlresolvers import reverse
from django.conf import settings
from django.db import models, transaction
from django.utils.text import slugify
from django.http import Http404

from opal.core import discoverable
from opal.models import Patient, Episode, EpisodeSubrecord
from opal.utils import stringport
from opal.utils import _itersubclasses
from opal.models import Patient, Episode


def extract_pathway_field(some_fun):
Expand Down Expand Up @@ -73,21 +68,8 @@ def pre_save(self, data, user):


class MultSaveStep(Step):
def to_dict(self):
result = super(MultSaveStep, self).to_dict()

if "template_url" not in self.other_args:
result["template_url"] = "/templates/pathway/multi_save.html"

if "controller_class" not in self.other_args:
result["controller_class"] = "MultiSaveCtrl"

result["model_form_url"] = self.model.get_form_url()

result["record_url"] = reverse(
"record_view", kwargs=dict(model=self.model.get_api_name())
),
return result
def template_url(self):
return "/templates/pathway/multi_save.html"


class RedirectsToPatientMixin(object):
Expand Down
40 changes: 0 additions & 40 deletions pathway/static/js/pathway/controllers/multi_save.js

This file was deleted.

27 changes: 16 additions & 11 deletions pathway/static/js/pathway/directives.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
var directives = angular.module('opal.directives', []);

directives.directive("saveMultiple", function($parse, $rootScope, Referencedata){
return {
scope: {
parentModel: "=saveMultiple",
form_url: "=?saveMultipleFormUrl",
display_name: "=?saveMultipleLabel",
model_name: "=?saveMultipleModelName"
},
templateUrl: "/templates/pathway/save_multiple.html",
link: function(scope, element, attrs){
var editingString = attrs.saveMultiple;
var model = editingString.substr(editingString.indexOf(".")+1);
var getModel = $parse(model);

if(!scope.model_name){
scope.model_name = editingString.substr(editingString.indexOf(".")+1);
}
var getModel = $parse(scope.model_name);

// hopefully we can do this nicer in future
Referencedata.then(function(referencedata){
Expand All @@ -22,19 +26,16 @@ directives.directive("saveMultiple", function($parse, $rootScope, Referencedata)
if(!fields){
throw "fields not loaded";
}
return fields[model][name];
return fields[scope.model_name][name];
}

var requiredAttrs = {
"form_url": "saveMultipleformUrl",
"form_url": "saveMultipleFormUrl",
"display_name": "saveMultipleLabel"
};

_.each(requiredAttrs, function(jsName, schemaName){
if(attrs[jsName]){
scope[schemaName] = attrs[jsName];
}
else{
if(!scope[schemaName]){
scope[schemaName] = getSchemaField(schemaName);
}
});
Expand All @@ -50,7 +51,11 @@ directives.directive("saveMultiple", function($parse, $rootScope, Referencedata)

// shallow copy not deep copy as angular copy can't
// deal with moments
scope.model = {'allModels': _.clone(scope.parentModel)};
scope.model = {'allModels': _.map(scope.parentModel, function(row){
var result = {};
result[scope.model_name] = row;
return result;
})};

scope.remove = function($index){
scope.model.allModels.splice($index, 1);
Expand Down
2 changes: 2 additions & 0 deletions pathway/static/js/pathway/services/multi_stage_form.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ angular.module('opal.services').provider('multistage', function(){
}
_.each(scope.steps, function(step){
var stepScope = scope.$new();
// always put the step on the scope
stepScope.step = step;
if(step.controller_class){
step.controller = $controller(step.controller_class, {
step: step,
Expand Down
2 changes: 1 addition & 1 deletion pathway/templates/_helpers/multisave.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<div save-multiple="{{ model }}" save-multiple-form-url="{{ form_url }}" save-multiple-label="{{ label }}"></div>
<div save-multiple="{{ model }}" save-multiple-form-url="'{{ form_url }}'" save-multiple-label="'{{ label }}'"></div>
12 changes: 1 addition & 11 deletions pathway/templates/pathway/multi_save.html
Original file line number Diff line number Diff line change
@@ -1,11 +1 @@
<div ng-repeat="editing in allEditing">
<div class="col-md-10 col-md-push-1 content-offset-below-10">
<h4>[[ instanceName ]] [[ $index + 1 ]] <a ng-click="remove($index)"><i class="fa fa-times-circle"></i></a></h4>
</div>
<span ng-include src="currentStep.model_form_url"></span>
</div>
<div class="row">
<div class="col-sm-11">
<a ng-click="addAnother()" class="pull-right btn btn-primary">Add Another</a>
</div>
</div>
<div save-multiple="editing[step.api_name]" save-multiple-model-name="step.api_name"></div>
4 changes: 2 additions & 2 deletions pathway/tests/test_template_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ def test_with_model(self, get_form_url):
rendered = template.render(Context(models))
self.assertIn('save-multiple="editing.colour"', rendered)
self.assertIn(
'save-multiple-form-url="/templates/forms/colour.html"', rendered
'save-multiple-form-url="\'/templates/forms/colour.html\'"', rendered
)
self.assertIn(
'save-multiple-label="Colour"', rendered
'save-multiple-label="\'Colour\'"', rendered
)

0 comments on commit fc50ba2

Please sign in to comment.