Skip to content

Commit

Permalink
Merge pull request #4492 from archesproject/4487_card_order
Browse files Browse the repository at this point in the history
Enforces card order in the MSM and persists that order to the mobile …
  • Loading branch information
chiatt authored Jan 30, 2019
2 parents 79e4edd + 32f1d26 commit 85adef2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
4 changes: 4 additions & 0 deletions arches/app/media/js/models/mobile-survey.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@ define([
this.parse(JSON.parse(this._mobilesurvey()), self);
},

getInitialSurvey: function() {
return JSON.parse(this._mobilesurvey());
},

_getURL: function(method) {
return this.url(this.id);
},
Expand Down
26 changes: 24 additions & 2 deletions arches/app/media/js/viewmodels/mobile-survey.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,17 @@ define([

this.displayUser = ko.observable();

this.resetCards = function(cards){
this.resetCards = function(){
var initialsurvey = this.mobilesurvey.getInitialSurvey();
var initialcards = initialsurvey.cards;
var initialresources = initialsurvey.datadownloadconfig.resources;
_.each(self.allResources, function(r){
_.each(r.cards(), function(c){
c.approved(_.contains(cards(), c.cardid));
c.approved(_.contains(initialcards, c.cardid));
});
r.hasApprovedCards() ? r.added(true) : r.added(false);
});
self.mobilesurvey.datadownloadconfig.resources(initialresources);
};

this.resetIdentities = function(mobilesurvey){
Expand All @@ -162,6 +166,24 @@ define([
_.each(this.allResources, this.initializeResource);
_.each(this.allIdentities, this.initializeIdentities);

this.resourceOrderedCards = ko.pureComputed(function(){
var cards = [];
self.allResources.forEach(function(r){
r.cards()
.filter(function(f){if (f.approved()){return f;}})
.forEach(function(m){
cards.push(m.cardid);
});
});
return cards;
});

this.mobilesurvey.cards(this.resourceOrderedCards());

this.resourceOrderedCards.subscribe(function(val){
self.mobilesurvey.cards(val);
});

this.selectedResourceIds = ko.pureComputed({
read: function() {
return this.allResources.filter(function(r) {
Expand Down
10 changes: 7 additions & 3 deletions arches/app/views/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,17 +152,21 @@ def get_child_cardids(card, cardset):
projects_for_couch = [project.serialize_for_mobile() for project in projects]
for project in projects_for_couch:
permitted_cards = set()
for card in models.CardModel.objects.filter(cardid__in=project['cards']):
ordered_project_cards = project['cards']
for rootcardid in project['cards']:
card = models.CardModel.objects.get(cardid=rootcardid)
if str(card.nodegroup_id) in permitted_nodegroups:
permitted_cards.add(str(card.cardid))
get_child_cardids(card, permitted_cards)
project['cards'] = list(permitted_cards)
for graph in project['graphs']:
cards = []
for card in graph['cards']:
if card['cardid'] in permitted_cards:
if card['cardid'] in project['cards']:
card['relative_position'] = ordered_project_cards.index(
card['cardid']) if card['cardid'] in ordered_project_cards else None
cards.append(card)
graph['cards'] = cards
graph['cards'] = sorted(cards, key=lambda x: x['relative_position'])
response = JSONResponse(projects_for_couch, indent=4)
return response

Expand Down
9 changes: 4 additions & 5 deletions arches/app/views/mobile_survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
from arches.app.views.base import MapBaseManagerView
import arches.app.views.search as search


def get_survey_resources(mobile_survey):
graphs = models.GraphModel.objects.filter(isresource=True).exclude(graphid=settings.SYSTEM_SETTINGS_RESOURCE_MODEL_ID)
resources = []
Expand All @@ -56,8 +55,10 @@ def get_survey_resources(mobile_survey):
for i, graph in enumerate(graphs):
cards = []
if i == 0 or unicode(graph.graphid) in active_graphs:
cards = [Card.objects.get(pk=card.cardid) for card in models.CardModel.objects.filter(graph=graph)]
resources.append({'name': graph.name, 'id': graph.graphid, 'subtitle': graph.subtitle, 'iconclass': graph.iconclass, 'cards': cards})
cards = [Card.objects.get(pk=card.cardid) for card in models.CardModel.objects.filter(
graph=graph).order_by('sortorder')]
resources.append({'name': graph.name, 'id': graph.graphid,
'subtitle': graph.subtitle, 'iconclass': graph.iconclass, 'cards': cards})

return resources

Expand Down Expand Up @@ -165,7 +166,6 @@ def get_history(survey, history):
history['editors'][entry['user_id']]['lastsync'] = entry['finished']
for id, editor in iter(history['editors'].items()):
editor['lastsync'] = datetime.strftime(editor['lastsync'], '%Y-%m-%d %H:%M:%S')
print(history)
return history

identities = []
Expand Down Expand Up @@ -322,7 +322,6 @@ def post(self, request, surveyid):
# self.notify_mobile_survey_start(request, mobile_survey)
# else:
# self.notify_mobile_survey_end(request, mobile_survey)

mobile_survey.name = data['name']
mobile_survey.description = data['description']
mobile_survey.onlinebasemaps = data['onlinebasemaps']
Expand Down

0 comments on commit 85adef2

Please sign in to comment.