Creating a SLA manager in Django

April 24, 2025

Adding a SLA application within a Django project

In this post, we'll walk through the steps required to create a Django submodule using django-admin startapp specifically for managing Service Level Agreements (SLAs). We'll cover the creation of models, views, URLs, and admin panels for efficient content management.

To note, we will be taking a simplistic approach to bootstrap the project. Enhancements for the data models and the creation of subsequent submodules will result in refactoring. But in the interim, we will maintain a flat architecture before identifying appropriate buckets for triaging submodules, and make decisions on architectural design as we move forward with meeting business needs.

Before we begin, let us define what an SLA actually is, particularly in the context of Cyber Security.

What is an SLA?

In the evolving landscape of cybersecurity, managing Common Vulnerabilities and Exposures (CVEs) and adhering to Vulnerability Service Level Agreements (SLAs) are crucial for maintaining the integrity and trustworthiness of your applications. Whether building a new Django application or managing an existing one, understanding and implementing best practices to handle these elements can make all the difference. Continuing our journey in Python development, let's explore how we can achieve this.

Understanding CVEs and Vulnerability SLAs

Before diving into the implementation, it’s essential to establish a clear understanding of CVEs and SLAs:

  • CVE: A Common Vulnerability and Exposure is a publicly disclosed cybersecurity vulnerability.
  • Vulnerability SLA: Service Level Agreements related to vulnerabilities define the timeframe within which a vulnerability should be resolved.

Why Managing CVEs and SLAs Matters

The importance of effectively managing CVEs and SLAs cannot be overstated. Not only do they help mitigate risks associated with cybersecurity threats, but they also ensure compliance and boost customer trust.

Software Time: Initial Setup

To begin, ensure you have Django installed. You can install it via pip with the following command:

		pip install django
	

Once installed, create a new Django project if you haven't already:

		django-admin startproject myproject
cd myproject
	

After creating your main Django project, proceed with creating an application for managing SLAs.

Creating the SLA Application

You can create a new Django app named sla using the django-admin tool:

		python manage.py startapp sla
	

This command will generate a basic structure for the sla application. Ensure you add 'sla' to the INSTALLED_APPS list in myproject/settings.py.

Developing Models

Start by defining the models needed to represent the SLAs. Open sla/models.py and add the following content:

		from django.db import models


class ServiceProvider(models.Model):
    name = models.CharField(max_length=100)
    email = models.EmailField()

    def __str__(self):
        return self.name


class SLA(models.Model):
    service_provider = models.ForeignKey(ServiceProvider, on_delete=models.CASCADE)
    name = models.CharField(max_length=200)
    description = models.TextField()
    start_date = models.DateField()
    end_date = models.DateField()
    level_of_service = models.CharField(max_length=100)

    def __str__(self):
        return self.name
	

Setting Up Views

Create basic views to list and display SLA details. We'll opt for function views for now, but it is often recommended to use class-based views, particularly when leveraging authentication and List/Detail/Create/Delete views. Open sla/views.py and write the following code:

		from django.shortcuts import render, get_object_or_404
from .models import SLA


def sla_list(request):
    slas = SLA.objects.all()
    return render(request, 'sla/sla_list.html', {'slas': slas})


def sla_detail(request, sla_id):
    sla = get_object_or_404(SLA, id=sla_id)
    return render(request, 'sla/sla_detail.html', {'sla': sla})
	

Configuring URLs

You need to route URLs to the SLA views. In sla/urls.py, write:

		from django.urls import path
from . import views


urlpatterns = [
    path('slas/', views.sla_list, name='sla_list'),
    path('slas//', views.sla_detail, name='sla_detail')
]
	

Ensure to include the sla URL config in your main project’s urls.py:

		from django.contrib import admin 
from django.urls import path, include


urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('sla.urls')),
]
	

Creating Templates

Create HTML templates for listing and displaying SLAs. In your sla app directory, create a directory named `templates/sla/` and add the following templates. Please note that you should follow the conventions of your chosen CSS or JavaScript Framework. Right now, we'll presume a Bulma CSS implementation for the frontend, but only build minimal/brutalist views before transitioning to stylings:

		<!-- sla/sla_list.html -->

<div class="container">
    <h1>Service Level Agreements</h1>
    {% for sla in slas %}
     - {{ sla.name }}
    {% endfor %}
</div>
	
		<!-- sla/sla_detail.html -->

<div class="container">
    <h1 class="title is-1>{{ sla.name }}</h1>
    <p class="content">
        {{ sla.description }}
    </p>
    <div class="block">
        Provider: {{ sla.service_provider.name }}
        Level of Service: {{ sla.level_of_service }}
        Start Date: {{ sla.start_date }}
        End Date: {{ sla.end_date }}
    </div>
</div>
	

Registering Models in Admin Panel

Finally, register the SLA and ServiceProvider models with the admin site for easy management. Edit sla/admin.py as shown below:

		from django.contrib import admin
from .models import SLA, ServiceProvider


admin.site.register(SLA)
admin.site.register(ServiceProvider)
	

Conclusion

Following the steps outlined, you've created a Django submodule tailored to manage SLAs. This module encompasses everything from model definitions to views, URL configuration, template management, and admin panel registration. This setup is easily expandable and can serve as a foundation for more complex SLA management needs. Happy coding!

Return to blog