From 418fe5ab3d9bd79b2a956b2dd7d1b3a81905f6fc Mon Sep 17 00:00:00 2001 From: Alfredos-Panagiotis Damkalis Date: Mon, 26 Apr 2021 12:20:33 +0300 Subject: [PATCH] Support suggestions for creating/editing satellite Signed-off-by: Alfredos-Panagiotis Damkalis --- db/base/forms.py | 38 +++++++++-- db/base/models.py | 11 ++-- db/base/urls.py | 1 + db/base/views.py | 65 +++++++++++++++++-- db/static/js/satellites.js | 8 +++ .../base/modals/satellite_create.html | 44 +++++++++++++ .../base/modals/transmitter_create.html | 3 +- db/templates/base/satellites.html | 33 +++++++++- db/templates/base/transmitters.html | 2 + .../includes/cards/transmitter_card.html | 36 +++++----- 10 files changed, 203 insertions(+), 38 deletions(-) create mode 100644 db/templates/base/modals/satellite_create.html diff --git a/db/base/forms.py b/db/base/forms.py index 0321cfb..0761eae 100644 --- a/db/base/forms.py +++ b/db/base/forms.py @@ -1,7 +1,7 @@ """SatNOGS DB django base Forms class""" from bootstrap_modal_forms.forms import BSModalModelForm from django.core.exceptions import ValidationError -from django.forms import NumberInput, TextInput +from django.forms import TextInput from django.utils.translation import ugettext_lazy as _ from db.base.models import SatelliteEntry, Transmitter, TransmitterEntry @@ -19,7 +19,7 @@ def existing_uuid(value): ) from error -class TransmitterModelForm(BSModalModelForm): # pylint: disable=too-many-ancestors +class TransmitterCreateForm(BSModalModelForm): # pylint: disable=too-many-ancestors """Model Form class for TransmitterEntry objects""" class Meta: model = TransmitterEntry @@ -57,16 +57,18 @@ class TransmitterUpdateForm(BSModalModelForm): # pylint: disable=too-many-ances } -class SatelliteModelForm(BSModalModelForm): +class SatelliteCreateForm(BSModalModelForm): """Form that uses django-bootstrap-modal-forms for satellite editing""" class Meta: model = SatelliteEntry fields = [ - 'norad_cat_id', 'name', 'names', 'operator', 'status', 'description', 'countries', - 'website', 'dashboard_url', 'launched', 'deployed', 'decayed', 'image' + 'norad_cat_id', 'norad_follow_id', 'name', 'names', 'description', 'operator', + 'status', 'countries', 'website', 'dashboard_url', 'launched', 'deployed', 'decayed', + 'image', 'citation' ] labels = { 'norad_cat_id': _('Norad ID'), + 'norad_follow_id': _('Followed Norad ID'), 'names': _('Other names'), 'countries': _('Countries of Origin'), 'launched': _('Launch Date'), @@ -76,4 +78,28 @@ class SatelliteModelForm(BSModalModelForm): 'dashboard_url': _('Dashboard URL'), 'operator': _('Owner/Operator'), } - widgets = {'norad_cat_id': NumberInput(attrs={'readonly': True}), 'names': TextInput()} + widgets = {'names': TextInput()} + + +class SatelliteUpdateForm(BSModalModelForm): + """Form that uses django-bootstrap-modal-forms for satellite editing""" + class Meta: + model = SatelliteEntry + fields = [ + 'norad_cat_id', 'norad_follow_id', 'name', 'names', 'description', 'operator', + 'status', 'countries', 'website', 'dashboard_url', 'launched', 'deployed', 'decayed', + 'image', 'citation' + ] + labels = { + 'norad_cat_id': _('Norad ID'), + 'norad_follow_id': _('Followed Norad ID'), + 'names': _('Other names'), + 'countries': _('Countries of Origin'), + 'launched': _('Launch Date'), + 'deployed': _('Deploy Date'), + 'decayed': _('Re-entry Date'), + 'description': _('Description'), + 'dashboard_url': _('Dashboard URL'), + 'operator': _('Owner/Operator'), + } + widgets = {'names': TextInput()} diff --git a/db/base/models.py b/db/base/models.py index f753004..434299b 100644 --- a/db/base/models.py +++ b/db/base/models.py @@ -254,10 +254,13 @@ class Satellite(models.Model): last_modified = models.DateTimeField(auto_now=True) def __str__(self): - return '{1} ({2}) | {0}'.format( - self.satellite_identifier.sat_id, self.satellite_entry.name, - self.satellite_entry.norad_cat_id - ) + if self.satellite_entry: + name = self.satellite_entry.name + norad_cat_id = self.satellite_entry.norad_cat_id + else: + name = '-' + norad_cat_id = '-' + return '{1} ({2}) | {0}'.format(self.satellite_identifier.sat_id, name, norad_cat_id) @property def transmitters(self): diff --git a/db/base/urls.py b/db/base/urls.py index 414e5c5..06a68f5 100644 --- a/db/base/urls.py +++ b/db/base/urls.py @@ -28,6 +28,7 @@ BASE_URLPATTERNS = ( path('users/edit/', views.users_edit, name='users_edit'), path(r'robots\.txt', views.robots, name='robots'), path('search/', views.search, name='search_results'), + path('create_satellite/', views.SatelliteCreateView.as_view(), name='create_satellite'), path( 'update_satellite//', views.SatelliteUpdateView.as_view(), diff --git a/db/base/views.py b/db/base/views.py index 9cc9e0f..326bf5f 100644 --- a/db/base/views.py +++ b/db/base/views.py @@ -19,10 +19,11 @@ from django.urls import reverse from django.utils.timezone import now from django.views.decorators.http import require_POST -from db.base.forms import SatelliteModelForm, TransmitterModelForm, TransmitterUpdateForm +from db.base.forms import SatelliteCreateForm, SatelliteUpdateForm, TransmitterCreateForm, \ + TransmitterUpdateForm from db.base.helpers import get_apikey -from db.base.models import DemodData, Satellite, SatelliteEntry, Transmitter, TransmitterEntry, \ - TransmitterSuggestion +from db.base.models import DemodData, Satellite, SatelliteEntry, SatelliteIdentifier, \ + Transmitter, TransmitterEntry, TransmitterSuggestion from db.base.tasks import export_frames, notify_suggestion from db.base.utils import cache_statistics, millify, read_influx @@ -353,7 +354,7 @@ class TransmitterCreateView(LoginRequiredMixin, BSModalCreateView): """A django-bootstrap-modal-forms view for creating transmitter suggestions""" template_name = 'base/modals/transmitter_create.html' model = TransmitterEntry - form_class = TransmitterModelForm + form_class = TransmitterCreateForm success_message = 'Your transmitter suggestion was stored successfully and will be \ reviewed by a moderator. Thanks for contibuting!' @@ -421,11 +422,63 @@ class TransmitterUpdateView(LoginRequiredMixin, BSModalUpdateView): return self.request.META.get('HTTP_REFERER') +class SatelliteCreateView(LoginRequiredMixin, BSModalCreateView): + """A django-bootstrap-modal-forms view for creating satellite suggestions""" + template_name = 'base/modals/satellite_create.html' + model = SatelliteEntry + form_class = SatelliteCreateForm + success_message = 'Your satellite suggestion was stored successfully and will be \ + reviewed by a moderator. Thanks for contributing!' + + user = get_user_model() + + def dispatch(self, request, *args, **kwargs): + self.user = request.user + return super().dispatch(request, *args, **kwargs) + + def form_valid(self, form): + satellite_entry = form.instance + satellite_obj = None + # Create Satellite Identifier only when POST request is for saving and + # NORAD ID is not used by other Satellite. + if not self.request.is_ajax(): + try: + satellite_obj = Satellite.objects.get( + satellite_entry__norad_cat_id=satellite_entry.norad_cat_id + ) + satellite_entry.satellite_identifier = satellite_obj.satellite_identifier + except Satellite.DoesNotExist: + satellite_entry.satellite_identifier = SatelliteIdentifier.objects.create() + + satellite_entry.created = now() + satellite_entry.created_by = self.user + + # form_valid triggers also save() allowing us to use satellite_entry + # for creating Satellite object, see comment bellow. + response = super().form_valid(form) + + # Prevents sending notification twice as form_valid is triggered for + # validation and saving. Also create and Satellite object only when POST + # request is for saving and NORAD ID is not used by other Satellite. + if not self.request.is_ajax(): + if not satellite_obj: + satellite_obj = Satellite.objects.create( + satellite_identifier=satellite_entry.satellite_identifier, + satellite_entry=satellite_entry + ) + notify_suggestion.delay(satellite_obj.satellite_entry.pk, self.user.id, 'satellite') + + return response + + def get_success_url(self): + return self.request.META.get('HTTP_REFERER') + + class SatelliteUpdateView(LoginRequiredMixin, BSModalUpdateView): - """A django-bootstrap-modal-forms view for updating satellite fields""" + """A django-bootstrap-modal-forms view for updating satellite entries""" template_name = 'base/modals/satellite_update.html' model = SatelliteEntry - form_class = SatelliteModelForm + form_class = SatelliteUpdateForm success_message = 'Your satellite suggestion was stored successfully and will be \ reviewed by a moderator. Thanks for contributing!' diff --git a/db/static/js/satellites.js b/db/static/js/satellites.js index 4f9bdae..46df39e 100644 --- a/db/static/js/satellites.js +++ b/db/static/js/satellites.js @@ -33,6 +33,14 @@ $(document).ready(function() { pageLength: 50 } ); + // Create Satellite + $('.create-satellite-link').each(function () { + $(this).modalForm({ + formURL: $(this).data('form-url'), + modalID: '#create-satellite-modal' + }); + }); + // Update Satellite $('.update-satellite-link').each(function () { $(this).modalForm({ diff --git a/db/templates/base/modals/satellite_create.html b/db/templates/base/modals/satellite_create.html new file mode 100644 index 0000000..f16bc85 --- /dev/null +++ b/db/templates/base/modals/satellite_create.html @@ -0,0 +1,44 @@ +{% load widget_tweaks %} + +{% if request.user.is_authenticated %} +
+ {% csrf_token %} + + + + +
+{% else %} + + +{% endif %} diff --git a/db/templates/base/modals/transmitter_create.html b/db/templates/base/modals/transmitter_create.html index ea5d3fd..77c2cb7 100644 --- a/db/templates/base/modals/transmitter_create.html +++ b/db/templates/base/modals/transmitter_create.html @@ -11,7 +11,6 @@ {% endblock %} diff --git a/db/templates/base/transmitters.html b/db/templates/base/transmitters.html index d777482..17f72df 100644 --- a/db/templates/base/transmitters.html +++ b/db/templates/base/transmitters.html @@ -108,6 +108,8 @@ UHF Amateur + + {% endblock %} {% block javascript %} diff --git a/db/templates/includes/cards/transmitter_card.html b/db/templates/includes/cards/transmitter_card.html index 256d0f3..a87c565 100644 --- a/db/templates/includes/cards/transmitter_card.html +++ b/db/templates/includes/cards/transmitter_card.html @@ -84,7 +84,7 @@ {% if transmitter.downlink_drift %}
Downlink Drifted
- {{ transmitter.downlink_drift }}
+ {{ transmitter.downlink_drift }} {% endif %} {% if transmitter.baud %}
Baud
@@ -97,22 +97,22 @@ {% if transmitter.downlink_high %}
Downlink High
- {{ transmitter.downlink_high }}
+ {{ transmitter.downlink_high }} {% endif %} {% if transmitter.uplink_low %}
Uplink Frequency
- {{ transmitter.uplink_low }}
+ {{ transmitter.uplink_low }} {% endif %} {% if transmitter.uplink_drift %}
Uplink Drift
- {{ transmitter.uplink_drift }}
+ {{ transmitter.uplink_drift }} {% endif %} {% if transmitter.uplink_high %}
Uplink High
- {{ transmitter.uplink_high }}
+ {{ transmitter.uplink_high }} {% endif %} {% if transmitter.invert %}
Inverted
@@ -142,23 +142,23 @@ {% endif %} -