Skip to content

Commit

Permalink
kiezradar: add radius based model, serializer, api, view, template
Browse files Browse the repository at this point in the history
  • Loading branch information
m4ra authored and goapunk committed Jan 21, 2025
1 parent f56feba commit e110236
Show file tree
Hide file tree
Showing 28 changed files with 918 additions and 408 deletions.
6 changes: 6 additions & 0 deletions changelog/8651.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
### Added

- KiezRadar model, serializer, view and api for radius based filter in kiezradar app

### Changed
- kiezradar one-to-one field in SearchProfile model
9 changes: 8 additions & 1 deletion meinberlin/apps/kiezradar/admin.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
from django.contrib import admin

from .models import KiezRadar
from .models import KiezradarQuery
from .models import ProjectType
from .models import SearchProfile


class SearchProfileAdmin(admin.ModelAdmin):
list_display = ("id", "name", "user", "number")
list_display = ("id", "name", "creator", "number")
readonly_fields = ("number",)
list_filter = (
"name",
"status",
)


class KiezRadarAdmin(admin.ModelAdmin):
list_display = ("id", "name")
list_filter = ("name",)


class KiezradarQueryAdmin(admin.ModelAdmin):
list_display = ("id", "text")
list_filter = ("text",)
Expand All @@ -25,5 +31,6 @@ class ProjectTypeAdmin(admin.ModelAdmin):


admin.site.register(SearchProfile, SearchProfileAdmin)
admin.site.register(KiezRadar, KiezRadarAdmin)
admin.site.register(KiezradarQuery, KiezradarQueryAdmin)
admin.site.register(ProjectType, ProjectTypeAdmin)
41 changes: 38 additions & 3 deletions meinberlin/apps/kiezradar/api.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,56 @@
from rest_framework import viewsets
from rest_framework.permissions import IsAuthenticated

from adhocracy4.api.permissions import ViewSetRulesPermission

from .models import KiezRadar
from .models import SearchProfile
from .serializers import KiezRadarSerializer
from .serializers import SearchProfileSerializer


class KiezRadarViewSet(viewsets.ModelViewSet):
"""
ViewSet for managing KiezRadar objects.
"""

queryset = KiezRadar.objects.all()
serializer_class = KiezRadarSerializer
permission_classes = [ViewSetRulesPermission]

def perform_create(self, serializer):
"""
Override to save the user from the request.
"""
serializer.save(creator=self.request.user)

def get_permission_object(self):
return None

def get_queryset(self):
if self.request.user.is_authenticated:
return KiezRadar.objects.filter(creator=self.request.user)
return KiezRadar.objects.none()


class SearchProfileViewSet(viewsets.ModelViewSet):
"""
ViewSet for managing SearchProfile objects.
"""

queryset = SearchProfile.objects.select_related("query").all()
serializer_class = SearchProfileSerializer
permission_classes = [IsAuthenticated]
permission_classes = [ViewSetRulesPermission]

def perform_create(self, serializer):
"""
Override to save the user from the request.
"""
serializer.save(user=self.request.user)
serializer.save(creator=self.request.user)

def get_permission_object(self):
return None

def get_queryset(self):
if self.request.user.is_authenticated:
return SearchProfile.objects.filter(creator=self.request.user)
return SearchProfile.objects.none()
145 changes: 130 additions & 15 deletions meinberlin/apps/kiezradar/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# Generated by Django 4.2.11 on 2024-12-02 16:53
# Generated by Django 4.2.11 on 2025-01-21 09:27

import adhocracy4.maps.fields
from django.conf import settings
import django.core.validators
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone


