Skip to content

Commit

Permalink
Merge branch 'master' into 857-bug-notifikationer-går-ikke-væk-når-ma…
Browse files Browse the repository at this point in the history
…n-tilmelder-barn-til-aktiviteten
  • Loading branch information
rasmusselsmark authored Oct 27, 2024
2 parents 3964b60 + a365cec commit 611e6a0
Show file tree
Hide file tree
Showing 13 changed files with 267 additions and 5 deletions.
3 changes: 3 additions & 0 deletions members/admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
EmailTemplate,
Equipment,
Family,
Municipality,
Payment,
Person,
Union,
Expand All @@ -25,6 +26,7 @@
from .department_admin import DepartmentAdmin
from .equipment_admin import EquipmentAdmin
from .family_admin import FamilyAdmin
from .municipality_admin import MunicipalityAdmin
from .payment_admin import PaymentAdmin
from .person_admin import PersonAdmin
from .union_admin import UnionAdmin
Expand All @@ -43,6 +45,7 @@
admin.site.register(EmailTemplate)
admin.site.register(Equipment, EquipmentAdmin)
admin.site.register(Family, FamilyAdmin)
admin.site.register(Municipality, MunicipalityAdmin)
admin.site.register(Payment, PaymentAdmin)
admin.site.register(Person, PersonAdmin)
admin.site.register(Union, UnionAdmin)
Expand Down
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
6 changes: 4 additions & 2 deletions members/admin/admin_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ def invite_many_to_activity_action(modelAdmin, request, queryset):
if request.user.is_superuser or request.user.has_perm(
"members.view_all_departments"
):
department_list_query = Department.objects.all().order_by("name")
department_list_query = Department.objects.filter(
closed_dtm__isnull=True
).order_by("name")
else:
department_list_query = Department.objects.filter(
adminuserinformation__user=request.user
adminuserinformation__user=request.user, closed_dtm__isnull=True
).order_by("name")
department_list = [("-", "-")]
for department in department_list_query:
Expand Down
5 changes: 5 additions & 0 deletions members/admin/municipality_admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.contrib import admin


class MunicipalityAdmin(admin.ModelAdmin):
list_display = ("municipality", "address", "zipcode", "city", "email")
45 changes: 45 additions & 0 deletions members/management/commands/import_municipalities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import csv
from django.core.management.base import BaseCommand
from members.models import Municipality

# run locally:
# docker compose run web ./manage.py import_municipalities members/management/commands/municipalities.csv


class Command(BaseCommand):
help = "Import municipalities data from a CSV file into the Municipality model"

def add_arguments(self, parser):
parser.add_argument(
"csv_file",
type=str,
help="Path to the CSV file containing municipalities data",
)

def handle(self, *args, **kwargs):
csv_file_path = kwargs["csv_file"]

try:
with open(csv_file_path, mode="r", encoding="utf-8") as file:
reader = csv.reader(file, delimiter=";")
for row in reader:
municipality, address, zipcode, city, email = row
Municipality.objects.create(
municipality=municipality,
address=address,
zipcode=zipcode,
city=city,
email=email,
)
self.stdout.write(f"Added municipality: {municipality}")

self.stdout.write(
self.style.SUCCESS("Successfully imported all municipalities")
)

