Skip to content

Commit

Permalink
feat: list issues and filter by referenced CVEs in REST API
Browse files Browse the repository at this point in the history
  • Loading branch information
fricklerhandwerk authored and RaitoBezarius committed Dec 12, 2023
1 parent 4f4baad commit 6d23bcc
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 2 deletions.
1 change: 1 addition & 0 deletions nix/overlay.nix
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ in
dj-database-url
django-allauth
django-debug-toolbar
django-filter
django-types
django_4
djangorestframework
Expand Down
11 changes: 11 additions & 0 deletions src/website/shared/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.urls import include, path
from rest_framework import routers

from shared.views import NixpkgsIssueViewSet

v1_router = routers.DefaultRouter()
v1_router.register(r"issues", NixpkgsIssueViewSet)

urlpatterns = [
path("v1/", include(v1_router.urls)),
]
36 changes: 35 additions & 1 deletion src/website/shared/views.py
Original file line number Diff line number Diff line change
@@ -1 +1,35 @@
# Create your views here.
from django_filters import rest_framework as filters
from rest_framework import serializers, viewsets
from rest_framework.permissions import AllowAny

from shared.models import NixpkgsIssue


class StringInFilter(filters.BaseInFilter, filters.CharFilter):
pass


class NixpkgsIssueViewSet(viewsets.ReadOnlyModelViewSet):
class Filter(filters.FilterSet):
cve = StringInFilter(
label="Filter by CVEs referenced",
field_name="cve__cve_id",
lookup_expr="in",
)

class Meta:
model = NixpkgsIssue
fields = ["cve"]

class Serializer(serializers.ModelSerializer):
status = serializers.CharField(source="get_status_display")

class Meta:
model = NixpkgsIssue
fields = ["code", "status"]

filterset_class = Filter

permission_classes = [AllowAny]
queryset = NixpkgsIssue.objects.all()
serializer_class = Serializer
6 changes: 5 additions & 1 deletion src/website/tracker/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def get_secret(name: str, encoding: str = "utf-8") -> str:
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"django_filters",
"debug_toolbar",
# AllAuth config
"allauth",
Expand Down Expand Up @@ -123,7 +124,6 @@ def get_secret(name: str, encoding: str = "utf-8") -> str:
]
]


AUTHENTICATION_BACKENDS = [
# Needed to login by username in Django admin, regardless of `allauth`
"django.contrib.auth.backends.ModelBackend",
Expand All @@ -146,6 +146,10 @@ def get_secret(name: str, encoding: str = "utf-8") -> str:
}
}

REST_FRAMEWORK = {
"DEFAULT_FILTER_BACKENDS": ["django_filters.rest_framework.DjangoFilterBackend"]
}

SITE_ID = 1

ACCOUNT_EMAIL_VERIFICATION = "none"
Expand Down
1 change: 1 addition & 0 deletions src/website/tracker/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

urlpatterns = [
path("", include("webview.urls")),
path("api/", include("shared.urls")),
path("admin/", admin.site.urls),
path("accounts/", include("allauth.urls")),
path("debug/", include("debug_toolbar.urls")),
Expand Down

0 comments on commit 6d23bcc

Please sign in to comment.