summaryrefslogtreecommitdiff
path: root/ishtar_common/views_api.py
blob: c054246e45065d3a311d415e67db0934d18f4dbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2025 Étienne Loks  <etienne.loks at iggdrasil dot net>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# See the file COPYING for details.

from rest_framework import serializers
from rest_framework.response import Response

from ishtar_common.rest import GISAPIView
from ishtar_common.models_imports import ImporterType


class ImporterTypeSerializer(serializers.ModelSerializer):
    model = serializers.SerializerMethodField()

    class Meta:
        model = ImporterType
        fields = ['slug', 'name', 'description', 'is_import', 'is_template', 'model']

    def get_model(self, obj):
        if not obj.associated_models:
            return ""
        return str(obj.associated_models)


class GISSourceAPI(GISAPIView):
    model = ImporterType

    def get_queryset(self):
        user = self.request.user
        if not user.ishtaruser:
            return self.model.filter(pk__isnull=True)
        return self.model.q_qgis_importers(user.ishtaruser)

    def get(self, request, format=None):
        serializer = ImporterTypeSerializer(self.get_queryset(), many=True)
        return Response(serializer.data)