except FileNotFoundError:
self.stdout.write(
self.style.ERROR(
f"File {csv_file_path} not found. Please check the file path."
)
)
98 changes: 98 additions & 0 deletions members/management/commands/municipalities.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
Albertslund Kommune;Nordmarks Allé;2620;Albertslund;[email protected]
Allerød Kommune;Bjarkesvej;3450;Allerød;[email protected]
Assens Kommune;Rådhus Allé 5;5610;Assens;[email protected]
Ballerup Kommune;Hold-an Vej 7;2750;Ballerup;[email protected]
Billund Kommune;Jorden Rundt 1;7200;Grindsted;[email protected]
Bornholms Regionskommune;Ullasvej 23;3700;Rønne;[email protected]
Brøndby Kommune;Park Allé 160;2605;Brøndby;[email protected]
Brønderslev Kommune;Ny Rådhusplads 1;9700;Brønderslev;[email protected]
Dragør Kommune;Kirkevej 7;2791;Dragør;[email protected]
Egedal Kommune;Dronning Dagmars Vej 200;3660;Stenløse;[email protected]
Esbjerg Kommune;Torvegade 74;6700;Esbjerg;[email protected]
Fanø Kommune;Skolevej 5-7;6720;Fanø;[email protected]
Favrskov Kommune;Skovvej 20;8382;Hinnerup;[email protected]
Faxe Kommune;Frederiksgade 9;4690;Haslev;[email protected]
Fredensborg Kommune;Egevangen 3 B;2980;Kokkedal;[email protected]
Fredericia Kommune;Gothersgade 20;7000;Fredericia;[email protected]
Frederiksberg Kommune;Smallegade 1;2000;Frederiksberg;[email protected]
Frederikshavn Kommune;Rådhus Allé 100;9900;Frederikshavn;[email protected]
Frederikssund Kommune;Torvet 2;3600;Frederikssund;[email protected]
Furesø Kommune;Rådhustorvet 2;3520;Farum;[email protected]
Faaborg-Midtfyn Kommune;Tinghøj Allé 2;5750;Ringe;[email protected]
Gentofte Kommune;Bernstorffsvej 161;2920;Charlottenlund;[email protected]
Gladsaxe Kommune;Rådhus Allé 7;2860;Søborg;[email protected]
Glostrup Kommune;Rådhusparken 2;2600;Glostrup;[email protected]
Greve Kommune;Rådhusholmen 10;2670;Greve;[email protected]
Gribskov Kommune;Rådhusvej 3;3200;Helsinge;[email protected]
Guldborgsund Kommune;Parkvej 37;4800;Nykøbing Falster;[email protected]
Haderslev Kommune;Christian X's Vej 39;6100;Haderslev;[email protected]
Halsnæs Kommune;Rådhuspladsen 1;3300;Frederiksværk;[email protected]
Hedensted Kommune;Niels Espes Vej 8;8722;Hedensted;[email protected]
Helsingør Kommune;Stengade 59;3000;Helsingør;[email protected]
Herlev Kommune;Herlev Bygade 90;2730;Herlev;[email protected]
Herning Kommune;Torvet;7400;Herning;[email protected]
Hillerød Kommune;Trollesmindealle 27;3400;Hillerød;[email protected]
Hjørring Kommune;Nørregade 2;9800;Hjørring;[email protected]
Holbæk Kommune;Kanalstræde 2;4300;Holbæk;[email protected]
Holstebro Kommune;Kirkestræde 11;7500;Holstebro;[email protected]
Horsens Kommune;Rådhustorvet 4;8700;Horsens;[email protected]
Hvidovre Kommune;Hvidovrevej 278;2650;Hvidovre;[email protected]
Høje-Taastrup Kommune;Rådhusstræde 1;2630;Taastrup;[email protected]
Hørsholm Kommune;Slotsmarken 13;2970;Hørsholm;[email protected]
Ikast-Brande Kommune;Rådhusstrædet 6;7430;Ikast;[email protected]
Ishøj Kommune;Ishøj Store Torv 20;2635;Ishøj;[email protected]
Jammerbugt Kommune;Toftevej 43;9440;Aabybro;[email protected]
Kalundborg Kommune;Klosterparkvej 7;4400;Kalundborg;[email protected]
Kerteminde Kommune;Hans Schacksvej 4;5300;Kerteminde;[email protected]
Kolding Kommune;Akseltorv 1;6000;Kolding;[email protected]
Københavns Kommune;Rådhuset;1599;København V;[email protected]
Køge Kommune;Torvet 1;4600;Køge;[email protected]
Langeland Kommune;Fredensvej 1;5900;Rudkøbing;[email protected]
Lejre Kommune;Møllebjergvej 4;4330;Hvalsø;[email protected]
Lemvig Kommune;Rådhusgade 2;7620;Lemvig;[email protected]
Lolland Kommune;Jernbanegade 7;4930;Maribo;[email protected]
Lyngby-Taarbæk Kommune;Lyngby Torv;2800;Kongens Lyngby;[email protected]
Læsø Kommune;Doktorvejen 2;9940;Læsø;[email protected]
Mariagerfjord Kommune;Nordre Kajgade 1;9500;Hobro;[email protected]
Middelfart Kommune;Nytorv 9;5500;Middelfart;[email protected]
Morsø Kommune;Jernbanevej 7;7900;Nykøbing Mors;[email protected]
Norddjurs Kommune;Torvet 3;8500;Grenaa;[email protected]
Nordfyns Kommune;Østergade 23;5400;Bogense;[email protected]
Nyborg Kommune;Torvet 1;5800;Nyborg;[email protected]
Næstved Kommune;Rådmandshaven 20;4700;Næstved;[email protected]
Odder Kommune;Rådhusgade 3;8300;Odder;[email protected]
Odense Kommune;Flakhaven 2;5000;Odense C;[email protected]
Odsherred Kommune;Nyvej 22;4573;Højby;[email protected]
Randers Kommune;Laksetorvet;8900;Randers C;[email protected]
Rebild Kommune;Hobrovej 110;9530;Støvring;[email protected]
Ringkøbing-Skjern Kommune;Ved Fjorden 6;6950;Ringkøbing;[email protected]
Ringsted Kommune;Sct. Bendtsgade 1;4100;Ringsted;[email protected]
Roskilde Kommune;Rådhusbuen 1;4000;Roskilde;[email protected]
Rudersdal Kommune;Øverødvej 2;2840;Holte;[email protected]
Rødovre Kommune;Rødovre Parkvej 150;2610;Rødovre;[email protected]
Samsø Kommune;Søtofte 10;8305;Samsø;[email protected]
Silkeborg Kommune;Søvej 1;8600;Silkeborg;[email protected]
Skanderborg Kommune;Skanderborg Fælled 1;8660;Skanderborg;[email protected]
Skive Kommune;Torvegade 10;7800;Skive;[email protected]
Slagelse Kommune;Rådhuspladsen 11;4200;Slagelse;[email protected]
Solrød Kommune;Solrød Center 1;2680;Solrød Strand;[email protected]
Sorø Kommune;Rådhusvej 8;4180;Sorø;[email protected]
Stevns Kommune;Rådhuspladsen 4;4660;Store Heddinge;[email protected]
Struer Kommune;Østergade 11-15;7600;Struer;[email protected]
Svendborg Kommune;Ramsherred 5;5700;Svendborg;[email protected]
Syddjurs Kommune;Lundbergsvej 2;8400;Ebbeltoft;[email protected]
Sønderborg Kommune;Rådhustorvet 10;6400;Sønderborg;[email protected]
Thisted Kommune;Asylgade 30;7700;Thisted;[email protected]
Tønder Kommune;Kongevej 57;6270;Tønder;[email protected]
Tårnby Kommune;Amager Landevej 76;2770;Kastrup;[email protected]
Vallensbæk Kommune;Vallensbæk Stationstorv 100;2665;Vallensbæk Strand;[email protected]
Varde Kommune;Bytoften 2;6800;Varde;[email protected]
Vejen Kommune;Rådhuspassagen 3;6600;Vejen;[email protected]
Vejle Kommune;Skolegade 1;7100;Vejle;[email protected]
Vesthimmerlands Kommune;Vestre Boulevard 7;9600;Aars;[email protected]
Viborg Kommune;Prinsens Alle 5;8800;Viborg;[email protected]
Vordingborg Kommune;Valdemarsgade 43;4760;Vordingborg;[email protected]
Ærø Kommune;Statene 2;5970;Ærøskøbing;[email protected]
Aabenraa Kommune;Skelbækvej 2;6200;Aabenraa;[email protected]
Aalborg Kommune;Boulevarden 13;9000;Aalborg;[email protected]
Aarhus Kommune;Rådhuspladsen 2;8000;Aarhus C;[email protected]
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",
),
),
]
40 changes: 40 additions & 0 deletions members/migrations/0057_municipality.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Generated by Django 4.2.16 on 2024-10-27 13:07

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("members", "0056_alter_activityinvite_activity"),
]

