From f05ac1d397f5aea5abfd7f29803f10f91c20971e Mon Sep 17 00:00:00 2001 From: Cyrus Hiatt Date: Tue, 29 Jan 2019 17:14:46 -0800 Subject: [PATCH 1/2] Enforces card order in the MSM and persists that order to the mobile app. Also fixes card dirty state issues. re #4487 --- arches/app/media/js/models/mobile-survey.js | 4 +++ .../app/media/js/viewmodels/mobile-survey.js | 26 +++++++++++++++++-- arches/app/views/api.py | 9 ++++--- arches/app/views/mobile_survey.py | 5 +--- 4 files changed, 35 insertions(+), 9 deletions(-) diff --git a/arches/app/media/js/models/mobile-survey.js b/arches/app/media/js/models/mobile-survey.js index 97d6e47025d..6f4eeb3e141 100644 --- a/arches/app/media/js/models/mobile-survey.js +++ b/arches/app/media/js/models/mobile-survey.js @@ -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); }, diff --git a/arches/app/media/js/viewmodels/mobile-survey.js b/arches/app/media/js/viewmodels/mobile-survey.js index 814d44ce2b2..bae7ba65deb 100644 --- a/arches/app/media/js/viewmodels/mobile-survey.js +++ b/arches/app/media/js/viewmodels/mobile-survey.js @@ -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){ @@ -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) { diff --git a/arches/app/views/api.py b/arches/app/views/api.py index 10391fb2819..128242159ff 100644 --- a/arches/app/views/api.py +++ b/arches/app/views/api.py @@ -152,7 +152,9 @@ 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) @@ -160,9 +162,10 @@ def get_child_cardids(card, cardset): 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 diff --git a/arches/app/views/mobile_survey.py b/arches/app/views/mobile_survey.py index fbc713da2c5..a7a10ca8b09 100644 --- a/arches/app/views/mobile_survey.py +++ b/arches/app/views/mobile_survey.py @@ -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 = [] @@ -56,7 +55,7 @@ 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)] + 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 @@ -165,7 +164,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 = [] @@ -322,7 +320,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'] From 32f1d2627964ac106b7f4c329b9afb0737bf0508 Mon Sep 17 00:00:00 2001 From: Cyrus Hiatt Date: Tue, 29 Jan 2019 18:13:26 -0800 Subject: [PATCH 2/2] lints --- arches/app/media/js/viewmodels/mobile-survey.js | 6 +++--- arches/app/views/api.py | 5 +++-- arches/app/views/mobile_survey.py | 6 ++++-- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/arches/app/media/js/viewmodels/mobile-survey.js b/arches/app/media/js/viewmodels/mobile-survey.js index bae7ba65deb..508977fb5b4 100644 --- a/arches/app/media/js/viewmodels/mobile-survey.js +++ b/arches/app/media/js/viewmodels/mobile-survey.js @@ -175,14 +175,14 @@ define([ cards.push(m.cardid); }); }); - return cards + return cards; }); - this.mobilesurvey.cards(this.resourceOrderedCards()) + this.mobilesurvey.cards(this.resourceOrderedCards()); this.resourceOrderedCards.subscribe(function(val){ self.mobilesurvey.cards(val); - }) + }); this.selectedResourceIds = ko.pureComputed({ read: function() { diff --git a/arches/app/views/api.py b/arches/app/views/api.py index 128242159ff..3b6a50a049b 100644 --- a/arches/app/views/api.py +++ b/arches/app/views/api.py @@ -152,7 +152,7 @@ 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() - ordered_project_cards = 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: @@ -163,7 +163,8 @@ def get_child_cardids(card, cardset): cards = [] for card in graph['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 + card['relative_position'] = ordered_project_cards.index( + card['cardid']) if card['cardid'] in ordered_project_cards else None cards.append(card) graph['cards'] = sorted(cards, key=lambda x: x['relative_position']) response = JSONResponse(projects_for_couch, indent=4) diff --git a/arches/app/views/mobile_survey.py b/arches/app/views/mobile_survey.py index a7a10ca8b09..529a8356ab4 100644 --- a/arches/app/views/mobile_survey.py +++ b/arches/app/views/mobile_survey.py @@ -55,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).order_by('sortorder')] - 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