|
| 1 | +import logging |
| 2 | + |
| 3 | +from django.contrib import messages |
| 4 | +from django.http import HttpResponseRedirect |
| 5 | +from django.shortcuts import get_object_or_404, render |
| 6 | +from django.urls import reverse |
| 7 | +from django.utils.translation import gettext as _ |
| 8 | + |
| 9 | +from dojo.authorization.authorization_decorators import user_is_configuration_authorized |
| 10 | +from dojo.forms import CICDInfrastructureForm |
| 11 | +from dojo.models import CICDInfrastructure |
| 12 | +from dojo.utils import add_breadcrumb |
| 13 | + |
| 14 | +logger = logging.getLogger(__name__) |
| 15 | + |
| 16 | + |
| 17 | +@user_is_configuration_authorized("dojo.view_cicdinfrastructure") |
| 18 | +def cicd_infrastructure(request): |
| 19 | + confs = CICDInfrastructure.objects.all().order_by("name") |
| 20 | + add_breadcrumb(title=_("CI/CD Infrastructure List"), top_level=not len(request.GET), request=request) |
| 21 | + return render(request, "dojo/cicd_infrastructure.html", {"confs": confs}) |
| 22 | + |
| 23 | + |
| 24 | +@user_is_configuration_authorized("dojo.add_cicdinfrastructure") |
| 25 | +def new_cicd_infrastructure(request): |
| 26 | + if request.method == "POST": |
| 27 | + form = CICDInfrastructureForm(request.POST) |
| 28 | + if form.is_valid(): |
| 29 | + form.save() |
| 30 | + messages.add_message(request, messages.SUCCESS, |
| 31 | + _("CI/CD Infrastructure successfully created."), |
| 32 | + extra_tags="alert-success") |
| 33 | + return HttpResponseRedirect(reverse("cicd_infrastructure")) |
| 34 | + else: |
| 35 | + form = CICDInfrastructureForm() |
| 36 | + add_breadcrumb(title=_("New CI/CD Infrastructure"), top_level=False, request=request) |
| 37 | + return render(request, "dojo/new_cicd_infrastructure.html", {"form": form}) |
| 38 | + |
| 39 | + |
| 40 | +@user_is_configuration_authorized("dojo.change_cicdinfrastructure") |
| 41 | +def edit_cicd_infrastructure(request, ciid): |
| 42 | + conf = get_object_or_404(CICDInfrastructure, pk=ciid) |
| 43 | + if request.method == "POST": |
| 44 | + form = CICDInfrastructureForm(request.POST, instance=conf) |
| 45 | + if form.is_valid(): |
| 46 | + form.save() |
| 47 | + messages.add_message(request, messages.SUCCESS, |
| 48 | + _("CI/CD Infrastructure successfully updated."), |
| 49 | + extra_tags="alert-success") |
| 50 | + return HttpResponseRedirect(reverse("cicd_infrastructure")) |
| 51 | + else: |
| 52 | + form = CICDInfrastructureForm(instance=conf) |
| 53 | + add_breadcrumb(title=_("Edit CI/CD Infrastructure"), top_level=False, request=request) |
| 54 | + return render(request, "dojo/edit_cicd_infrastructure.html", {"form": form, "conf": conf}) |
| 55 | + |
| 56 | + |
| 57 | +@user_is_configuration_authorized("dojo.delete_cicdinfrastructure") |
| 58 | +def delete_cicd_infrastructure(request, ciid): |
| 59 | + conf = get_object_or_404(CICDInfrastructure, pk=ciid) |
| 60 | + if request.method == "POST": |
| 61 | + conf.delete() |
| 62 | + messages.add_message(request, messages.SUCCESS, |
| 63 | + _("CI/CD Infrastructure successfully deleted."), |
| 64 | + extra_tags="alert-success") |
| 65 | + return HttpResponseRedirect(reverse("cicd_infrastructure")) |
| 66 | + add_breadcrumb(title=_("Delete CI/CD Infrastructure"), top_level=False, request=request) |
| 67 | + return render(request, "dojo/delete_cicd_infrastructure.html", {"conf": conf}) |
0 commit comments