Skip to content

Commit

Permalink
feat: implemented working django rest framework
Browse files Browse the repository at this point in the history
  • Loading branch information
c0rydoras committed Oct 23, 2023
1 parent 55bdb42 commit 5f97ca6
Show file tree
Hide file tree
Showing 36 changed files with 280 additions and 1,874 deletions.
2 changes: 1 addition & 1 deletion backend/projects/environ.py → backend/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
import environ

env = environ.Env()
env.read_env(".env")
env.read_env("../.env")
6 changes: 3 additions & 3 deletions backend/manage.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/usr/bin/env python
"""Djangos command-line utility for administrative tasks."""
"""Django's command-line utility for administrative tasks."""
import os
import sys


def main():
"""Run administrative tasks."""
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "outdated.settings")
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'outdated.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
Expand All @@ -18,5 +18,5 @@ def main():
execute_from_command_line(sys.argv)


if __name__ == "__main__":
if __name__ == '__main__':
main()
2 changes: 1 addition & 1 deletion backend/outdated/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@

from django.core.asgi import get_asgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "outdated.settings")
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'outdated.settings')

application = get_asgi_application()
51 changes: 51 additions & 0 deletions backend/outdated/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Generated by Django 3.2.16 on 2023-01-25 13:43

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Dependency',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100, unique=True)),
],
options={
'ordering': ['name'],
},
),
migrations.CreateModel(
name='DependencyVersion',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('version', models.CharField(max_length=100)),
('release_date', models.DateField()),
('end_of_life_date', models.DateField()),
('dependency', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='outdated.dependency')),
],
options={
'ordering': ['end_of_life_date'],
'unique_together': {('dependency', 'version')},
},
),
migrations.CreateModel(
name='Project',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100, unique=True)),
('repo', models.URLField(unique=True)),
('dependency_versions', models.ManyToManyField(blank=True, to='outdated.DependencyVersion')),
],
options={
'ordering': ['status'],
},
),
]
17 changes: 17 additions & 0 deletions backend/outdated/migrations/0002_alter_project_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 3.2.16 on 2023-01-25 14:02

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('outdated', '0001_initial'),
]

operations = [
migrations.AlterModelOptions(
name='project',
options={'ordering': ['name']},
),
]
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions backend/outdated/outdated/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.apps import AppConfig


class ProjectsConfig(AppConfig):
class OutdatedConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "projects"
name = "outdatedf"
File renamed without changes.
54 changes: 54 additions & 0 deletions backend/outdated/outdated/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from datetime import date, timedelta
from django.db import models

# Create your models here.


class Dependency(models.Model):
name = models.CharField(max_length=100, unique=True)

class Meta:
ordering = ["name"]

def __str__(self):
return self.name


class DependencyVersion(models.Model):

dependency = models.ForeignKey(Dependency, on_delete=models.CASCADE)
version = models.CharField(max_length=100)
release_date = models.DateField()
end_of_life_date = models.DateField()

class Meta:
ordering = ["end_of_life_date"]
unique_together = ("dependency", "version")

def __str__(self):
return self.dependency.name + " " + self.version

@property
def status(self):
if date.today() >= self.end_of_life_date:
return "OUTDATED"
elif date.today() + timedelta(days=30) >= self.end_of_life_date:
return "WARNING"
return "UP-TO-DATE"


class Project(models.Model):

name = models.CharField(max_length=100, unique=True)
repo = models.URLField(max_length=200, unique=True)
dependency_versions = models.ManyToManyField(DependencyVersion, blank=True)

class Meta:
ordering = ["name"]

def __str__(self):
return self.name

@property
def status(self) -> str:
return self.dependency_versions.first().status
24 changes: 24 additions & 0 deletions backend/outdated/outdated/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from outdated.outdated.models import Dependency, DependencyVersion, Project
from rest_framework_json_api import serializers


class DependencySerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Dependency
fields = "__all__"


class DependencyVersionSerializer(serializers.HyperlinkedModelSerializer):
status = serializers.ReadOnlyField()

class Meta:
model = DependencyVersion
fields = "__all__"


class ProjectSerializer(serializers.HyperlinkedModelSerializer):
status = serializers.ReadOnlyField()

class Meta:
model = Project
fields = "__all__"
3 changes: 3 additions & 0 deletions backend/outdated/outdated/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
22 changes: 22 additions & 0 deletions backend/outdated/outdated/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from outdated.outdated.models import Dependency, DependencyVersion, Project
from outdated.outdated.serializers import (
DependencySerializer,
DependencyVersionSerializer,
ProjectSerializer,
)
from rest_framework import viewsets


class ProjectViewSet(viewsets.ModelViewSet):
queryset = Project.objects.all()
serializer_class = ProjectSerializer


class DependencyVersionViewSet(viewsets.ModelViewSet):
queryset = DependencyVersion.objects.all()
serializer_class = DependencyVersionSerializer


class DependencyViewSet(viewsets.ModelViewSet):
queryset = Dependency.objects.all()
serializer_class = DependencySerializer
23 changes: 6 additions & 17 deletions backend/outdated/settings.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,30 @@
"""
Django settings for outdated project.
Generated by 'django-admin startproject' using Django 3.2.16.
For more information on this file, see
https://docs.djangoproject.com/en/3.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
"""

import os
from pathlib import Path

from projects.environ import env
from env import env

DEBUG = True

SECRET_KEY = env("DJANGO_SECRET_KEY")

BASE_DIR = Path(__file__).resolve().parent.parent

ALLOWED_HOSTS = ["127.0.0.1"]
ALLOWED_HOSTS = ["localhost", "127.0.0.1"]


# Application definition


INSTALLED_APPS = [
"projects.apps.ProjectsConfig",
"outdated",
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"templateutils",
"rest_framework",
"rest_framework_json_api",
]


Expand Down Expand Up @@ -106,7 +95,7 @@

LANGUAGE_CODE = "en-us"

TIME_ZONE = "UTC"
TIME_ZONE = "CET"

USE_I18N = True

Expand Down
12 changes: 8 additions & 4 deletions backend/outdated/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from django.contrib import admin
from django.urls import include, path
from outdated.outdated import views
from rest_framework import routers

urlpatterns = [
path("admin/", admin.site.urls),
path("", include("projects.urls")),
]
router = routers.DefaultRouter()
router.register(r"projects", views.ProjectViewSet)
router.register(r"dependencies", views.DependencyViewSet)
router.register(r"dependency-versions", views.DependencyVersionViewSet)

urlpatterns = [path("admin/", admin.site.urls), path("", include(router.urls))]
2 changes: 1 addition & 1 deletion backend/outdated/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "outdated.settings")
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'outdated.settings')

application = get_wsgi_application()
Loading

0 comments on commit 5f97ca6

Please sign in to comment.