operations = [
migrations.CreateModel(
name="Municipality",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"municipality",
models.CharField(max_length=255, verbose_name="Kommune"),
),
("address", models.CharField(max_length=255, verbose_name="Adresse")),
("zipcode", models.CharField(max_length=10, verbose_name="Postnr")),
("city", models.CharField(max_length=100, verbose_name="By")),
("email", models.EmailField(max_length=254, verbose_name="E-mail")),
],
options={
"verbose_name": "Kommune",
"verbose_name_plural": "Kommuner",
"ordering": ["municipality"],
},
),
]
3 changes: 3 additions & 0 deletions members/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import members.models.equipment
import members.models.equipmentloan
import members.models.family
import members.models.municipality
import members.models.notification
import members.models.payment
import members.models.person
Expand All @@ -41,6 +42,7 @@
from .equipment import Equipment
from .equipmentloan import EquipmentLoan
from .family import Family
from .municipality import Municipality
from .notification import Notification
from .payment import Payment
from .person import Person
Expand Down Expand Up @@ -69,6 +71,7 @@
EquipmentLoan,
Family,
gatherDayliStatistics,
Municipality,
Notification,
Payment,
Person,
Expand Down
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
17 changes: 17 additions & 0 deletions members/models/municipality.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from django.db import models


class Municipality(models.Model):
municipality = models.CharField(max_length=255, verbose_name="Kommune")
address = models.CharField(max_length=255, verbose_name="Adresse")
zipcode = models.CharField(max_length=10, verbose_name="Postnr")
city = models.CharField(max_length=100, verbose_name="By")
email = models.EmailField(verbose_name="E-mail")

def __str__(self):
return f"{self.municipality}, {self.zipcode} {self.city}"

class Meta:
verbose_name = "Kommune"
verbose_name_plural = "Kommuner"
ordering = ["municipality"]
2 changes: 1 addition & 1 deletion members/tests/test_admin_admin_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def setUp(self):
self.admin = AdminActions(Activity, admin.site)

self.union = UnionFactory()
self.department = DepartmentFactory(union=self.union)
self.department = DepartmentFactory(union=self.union, closed_dtm=None)

self.user = User.objects.create_user(
username="user",
Expand Down

0 comments on commit 611e6a0

Please sign in to comment.