from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse

from .forms import *
from .models import *


@login_required
def index(request):
    return render(request, 'satsound/index.html')


@login_required
def satellite(request, norad_id):
    sat = {'pk': norad_id, 'name': 'not found'}
    try:
        sat = Satellite.objects.get(pk=norad_id)
    except Satellite.DoesNotExist:
        pass

    if request.method == 'POST':
        form = SatelliteAudioForm(request.POST, request.FILES)
        if form.is_valid():
            sa = SatelliteAudio()
            sa.satellite = sat
            sa.user = request.user
            sa.attribution = form.cleaned_data['attribution']
            sa.audio = request.FILES['audio']
            sa.type = form.cleaned_data['type']
            sa.save()

            return HttpResponseRedirect(reverse('index'))

    else:
        form = SatelliteAudioForm()
    return render(request, 'satsound/satellite.html', {'sat': sat, 'form': form})

from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse

from .forms import *
from .models import *


@login_required
def index(request):
    return render(request, 'satsound/index.html')


@login_required
def satellite(request, norad_id):
    sat = {'pk': norad_id, 'name': 'TBD'}
    newsat = False
    try:
        sat = Satellite.objects.get(pk=norad_id)
    except Satellite.DoesNotExist:
        newsat = True
        st = SpaceTrackClient(identity=settings.SPACETRACK_IDENTITY, password=settings.SPACETRACK_PASSWORD)
        # https://www.space-track.org/basicspacedata/query/class/satcat/NORAD_CAT_ID/3/orderby/INTLDES asc/metadata/false
        params = {
            'norad_cat_id': norad_id,
            'metadata': False
        }
        response = st.satcat(**params)

        if len(response) == 1:
            sat = Satellite(
                norad_id=norad_id,
                name=response[0].get('OBJECT_NAME', '')
            )

    if request.method == 'POST':
        form = SatelliteAudioForm(request.POST, request.FILES)
        if form.is_valid():
            sa = SatelliteAudio()
            if newsat:
                sat.save()
            sa.satellite = sat
            sa.user = request.user
            sa.attribution = form.cleaned_data['attribution']
            sa.audio = request.FILES['audio']
            sa.type = form.cleaned_data['type']
            sa.save()

            return HttpResponseRedirect(reverse('index'))

    else:
        form = SatelliteAudioForm()
    return render(request, 'satsound/satellite.html', {'sat': sat, 'form': form})

Create new satellite from spacetrack if satellite audio upload is for a nonexistent satellite
Create new satellite from spacetrack if satellite audio upload is for a nonexistent satellite

  from django.contrib.auth.decorators import login_required
  from django.http import HttpResponseRedirect
  from django.shortcuts import render
  from django.urls import reverse
  
  from .forms import *
  from .models import *
  
  
  @login_required
  def index(request):
      return render(request, 'satsound/index.html')
  
  
  @login_required
  def satellite(request, norad_id):
-     sat = {'pk': norad_id, 'name': 'not found'}
+     sat = {'pk': norad_id, 'name': 'TBD'}
+     newsat = False
      try:
          sat = Satellite.objects.get(pk=norad_id)
      except Satellite.DoesNotExist:
+         newsat = True
+         st = SpaceTrackClient(identity=settings.SPACETRACK_IDENTITY, password=settings.SPACETRACK_PASSWORD)
+         # https://www.space-track.org/basicspacedata/query/class/satcat/NORAD_CAT_ID/3/orderby/INTLDES asc/metadata/false
+         params = {
+             'norad_cat_id': norad_id,
+             'metadata': False
-         pass
+         }
+         response = st.satcat(**params)
+ 
+         if len(response) == 1:
+             sat = Satellite(
+                 norad_id=norad_id,
+                 name=response[0].get('OBJECT_NAME', '')
+             )
  
      if request.method == 'POST':
          form = SatelliteAudioForm(request.POST, request.FILES)
          if form.is_valid():
              sa = SatelliteAudio()
+             if newsat:
+                 sat.save()
              sa.satellite = sat
              sa.user = request.user
              sa.attribution = form.cleaned_data['attribution']
              sa.audio = request.FILES['audio']
              sa.type = form.cleaned_data['type']
              sa.save()
  
              return HttpResponseRedirect(reverse('index'))
  
      else:
          form = SatelliteAudioForm()
      return render(request, 'satsound/satellite.html', {'sat': sat, 'form': form})
  
Create new satellite from spacetrack if satellite audio upload is for a nonexistent satellite
## Code Before:
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse

from .forms import *
from .models import *


@login_required
def index(request):
    return render(request, 'satsound/index.html')


@login_required
def satellite(request, norad_id):
    sat = {'pk': norad_id, 'name': 'not found'}
    try:
        sat = Satellite.objects.get(pk=norad_id)
    except Satellite.DoesNotExist:
        pass

    if request.method == 'POST':
        form = SatelliteAudioForm(request.POST, request.FILES)
        if form.is_valid():
            sa = SatelliteAudio()
            sa.satellite = sat
            sa.user = request.user
            sa.attribution = form.cleaned_data['attribution']
            sa.audio = request.FILES['audio']
            sa.type = form.cleaned_data['type']
            sa.save()

            return HttpResponseRedirect(reverse('index'))

    else:
        form = SatelliteAudioForm()
    return render(request, 'satsound/satellite.html', {'sat': sat, 'form': form})

## Instruction:
Create new satellite from spacetrack if satellite audio upload is for a nonexistent satellite
## Code After:
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse

from .forms import *
from .models import *


@login_required
def index(request):
    return render(request, 'satsound/index.html')


@login_required
def satellite(request, norad_id):
    sat = {'pk': norad_id, 'name': 'TBD'}
    newsat = False
    try:
        sat = Satellite.objects.get(pk=norad_id)
    except Satellite.DoesNotExist:
        newsat = True
        st = SpaceTrackClient(identity=settings.SPACETRACK_IDENTITY, password=settings.SPACETRACK_PASSWORD)
        # https://www.space-track.org/basicspacedata/query/class/satcat/NORAD_CAT_ID/3/orderby/INTLDES asc/metadata/false
        params = {
            'norad_cat_id': norad_id,
            'metadata': False
        }
        response = st.satcat(**params)

        if len(response) == 1:
            sat = Satellite(
                norad_id=norad_id,
                name=response[0].get('OBJECT_NAME', '')
            )

    if request.method == 'POST':
        form = SatelliteAudioForm(request.POST, request.FILES)
        if form.is_valid():
            sa = SatelliteAudio()
            if newsat:
                sat.save()
            sa.satellite = sat
            sa.user = request.user
            sa.attribution = form.cleaned_data['attribution']
            sa.audio = request.FILES['audio']
            sa.type = form.cleaned_data['type']
            sa.save()

            return HttpResponseRedirect(reverse('index'))

    else:
        form = SatelliteAudioForm()
    return render(request, 'satsound/satellite.html', {'sat': sat, 'form': form})
