1
0
Fork 0

Add merge satellites functionality

Signed-off-by: Alfredos-Panagiotis Damkalis <fredy@fredy.gr>
spacecruft
Alfredos-Panagiotis Damkalis 2021-07-19 14:39:55 +03:00
parent 06e2f032fe
commit 0d137863b3
8 changed files with 108 additions and 1 deletions

View File

@ -0,0 +1,17 @@
# Generated by Django 3.1.5 on 2021-07-15 02:20
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('base', '0047_norad_not_required'),
]
operations = [
migrations.AlterModelOptions(
name='satellite',
options={'permissions': (('merge_satellites', 'Can merge satellites'),)},
),
]

View File

@ -255,6 +255,9 @@ class Satellite(models.Model):
)
last_modified = models.DateTimeField(auto_now=True)
class Meta:
permissions = (('merge_satellites', 'Can merge satellites'), )
def __str__(self):
if self.satellite_entry:
name = self.satellite_entry.name

View File

@ -33,6 +33,7 @@ BASE_URLPATTERNS = (
path('users/edit/', views.users_edit, name='users_edit'),
path('robots.txt', views.robots, name='robots'),
path('search/', views.search, name='search_results'),
path('merge_satellites/', views.merge_satellites, name='merge_satellites'),
path('create_satellite/', views.SatelliteCreateView.as_view(), name='create_satellite'),
path(
'update_satellite/<int:pk>/',

View File

@ -462,6 +462,26 @@ class TransmitterUpdateView(LoginRequiredMixin, BSModalUpdateView):
return self.request.META.get('HTTP_REFERER')
@login_required
@require_POST
def merge_satellites(request):
"""Merges satellites if user has merge permission.
"""
primary_satellite = get_object_or_404(Satellite, pk=request.POST['primary-satellite'])
associated_satellite = get_object_or_404(Satellite, pk=request.POST['associated-satellite'])
if request.user.has_perm('base.merge_satellites'):
if primary_satellite.pk == associated_satellite.pk:
messages.error(request, ('Primary and Associated satellites are the same!'))
else:
associated_satellite.associated_satellite = primary_satellite
associated_satellite.save(update_fields=['associated_satellite'])
messages.success(request, ('Merge is complete!'))
else:
messages.error(request, ('No permission to merge satellites!'))
return redirect(reverse('satellites'))
class SatelliteCreateView(LoginRequiredMixin, BSModalCreateView):
"""A django-bootstrap-modal-forms view for creating satellite suggestions"""
template_name = 'base/modals/satellite_create.html'

View File

@ -28,6 +28,10 @@ $(document).ready(function() {
alerticon = 'far fa-thumbs-up';
alerttitle = 'Success';
}
if ($(this).data('alertclass') == 'alert-error') {
alerticon = 'fas fa-ban';
alerttitle = 'Error';
}
if ($(this).data('alertclass') == 'alert-warning') {
alerticon = 'fas fa-exclamation';
alerttitle = 'Alert';

View File

@ -62,4 +62,10 @@ $(document).ready(function() {
table.on('draw', function(){
updateSatelliteModalForm();
});
$('.satellite-to-merge').select2({
placeholder: 'Select a satellite',
dropdownAutoWidth: true,
dropdownParent: $('#merge-satellites-modal')
});
});

View File

@ -244,4 +244,4 @@
{% endcompress %}
</body>
</html>
</html>

View File

@ -7,6 +7,7 @@
<link rel="stylesheet" href="{% static 'lib/admin-lte/plugins/datatables-bs4/css/dataTables.bootstrap4.min.css' %}">
<link rel="stylesheet" href="{% static 'lib/admin-lte/plugins/datatables-buttons/css/buttons.bootstrap4.min.css' %}">
<link rel="stylesheet" href="{% static 'lib/admin-lte/plugins/flag-icon-css/css/flag-icon.min.css' %}">
<link rel="stylesheet" href="{% static 'lib/admin-lte/plugins/select2/css/select2.min.css' %}">
{% endblock %}
{% block top-menu-left %}
@ -28,6 +29,13 @@
<i class="nav-icon mr-2 fas fa-plus-square"></i>
<p class="mb-0">Submit New Satellite</p>
</a>
{% if perms.base.merge_satellites %}
<a class="dropdown-item d-flex align-items-center" data-toggle="modal" data-target="#merge-satellites-modal"
href="#" id="merge-satellites-link" aria-label="Merge satellites">
<i class="nav-icon mr-2 fas fa-compress-arrows-alt"></i>
<p class="mb-0">Merge Satellites</p>
</a>
{% endif %}
</div>
</li>
</ul>
@ -128,6 +136,53 @@
<div class="modal-content"></div>
</div>
</div>
{% if perms.base.merge_satellites %}
<div class="modal fade" id="merge-satellites-modal" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<form id="merge-satellites-form" method="POST" action="{% url 'merge_satellites' %}">
{% csrf_token %}
<div class="modal-header">
<h3 class="modal-title">Merge Satellites</h3>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<div class="input-group my-3">
<label class="input-group-prepend input-group-text" for="primary-satellite">Primary Satellite</label>
<select class="satellite-to-merge form-control" id="primary-satellite" name="primary-satellite">
<option></option> <!-- Empty <option> for showing placeholder -->
{% for sat in satellites %}
<option value="{{sat.pk}}">{{sat}}</option>
{% endfor %}
</select>
</div>
<div class="input-group my-3">
<label class="input-group-prepend input-group-text" for="associated-satellite">Associated Satellite</label>
<select class="satellite-to-merge" id="associated-satellite" name="associated-satellite">
<option></option> <!-- Empty <option> for showing placeholder -->
{% for sat in satellites %}
<option value="{{sat.pk}}">{{sat}}</option>
{% endfor %}
</select>
</div>
</div>
<div class="col-12">
<div class="callout callout-warning">
<h5>WARNING</h5>
<p>"Associated Satellite" data will not be visible after merging.</p>
</div>
</div>
<div class="modal-footer">
<button type="submit" id="merge-satellites" class="btn btn-primary">Merge Satellites</button>
</div>
</form>
</div>
</div>
</div>
{% endif %}
{% endif %}
</div>
@ -140,6 +195,7 @@
<script src="{% static 'lib/admin-lte/plugins/datatables-buttons/js/buttons.colVis.js' %}"></script>
<script src="{% static 'lib/admin-lte/plugins/datatables-bs4/js/dataTables.bootstrap4.min.js' %}"></script>
<script src="{% static 'lib/admin-lte/plugins/datatables-buttons/js/buttons.bootstrap4.min.js' %}"></script>
<script src="{% static 'lib/admin-lte/plugins/select2/js/select2.min.js' %}"></script>
<script src="{% static 'js/jquery.bootstrap.modal.forms.min.js' %}"></script>
<script src="{% static 'js/satellites.js' %}"></script>
{% endblock %}