Skip to content

Commit

Permalink
Routecalc in MADmin (Map-A-Droid#643)
Browse files Browse the repository at this point in the history
* Routecalc in MADmin

Routecalcs are now editable in MADmin.  A legend has been added to the area page to describe each button
  • Loading branch information
Expl0dingBanana authored Dec 29, 2019
1 parent d61d407 commit d00d4ee
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 6 deletions.
35 changes: 31 additions & 4 deletions madmin/routes/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def add_route(self):
("/settings/ivlists", self.settings_ivlists),
("/settings/monsearch", self.monsearch),
("/settings/shared", self.settings_pools),
("/settings/routecalc", self.settings_routecalc),
("/settings/walker", self.settings_walkers),
("/settings/walker/areaeditor", self.settings_walker_area),
("/reload", self.reload)
Expand Down Expand Up @@ -91,7 +92,8 @@ def process_element(self, **kwargs):
html_single = kwargs.get('html_single')
html_all = kwargs.get('html_all')
subtab = kwargs.get('subtab')
var_parser_section = kwargs.get('var_parser_section', subtab)
section = kwargs.get('section', subtab)
var_parser_section = kwargs.get('var_parser_section', section)
required_data = kwargs.get('required_data', {})
mode_required = kwargs.get('mode_required', False)
passthrough = kwargs.get('passthrough', {})
Expand All @@ -113,7 +115,7 @@ def process_element(self, **kwargs):
else:
pass
try:
settings_vars = self._data_manager.get_settings(subtab, mode=mode)
settings_vars = self._data_manager.get_settings(section, mode=mode)
except (utils.data_manager.dm_exceptions.ModeNotSpecified, utils.data_manager.dm_exceptions.ModeUnknown):
if identifier:
raise
Expand Down Expand Up @@ -141,7 +143,7 @@ def process_element(self, **kwargs):
if identifier is not None:
req = self._data_manager.get_resource(data_source, identifier=identifier)
element = req
included_data[subtab] = element
included_data[section] = element
return render_template(html_single,
uri='%s/%s' % (included_data['base_uri'], identifier),
redirect=redirect_uri,
Expand All @@ -151,7 +153,7 @@ def process_element(self, **kwargs):
settings_vars=settings_vars,
**included_data)
else:
included_data[subtab] = self._data_manager.get_root_resource(data_source)
included_data[section] = self._data_manager.get_root_resource(data_source)
return render_template(html_all,
subtab=subtab,
**included_data
Expand Down Expand Up @@ -287,6 +289,31 @@ def settings_pools(self):
}
return self.process_element(**required_data)

@logger.catch
@auth_required
def settings_routecalc(self):
try:
area_id = request.args.get('area_id')
area = self._data_manager.get_resource('area', identifier=area_id)
if area['routecalc'] != int(request.args.get('id')):
return redirect(url_for('settings_areas'), code=302)
except:
return redirect(url_for('settings_areas'), code=302)
required_data = {
'identifier': 'id',
'base_uri': 'api_routecalc',
'data_source': 'routecalc',
'redirect': 'settings_areas',
'html_single': 'settings_singleroutecalc.html',
'html_all': 'settings_singleroutecalc.html',
'subtab': 'area',
'section': 'routecalc',
'passthrough': {
'areaname': area['name']
}
}
return self.process_element(**required_data)

@logger.catch
@auth_required
def settings_walkers(self):
Expand Down
26 changes: 26 additions & 0 deletions madmin/templates/settings_areas.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,31 @@
<p class="card-text">Areas define zones where scanning should happen. Areas can be assigned to Walkers and Walkers must be assigned to Devices. Every area requires a file containing one or more geofences. MAD will build a route based on these areas and the elements within them.</p>
</div>
</div>
<div class="legend">
<h4>Legend</h4>
<table>
<tbody>
<tr>
<td><button type="button" class="btn btn-success btn-sm"><i class="fas fa-route"></i></button></td>
<td>Edit Route calc</td>
</tr>
{% if config_mode == False %}
<tr>
<td><button type="button" class="btn btn-info btn-sm"><i class="fa fa-sync"></i></button></td>
<td>Recalculate route</td>
</tr>
{% endif %}
<tr>
<td><button type="button" class="btn btn-success btn-sm"><i class="far fa-edit"></i></button></td>
<td>Edit area</td>
</tr>
<tr>
<td><button type="button" class="btn btn-danger btn-sm"><i class="fas fa-trash-alt"></i></i></td>
<td>Delete area</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="row mt-3">
Expand Down Expand Up @@ -139,6 +164,7 @@
</p>
</td>
<td class="text-right align-middle">
<a href="{{ url_for('settings_routecalc') }}?id={{ area.routecalc }}&area_id={{ area_id }}"><button type="button" class="btn btn-success btn-sm edit"><i class="fas fa-route"></i></button></a>
{% if config_mode == False %}
<button data-area='{{ area_id }}' type="button" class="recalculate btn btn-info btn-sm"><i class="fa fa-sync"></i></button>
{% endif %}
Expand Down
52 changes: 52 additions & 0 deletions madmin/templates/settings_singleroutecalc.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{% extends "settings.html" %}

{% block header %}
{{ super() }}
{% endblock %}

{% block scripts %}
{{ super() }}
<script type="text/javascript" src="{{ url_for('static', filename='js/madmin_settings.js') }}"></script>
<script>
$(document).ready(function () {
$("#submit").click(function() {
$.blockUI({ message: '<img src="{{ url_for('static', filename='loading.gif') }}" width="100px" /><br /><h2>Saving {{ subtab }}...</h2>' });
save_data = get_save_data();
if(isEmptyObj(save_data) != false) {
alert('No changes have been made');
window.location.replace('{{ redirect }}');
} else {
process_api_request("{{ uri }}", "{{ method }}", "{{ redirect }}");
}
});
});

function get_routefile_definition() {
var routefile = [];
$.each($("#routefile").val().trim().split("\n"), function() {
if(this.length > 0) {
routefile.push(this);
}
});
return routefile;
}

</script>
{% endblock %}

{% block content %}
{{ super() }}

<h1 class="display-4">Routecalc for {{ areaname }}</h1>
<div class="row">
<div class="col-sm">
<div class="form-group">
<label for="routefile">routefile</label>
<textarea data-callback='get_routefile_definition' rows=20 class="form-control" id="routefile" name="routefile" value="{{ element.routefile }}" data-default="{{ element.routefile }}">{{ element.routefile|join('\n') }}</textarea>
<small class="form-text text-muted">Route stops</small>
</div>
<button type="button" id="submit" class="btn btn-success btn-lg btn-block">Save</button>
</div>
</div>
{% endblock %}

10 changes: 8 additions & 2 deletions tests/api/test_routecalc.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ def test_invalid_post(self):
payload = {
'routefile': 'not a list',
}
errors = {'invalid': [['routefile', 'Comma-delimited list']]}
errors = {'invalid': [
['routefile', 'Comma-delimited list'],
['routefile', 'Must be one coord set per line (float,float)']
]}
super().invalid_post(payload, errors)
self.remove_resources()

Expand All @@ -31,7 +34,10 @@ def test_invalid_put(self):
payload = {
'routefile': 'not a list',
}
errors = {'invalid': [['routefile', 'Comma-delimited list']]}
errors = {'invalid': [
['routefile', 'Comma-delimited list'],
['routefile', 'Must be one coord set per line (float,float)']
]}
super().invalid_put(payload, errors)
self.remove_resources()

Expand Down
19 changes: 19 additions & 0 deletions utils/data_manager/modules/routecalc.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,25 @@ def save(self, force_insert=False, ignore_issues=[]):
core_data['routefile'] = json.dumps(self._data['fields']['routefile'])
super().save(core_data=core_data, force_insert=force_insert, ignore_issues=ignore_issues)

def validate_custom(self):
issues = {}
for row in self['routefile']:
row_split = row.split(',')
if len(row_split) != 2:
issues = {
'invalid': [('routefile', 'Must be one coord set per line (float,float)')]
}
break
try:
float(row_split[0])
float(row_split[1])
except:
issues = {
'invalid': [('routefile', 'Must be one coord set per line (float,float)')]
}
break
return issues

# =====================================================
# ============ Resource-Specific Functions ============
# =====================================================
Expand Down

0 comments on commit d00d4ee

Please sign in to comment.