views.py 1.57 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'))