Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vis ikke fejl ved sletning af aktiviteter med inviterede #1120

Merged
merged 5 commits into from
Oct 27, 2024
Merged
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
18 changes: 17 additions & 1 deletion members/admin/activity_admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.contrib import admin
from django.contrib import admin, messages
from django.conf import settings
from django.core.exceptions import ValidationError
from django.urls import reverse
from django.utils.safestring import mark_safe
from django.utils.html import escape, format_html
Expand Down Expand Up @@ -250,6 +251,21 @@ def formfield_for_foreignkey(self, db_field, request, **kwargs):
kwargs["queryset"] = Address.get_user_addresses(request.user)
return super().formfield_for_foreignkey(db_field, request, **kwargs)

def delete_queryset(self, request, queryset):
for activity in queryset:
print(activity)
self.delete_model(request, activity)

def delete_model(self, request, activity):
try:
activity.delete()
messages.success(request, f'Aktivitet "{activity.name}" slettet.')
except ValidationError as e:
messages.error(request, e.message)

except Exception as e:
messages.error(request, f"Fejl: {str(e)}")

fieldsets = [
(
"Afdeling",
Expand Down
23 changes: 23 additions & 0 deletions members/migrations/0056_alter_activityinvite_activity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 4.2.16 on 2024-10-27 09:18

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


class Migration(migrations.Migration):

dependencies = [
("members", "0055_rename_union_email_union_email"),
]

operations = [
migrations.AlterField(
model_name="activityinvite",
name="activity",
field=models.ForeignKey(
on_delete=django.db.models.deletion.DO_NOTHING,
to="members.activity",
verbose_name="Aktivitet",
),
),
]
10 changes: 10 additions & 0 deletions members/models/activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ def seats_left(self):
def participants(self):
return self.activityparticipant_set.count()

def invitations(self):
return self.activityinvite_set.count()

participants.short_description = "Deltagere"

def get_min_amount(self, activitytype):
Expand Down Expand Up @@ -166,3 +169,10 @@ def clean(self):

if errors:
raise ValidationError(errors)

def delete(self, *args, **kwargs):
if self.invitations() > 0 or self.participants() > 0:
raise ValidationError(
f'Aktivitet "{self.name}" kan ikke slettes, da der er tilmeldte eller inviterede personer. Muligvis vil systemet skrive at aktiviteten er slettet, men det er den ikke.'
)
super().delete(*args, **kwargs)
2 changes: 1 addition & 1 deletion members/models/activityinvite.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Meta:
unique_together = ("activity", "person")

activity = models.ForeignKey(
"Activity", on_delete=models.CASCADE, verbose_name="Aktivitet"
"Activity", on_delete=models.DO_NOTHING, verbose_name="Aktivitet"
)
person = models.ForeignKey("Person", on_delete=models.CASCADE)
invite_dtm = models.DateField("Inviteret", default=timezone.now)
Expand Down
Loading