Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified backend/data/nest.dump
Binary file not shown.
12 changes: 6 additions & 6 deletions backend/make/apps/owasp.mk
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ owasp-create-project-metadata-file:
@echo "Generating metadata"
@CMD="python manage.py owasp_create_project_metadata_file $(entity_key)" $(MAKE) backend-exec-command

SNAPSHOT_FREQUENCY ?= weekly

owasp-create-snapshot:
@echo "Creating OWASP community snapshot"
@CMD="python manage.py owasp_create_snapshot --frequency $(SNAPSHOT_FREQUENCY)" $(MAKE) exec-backend-command

owasp-enrich-chapters:
@echo "Enriching OWASP chapters"
@CMD="python manage.py owasp_enrich_chapters" $(MAKE) backend-exec-command
Expand Down Expand Up @@ -58,12 +64,6 @@ owasp-generate-community-snapshot-video:
nest-snapshot-video \
python manage.py owasp_generate_community_snapshot_video $(snapshot_key) /home/owasp/generated_videos

SNAPSHOT_FREQUENCY ?= weekly

owasp-create-snapshot:
@echo "Creating OWASP community snapshot"
@CMD="python manage.py owasp_create_snapshot --frequency $(SNAPSHOT_FREQUENCY)" $(MAKE) exec-backend-command

owasp-process-snapshots:
@echo "Processing OWASP snapshots"
@CMD="python manage.py owasp_process_snapshots" $(MAKE) backend-exec-command
Expand Down
103 changes: 96 additions & 7 deletions backend/src/apps/owasp/admin/snapshot_subscription.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,109 @@
"""Admin registration for SnapshotSubscription model."""

from django import forms
from django.contrib import admin
from django.core.exceptions import ValidationError

from apps.owasp.models.snapshot_subscription import SnapshotSubscription


class SnapshotSubscriptionAdminForm(forms.ModelForm):
"""Custom form for SnapshotSubscription admin to validate duplicates with M2M."""

class Meta:
"""Meta options."""

model = SnapshotSubscription
fields = (
"user",
"name",
"frequency",
"is_active",
"include_chapters",
"include_events",
"include_issues",
"include_posts",
"include_projects",
"include_pull_requests",
"include_releases",
"include_users",
"subscribed_projects",
"subscribed_chapters",
"subscribed_committees",
)

def clean(self):
"""Validate form data, including M2M fields, to check for duplicates."""
if self.instance:
self.instance._is_admin_form = True # noqa: SLF001
cleaned_data = super().clean()

user = cleaned_data.get("user")
if not user:
return cleaned_data

duplicate_found = SnapshotSubscription.check_duplicate_setup(
user=user,
frequency=cleaned_data.get("frequency"),
include_chapters=cleaned_data.get("include_chapters"),
include_events=cleaned_data.get("include_events"),
include_issues=cleaned_data.get("include_issues"),
include_posts=cleaned_data.get("include_posts"),
include_projects=cleaned_data.get("include_projects"),
include_pull_requests=cleaned_data.get("include_pull_requests"),
include_releases=cleaned_data.get("include_releases"),
include_users=cleaned_data.get("include_users"),
entity_ids={
"projects": [p.pk for p in (cleaned_data.get("subscribed_projects") or [])],
"chapters": [c.pk for c in (cleaned_data.get("subscribed_chapters") or [])],
"committees": [c.pk for c in (cleaned_data.get("subscribed_committees") or [])],
},
exclude_pk=self.instance.pk if self.instance else None,
)

toggles = [
cleaned_data.get("include_chapters"),
cleaned_data.get("include_events"),
cleaned_data.get("include_issues"),
cleaned_data.get("include_posts"),
cleaned_data.get("include_projects"),
cleaned_data.get("include_pull_requests"),
cleaned_data.get("include_releases"),
cleaned_data.get("include_users"),
]
has_entities = bool(
cleaned_data.get("subscribed_projects")
or cleaned_data.get("subscribed_chapters")
or cleaned_data.get("subscribed_committees")
)

if not any(toggles) and not has_entities:
msg = "Your subscription cannot be empty. Please choose something to follow."
raise ValidationError(msg)

if duplicate_found:
msg = "A subscription with the same configuration already exists."
raise ValidationError(msg)

return cleaned_data


class SnapshotSubscriptionAdmin(admin.ModelAdmin):
"""Admin for SnapshotSubscription model."""

list_display = ("user", "frequency", "is_active", "created_at", "updated_at")
form = SnapshotSubscriptionAdminForm

list_display = ("user", "name", "frequency", "is_active", "created_at", "updated_at")
list_filter = ("frequency", "is_active", "created_at")
search_fields = ("user__email", "user__username")
search_fields = ("user__email", "user__username", "name")
raw_id_fields = ("user",)
readonly_fields = ("unsubscribe_token", "created_at", "updated_at")
autocomplete_fields = ("subscribed_projects", "subscribed_chapters")
autocomplete_fields = ("subscribed_projects", "subscribed_chapters", "subscribed_committees")

fieldsets = (
(None, {"fields": ("user", "frequency", "is_active")}),
(None, {"fields": ("user", "name", "frequency", "is_active")}),
(
"Content Preferences",
"Content Toggles",
{
"fields": (
"include_chapters",
Expand All @@ -33,9 +118,13 @@ class SnapshotSubscriptionAdmin(admin.ModelAdmin):
},
),
(
"Subscription Filters",
"Subscribed Entities",
{
"fields": ("subscribed_projects", "subscribed_chapters"),
"fields": (
"subscribed_projects",
"subscribed_chapters",
"subscribed_committees",
),
},
),
(
Expand Down
10 changes: 10 additions & 0 deletions backend/src/apps/owasp/api/internal/mutations/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""OWASP app mutations."""

import strawberry

from .snapshot_subscription import SnapshotSubscriptionMutations


@strawberry.type
class OwaspMutations(SnapshotSubscriptionMutations):
"""OWASP mutations."""
Loading
Loading