class Migration(migrations.Migration):
Expand All @@ -11,12 +14,69 @@ class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.A4_ORGANISATIONS_MODEL),
("a4administrative_districts", "0001_initial"),
("a4projects", "0047_alter_project_image_alter_project_tile_image"),
("a4administrative_districts", "0001_initial"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name="KiezRadar",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"created",
models.DateTimeField(
default=django.utils.timezone.now,
editable=False,
verbose_name="Created",
),
),
(
"modified",
models.DateTimeField(
blank=True, editable=False, null=True, verbose_name="Modified"
),
),
("name", models.CharField(max_length=125)),
(
"point",
adhocracy4.maps.fields.PointField(
blank=True,
help_text="Click inside the marked area or type in an address to set the marker. A set marker can be dragged when pressed.",
verbose_name="Where can your address be located on a map?",
),
),
(
"radius",
models.FloatField(
help_text="How long should be the radius area?",
validators=[
django.core.validators.MinValueValidator(500.0),
django.core.validators.MaxValueValidator(3000.0),
],
),
),
(
"creator",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL,
),
),
],
options={
"abstract": False,
},
),
migrations.CreateModel(
name="KiezradarQuery",
fields=[
Expand All @@ -35,6 +95,27 @@ class Migration(migrations.Migration):
"verbose_name_plural": "queries",
},
),
migrations.CreateModel(
name="ProjectStatus",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"status",
models.SmallIntegerField(
choices=[(0, "running"), (1, "done"), (2, "future")],
verbose_name="Status",
),
),
],
),
migrations.CreateModel(
name="ProjectType",
fields=[
Expand Down Expand Up @@ -73,15 +154,30 @@ class Migration(migrations.Migration):
verbose_name="ID",
),
),
("name", models.CharField(max_length=255)),
(
"created",
models.DateTimeField(
default=django.utils.timezone.now,
editable=False,
verbose_name="Created",
),
),
(
"modified",
models.DateTimeField(
blank=True, editable=False, null=True, verbose_name="Modified"
),
),
("name", models.CharField(max_length=255, null=True)),
("number", models.PositiveSmallIntegerField()),
("description", models.TextField(blank=True, null=True)),
("disabled", models.BooleanField(default=False)),
("notification", models.BooleanField(default=False)),
(
"status",
models.SmallIntegerField(
choices=[(0, "running"), (1, "done")],
default=0,
verbose_name="Status",
"creator",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL,
),
),
(
Expand All @@ -92,6 +188,16 @@ class Migration(migrations.Migration):
to="a4administrative_districts.administrativedistrict",
),
),
(
"kiezradar",
models.OneToOneField(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="search_profile",
to="meinberlin_kiezradar.kiezradar",
),
),
(
"organisations",
models.ManyToManyField(
Expand Down Expand Up @@ -119,24 +225,33 @@ class Migration(migrations.Migration):
),
),
(
"topics",
"status",
models.ManyToManyField(
blank=True,
related_name="search_profiles",
to="a4projects.topic",
to="meinberlin_kiezradar.projectstatus",
),
),
(
"user",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
"topics",
models.ManyToManyField(
blank=True,
related_name="search_profiles",
to=settings.AUTH_USER_MODEL,
to="a4projects.topic",
),
),
],
options={
"ordering": ["name"],
"ordering": ["number"],
"indexes": [
models.Index(models.F("number"), name="searchprofile_number_idx")
],
},
),
migrations.AddConstraint(
model_name="searchprofile",
constraint=models.UniqueConstraint(
models.F("creator"), models.F("number"), name="unique-search-profile"
),
),
]
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,23 @@ def populate_project_type(apps, schema_editor):
)


class Migration(migrations.Migration):
def populate_project_status(apps, schema_editor):
ProjectStatus = apps.get_model("meinberlin_kiezradar", "ProjectStatus")
ProjectStatus.objects.bulk_create(
[
ProjectStatus(status=0),
ProjectStatus(status=1),
ProjectStatus(status=2),
]
)


class Migration(migrations.Migration):
dependencies = [
("meinberlin_kiezradar", "0001_initial"),
]

operations = [
migrations.RunPython(populate_project_type),
migrations.RunPython(populate_project_status),
]

This file was deleted.

47 changes: 0 additions & 47 deletions meinberlin/apps/kiezradar/migrations/0004_add_project_status.py

This file was deleted.

Loading

0 comments on commit e110236

Please sign in to comment.