Add govplan API
This commit is contained in:
parent
695ba7daed
commit
7b1c6dc0a7
2 changed files with 94 additions and 0 deletions
|
|
@ -6,11 +6,13 @@ from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
from adminsortable2.admin import SortableAdminMixin
|
from adminsortable2.admin import SortableAdminMixin
|
||||||
|
|
||||||
|
from froide.api import api_router
|
||||||
from froide.follow.admin import FollowerAdmin
|
from froide.follow.admin import FollowerAdmin
|
||||||
from froide.helper.admin_utils import make_choose_object_action, make_emptyfilter
|
from froide.helper.admin_utils import make_choose_object_action, make_emptyfilter
|
||||||
from froide.helper.widgets import TagAutocompleteWidget
|
from froide.helper.widgets import TagAutocompleteWidget
|
||||||
from froide.organization.models import Organization
|
from froide.organization.models import Organization
|
||||||
|
|
||||||
|
from .api_views import GovernmentPlanViewSet
|
||||||
from .forms import (
|
from .forms import (
|
||||||
GovernmentPlanForm,
|
GovernmentPlanForm,
|
||||||
GovernmentPlanUpdateAcceptProposalForm,
|
GovernmentPlanUpdateAcceptProposalForm,
|
||||||
|
|
@ -26,6 +28,8 @@ from .models import (
|
||||||
|
|
||||||
User = auth.get_user_model()
|
User = auth.get_user_model()
|
||||||
|
|
||||||
|
api_router.register(r"governmentplan", GovernmentPlanViewSet, basename="governmentplan")
|
||||||
|
|
||||||
|
|
||||||
class GovPlanAdminSite(admin.AdminSite):
|
class GovPlanAdminSite(admin.AdminSite):
|
||||||
site_header = "Regierungsvorhaben"
|
site_header = "Regierungsvorhaben"
|
||||||
|
|
|
||||||
90
froide_govplan/api_views.py
Normal file
90
froide_govplan/api_views.py
Normal file
|
|
@ -0,0 +1,90 @@
|
||||||
|
from django_filters import rest_framework as filters
|
||||||
|
from rest_framework import serializers, viewsets
|
||||||
|
|
||||||
|
from .models import Government, GovernmentPlan, GovernmentPlanUpdate
|
||||||
|
|
||||||
|
|
||||||
|
class GovernmentSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = Government
|
||||||
|
fields = ("id", "name", "slug", "start_date", "end_date", "planning_document")
|
||||||
|
|
||||||
|
|
||||||
|
class GovernmentPlanSerializer(serializers.ModelSerializer):
|
||||||
|
site_url = serializers.CharField(source="get_absolute_domain_url")
|
||||||
|
updates = serializers.SerializerMethodField()
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = GovernmentPlan
|
||||||
|
fields = (
|
||||||
|
"id",
|
||||||
|
"site_url",
|
||||||
|
"government",
|
||||||
|
"title",
|
||||||
|
"slug",
|
||||||
|
"description",
|
||||||
|
"quote",
|
||||||
|
"due_date",
|
||||||
|
"measure",
|
||||||
|
"status",
|
||||||
|
"rating",
|
||||||
|
"properties",
|
||||||
|
"updates",
|
||||||
|
)
|
||||||
|
|
||||||
|
def get_updates(self, obj):
|
||||||
|
return GovernmentPlanUpdateSerializer(
|
||||||
|
obj.updates.all(), read_only=True, many=True, context=self.context
|
||||||
|
).data
|
||||||
|
|
||||||
|
|
||||||
|
class GovernmentPlanUpdateSerializer(serializers.ModelSerializer):
|
||||||
|
site_url = serializers.CharField(source="get_absolute_domain_url")
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = GovernmentPlanUpdate
|
||||||
|
fields = (
|
||||||
|
"timestamp",
|
||||||
|
"title",
|
||||||
|
"content",
|
||||||
|
"site_url",
|
||||||
|
"url",
|
||||||
|
"status",
|
||||||
|
"rating",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class GovernmentPlanFilter(filters.FilterSet):
|
||||||
|
government = filters.ModelChoiceFilter(
|
||||||
|
queryset=Government.objects.filter(public=True)
|
||||||
|
)
|
||||||
|
properties = filters.CharFilter(method="properties_filter")
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = GovernmentPlan
|
||||||
|
fields = (
|
||||||
|
"government",
|
||||||
|
"status",
|
||||||
|
"rating",
|
||||||
|
"properties",
|
||||||
|
)
|
||||||
|
|
||||||
|
def properties_filter(self, queryset, name, value):
|
||||||
|
try:
|
||||||
|
key, value = value.split(":", 1)
|
||||||
|
except ValueError:
|
||||||
|
return queryset.filter(properties__has_key=value)
|
||||||
|
|
||||||
|
return queryset.filter(**{"properties__%s__contains" % key: value})
|
||||||
|
|
||||||
|
|
||||||
|
class GovernmentPlanViewSet(viewsets.ReadOnlyModelViewSet):
|
||||||
|
serializer_class = GovernmentPlanSerializer
|
||||||
|
filterset_class = GovernmentPlanFilter
|
||||||
|
|
||||||
|
def get_queryset(self):
|
||||||
|
return (
|
||||||
|
GovernmentPlan.objects.filter(public=True)
|
||||||
|
.select_related("government")
|
||||||
|
.prefetch_related("updates")
|
||||||
|
)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue