views.py 2.35 KB
from django.shortcuts import render,redirect,get_object_or_404,reverse
from django.contrib.auth import authenticate
from django.contrib.auth.decorators import login_required
from .forms import ScientificProgramForm
from src.core.pyros_django.dashboard.decorator import level_required
from common.models import ScientificProgram
from django.http import HttpResponseRedirect


@level_required(6)
@login_required
def create_sp(request):
    form = ScientificProgramForm(request.POST)
    if request.POST:
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse("proposal"))
    form = ScientificProgramForm()
    return render(request,"scientific_program/create_scientific_program.html",{"form": form})

@level_required(6)
@login_required
def edit_sp(request,id):
    edit = get_object_or_404(ScientificProgram, pk=id)
    form = ScientificProgramForm(request.POST or None, instance=edit)
    if form.is_valid():
        form.save()
        return redirect('detail_scientific_program', id=id)
    return render(request, 'scientific_program/scientific_program_detail_edit.html', {'id' : id, 'form': form})


@login_required
def detail_sp(request,id):
    scientific_program = get_object_or_404(ScientificProgram, pk=id)
    return render(request, 'scientific_program/scientific_program_detail.html', {'scientific_program': scientific_program})

@level_required(6)
@login_required()
def delete_sp(request,id):
    scientific_program = get_object_or_404(ScientificProgram, pk=int(id))
    scientific_program.delete()
    return HttpResponseRedirect(reverse('proposal'))


@login_required
@level_required(2)
def scientific_program_list(request):
    if (len(ScientificProgram.objects.all()) > 0):                              # checking if the observatory table is empty
        proposal_info = ScientificProgram.objects.order_by("-id")[:100]         # Sorting Weather table
        nb_info_proposal = len(proposal_info)                                   # Get the number of data available

    else:                                                                       # if empty set everything to 0 / None (variables are checked in src/templates/scheduler/_current_weather.html)
        proposal_info = None
        nb_info_proposal = 0

    return render(request, 'dashboard/proposal.html', {'proposal_info' : proposal_info, 'nb_info_proposal' : nb_info_proposal})