From 4f1dc1fe522c6633d58c13b47a761c47ad4b0ca2 Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Tue, 29 Jan 2019 16:33:26 +0100 Subject: [PATCH] Split les fonctions de base api dans chaque app/api + modularise api urls --- api/serializers.py | 770 +++------------------------------ api/urls.py | 80 +--- api/views.py | 513 +--------------------- cotisations/api/serializers.py | 92 ++++ cotisations/api/urls.py | 41 ++ cotisations/api/views.py | 93 ++++ machines/api/__init__.py | 0 machines/api/serializers.py | 245 +++++++++++ machines/api/urls.py | 48 ++ machines/api/views.py | 185 ++++++++ preferences/api/serializers.py | 145 +++++++ preferences/api/urls.py | 44 ++ preferences/api/views.py | 136 ++++++ re2o/serializers.py | 60 +++ topologie/api/serializers.py | 236 ++++++++++ topologie/api/urls.py | 47 ++ topologie/api/views.py | 126 ++++++ users/api/serializers.py | 169 ++++++++ users/api/urls.py | 48 ++ users/api/views.py | 145 +++++++ 20 files changed, 1937 insertions(+), 1286 deletions(-) create mode 100644 cotisations/api/serializers.py create mode 100644 cotisations/api/urls.py create mode 100644 cotisations/api/views.py create mode 100644 machines/api/__init__.py create mode 100644 machines/api/serializers.py create mode 100644 machines/api/urls.py create mode 100644 machines/api/views.py create mode 100644 preferences/api/serializers.py create mode 100644 preferences/api/urls.py create mode 100644 preferences/api/views.py create mode 100644 re2o/serializers.py create mode 100644 topologie/api/serializers.py create mode 100644 topologie/api/urls.py create mode 100644 topologie/api/views.py create mode 100644 users/api/serializers.py create mode 100644 users/api/urls.py create mode 100644 users/api/views.py diff --git a/api/serializers.py b/api/serializers.py index 8c22ed21..d927494b 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -30,718 +30,28 @@ import preferences.models as preferences import topologie.models as topologie import users.models as users -# The namespace used for the API. It must match the namespace used in the -# urlpatterns to include the API URLs. -API_NAMESPACE = 'api' - - -class NamespacedHRField(serializers.HyperlinkedRelatedField): - """A `rest_framework.serializers.HyperlinkedRelatedField` subclass to - automatically prefix view names with the API namespace. - """ - - def __init__(self, view_name=None, **kwargs): - if view_name is not None: - view_name = '%s:%s' % (API_NAMESPACE, view_name) - super(NamespacedHRField, self).__init__(view_name=view_name, **kwargs) - - -class NamespacedHIField(serializers.HyperlinkedIdentityField): - """A `rest_framework.serializers.HyperlinkedIdentityField` subclass to - automatically prefix view names with teh API namespace. - """ - - def __init__(self, view_name=None, **kwargs): - if view_name is not None: - view_name = '%s:%s' % (API_NAMESPACE, view_name) - super(NamespacedHIField, self).__init__(view_name=view_name, **kwargs) - - -class NamespacedHMSerializer(serializers.HyperlinkedModelSerializer): - """A `rest_framework.serializers.HyperlinkedModelSerializer` subclass to - automatically prefix view names with the API namespace. - """ - serializer_related_field = NamespacedHRField - serializer_url_field = NamespacedHIField - - -# COTISATIONS - - -class FactureSerializer(NamespacedHMSerializer): - """Serialize `cotisations.models.Facture` objects. - """ - - class Meta: - model = cotisations.Facture - fields = ('user', 'paiement', 'banque', 'cheque', 'date', 'valid', - 'control', 'prix_total', 'name', 'api_url') - - -class BaseInvoiceSerializer(NamespacedHMSerializer): - class Meta: - model = cotisations.BaseInvoice - fields = ('__all__') - -class VenteSerializer(NamespacedHMSerializer): - """Serialize `cotisations.models.Vente` objects. - """ - - class Meta: - model = cotisations.Vente - fields = ('facture', - 'number', 'name', 'prix', 'duration', - 'type_cotisation', 'prix_total', 'api_url') - - -class ArticleSerializer(NamespacedHMSerializer): - """Serialize `cotisations.models.Article` objects. - """ - - class Meta: - model = cotisations.Article - fields = ('name', 'prix', 'duration', 'type_user', - 'type_cotisation', 'api_url') - - -class BanqueSerializer(NamespacedHMSerializer): - """Serialize `cotisations.models.Banque` objects. - """ - - class Meta: - model = cotisations.Banque - fields = ('name', 'api_url') - - -class PaiementSerializer(NamespacedHMSerializer): - """Serialize `cotisations.models.Paiement` objects. - """ - - class Meta: - model = cotisations.Paiement - fields = ('moyen', 'api_url') - - -class CotisationSerializer(NamespacedHMSerializer): - """Serialize `cotisations.models.Cotisation` objects. - """ - - class Meta: - model = cotisations.Cotisation - fields = ('vente', 'type_cotisation', 'date_start', 'date_end', - 'api_url') - - -# MACHINES - - -class MachineSerializer(NamespacedHMSerializer): - """Serialize `machines.models.Machine` objects. - """ - - class Meta: - model = machines.Machine - fields = ('user', 'name', 'active', 'api_url') - - -class MachineTypeSerializer(NamespacedHMSerializer): - """Serialize `machines.models.MachineType` objects. - """ - - class Meta: - model = machines.MachineType - fields = ('type', 'ip_type', 'api_url') - - -class IpTypeSerializer(NamespacedHMSerializer): - """Serialize `machines.models.IpType` objects. - """ - - class Meta: - model = machines.IpType - fields = ('type', 'extension', 'need_infra', 'domaine_ip_start', - 'domaine_ip_stop', 'prefix_v6', 'vlan', 'ouverture_ports', - 'api_url') - - -class VlanSerializer(NamespacedHMSerializer): - """Serialize `machines.models.Vlan` objects. - """ - - class Meta: - model = machines.Vlan - fields = ('vlan_id', 'name', 'comment', 'arp_protect', 'dhcp_snooping', - 'dhcpv6_snooping', 'igmp', 'mld', 'api_url') - - -class NasSerializer(NamespacedHMSerializer): - """Serialize `machines.models.Nas` objects. - """ - - class Meta: - model = machines.Nas - fields = ('name', 'nas_type', 'machine_type', 'port_access_mode', - 'autocapture_mac', 'api_url') - - -class SOASerializer(NamespacedHMSerializer): - """Serialize `machines.models.SOA` objects. - """ - - class Meta: - model = machines.SOA - fields = ('name', 'mail', 'refresh', 'retry', 'expire', 'ttl', - 'api_url') - - -class ExtensionSerializer(NamespacedHMSerializer): - """Serialize `machines.models.Extension` objects. - """ - - class Meta: - model = machines.Extension - fields = ('name', 'need_infra', 'origin', 'origin_v6', 'soa', - 'api_url') - - -class MxSerializer(NamespacedHMSerializer): - """Serialize `machines.models.Mx` objects. - """ - - class Meta: - model = machines.Mx - fields = ('zone', 'priority', 'name', 'api_url') - - -class DNameSerializer(NamespacedHMSerializer): - """Serialize `machines.models.DName` objects. - """ - - class Meta: - model = machines.DName - fields = ('zone', 'alias', 'api_url') - - -class NsSerializer(NamespacedHMSerializer): - """Serialize `machines.models.Ns` objects. - """ - - class Meta: - model = machines.Ns - fields = ('zone', 'ns', 'api_url') - - -class TxtSerializer(NamespacedHMSerializer): - """Serialize `machines.models.Txt` objects. - """ - - class Meta: - model = machines.Txt - fields = ('zone', 'field1', 'field2', 'api_url') - - -class SrvSerializer(NamespacedHMSerializer): - """Serialize `machines.models.Srv` objects. - """ - - class Meta: - model = machines.Srv - fields = ('service', 'protocole', 'extension', 'ttl', 'priority', - 'weight', 'port', 'target', 'api_url') - - -class SshFpSerializer(NamespacedHMSerializer): - """Serialize `machines.models.SSHFP` objects. - """ - - class Meta: - model = machines.SshFp - field = ('machine', 'pub_key_entry', 'algo', 'comment', 'api_url') - - -class InterfaceSerializer(NamespacedHMSerializer): - """Serialize `machines.models.Interface` objects. - """ - mac_address = serializers.CharField() - active = serializers.BooleanField(source='is_active') - - class Meta: - model = machines.Interface - fields = ('ipv4', 'mac_address', 'machine', 'type', 'details', - 'port_lists', 'active', 'api_url') - - -class Ipv6ListSerializer(NamespacedHMSerializer): - """Serialize `machines.models.Ipv6List` objects. - """ - - class Meta: - model = machines.Ipv6List - fields = ('ipv6', 'interface', 'slaac_ip', 'api_url') - - -class DomainSerializer(NamespacedHMSerializer): - """Serialize `machines.models.Domain` objects. - """ - - class Meta: - model = machines.Domain - fields = ('interface_parent', 'name', 'extension', 'cname', - 'api_url') - - -class IpListSerializer(NamespacedHMSerializer): - """Serialize `machines.models.IpList` objects. - """ - - class Meta: - model = machines.IpList - fields = ('ipv4', 'ip_type', 'need_infra', 'api_url') - - -class ServiceSerializer(NamespacedHMSerializer): - """Serialize `machines.models.Service` objects. - """ - - class Meta: - model = machines.Service - fields = ('service_type', 'min_time_regen', 'regular_time_regen', - 'servers', 'api_url') - - -class ServiceLinkSerializer(NamespacedHMSerializer): - """Serialize `machines.models.Service_link` objects. - """ - - class Meta: - model = machines.Service_link - fields = ('service', 'server', 'last_regen', 'asked_regen', - 'need_regen', 'api_url') - extra_kwargs = { - 'api_url': {'view_name': 'servicelink-detail'} - } - - -class OuverturePortListSerializer(NamespacedHMSerializer): - """Serialize `machines.models.OuverturePortList` objects. - """ - tcp_ports_in = NamespacedHRField(view_name='ouvertureport-detail', many=True, read_only=True) - udp_ports_in = NamespacedHRField(view_name='ouvertureport-detail', many=True, read_only=True) - tcp_ports_out = NamespacedHRField(view_name='ouvertureport-detail', many=True, read_only=True) - udp_ports_out = NamespacedHRField(view_name='ouvertureport-detail', many=True, read_only=True) - - class Meta: - model = machines.OuverturePortList - fields = ('name', 'tcp_ports_in', 'udp_ports_in', 'tcp_ports_out', - 'udp_ports_out', 'api_url') - - -class OuverturePortSerializer(NamespacedHMSerializer): - """Serialize `machines.models.OuverturePort` objects. - """ - - class Meta: - model = machines.OuverturePort - fields = ('begin', 'end', 'port_list', 'protocole', 'io', 'api_url') - - -class RoleSerializer(NamespacedHMSerializer): - """Serialize `machines.models.OuverturePort` objects. - """ - servers = InterfaceSerializer(read_only=True, many=True) - - class Meta: - model = machines.Role - fields = ('role_type', 'servers', 'api_url') - - -# PREFERENCES - - -class OptionalUserSerializer(NamespacedHMSerializer): - """Serialize `preferences.models.OptionalUser` objects. - """ - tel_mandatory = serializers.BooleanField(source='is_tel_mandatory') - shell_default = serializers.StringRelatedField() - - class Meta: - model = preferences.OptionalUser - fields = ('tel_mandatory', 'gpg_fingerprint', - 'all_can_create_club', 'self_adhesion', 'shell_default', - 'self_change_shell', 'local_email_accounts_enabled', 'local_email_domain', - 'max_email_address', - ) - - -class OptionalMachineSerializer(NamespacedHMSerializer): - """Serialize `preferences.models.OptionalMachine` objects. - """ - - class Meta: - model = preferences.OptionalMachine - fields = ('password_machine', 'max_lambdauser_interfaces', - 'max_lambdauser_aliases', 'ipv6_mode', 'create_machine', - 'ipv6') - - -class OptionalTopologieSerializer(NamespacedHMSerializer): - """Serialize `preferences.models.OptionalTopologie` objects. - """ - switchs_management_interface_ip = serializers.CharField() - - class Meta: - model = preferences.OptionalTopologie - fields = ('switchs_ip_type', 'switchs_web_management', - 'switchs_web_management_ssl', 'switchs_rest_management', - 'switchs_management_utils', 'switchs_management_interface_ip', - 'provision_switchs_enabled', 'switchs_provision', 'switchs_management_sftp_creds') - - -class RadiusOptionSerializer(NamespacedHMSerializer): - """Serialize `preferences.models.RadiusOption` objects - """ - - class Meta: - model = preferences.RadiusOption - fields = ('radius_general_policy', 'unknown_machine', - 'unknown_machine_vlan', 'unknown_port', - 'unknown_port_vlan', 'unknown_room', 'unknown_room_vlan', - 'non_member', 'non_member_vlan', 'banned', 'banned_vlan', - 'vlan_decision_ok') - - -class GeneralOptionSerializer(NamespacedHMSerializer): - """Serialize `preferences.models.GeneralOption` objects. - """ - - class Meta: - model = preferences.GeneralOption - fields = ('general_message_fr', 'general_message_en', - 'search_display_page', 'pagination_number', - 'pagination_large_number', 'req_expire_hrs', - 'site_name', 'main_site_url', 'email_from', - 'GTU_sum_up', 'GTU') - -class HomeServiceSerializer(NamespacedHMSerializer): - """Serialize `preferences.models.Service` objects. - """ - - class Meta: - model = preferences.Service - fields = ('name', 'url', 'description', 'image', 'api_url') - extra_kwargs = { - 'api_url': {'view_name': 'homeservice-detail'} - } - - -class AssoOptionSerializer(NamespacedHMSerializer): - """Serialize `preferences.models.AssoOption` objects. - """ - - class Meta: - model = preferences.AssoOption - fields = ('name', 'siret', 'adresse1', 'adresse2', 'contact', - 'telephone', 'pseudo', 'utilisateur_asso', 'description') - - -class HomeOptionSerializer(NamespacedHMSerializer): - """Serialize `preferences.models.HomeOption` objects. - """ - - class Meta: - model = preferences.HomeOption - fields = ('facebook_url', 'twitter_url', 'twitter_account_name') - - -class MailMessageOptionSerializer(NamespacedHMSerializer): - """Serialize `preferences.models.MailMessageOption` objects. - """ - - class Meta: - model = preferences.MailMessageOption - fields = ('welcome_mail_fr', 'welcome_mail_en') - - -# TOPOLOGIE - - -class StackSerializer(NamespacedHMSerializer): - """Serialize `topologie.models.Stack` objects - """ - - class Meta: - model = topologie.Stack - fields = ('name', 'stack_id', 'details', 'member_id_min', - 'member_id_max', 'api_url') - - -class AccessPointSerializer(NamespacedHMSerializer): - """Serialize `topologie.models.AccessPoint` objects - """ - - class Meta: - model = topologie.AccessPoint - fields = ('user', 'name', 'active', 'location', 'api_url') - - -class SwitchSerializer(NamespacedHMSerializer): - """Serialize `topologie.models.Switch` objects - """ - port_amount = serializers.IntegerField(source='number') - - class Meta: - model = topologie.Switch - fields = ('user', 'name', 'active', 'port_amount', 'stack', - 'stack_member_id', 'model', 'switchbay', 'api_url') - - -class ServerSerializer(NamespacedHMSerializer): - """Serialize `topologie.models.Server` objects - """ - - class Meta: - model = topologie.Server - fields = ('user', 'name', 'active', 'api_url') - - -class ModelSwitchSerializer(NamespacedHMSerializer): - """Serialize `topologie.models.ModelSwitch` objects - """ - - class Meta: - model = topologie.ModelSwitch - fields = ('reference', 'constructor', 'api_url') - - -class ConstructorSwitchSerializer(NamespacedHMSerializer): - """Serialize `topologie.models.ConstructorSwitch` objects - """ - - class Meta: - model = topologie.ConstructorSwitch - fields = ('name', 'api_url') - - -class SwitchBaySerializer(NamespacedHMSerializer): - """Serialize `topologie.models.SwitchBay` objects - """ - - class Meta: - model = topologie.SwitchBay - fields = ('name', 'building', 'info', 'api_url') - - -class BuildingSerializer(NamespacedHMSerializer): - """Serialize `topologie.models.Building` objects - """ - - class Meta: - model = topologie.Building - fields = ('name', 'api_url') - - -class SwitchPortSerializer(NamespacedHMSerializer): - """Serialize `topologie.models.Port` objects - """ - - get_port_profile = NamespacedHIField(view_name='portprofile-detail', read_only=True) - - class Meta: - model = topologie.Port - fields = ('switch', 'port', 'room', 'machine_interface', 'related', - 'custom_profile', 'state', 'get_port_profile', 'details', 'api_url') - extra_kwargs = { - 'related': {'view_name': 'switchport-detail'}, - 'api_url': {'view_name': 'switchport-detail'}, - } - - -class PortProfileSerializer(NamespacedHMSerializer): - """Serialize `topologie.models.Room` objects - """ - class Meta: - model = topologie.PortProfile - fields = ('name', 'profil_default', 'vlan_untagged', 'vlan_tagged', - 'radius_type', 'radius_mode', 'speed', 'mac_limit', 'flow_control', - 'dhcp_snooping', 'dhcpv6_snooping', 'dhcpv6_snooping', 'arp_protect', - 'ra_guard', 'loop_protect', 'api_url') - - -class RoomSerializer(NamespacedHMSerializer): - """Serialize `topologie.models.Room` objects - """ - - class Meta: - model = topologie.Room - fields = ('name', 'details', 'api_url') - - -class PortProfileSerializer(NamespacedHMSerializer): - vlan_untagged = VlanSerializer(read_only=True) - - class Meta: - model = topologie.PortProfile - fields = ('name', 'profil_default', 'vlan_untagged', 'vlan_tagged', - 'radius_type', 'radius_mode', 'speed', 'mac_limit', - 'flow_control', 'dhcp_snooping', 'dhcpv6_snooping', - 'arp_protect', 'ra_guard', 'loop_protect', 'vlan_untagged', - 'vlan_tagged') - - -# USERS - - -class UserSerializer(NamespacedHMSerializer): - """Serialize `users.models.User` objects. - """ - access = serializers.BooleanField(source='has_access') - uid = serializers.IntegerField(source='uid_number') - - class Meta: - model = users.User - fields = ('surname', 'pseudo', 'email', 'local_email_redirect', - 'local_email_enabled', 'school', 'shell', 'comment', - 'state', 'registered', 'telephone', 'solde', 'access', - 'end_access', 'uid', 'class_name', 'api_url') - extra_kwargs = { - 'shell': {'view_name': 'shell-detail'} - } - - -class ClubSerializer(NamespacedHMSerializer): - """Serialize `users.models.Club` objects. - """ - name = serializers.CharField(source='surname') - access = serializers.BooleanField(source='has_access') - uid = serializers.IntegerField(source='uid_number') - - class Meta: - model = users.Club - fields = ('name', 'pseudo', 'email', 'local_email_redirect', - 'local_email_enabled', 'school', 'shell', 'comment', - 'state', 'registered', 'telephone', 'solde', 'room', - 'access', 'end_access', 'administrators', 'members', - 'mailing', 'uid', 'api_url') - extra_kwargs = { - 'shell': {'view_name': 'shell-detail'} - } - - -class AdherentSerializer(NamespacedHMSerializer): - """Serialize `users.models.Adherent` objects. - """ - access = serializers.BooleanField(source='has_access') - uid = serializers.IntegerField(source='uid_number') - - class Meta: - model = users.Adherent - fields = ('name', 'surname', 'pseudo', 'email', 'local_email_redirect', - 'local_email_enabled', 'school', 'shell', 'comment', - 'state', 'registered', 'telephone', 'room', 'solde', - 'access', 'end_access', 'uid', 'api_url', 'gid') - extra_kwargs = { - 'shell': {'view_name': 'shell-detail'} - } - - -class BasicUserSerializer(NamespacedHMSerializer): - """Serialize 'users.models.User' minimal infos""" - uid = serializers.IntegerField(source='uid_number') - gid = serializers.IntegerField(source='gid_number') - - class Meta: - model = users.User - fields = ('pseudo', 'uid', 'gid') - - -class ServiceUserSerializer(NamespacedHMSerializer): - """Serialize `users.models.ServiceUser` objects. - """ - - class Meta: - model = users.ServiceUser - fields = ('pseudo', 'access_group', 'comment', 'api_url') - - -class SchoolSerializer(NamespacedHMSerializer): - """Serialize `users.models.School` objects. - """ - - class Meta: - model = users.School - fields = ('name', 'api_url') - - -class ListRightSerializer(NamespacedHMSerializer): - """Serialize `users.models.ListRight` objects. - """ - - class Meta: - model = users.ListRight - fields = ('unix_name', 'gid', 'critical', 'details', 'api_url') - - -class ShellSerializer(NamespacedHMSerializer): - """Serialize `users.models.ListShell` objects. - """ - - class Meta: - model = users.ListShell - fields = ('shell', 'api_url') - extra_kwargs = { - 'api_url': {'view_name': 'shell-detail'} - } - - -class BanSerializer(NamespacedHMSerializer): - """Serialize `users.models.Ban` objects. - """ - active = serializers.BooleanField(source='is_active') - - class Meta: - model = users.Ban - fields = ('user', 'raison', 'date_start', 'date_end', 'state', - 'active', 'api_url') - - -class WhitelistSerializer(NamespacedHMSerializer): - """Serialize `users.models.Whitelist` objects. - """ - active = serializers.BooleanField(source='is_active') - - class Meta: - model = users.Whitelist - fields = ('user', 'raison', 'date_start', 'date_end', 'active', 'api_url') - - -class EMailAddressSerializer(NamespacedHMSerializer): - """Serialize `users.models.EMailAddress` objects. - """ - user = serializers.CharField(source='user.pseudo', read_only=True) - - class Meta: - model = users.EMailAddress - fields = ('user', 'local_part', 'complete_email_address', 'api_url') - - -# SERVICE REGEN - - -class ServiceRegenSerializer(NamespacedHMSerializer): - """Serialize the data about the services to regen. - """ - hostname = serializers.CharField(source='server.domain.name', read_only=True) - service_name = serializers.CharField(source='service.service_type', read_only=True) - need_regen = serializers.BooleanField() - - class Meta: - model = machines.Service_link - fields = ('hostname', 'service_name', 'need_regen', 'api_url') - extra_kwargs = { - 'api_url': {'view_name': 'serviceregen-detail'} - } - -# Switches et ports +from re2o.serializers import NamespacedHRField, NamespacedHIField, NamespacedHMSerializer + +from machines.api.serializers import ( + VlanSerializer, + Ipv6ListSerializer, + SOASerializer, + IpListSerializer, + NsSerializer, + MxSerializer, + TxtSerializer, + SrvSerializer, + SshFpSerializer, +) + +from users.api.serializers import ( + UserSerializer, + ClubSerializer, + EMailAddressSerializer +) + + +# CONF SWITCH class InterfaceVlanSerializer(NamespacedHMSerializer): domain = serializers.CharField(read_only=True) @@ -826,19 +136,22 @@ class SwitchPortSerializer(serializers.ModelSerializer): 'web_management_enabled', 'get_radius_key_value', 'get_management_cred_value', 'list_modules') -# LOCAL EMAILS +# SERVICE REGEN -class LocalEmailUsersSerializer(NamespacedHMSerializer): - email_address = EMailAddressSerializer( - read_only=True, - many=True - ) +class ServiceRegenSerializer(NamespacedHMSerializer): + """Serialize the data about the services to regen. + """ + hostname = serializers.CharField(source='server.domain.name', read_only=True) + service_name = serializers.CharField(source='service.service_type', read_only=True) + need_regen = serializers.BooleanField() class Meta: - model = users.User - fields = ('local_email_enabled', 'local_email_redirect', - 'email_address', 'email') + model = machines.Service_link + fields = ('hostname', 'service_name', 'need_regen', 'api_url') + extra_kwargs = { + 'api_url': {'view_name': 'serviceregen-detail'} + } # Firewall @@ -1104,3 +417,18 @@ class MailingSerializer(ClubSerializer): class Meta(ClubSerializer.Meta): fields = ('name', 'members', 'admins') + + +# LOCAL EMAILS + + +class LocalEmailUsersSerializer(NamespacedHMSerializer): + email_address = EMailAddressSerializer( + read_only=True, + many=True + ) + + class Meta: + model = users.User + fields = ('local_email_enabled', 'local_email_redirect', + 'email_address', 'email') diff --git a/api/urls.py b/api/urls.py index 4a34c1de..dc08b1c6 100644 --- a/api/urls.py +++ b/api/urls.py @@ -27,79 +27,25 @@ can also be register. That way a complete API root page presenting all URLs can be generated automatically. """ +import importlib + +from django.conf import settings from django.conf.urls import url, include from . import views from .routers import AllViewsRouter router = AllViewsRouter() -# COTISATIONS -router.register_viewset(r'cotisations/facture', views.FactureViewSet) -router.register_viewset(r'cotisations/vente', views.VenteViewSet) -router.register_viewset(r'cotisations/article', views.ArticleViewSet) -router.register_viewset(r'cotisations/banque', views.BanqueViewSet) -router.register_viewset(r'cotisations/paiement', views.PaiementViewSet) -router.register_viewset(r'cotisations/cotisation', views.CotisationViewSet) -# MACHINES -router.register_viewset(r'machines/machine', views.MachineViewSet) -router.register_viewset(r'machines/machinetype', views.MachineTypeViewSet) -router.register_viewset(r'machines/iptype', views.IpTypeViewSet) -router.register_viewset(r'machines/vlan', views.VlanViewSet) -router.register_viewset(r'machines/nas', views.NasViewSet) -router.register_viewset(r'machines/soa', views.SOAViewSet) -router.register_viewset(r'machines/extension', views.ExtensionViewSet) -router.register_viewset(r'machines/mx', views.MxViewSet) -router.register_viewset(r'machines/ns', views.NsViewSet) -router.register_viewset(r'machines/txt', views.TxtViewSet) -router.register_viewset(r'machines/dname', views.DNameViewSet) -router.register_viewset(r'machines/srv', views.SrvViewSet) -router.register_viewset(r'machines/sshfp', views.SshFpViewSet) -router.register_viewset(r'machines/interface', views.InterfaceViewSet) -router.register_viewset(r'machines/ipv6list', views.Ipv6ListViewSet) -router.register_viewset(r'machines/domain', views.DomainViewSet) -router.register_viewset(r'machines/iplist', views.IpListViewSet) -router.register_viewset(r'machines/service', views.ServiceViewSet) -router.register_viewset(r'machines/servicelink', views.ServiceLinkViewSet, base_name='servicelink') -router.register_viewset(r'machines/ouvertureportlist', views.OuverturePortListViewSet) -router.register_viewset(r'machines/ouvertureport', views.OuverturePortViewSet) -router.register_viewset(r'machines/role', views.RoleViewSet) -# PREFERENCES -router.register_view(r'preferences/optionaluser', views.OptionalUserView), -router.register_view(r'preferences/optionalmachine', views.OptionalMachineView), -router.register_view(r'preferences/optionaltopologie', views.OptionalTopologieView), -router.register_view(r'preferences/radiusoption', views.RadiusOptionView), -router.register_view(r'preferences/generaloption', views.GeneralOptionView), -router.register_viewset(r'preferences/service', views.HomeServiceViewSet, base_name='homeservice'), -router.register_view(r'preferences/assooption', views.AssoOptionView), -router.register_view(r'preferences/homeoption', views.HomeOptionView), -router.register_view(r'preferences/mailmessageoption', views.MailMessageOptionView), -# TOPOLOGIE -router.register_viewset(r'topologie/stack', views.StackViewSet) -router.register_viewset(r'topologie/acesspoint', views.AccessPointViewSet) -router.register_viewset(r'topologie/switch', views.SwitchViewSet) -router.register_viewset(r'topologie/server', views.ServerViewSet) -router.register_viewset(r'topologie/modelswitch', views.ModelSwitchViewSet) -router.register_viewset(r'topologie/constructorswitch', views.ConstructorSwitchViewSet) -router.register_viewset(r'topologie/switchbay', views.SwitchBayViewSet) -router.register_viewset(r'topologie/building', views.BuildingViewSet) -router.register_viewset(r'topologie/switchport', views.SwitchPortViewSet, base_name='switchport') -router.register_viewset(r'topologie/portprofile', views.PortProfileViewSet, base_name='portprofile') -router.register_viewset(r'topologie/room', views.RoomViewSet) -router.register(r'topologie/portprofile', views.PortProfileViewSet) -# USERS -router.register_viewset(r'users/user', views.UserViewSet, base_name='user') -router.register_viewset(r'users/homecreation', views.HomeCreationViewSet, base_name='homecreation') -router.register_viewset(r'users/normaluser', views.NormalUserViewSet, base_name='normaluser') -router.register_viewset(r'users/criticaluser', views.CriticalUserViewSet, base_name='criticaluser') -router.register_viewset(r'users/club', views.ClubViewSet) -router.register_viewset(r'users/adherent', views.AdherentViewSet) -router.register_viewset(r'users/serviceuser', views.ServiceUserViewSet) -router.register_viewset(r'users/school', views.SchoolViewSet) -router.register_viewset(r'users/listright', views.ListRightViewSet) -router.register_viewset(r'users/shell', views.ShellViewSet, base_name='shell') -router.register_viewset(r'users/ban', views.BanViewSet) -router.register_viewset(r'users/whitelist', views.WhitelistViewSet) -router.register_viewset(r'users/emailaddress', views.EMailAddressViewSet) + +for app in settings.INSTALLED_APPS: + try: + module = importlib.import_module(app + '.api.urls') + add_to_router = getattr(module, 'add_to_router') + add_to_router(router) + except ImportError: + pass + + # SERVICE REGEN router.register_viewset(r'services/regen', views.ServiceRegenViewSet, base_name='serviceregen') # DHCP diff --git a/api/views.py b/api/views.py index 3108f9f3..a43b8bbc 100644 --- a/api/views.py +++ b/api/views.py @@ -45,506 +45,8 @@ from . import serializers from .pagination import PageSizedPagination from .permissions import ACLPermission - -# COTISATIONS - - -class FactureViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `cotisations.models.Facture` objects. - """ - queryset = cotisations.Facture.objects.all() - serializer_class = serializers.FactureSerializer - -class FactureViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `cotisations.models.Facture` objects. - """ - queryset = cotisations.BaseInvoice.objects.all() - serializer_class = serializers.BaseInvoiceSerializer - - -class VenteViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `cotisations.models.Vente` objects. - """ - queryset = cotisations.Vente.objects.all() - serializer_class = serializers.VenteSerializer - - -class ArticleViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `cotisations.models.Article` objects. - """ - queryset = cotisations.Article.objects.all() - serializer_class = serializers.ArticleSerializer - - -class BanqueViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `cotisations.models.Banque` objects. - """ - queryset = cotisations.Banque.objects.all() - serializer_class = serializers.BanqueSerializer - - -class PaiementViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `cotisations.models.Paiement` objects. - """ - queryset = cotisations.Paiement.objects.all() - serializer_class = serializers.PaiementSerializer - - -class CotisationViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `cotisations.models.Cotisation` objects. - """ - queryset = cotisations.Cotisation.objects.all() - serializer_class = serializers.CotisationSerializer - - -# MACHINES - - -class MachineViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `machines.models.Machine` objects. - """ - queryset = machines.Machine.objects.all() - serializer_class = serializers.MachineSerializer - - -class MachineTypeViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `machines.models.MachineType` objects. - """ - queryset = machines.MachineType.objects.all() - serializer_class = serializers.MachineTypeSerializer - - -class IpTypeViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `machines.models.IpType` objects. - """ - queryset = machines.IpType.objects.all() - serializer_class = serializers.IpTypeSerializer - - -class VlanViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `machines.models.Vlan` objects. - """ - queryset = machines.Vlan.objects.all() - serializer_class = serializers.VlanSerializer - - -class NasViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `machines.models.Nas` objects. - """ - queryset = machines.Nas.objects.all() - serializer_class = serializers.NasSerializer - - -class SOAViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `machines.models.SOA` objects. - """ - queryset = machines.SOA.objects.all() - serializer_class = serializers.SOASerializer - - -class ExtensionViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `machines.models.Extension` objects. - """ - queryset = machines.Extension.objects.all() - serializer_class = serializers.ExtensionSerializer - - -class MxViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `machines.models.Mx` objects. - """ - queryset = machines.Mx.objects.all() - serializer_class = serializers.MxSerializer - - -class NsViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `machines.models.Ns` objects. - """ - queryset = machines.Ns.objects.all() - serializer_class = serializers.NsSerializer - - -class TxtViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `machines.models.Txt` objects. - """ - queryset = machines.Txt.objects.all() - serializer_class = serializers.TxtSerializer - - -class DNameViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `machines.models.DName` objects. - """ - queryset = machines.DName.objects.all() - serializer_class = serializers.DNameSerializer - - -class SrvViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `machines.models.Srv` objects. - """ - queryset = machines.Srv.objects.all() - serializer_class = serializers.SrvSerializer - - -class SshFpViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `machines.models.SshFp` objects. - """ - queryset = machines.SshFp.objects.all() - serializer_class = serializers.SshFpSerializer - - -class InterfaceViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `machines.models.Interface` objects. - """ - queryset = machines.Interface.objects.all() - serializer_class = serializers.InterfaceSerializer - - -class Ipv6ListViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `machines.models.Ipv6List` objects. - """ - queryset = machines.Ipv6List.objects.all() - serializer_class = serializers.Ipv6ListSerializer - - -class DomainViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `machines.models.Domain` objects. - """ - queryset = machines.Domain.objects.all() - serializer_class = serializers.DomainSerializer - - -class IpListViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `machines.models.IpList` objects. - """ - queryset = machines.IpList.objects.all() - serializer_class = serializers.IpListSerializer - - -class ServiceViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `machines.models.Service` objects. - """ - queryset = machines.Service.objects.all() - serializer_class = serializers.ServiceSerializer - - -class ServiceLinkViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `machines.models.Service_link` objects. - """ - queryset = machines.Service_link.objects.all() - serializer_class = serializers.ServiceLinkSerializer - - -class OuverturePortListViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `machines.models.OuverturePortList` - objects. - """ - queryset = machines.OuverturePortList.objects.all() - serializer_class = serializers.OuverturePortListSerializer - - -class OuverturePortViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `machines.models.OuverturePort` objects. - """ - queryset = machines.OuverturePort.objects.all() - serializer_class = serializers.OuverturePortSerializer - - -class RoleViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `machines.models.Machine` objects. - """ - queryset = machines.Role.objects.all() - serializer_class = serializers.RoleSerializer - - -# PREFERENCES -# Those views differ a bit because there is only one object -# to display, so we don't bother with the listing part - -class OptionalUserView(generics.RetrieveAPIView): - """Exposes details of `preferences.models.` settings. - """ - permission_classes = (ACLPermission,) - perms_map = {'GET': [preferences.OptionalUser.can_view_all]} - serializer_class = serializers.OptionalUserSerializer - - def get_object(self): - return preferences.OptionalUser.objects.first() - - -class OptionalMachineView(generics.RetrieveAPIView): - """Exposes details of `preferences.models.OptionalMachine` settings. - """ - permission_classes = (ACLPermission,) - perms_map = {'GET': [preferences.OptionalMachine.can_view_all]} - serializer_class = serializers.OptionalMachineSerializer - - def get_object(self): - return preferences.OptionalMachine.objects.first() - - -class OptionalTopologieView(generics.RetrieveAPIView): - """Exposes details of `preferences.models.OptionalTopologie` settings. - """ - permission_classes = (ACLPermission,) - perms_map = {'GET': [preferences.OptionalTopologie.can_view_all]} - serializer_class = serializers.OptionalTopologieSerializer - - def get_object(self): - return preferences.OptionalTopologie.objects.first() - - -class RadiusOptionView(generics.RetrieveAPIView): - """Exposes details of `preferences.models.OptionalTopologie` settings. - """ - permission_classes = (ACLPermission,) - perms_map = {'GET': [preferences.RadiusOption.can_view_all]} - serializer_class = serializers.RadiusOptionSerializer - - def get_object(self): - return preferences.RadiusOption.objects.first() - - -class GeneralOptionView(generics.RetrieveAPIView): - """Exposes details of `preferences.models.GeneralOption` settings. - """ - permission_classes = (ACLPermission,) - perms_map = {'GET': [preferences.GeneralOption.can_view_all]} - serializer_class = serializers.GeneralOptionSerializer - - def get_object(self): - return preferences.GeneralOption.objects.first() - - -class HomeServiceViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `preferences.models.Service` objects. - """ - queryset = preferences.Service.objects.all() - serializer_class = serializers.HomeServiceSerializer - - -class AssoOptionView(generics.RetrieveAPIView): - """Exposes details of `preferences.models.AssoOption` settings. - """ - permission_classes = (ACLPermission,) - perms_map = {'GET': [preferences.AssoOption.can_view_all]} - serializer_class = serializers.AssoOptionSerializer - - def get_object(self): - return preferences.AssoOption.objects.first() - - -class HomeOptionView(generics.RetrieveAPIView): - """Exposes details of `preferences.models.HomeOption` settings. - """ - permission_classes = (ACLPermission,) - perms_map = {'GET': [preferences.HomeOption.can_view_all]} - serializer_class = serializers.HomeOptionSerializer - - def get_object(self): - return preferences.HomeOption.objects.first() - - -class MailMessageOptionView(generics.RetrieveAPIView): - """Exposes details of `preferences.models.MailMessageOption` settings. - """ - permission_classes = (ACLPermission,) - perms_map = {'GET': [preferences.MailMessageOption.can_view_all]} - serializer_class = serializers.MailMessageOptionSerializer - - def get_object(self): - return preferences.MailMessageOption.objects.first() - - -# TOPOLOGIE - - -class StackViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `topologie.models.Stack` objects. - """ - queryset = topologie.Stack.objects.all() - serializer_class = serializers.StackSerializer - - -class AccessPointViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `topologie.models.AccessPoint` objects. - """ - queryset = topologie.AccessPoint.objects.all() - serializer_class = serializers.AccessPointSerializer - - -class SwitchViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `topologie.models.Switch` objects. - """ - queryset = topologie.Switch.objects.all() - serializer_class = serializers.SwitchSerializer - - -class ServerViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `topologie.models.Server` objects. - """ - queryset = topologie.Server.objects.all() - serializer_class = serializers.ServerSerializer - - -class ModelSwitchViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `topologie.models.ModelSwitch` objects. - """ - queryset = topologie.ModelSwitch.objects.all() - serializer_class = serializers.ModelSwitchSerializer - - -class ConstructorSwitchViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `topologie.models.ConstructorSwitch` - objects. - """ - queryset = topologie.ConstructorSwitch.objects.all() - serializer_class = serializers.ConstructorSwitchSerializer - - -class SwitchBayViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `topologie.models.SwitchBay` objects. - """ - queryset = topologie.SwitchBay.objects.all() - serializer_class = serializers.SwitchBaySerializer - - -class BuildingViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `topologie.models.Building` objects. - """ - queryset = topologie.Building.objects.all() - serializer_class = serializers.BuildingSerializer - - -class SwitchPortViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `topologie.models.Port` objects. - """ - queryset = topologie.Port.objects.all() - serializer_class = serializers.SwitchPortSerializer - - -class PortProfileViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `topologie.models.PortProfile` objects. - """ - queryset = topologie.PortProfile.objects.all() - serializer_class = serializers.PortProfileSerializer - - -class RoomViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `topologie.models.Room` objects. - """ - queryset = topologie.Room.objects.all() - serializer_class = serializers.RoomSerializer - - -class PortProfileViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `topologie.models.PortProfile` objects. - """ - queryset = topologie.PortProfile.objects.all() - serializer_class = serializers.PortProfileSerializer - - -# USER - - -class UserViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `users.models.Users` objects. - """ - queryset = users.User.objects.all() - serializer_class = serializers.UserSerializer - - -class HomeCreationViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes infos of `users.models.Users` objects to create homes. - """ - queryset = users.User.objects.exclude(Q(state=users.User.STATE_DISABLED) | Q(state=users.User.STATE_NOT_YET_ACTIVE)) - serializer_class = serializers.BasicUserSerializer - - -class NormalUserViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes infos of `users.models.Users`without specific rights objects.""" - queryset = users.User.objects.exclude(groups__listright__critical=True).distinct() - serializer_class = serializers.BasicUserSerializer - - -class CriticalUserViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes infos of `users.models.Users`without specific rights objects.""" - queryset = users.User.objects.filter(groups__listright__critical=True).distinct() - serializer_class = serializers.BasicUserSerializer - - -class ClubViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `users.models.Club` objects. - """ - queryset = users.Club.objects.all() - serializer_class = serializers.ClubSerializer - - -class AdherentViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `users.models.Adherent` objects. - """ - queryset = users.Adherent.objects.all() - serializer_class = serializers.AdherentSerializer - - -class ServiceUserViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `users.models.ServiceUser` objects. - """ - queryset = users.ServiceUser.objects.all() - serializer_class = serializers.ServiceUserSerializer - - -class SchoolViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `users.models.School` objects. - """ - queryset = users.School.objects.all() - serializer_class = serializers.SchoolSerializer - - -class ListRightViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `users.models.ListRight` objects. - """ - queryset = users.ListRight.objects.all() - serializer_class = serializers.ListRightSerializer - - -class ShellViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `users.models.ListShell` objects. - """ - queryset = users.ListShell.objects.all() - serializer_class = serializers.ShellSerializer - - -class BanViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `users.models.Ban` objects. - """ - queryset = users.Ban.objects.all() - serializer_class = serializers.BanSerializer - - -class WhitelistViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `users.models.Whitelist` objects. - """ - queryset = users.Whitelist.objects.all() - serializer_class = serializers.WhitelistSerializer - - -class EMailAddressViewSet(viewsets.ReadOnlyModelViewSet): - """Exposes list and details of `users.models.EMailAddress` objects. - """ - serializer_class = serializers.EMailAddressSerializer - queryset = users.EMailAddress.objects.none() - - def get_queryset(self): - if preferences.OptionalUser.get_cached_value( - 'local_email_accounts_enabled'): - return (users.EMailAddress.objects - .filter(user__local_email_enabled=True)) - else: - return users.EMailAddress.objects.none() - - # SERVICE REGEN - class ServiceRegenViewSet(viewsets.ModelViewSet): """Exposes list and details of the services to regen """ @@ -713,3 +215,18 @@ class ObtainExpiringAuthToken(ObtainAuthToken): 'token': token.key, 'expiration': token.created + token_duration }) + + +class EMailAddressViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `users.models.EMailAddress` objects. + """ + serializer_class = serializers.EMailAddressSerializer + queryset = users.EMailAddress.objects.none() + + def get_queryset(self): + if preferences.OptionalUser.get_cached_value( + 'local_email_accounts_enabled'): + return (users.EMailAddress.objects + .filter(user__local_email_enabled=True)) + else: + return users.EMailAddress.objects.none() diff --git a/cotisations/api/serializers.py b/cotisations/api/serializers.py new file mode 100644 index 00000000..f546f24c --- /dev/null +++ b/cotisations/api/serializers.py @@ -0,0 +1,92 @@ +# -*- mode: python; coding: utf-8 -*- +# Re2o est un logiciel d'administration développé initiallement au rezometz. Il +# se veut agnostique au réseau considéré, de manière à être installable en +# quelques clics. +# +# Copyright © 2019 Arthur Grisel-Davy +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +"""Defines the serializers of the API +""" + +from rest_framework import serializers + +import cotisations.models as cotisations + +from re2o.serializers import NamespacedHRField, NamespacedHIField, NamespacedHMSerializer + +class FactureSerializer(NamespacedHMSerializer): + """Serialize `cotisations.models.Facture` objects. + """ + + class Meta: + model = cotisations.Facture + fields = ('user', 'paiement', 'banque', 'cheque', 'date', 'valid', + 'control', 'prix_total', 'name', 'api_url') + + +class BaseInvoiceSerializer(NamespacedHMSerializer): + class Meta: + model = cotisations.BaseInvoice + fields = ('__all__') + +class VenteSerializer(NamespacedHMSerializer): + """Serialize `cotisations.models.Vente` objects. + """ + + class Meta: + model = cotisations.Vente + fields = ('facture', + 'number', 'name', 'prix', 'duration', + 'type_cotisation', 'prix_total', 'api_url') + + +class ArticleSerializer(NamespacedHMSerializer): + """Serialize `cotisations.models.Article` objects. + """ + + class Meta: + model = cotisations.Article + fields = ('name', 'prix', 'duration', 'type_user', + 'type_cotisation', 'api_url') + + +class BanqueSerializer(NamespacedHMSerializer): + """Serialize `cotisations.models.Banque` objects. + """ + + class Meta: + model = cotisations.Banque + fields = ('name', 'api_url') + + +class PaiementSerializer(NamespacedHMSerializer): + """Serialize `cotisations.models.Paiement` objects. + """ + + class Meta: + model = cotisations.Paiement + fields = ('moyen', 'api_url') + + +class CotisationSerializer(NamespacedHMSerializer): + """Serialize `cotisations.models.Cotisation` objects. + """ + + class Meta: + model = cotisations.Cotisation + fields = ('vente', 'type_cotisation', 'date_start', 'date_end', + 'api_url') diff --git a/cotisations/api/urls.py b/cotisations/api/urls.py new file mode 100644 index 00000000..9b9fbd23 --- /dev/null +++ b/cotisations/api/urls.py @@ -0,0 +1,41 @@ +# -*- mode: python; coding: utf-8 -*- +# Re2o est un logiciel d'administration développé initiallement au rezometz. Il +# se veut agnostique au réseau considéré, de manière à être installable en +# quelques clics. +# +# Copyright © 2019 Arthur Grisel-Davy +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +"""Defines the URLs of the API for Cotisation + +A custom router is used to register all the routes. That allows to register +all the URL patterns from the viewsets as a standard router but, the views +can also be register. That way a complete API root page presenting all URLs +can be generated automatically. +""" + +from django.conf.urls import url, include + +from . import views +from api.routers import AllViewsRouter + +def add_to_router(router): + router.register_viewset(r'cotisations/facture', views.FactureViewSet) + router.register_viewset(r'cotisations/vente', views.VenteViewSet) + router.register_viewset(r'cotisations/article', views.ArticleViewSet) + router.register_viewset(r'cotisations/banque', views.BanqueViewSet) + router.register_viewset(r'cotisations/paiement', views.PaiementViewSet) + router.register_viewset(r'cotisations/cotisation', views.CotisationViewSet) diff --git a/cotisations/api/views.py b/cotisations/api/views.py new file mode 100644 index 00000000..cd71c157 --- /dev/null +++ b/cotisations/api/views.py @@ -0,0 +1,93 @@ +# -*- mode: python; coding: utf-8 -*- +# Re2o est un logiciel d'administration développé initiallement au rezometz. Il +# se veut agnostique au réseau considéré, de manière à être installable en +# quelques clics. +# +# Copyright © 2019 Arthur Grisel-Davy +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +"""Defines the views of the API + +All views inherits the `rest_framework.views.APIview` to respect the +REST API requirements such as dealing with HTTP status code, format of +the response (JSON or other), the CSRF exempting, ... +""" + +import datetime + +from django.conf import settings +from django.db.models import Q +from rest_framework import viewsets, generics, views +from rest_framework.authtoken.models import Token +from rest_framework.authtoken.views import ObtainAuthToken +from rest_framework.response import Response + +import cotisations.models as cotisations +from re2o.utils import all_active_interfaces, all_has_access +from . import serializers +from api.pagination import PageSizedPagination +from api.permissions import ACLPermission + + +# COTISATIONS + + +class FactureViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `cotisations.models.Facture` objects. + """ + queryset = cotisations.Facture.objects.all() + serializer_class = serializers.FactureSerializer + +class FactureViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `cotisations.models.Facture` objects. + """ + queryset = cotisations.BaseInvoice.objects.all() + serializer_class = serializers.BaseInvoiceSerializer + + +class VenteViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `cotisations.models.Vente` objects. + """ + queryset = cotisations.Vente.objects.all() + serializer_class = serializers.VenteSerializer + + +class ArticleViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `cotisations.models.Article` objects. + """ + queryset = cotisations.Article.objects.all() + serializer_class = serializers.ArticleSerializer + + +class BanqueViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `cotisations.models.Banque` objects. + """ + queryset = cotisations.Banque.objects.all() + serializer_class = serializers.BanqueSerializer + + +class PaiementViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `cotisations.models.Paiement` objects. + """ + queryset = cotisations.Paiement.objects.all() + serializer_class = serializers.PaiementSerializer + + +class CotisationViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `cotisations.models.Cotisation` objects. + """ + queryset = cotisations.Cotisation.objects.all() + serializer_class = serializers.CotisationSerializer diff --git a/machines/api/__init__.py b/machines/api/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/machines/api/serializers.py b/machines/api/serializers.py new file mode 100644 index 00000000..5c42f43a --- /dev/null +++ b/machines/api/serializers.py @@ -0,0 +1,245 @@ +# -*- mode: python; coding: utf-8 -*- +# Re2o est un logiciel d'administration développé initiallement au rezometz. Il +# se veut agnostique au réseau considéré, de manière à être installable en +# quelques clics. +# +# Copyright © 2019 Arthur Grisel-Davy +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +from rest_framework import serializers + +from re2o.serializers import NamespacedHRField, NamespacedHIField, NamespacedHMSerializer + +import machines.models as machines + +class MachineSerializer(NamespacedHMSerializer): + """Serialize `machines.models.Machine` objects. + """ + + class Meta: + model = machines.Machine + fields = ('user', 'name', 'active', 'api_url') + + +class MachineTypeSerializer(NamespacedHMSerializer): + """Serialize `machines.models.MachineType` objects. + """ + + class Meta: + model = machines.MachineType + fields = ('type', 'ip_type', 'api_url') + + +class IpTypeSerializer(NamespacedHMSerializer): + """Serialize `machines.models.IpType` objects. + """ + + class Meta: + model = machines.IpType + fields = ('type', 'extension', 'need_infra', 'domaine_ip_start', + 'domaine_ip_stop', 'prefix_v6', 'vlan', 'ouverture_ports', + 'api_url') + + +class VlanSerializer(NamespacedHMSerializer): + """Serialize `machines.models.Vlan` objects. + """ + + class Meta: + model = machines.Vlan + fields = ('vlan_id', 'name', 'comment', 'arp_protect', 'dhcp_snooping', + 'dhcpv6_snooping', 'igmp', 'mld', 'api_url') + + +class NasSerializer(NamespacedHMSerializer): + """Serialize `machines.models.Nas` objects. + """ + + class Meta: + model = machines.Nas + fields = ('name', 'nas_type', 'machine_type', 'port_access_mode', + 'autocapture_mac', 'api_url') + + +class SOASerializer(NamespacedHMSerializer): + """Serialize `machines.models.SOA` objects. + """ + + class Meta: + model = machines.SOA + fields = ('name', 'mail', 'refresh', 'retry', 'expire', 'ttl', + 'api_url') + + +class ExtensionSerializer(NamespacedHMSerializer): + """Serialize `machines.models.Extension` objects. + """ + + class Meta: + model = machines.Extension + fields = ('name', 'need_infra', 'origin', 'origin_v6', 'soa', + 'api_url') + + +class MxSerializer(NamespacedHMSerializer): + """Serialize `machines.models.Mx` objects. + """ + + class Meta: + model = machines.Mx + fields = ('zone', 'priority', 'name', 'api_url') + + +class DNameSerializer(NamespacedHMSerializer): + """Serialize `machines.models.DName` objects. + """ + + class Meta: + model = machines.DName + fields = ('zone', 'alias', 'api_url') + + +class NsSerializer(NamespacedHMSerializer): + """Serialize `machines.models.Ns` objects. + """ + + class Meta: + model = machines.Ns + fields = ('zone', 'ns', 'api_url') + + +class TxtSerializer(NamespacedHMSerializer): + """Serialize `machines.models.Txt` objects. + """ + + class Meta: + model = machines.Txt + fields = ('zone', 'field1', 'field2', 'api_url') + + +class SrvSerializer(NamespacedHMSerializer): + """Serialize `machines.models.Srv` objects. + """ + + class Meta: + model = machines.Srv + fields = ('service', 'protocole', 'extension', 'ttl', 'priority', + 'weight', 'port', 'target', 'api_url') + + +class SshFpSerializer(NamespacedHMSerializer): + """Serialize `machines.models.SSHFP` objects. + """ + + class Meta: + model = machines.SshFp + field = ('machine', 'pub_key_entry', 'algo', 'comment', 'api_url') + + +class InterfaceSerializer(NamespacedHMSerializer): + """Serialize `machines.models.Interface` objects. + """ + mac_address = serializers.CharField() + active = serializers.BooleanField(source='is_active') + + class Meta: + model = machines.Interface + fields = ('ipv4', 'mac_address', 'machine', 'type', 'details', + 'port_lists', 'active', 'api_url') + + +class Ipv6ListSerializer(NamespacedHMSerializer): + """Serialize `machines.models.Ipv6List` objects. + """ + + class Meta: + model = machines.Ipv6List + fields = ('ipv6', 'interface', 'slaac_ip', 'api_url') + + +class DomainSerializer(NamespacedHMSerializer): + """Serialize `machines.models.Domain` objects. + """ + + class Meta: + model = machines.Domain + fields = ('interface_parent', 'name', 'extension', 'cname', + 'api_url') + + +class IpListSerializer(NamespacedHMSerializer): + """Serialize `machines.models.IpList` objects. + """ + + class Meta: + model = machines.IpList + fields = ('ipv4', 'ip_type', 'need_infra', 'api_url') + + +class ServiceSerializer(NamespacedHMSerializer): + """Serialize `machines.models.Service` objects. + """ + + class Meta: + model = machines.Service + fields = ('service_type', 'min_time_regen', 'regular_time_regen', + 'servers', 'api_url') + + +class ServiceLinkSerializer(NamespacedHMSerializer): + """Serialize `machines.models.Service_link` objects. + """ + + class Meta: + model = machines.Service_link + fields = ('service', 'server', 'last_regen', 'asked_regen', + 'need_regen', 'api_url') + extra_kwargs = { + 'api_url': {'view_name': 'servicelink-detail'} + } + + +class OuverturePortListSerializer(NamespacedHMSerializer): + """Serialize `machines.models.OuverturePortList` objects. + """ + tcp_ports_in = NamespacedHRField(view_name='ouvertureport-detail', many=True, read_only=True) + udp_ports_in = NamespacedHRField(view_name='ouvertureport-detail', many=True, read_only=True) + tcp_ports_out = NamespacedHRField(view_name='ouvertureport-detail', many=True, read_only=True) + udp_ports_out = NamespacedHRField(view_name='ouvertureport-detail', many=True, read_only=True) + + class Meta: + model = machines.OuverturePortList + fields = ('name', 'tcp_ports_in', 'udp_ports_in', 'tcp_ports_out', + 'udp_ports_out', 'api_url') + + +class OuverturePortSerializer(NamespacedHMSerializer): + """Serialize `machines.models.OuverturePort` objects. + """ + + class Meta: + model = machines.OuverturePort + fields = ('begin', 'end', 'port_list', 'protocole', 'io', 'api_url') + + +class RoleSerializer(NamespacedHMSerializer): + """Serialize `machines.models.OuverturePort` objects. + """ + servers = InterfaceSerializer(read_only=True, many=True) + + class Meta: + model = machines.Role + fields = ('role_type', 'servers', 'api_url') diff --git a/machines/api/urls.py b/machines/api/urls.py new file mode 100644 index 00000000..da4294cc --- /dev/null +++ b/machines/api/urls.py @@ -0,0 +1,48 @@ +# -*- mode: python; coding: utf-8 -*- +# Re2o est un logiciel d'administration développé initiallement au rezometz. Il +# se veut agnostique au réseau considéré, de manière à être installable en +# quelques clics. +# +# Copyright © 2019 Arthur Grisel-Davy +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +from django.conf.urls import url, include +from . import views +from api.routers import AllViewsRouter + +def add_to_router(router): + router.register_viewset(r'machines/machine', views.MachineViewSet) + router.register_viewset(r'machines/machinetype', views.MachineTypeViewSet) + router.register_viewset(r'machines/iptype', views.IpTypeViewSet) + router.register_viewset(r'machines/vlan', views.VlanViewSet) + router.register_viewset(r'machines/nas', views.NasViewSet) + router.register_viewset(r'machines/soa', views.SOAViewSet) + router.register_viewset(r'machines/extension', views.ExtensionViewSet) + router.register_viewset(r'machines/mx', views.MxViewSet) + router.register_viewset(r'machines/ns', views.NsViewSet) + router.register_viewset(r'machines/txt', views.TxtViewSet) + router.register_viewset(r'machines/dname', views.DNameViewSet) + router.register_viewset(r'machines/srv', views.SrvViewSet) + router.register_viewset(r'machines/sshfp', views.SshFpViewSet) + router.register_viewset(r'machines/interface', views.InterfaceViewSet) + router.register_viewset(r'machines/ipv6list', views.Ipv6ListViewSet) + router.register_viewset(r'machines/domain', views.DomainViewSet) + router.register_viewset(r'machines/iplist', views.IpListViewSet) + router.register_viewset(r'machines/service', views.ServiceViewSet) + router.register_viewset(r'machines/servicelink', views.ServiceLinkViewSet, base_name='servicelink') + router.register_viewset(r'machines/ouvertureportlist', views.OuverturePortListViewSet) + router.register_viewset(r'machines/ouvertureport', views.OuverturePortViewSet) + router.register_viewset(r'machines/role', views.RoleViewSet) diff --git a/machines/api/views.py b/machines/api/views.py new file mode 100644 index 00000000..d546be74 --- /dev/null +++ b/machines/api/views.py @@ -0,0 +1,185 @@ +# -*- mode: python; coding: utf-8 -*- +# Re2o est un logiciel d'administration développé initiallement au rezometz. Il +# se veut agnostique au réseau considéré, de manière à être installable en +# quelques clics. +# +# Copyright © 2019 Arthur Grisel-Davy +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +from rest_framework import viewsets, generics, views +from rest_framework.authtoken.models import Token +from rest_framework.authtoken.views import ObtainAuthToken +from rest_framework.response import Response + +import machines.models as machines + +from . import serializers +from api.pagination import PageSizedPagination +from api.permissions import ACLPermission + +class MachineViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `machines.models.Machine` objects. + """ + queryset = machines.Machine.objects.all() + serializer_class = serializers.MachineSerializer + + +class MachineTypeViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `machines.models.MachineType` objects. + """ + queryset = machines.MachineType.objects.all() + serializer_class = serializers.MachineTypeSerializer + + +class IpTypeViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `machines.models.IpType` objects. + """ + queryset = machines.IpType.objects.all() + serializer_class = serializers.IpTypeSerializer + + +class VlanViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `machines.models.Vlan` objects. + """ + queryset = machines.Vlan.objects.all() + serializer_class = serializers.VlanSerializer + + +class NasViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `machines.models.Nas` objects. + """ + queryset = machines.Nas.objects.all() + serializer_class = serializers.NasSerializer + + +class SOAViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `machines.models.SOA` objects. + """ + queryset = machines.SOA.objects.all() + serializer_class = serializers.SOASerializer + + +class ExtensionViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `machines.models.Extension` objects. + """ + queryset = machines.Extension.objects.all() + serializer_class = serializers.ExtensionSerializer + + +class MxViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `machines.models.Mx` objects. + """ + queryset = machines.Mx.objects.all() + serializer_class = serializers.MxSerializer + + +class NsViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `machines.models.Ns` objects. + """ + queryset = machines.Ns.objects.all() + serializer_class = serializers.NsSerializer + + +class TxtViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `machines.models.Txt` objects. + """ + queryset = machines.Txt.objects.all() + serializer_class = serializers.TxtSerializer + + +class DNameViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `machines.models.DName` objects. + """ + queryset = machines.DName.objects.all() + serializer_class = serializers.DNameSerializer + + +class SrvViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `machines.models.Srv` objects. + """ + queryset = machines.Srv.objects.all() + serializer_class = serializers.SrvSerializer + + +class SshFpViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `machines.models.SshFp` objects. + """ + queryset = machines.SshFp.objects.all() + serializer_class = serializers.SshFpSerializer + + +class InterfaceViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `machines.models.Interface` objects. + """ + queryset = machines.Interface.objects.all() + serializer_class = serializers.InterfaceSerializer + + +class Ipv6ListViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `machines.models.Ipv6List` objects. + """ + queryset = machines.Ipv6List.objects.all() + serializer_class = serializers.Ipv6ListSerializer + + +class DomainViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `machines.models.Domain` objects. + """ + queryset = machines.Domain.objects.all() + serializer_class = serializers.DomainSerializer + + +class IpListViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `machines.models.IpList` objects. + """ + queryset = machines.IpList.objects.all() + serializer_class = serializers.IpListSerializer + + +class ServiceViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `machines.models.Service` objects. + """ + queryset = machines.Service.objects.all() + serializer_class = serializers.ServiceSerializer + + +class ServiceLinkViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `machines.models.Service_link` objects. + """ + queryset = machines.Service_link.objects.all() + serializer_class = serializers.ServiceLinkSerializer + + +class OuverturePortListViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `machines.models.OuverturePortList` + objects. + """ + queryset = machines.OuverturePortList.objects.all() + serializer_class = serializers.OuverturePortListSerializer + + +class OuverturePortViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `machines.models.OuverturePort` objects. + """ + queryset = machines.OuverturePort.objects.all() + serializer_class = serializers.OuverturePortSerializer + + +class RoleViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `machines.models.Machine` objects. + """ + queryset = machines.Role.objects.all() + serializer_class = serializers.RoleSerializer diff --git a/preferences/api/serializers.py b/preferences/api/serializers.py new file mode 100644 index 00000000..29ea70b7 --- /dev/null +++ b/preferences/api/serializers.py @@ -0,0 +1,145 @@ +# -*- mode: python; coding: utf-8 -*- +# Re2o est un logiciel d'administration développé initiallement au rezometz. Il +# se veut agnostique au réseau considéré, de manière à être installable en +# quelques clics. +# +# Copyright © 2019 Arthur Grisel-Davy +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +"""Defines the serializers of the API +""" + +from rest_framework import serializers + +import preferences.models as preferences + +from re2o.serializers import NamespacedHRField, NamespacedHIField, NamespacedHMSerializer + +from machines.api.serializers import ( + VlanSerializer, + Ipv6ListSerializer, + SOASerializer, + IpListSerializer, + NsSerializer, + MxSerializer, + TxtSerializer, + SrvSerializer, + SshFpSerializer, +) + + +class OptionalUserSerializer(NamespacedHMSerializer): + """Serialize `preferences.models.OptionalUser` objects. + """ + tel_mandatory = serializers.BooleanField(source='is_tel_mandatory') + shell_default = serializers.StringRelatedField() + + class Meta: + model = preferences.OptionalUser + fields = ('tel_mandatory', 'gpg_fingerprint', + 'all_can_create_club', 'self_adhesion', 'shell_default', + 'self_change_shell', 'local_email_accounts_enabled', 'local_email_domain', + 'max_email_address', + ) + + +class OptionalMachineSerializer(NamespacedHMSerializer): + """Serialize `preferences.models.OptionalMachine` objects. + """ + + class Meta: + model = preferences.OptionalMachine + fields = ('password_machine', 'max_lambdauser_interfaces', + 'max_lambdauser_aliases', 'ipv6_mode', 'create_machine', + 'ipv6') + + +class OptionalTopologieSerializer(NamespacedHMSerializer): + """Serialize `preferences.models.OptionalTopologie` objects. + """ + switchs_management_interface_ip = serializers.CharField() + + class Meta: + model = preferences.OptionalTopologie + fields = ('switchs_ip_type', 'switchs_web_management', + 'switchs_web_management_ssl', 'switchs_rest_management', + 'switchs_management_utils', 'switchs_management_interface_ip', + 'provision_switchs_enabled', 'switchs_provision', 'switchs_management_sftp_creds') + + +class RadiusOptionSerializer(NamespacedHMSerializer): + """Serialize `preferences.models.RadiusOption` objects + """ + + class Meta: + model = preferences.RadiusOption + fields = ('radius_general_policy', 'unknown_machine', + 'unknown_machine_vlan', 'unknown_port', + 'unknown_port_vlan', 'unknown_room', 'unknown_room_vlan', + 'non_member', 'non_member_vlan', 'banned', 'banned_vlan', + 'vlan_decision_ok') + + +class GeneralOptionSerializer(NamespacedHMSerializer): + """Serialize `preferences.models.GeneralOption` objects. + """ + + class Meta: + model = preferences.GeneralOption + fields = ('general_message_fr', 'general_message_en', + 'search_display_page', 'pagination_number', + 'pagination_large_number', 'req_expire_hrs', + 'site_name', 'main_site_url', 'email_from', + 'GTU_sum_up', 'GTU') + +class HomeServiceSerializer(NamespacedHMSerializer): + """Serialize `preferences.models.Service` objects. + """ + + class Meta: + model = preferences.Service + fields = ('name', 'url', 'description', 'image', 'api_url') + extra_kwargs = { + 'api_url': {'view_name': 'homeservice-detail'} + } + + +class AssoOptionSerializer(NamespacedHMSerializer): + """Serialize `preferences.models.AssoOption` objects. + """ + + class Meta: + model = preferences.AssoOption + fields = ('name', 'siret', 'adresse1', 'adresse2', 'contact', + 'telephone', 'pseudo', 'utilisateur_asso', 'description') + + +class HomeOptionSerializer(NamespacedHMSerializer): + """Serialize `preferences.models.HomeOption` objects. + """ + + class Meta: + model = preferences.HomeOption + fields = ('facebook_url', 'twitter_url', 'twitter_account_name') + + +class MailMessageOptionSerializer(NamespacedHMSerializer): + """Serialize `preferences.models.MailMessageOption` objects. + """ + + class Meta: + model = preferences.MailMessageOption + fields = ('welcome_mail_fr', 'welcome_mail_en') diff --git a/preferences/api/urls.py b/preferences/api/urls.py new file mode 100644 index 00000000..cbf9286a --- /dev/null +++ b/preferences/api/urls.py @@ -0,0 +1,44 @@ +# -*- mode: python; coding: utf-8 -*- +# Re2o est un logiciel d'administration développé initiallement au rezometz. Il +# se veut agnostique au réseau considéré, de manière à être installable en +# quelques clics. +# +# Copyright © 2019 Arthur Grisel-Davy +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +"""Defines the URLs of the API + +A custom router is used to register all the routes. That allows to register +all the URL patterns from the viewsets as a standard router but, the views +can also be register. That way a complete API root page presenting all URLs +can be generated automatically. +""" + +from django.conf.urls import url, include + +from . import views +from api.routers import AllViewsRouter + +def add_to_router(router): + router.register_view(r'preferences/optionaluser', views.OptionalUserView), + router.register_view(r'preferences/optionalmachine', views.OptionalMachineView), + router.register_view(r'preferences/optionaltopologie', views.OptionalTopologieView), + router.register_view(r'preferences/radiusoption', views.RadiusOptionView), + router.register_view(r'preferences/generaloption', views.GeneralOptionView), + router.register_viewset(r'preferences/service', views.HomeServiceViewSet, base_name='homeservice'), + router.register_view(r'preferences/assooption', views.AssoOptionView), + router.register_view(r'preferences/homeoption', views.HomeOptionView), + router.register_view(r'preferences/mailmessageoption', views.MailMessageOptionView), diff --git a/preferences/api/views.py b/preferences/api/views.py new file mode 100644 index 00000000..f21c8827 --- /dev/null +++ b/preferences/api/views.py @@ -0,0 +1,136 @@ +# -*- mode: python; coding: utf-8 -*- +# Re2o est un logiciel d'administration développé initiallement au rezometz. Il +# se veut agnostique au réseau considéré, de manière à être installable en +# quelques clics. +# +# Copyright © 2019 Arthur Grisel-Davy +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +"""Defines the views of the API + +All views inherits the `rest_framework.views.APIview` to respect the +REST API requirements such as dealing with HTTP status code, format of +the response (JSON or other), the CSRF exempting, ... +""" + + +from django.conf import settings +from django.db.models import Q +from rest_framework import viewsets, generics, views +from rest_framework.authtoken.models import Token +from rest_framework.authtoken.views import ObtainAuthToken +from rest_framework.response import Response + +import preferences.models as preferences +from re2o.utils import all_active_interfaces, all_has_access +from . import serializers +from api.pagination import PageSizedPagination +from api.permissions import ACLPermission + + +class OptionalUserView(generics.RetrieveAPIView): + """Exposes details of `preferences.models.` settings. + """ + permission_classes = (ACLPermission,) + perms_map = {'GET': [preferences.OptionalUser.can_view_all]} + serializer_class = serializers.OptionalUserSerializer + + def get_object(self): + return preferences.OptionalUser.objects.first() + + +class OptionalMachineView(generics.RetrieveAPIView): + """Exposes details of `preferences.models.OptionalMachine` settings. + """ + permission_classes = (ACLPermission,) + perms_map = {'GET': [preferences.OptionalMachine.can_view_all]} + serializer_class = serializers.OptionalMachineSerializer + + def get_object(self): + return preferences.OptionalMachine.objects.first() + + +class OptionalTopologieView(generics.RetrieveAPIView): + """Exposes details of `preferences.models.OptionalTopologie` settings. + """ + permission_classes = (ACLPermission,) + perms_map = {'GET': [preferences.OptionalTopologie.can_view_all]} + serializer_class = serializers.OptionalTopologieSerializer + + def get_object(self): + return preferences.OptionalTopologie.objects.first() + + +class RadiusOptionView(generics.RetrieveAPIView): + """Exposes details of `preferences.models.OptionalTopologie` settings. + """ + permission_classes = (ACLPermission,) + perms_map = {'GET': [preferences.RadiusOption.can_view_all]} + serializer_class = serializers.RadiusOptionSerializer + + def get_object(self): + return preferences.RadiusOption.objects.first() + + +class GeneralOptionView(generics.RetrieveAPIView): + """Exposes details of `preferences.models.GeneralOption` settings. + """ + permission_classes = (ACLPermission,) + perms_map = {'GET': [preferences.GeneralOption.can_view_all]} + serializer_class = serializers.GeneralOptionSerializer + + def get_object(self): + return preferences.GeneralOption.objects.first() + + +class HomeServiceViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `preferences.models.Service` objects. + """ + queryset = preferences.Service.objects.all() + serializer_class = serializers.HomeServiceSerializer + + +class AssoOptionView(generics.RetrieveAPIView): + """Exposes details of `preferences.models.AssoOption` settings. + """ + permission_classes = (ACLPermission,) + perms_map = {'GET': [preferences.AssoOption.can_view_all]} + serializer_class = serializers.AssoOptionSerializer + + def get_object(self): + return preferences.AssoOption.objects.first() + + +class HomeOptionView(generics.RetrieveAPIView): + """Exposes details of `preferences.models.HomeOption` settings. + """ + permission_classes = (ACLPermission,) + perms_map = {'GET': [preferences.HomeOption.can_view_all]} + serializer_class = serializers.HomeOptionSerializer + + def get_object(self): + return preferences.HomeOption.objects.first() + + +class MailMessageOptionView(generics.RetrieveAPIView): + """Exposes details of `preferences.models.MailMessageOption` settings. + """ + permission_classes = (ACLPermission,) + perms_map = {'GET': [preferences.MailMessageOption.can_view_all]} + serializer_class = serializers.MailMessageOptionSerializer + + def get_object(self): + return preferences.MailMessageOption.objects.first() diff --git a/re2o/serializers.py b/re2o/serializers.py new file mode 100644 index 00000000..5931d1d8 --- /dev/null +++ b/re2o/serializers.py @@ -0,0 +1,60 @@ +# -*- mode: python; coding: utf-8 -*- +# Re2o est un logiciel d'administration développé initiallement au rezometz. Il +# se veut agnostique au réseau considéré, de manière à être installable en +# quelques clics. +# +# Copyright © 2018 Maël Kervella +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +"""Defines the serializers of the API +""" + +from rest_framework import serializers + + +# The namespace used for the API. It must match the namespace used in the +# urlpatterns to include the API URLs. +API_NAMESPACE = 'api' + +class NamespacedHRField(serializers.HyperlinkedRelatedField): + """A `rest_framework.serializers.HyperlinkedRelatedField` subclass to + automatically prefix view names with the API namespace. + """ + + def __init__(self, view_name=None, **kwargs): + if view_name is not None: + view_name = '%s:%s' % (API_NAMESPACE, view_name) + super(NamespacedHRField, self).__init__(view_name=view_name, **kwargs) + + +class NamespacedHIField(serializers.HyperlinkedIdentityField): + """A `rest_framework.serializers.HyperlinkedIdentityField` subclass to + automatically prefix view names with teh API namespace. + """ + + def __init__(self, view_name=None, **kwargs): + if view_name is not None: + view_name = '%s:%s' % (API_NAMESPACE, view_name) + super(NamespacedHIField, self).__init__(view_name=view_name, **kwargs) + + +class NamespacedHMSerializer(serializers.HyperlinkedModelSerializer): + """A `rest_framework.serializers.HyperlinkedModelSerializer` subclass to + automatically prefix view names with the API namespace. + """ + serializer_related_field = NamespacedHRField + serializer_url_field = NamespacedHIField + diff --git a/topologie/api/serializers.py b/topologie/api/serializers.py new file mode 100644 index 00000000..3a9f90bc --- /dev/null +++ b/topologie/api/serializers.py @@ -0,0 +1,236 @@ +# -*- mode: python; coding: utf-8 -*- +# Re2o est un logiciel d'administration développé initiallement au rezometz. Il +# se veut agnostique au réseau considéré, de manière à être installable en +# quelques clics. +# +# Copyright © 2019 Arthur Grisel-Davy +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +"""Defines the serializers of the API +""" + +from rest_framework import serializers + +import topologie.models as topologie + +from re2o.serializers import NamespacedHRField, NamespacedHIField, NamespacedHMSerializer + +from machines.api.serializer import VlanSerializer + +class StackSerializer(NamespacedHMSerializer): + """Serialize `topologie.models.Stack` objects + """ + + class Meta: + model = topologie.Stack + fields = ('name', 'stack_id', 'details', 'member_id_min', + 'member_id_max', 'api_url') + + +class AccessPointSerializer(NamespacedHMSerializer): + """Serialize `topologie.models.AccessPoint` objects + """ + + class Meta: + model = topologie.AccessPoint + fields = ('user', 'name', 'active', 'location', 'api_url') + + +class SwitchSerializer(NamespacedHMSerializer): + """Serialize `topologie.models.Switch` objects + """ + port_amount = serializers.IntegerField(source='number') + + class Meta: + model = topologie.Switch + fields = ('user', 'name', 'active', 'port_amount', 'stack', + 'stack_member_id', 'model', 'switchbay', 'api_url') + + +class ServerSerializer(NamespacedHMSerializer): + """Serialize `topologie.models.Server` objects + """ + + class Meta: + model = topologie.Server + fields = ('user', 'name', 'active', 'api_url') + + +class ModelSwitchSerializer(NamespacedHMSerializer): + """Serialize `topologie.models.ModelSwitch` objects + """ + + class Meta: + model = topologie.ModelSwitch + fields = ('reference', 'constructor', 'api_url') + + +class ConstructorSwitchSerializer(NamespacedHMSerializer): + """Serialize `topologie.models.ConstructorSwitch` objects + """ + + class Meta: + model = topologie.ConstructorSwitch + fields = ('name', 'api_url') + + +class SwitchBaySerializer(NamespacedHMSerializer): + """Serialize `topologie.models.SwitchBay` objects + """ + + class Meta: + model = topologie.SwitchBay + fields = ('name', 'building', 'info', 'api_url') + + +class BuildingSerializer(NamespacedHMSerializer): + """Serialize `topologie.models.Building` objects + """ + + class Meta: + model = topologie.Building + fields = ('name', 'api_url') + + +class SwitchPortSerializer(NamespacedHMSerializer): + """Serialize `topologie.models.Port` objects + """ + + get_port_profile = NamespacedHIField(view_name='portprofile-detail', read_only=True) + + class Meta: + model = topologie.Port + fields = ('switch', 'port', 'room', 'machine_interface', 'related', + 'custom_profile', 'state', 'get_port_profile', 'details', 'api_url') + extra_kwargs = { + 'related': {'view_name': 'switchport-detail'}, + 'api_url': {'view_name': 'switchport-detail'}, + } + + +class PortProfileSerializer(NamespacedHMSerializer): + """Serialize `topologie.models.Room` objects + """ + class Meta: + model = topologie.PortProfile + fields = ('name', 'profil_default', 'vlan_untagged', 'vlan_tagged', + 'radius_type', 'radius_mode', 'speed', 'mac_limit', 'flow_control', + 'dhcp_snooping', 'dhcpv6_snooping', 'dhcpv6_snooping', 'arp_protect', + 'ra_guard', 'loop_protect', 'api_url') + + +class RoomSerializer(NamespacedHMSerializer): + """Serialize `topologie.models.Room` objects + """ + + class Meta: + model = topologie.Room + fields = ('name', 'details', 'api_url') + + +class PortProfileSerializer(NamespacedHMSerializer): + vlan_untagged = VlanSerializer(read_only=True) + + class Meta: + model = topologie.PortProfile + fields = ('name', 'profil_default', 'vlan_untagged', 'vlan_tagged', + 'radius_type', 'radius_mode', 'speed', 'mac_limit', + 'flow_control', 'dhcp_snooping', 'dhcpv6_snooping', + 'arp_protect', 'ra_guard', 'loop_protect', 'vlan_untagged', + 'vlan_tagged') + +class InterfaceVlanSerializer(NamespacedHMSerializer): + domain = serializers.CharField(read_only=True) + ipv4 = serializers.CharField(read_only=True) + ipv6 = Ipv6ListSerializer(read_only=True, many=True) + vlan_id = serializers.IntegerField(source='type.ip_type.vlan.vlan_id', read_only=True) + + class Meta: + model = machines.Interface + fields = ('ipv4', 'ipv6', 'domain', 'vlan_id') + +class InterfaceRoleSerializer(NamespacedHMSerializer): + interface = InterfaceVlanSerializer(source='machine.interface_set', read_only=True, many=True) + + class Meta: + model = machines.Interface + fields = ('interface',) + + +class RoleSerializer(NamespacedHMSerializer): + """Serialize `machines.models.OuverturePort` objects. + """ + servers = InterfaceRoleSerializer(read_only=True, many=True) + + class Meta: + model = machines.Role + fields = ('role_type', 'servers', 'specific_role') + + +class VlanPortSerializer(NamespacedHMSerializer): + class Meta: + model = machines.Vlan + fields = ('vlan_id', 'name') + + +class ProfilSerializer(NamespacedHMSerializer): + vlan_untagged = VlanSerializer(read_only=True) + vlan_tagged = VlanPortSerializer(read_only=True, many=True) + + class Meta: + model = topologie.PortProfile + fields = ('name', 'profil_default', 'vlan_untagged', 'vlan_tagged', 'radius_type', 'radius_mode', 'speed', 'mac_limit', 'flow_control', 'dhcp_snooping', 'dhcpv6_snooping', 'arp_protect', 'ra_guard', 'loop_protect', 'vlan_untagged', 'vlan_tagged') + + +class ModelSwitchSerializer(NamespacedHMSerializer): + constructor = serializers.CharField(read_only=True) + + class Meta: + model = topologie.ModelSwitch + fields = ('reference', 'firmware', 'constructor') + + +class SwitchBaySerializer(NamespacedHMSerializer): + class Meta: + model = topologie.SwitchBay + fields = ('name',) + + +class PortsSerializer(NamespacedHMSerializer): + """Serialize `machines.models.Ipv6List` objects. + """ + get_port_profile = ProfilSerializer(read_only=True) + + + class Meta: + model = topologie.Port + fields = ('state', 'port', 'pretty_name', 'get_port_profile') + + + +class SwitchPortSerializer(serializers.ModelSerializer): + """Serialize the data about the switches""" + ports = PortsSerializer(many=True, read_only=True) + model = ModelSwitchSerializer(read_only=True) + switchbay = SwitchBaySerializer(read_only=True) + + + class Meta: + model = topologie.Switch + fields = ('short_name', 'model', 'switchbay', 'ports', 'ipv4', 'ipv6', + 'interfaces_subnet', 'interfaces6_subnet', 'automatic_provision', 'rest_enabled', + 'web_management_enabled', 'get_radius_key_value', 'get_management_cred_value', + 'list_modules') diff --git a/topologie/api/urls.py b/topologie/api/urls.py new file mode 100644 index 00000000..1aabbc13 --- /dev/null +++ b/topologie/api/urls.py @@ -0,0 +1,47 @@ +# -*- mode: python; coding: utf-8 -*- +# Re2o est un logiciel d'administration développé initiallement au rezometz. Il +# se veut agnostique au réseau considéré, de manière à être installable en +# quelques clics. +# +# Copyright © 2019 Arthur Grisel-Davy +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +"""Defines the URLs of the API + +A custom router is used to register all the routes. That allows to register +all the URL patterns from the viewsets as a standard router but, the views +can also be register. That way a complete API root page presenting all URLs +can be generated automatically. +""" + +from django.conf.urls import url, include + +from . import views +from api.routers import AllViewsRouter + +def add_to_router(router): + router.register_viewset(r'topologie/stack', views.StackViewSet) + router.register_viewset(r'topologie/acesspoint', views.AccessPointViewSet) + router.register_viewset(r'topologie/switch', views.SwitchViewSet) + router.register_viewset(r'topologie/server', views.ServerViewSet) + router.register_viewset(r'topologie/modelswitch', views.ModelSwitchViewSet) + router.register_viewset(r'topologie/constructorswitch', views.ConstructorSwitchViewSet) + router.register_viewset(r'topologie/switchbay', views.SwitchBayViewSet) + router.register_viewset(r'topologie/building', views.BuildingViewSet) + router.register_viewset(r'topologie/switchport', views.SwitchPortViewSet, base_name='switchport') + router.register_viewset(r'topologie/portprofile', views.PortProfileViewSet, base_name='portprofile') + router.register_viewset(r'topologie/room', views.RoomViewSet) + router.register(r'topologie/portprofile', views.PortProfileViewSet) diff --git a/topologie/api/views.py b/topologie/api/views.py new file mode 100644 index 00000000..fb454afd --- /dev/null +++ b/topologie/api/views.py @@ -0,0 +1,126 @@ +# -*- mode: python; coding: utf-8 -*- +# Re2o est un logiciel d'administration développé initiallement au rezometz. Il +# se veut agnostique au réseau considéré, de manière à être installable en +# quelques clics. +# +# Copyright © 2019 Arthur Grisel-Davy +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +"""Defines the views of the API + +All views inherits the `rest_framework.views.APIview` to respect the +REST API requirements such as dealing with HTTP status code, format of +the response (JSON or other), the CSRF exempting, ... +""" + +import datetime + +from django.conf import settings +from django.db.models import Q +from rest_framework import viewsets, generics, views +from rest_framework.authtoken.models import Token +from rest_framework.authtoken.views import ObtainAuthToken +from rest_framework.response import Response + +import topologie.models as topologie + +from . import serializers +from api.pagination import PageSizedPagination +from api.permissions import ACLPermission + +class StackViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `topologie.models.Stack` objects. + """ + queryset = topologie.Stack.objects.all() + serializer_class = serializers.StackSerializer + + +class AccessPointViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `topologie.models.AccessPoint` objects. + """ + queryset = topologie.AccessPoint.objects.all() + serializer_class = serializers.AccessPointSerializer + + +class SwitchViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `topologie.models.Switch` objects. + """ + queryset = topologie.Switch.objects.all() + serializer_class = serializers.SwitchSerializer + + +class ServerViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `topologie.models.Server` objects. + """ + queryset = topologie.Server.objects.all() + serializer_class = serializers.ServerSerializer + + +class ModelSwitchViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `topologie.models.ModelSwitch` objects. + """ + queryset = topologie.ModelSwitch.objects.all() + serializer_class = serializers.ModelSwitchSerializer + + +class ConstructorSwitchViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `topologie.models.ConstructorSwitch` + objects. + """ + queryset = topologie.ConstructorSwitch.objects.all() + serializer_class = serializers.ConstructorSwitchSerializer + + +class SwitchBayViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `topologie.models.SwitchBay` objects. + """ + queryset = topologie.SwitchBay.objects.all() + serializer_class = serializers.SwitchBaySerializer + + +class BuildingViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `topologie.models.Building` objects. + """ + queryset = topologie.Building.objects.all() + serializer_class = serializers.BuildingSerializer + + +class SwitchPortViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `topologie.models.Port` objects. + """ + queryset = topologie.Port.objects.all() + serializer_class = serializers.SwitchPortSerializer + + +class PortProfileViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `topologie.models.PortProfile` objects. + """ + queryset = topologie.PortProfile.objects.all() + serializer_class = serializers.PortProfileSerializer + + +class RoomViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `topologie.models.Room` objects. + """ + queryset = topologie.Room.objects.all() + serializer_class = serializers.RoomSerializer + + +class PortProfileViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `topologie.models.PortProfile` objects. + """ + queryset = topologie.PortProfile.objects.all() + serializer_class = serializers.PortProfileSerializer diff --git a/users/api/serializers.py b/users/api/serializers.py new file mode 100644 index 00000000..67252643 --- /dev/null +++ b/users/api/serializers.py @@ -0,0 +1,169 @@ +# -*- mode: python; coding: utf-8 -*- +# Re2o est un logiciel d'administration développé initiallement au rezometz. Il +# se veut agnostique au réseau considéré, de manière à être installable en +# quelques clics. +# +# Copyright © 2018 Maël Kervella +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +"""Defines the serializers of the API +For app Users +""" + +import datetime + +from rest_framework import serializers + +import users.models as users + +from re2o.serializers import NamespacedHRField, NamespacedHIField, NamespacedHMSerializer + +# USERS + + +class UserSerializer(NamespacedHMSerializer): + """Serialize `users.models.User` objects. + """ + access = serializers.BooleanField(source='has_access') + uid = serializers.IntegerField(source='uid_number') + + class Meta: + model = users.User + fields = ('surname', 'pseudo', 'email', 'local_email_redirect', + 'local_email_enabled', 'school', 'shell', 'comment', + 'state', 'registered', 'telephone', 'solde', 'access', + 'end_access', 'uid', 'class_name', 'api_url') + extra_kwargs = { + 'shell': {'view_name': 'shell-detail'} + } + + +class ClubSerializer(NamespacedHMSerializer): + """Serialize `users.models.Club` objects. + """ + name = serializers.CharField(source='surname') + access = serializers.BooleanField(source='has_access') + uid = serializers.IntegerField(source='uid_number') + + class Meta: + model = users.Club + fields = ('name', 'pseudo', 'email', 'local_email_redirect', + 'local_email_enabled', 'school', 'shell', 'comment', + 'state', 'registered', 'telephone', 'solde', 'room', + 'access', 'end_access', 'administrators', 'members', + 'mailing', 'uid', 'api_url') + extra_kwargs = { + 'shell': {'view_name': 'shell-detail'} + } + + +class AdherentSerializer(NamespacedHMSerializer): + """Serialize `users.models.Adherent` objects. + """ + access = serializers.BooleanField(source='has_access') + uid = serializers.IntegerField(source='uid_number') + + class Meta: + model = users.Adherent + fields = ('name', 'surname', 'pseudo', 'email', 'local_email_redirect', + 'local_email_enabled', 'school', 'shell', 'comment', + 'state', 'registered', 'telephone', 'room', 'solde', + 'access', 'end_access', 'uid', 'api_url', 'gid') + extra_kwargs = { + 'shell': {'view_name': 'shell-detail'} + } + + +class BasicUserSerializer(NamespacedHMSerializer): + """Serialize 'users.models.User' minimal infos""" + uid = serializers.IntegerField(source='uid_number') + gid = serializers.IntegerField(source='gid_number') + + class Meta: + model = users.User + fields = ('pseudo', 'uid', 'gid') + + +class ServiceUserSerializer(NamespacedHMSerializer): + """Serialize `users.models.ServiceUser` objects. + """ + + class Meta: + model = users.ServiceUser + fields = ('pseudo', 'access_group', 'comment', 'api_url') + + +class SchoolSerializer(NamespacedHMSerializer): + """Serialize `users.models.School` objects. + """ + + class Meta: + model = users.School + fields = ('name', 'api_url') + + +class ListRightSerializer(NamespacedHMSerializer): + """Serialize `users.models.ListRight` objects. + """ + + class Meta: + model = users.ListRight + fields = ('unix_name', 'gid', 'critical', 'details', 'api_url') + + +class ShellSerializer(NamespacedHMSerializer): + """Serialize `users.models.ListShell` objects. + """ + + class Meta: + model = users.ListShell + fields = ('shell', 'api_url') + extra_kwargs = { + 'api_url': {'view_name': 'shell-detail'} + } + + +class BanSerializer(NamespacedHMSerializer): + """Serialize `users.models.Ban` objects. + """ + active = serializers.BooleanField(source='is_active') + + class Meta: + model = users.Ban + fields = ('user', 'raison', 'date_start', 'date_end', 'state', + 'active', 'api_url') + + +class WhitelistSerializer(NamespacedHMSerializer): + """Serialize `users.models.Whitelist` objects. + """ + active = serializers.BooleanField(source='is_active') + + class Meta: + model = users.Whitelist + fields = ('user', 'raison', 'date_start', 'date_end', 'active', 'api_url') + + +class EMailAddressSerializer(NamespacedHMSerializer): + """Serialize `users.models.EMailAddress` objects. + """ + user = serializers.CharField(source='user.pseudo', read_only=True) + + class Meta: + model = users.EMailAddress + fields = ('user', 'local_part', 'complete_email_address', 'api_url') + + diff --git a/users/api/urls.py b/users/api/urls.py new file mode 100644 index 00000000..e5a702fa --- /dev/null +++ b/users/api/urls.py @@ -0,0 +1,48 @@ +# -*- mode: python; coding: utf-8 -*- +# Re2o est un logiciel d'administration développé initiallement au rezometz. Il +# se veut agnostique au réseau considéré, de manière à être installable en +# quelques clics. +# +# Copyright © 2018 Maël Kervella +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +"""Defines the URLs of the API User + +A custom router is used to register all the routes. That allows to register +all the URL patterns from the viewsets as a standard router but, the views +can also be register. That way a complete API root page presenting all URLs +can be generated automatically. +""" + +from django.conf.urls import url, include + +from . import views +from api.routers import AllViewsRouter + +def add_to_router(router): + router.register_viewset(r'users/user', views.UserViewSet, base_name='user') + router.register_viewset(r'users/homecreation', views.HomeCreationViewSet, base_name='homecreation') + router.register_viewset(r'users/normaluser', views.NormalUserViewSet, base_name='normaluser') + router.register_viewset(r'users/criticaluser', views.CriticalUserViewSet, base_name='criticaluser') + router.register_viewset(r'users/club', views.ClubViewSet) + router.register_viewset(r'users/adherent', views.AdherentViewSet) + router.register_viewset(r'users/serviceuser', views.ServiceUserViewSet) + router.register_viewset(r'users/school', views.SchoolViewSet) + router.register_viewset(r'users/listright', views.ListRightViewSet) + router.register_viewset(r'users/shell', views.ShellViewSet, base_name='shell') + router.register_viewset(r'users/ban', views.BanViewSet) + router.register_viewset(r'users/whitelist', views.WhitelistViewSet) + router.register_viewset(r'users/emailaddress', views.EMailAddressViewSet) diff --git a/users/api/views.py b/users/api/views.py new file mode 100644 index 00000000..40fc359e --- /dev/null +++ b/users/api/views.py @@ -0,0 +1,145 @@ +# -*- mode: python; coding: utf-8 -*- +# Re2o est un logiciel d'administration développé initiallement au rezometz. Il +# se veut agnostique au réseau considéré, de manière à être installable en +# quelques clics. +# +# Copyright © 2018 Maël Kervella +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +"""Defines the views of the API + +All views inherits the `rest_framework.views.APIview` to respect the +REST API requirements such as dealing with HTTP status code, format of +the response (JSON or other), the CSRF exempting, ... +""" + +import datetime + +from django.conf import settings +from django.db.models import Q +from rest_framework import viewsets, generics, views +from rest_framework.authtoken.models import Token +from rest_framework.authtoken.views import ObtainAuthToken +from rest_framework.response import Response + +import users.models as users + +from preferences.models import OptionalUser + +from . import serializers +from api.pagination import PageSizedPagination +from api.permissions import ACLPermission + +# USER + + +class UserViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `users.models.Users` objects. + """ + queryset = users.User.objects.all() + serializer_class = serializers.UserSerializer + + +class HomeCreationViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes infos of `users.models.Users` objects to create homes. + """ + queryset = users.User.objects.exclude(Q(state=users.User.STATE_DISABLED) | Q(state=users.User.STATE_NOT_YET_ACTIVE)) + serializer_class = serializers.BasicUserSerializer + + +class NormalUserViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes infos of `users.models.Users`without specific rights objects.""" + queryset = users.User.objects.exclude(groups__listright__critical=True).distinct() + serializer_class = serializers.BasicUserSerializer + + +class CriticalUserViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes infos of `users.models.Users`without specific rights objects.""" + queryset = users.User.objects.filter(groups__listright__critical=True).distinct() + serializer_class = serializers.BasicUserSerializer + +class ClubViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `users.models.Club` objects. + """ + queryset = users.Club.objects.all() + serializer_class = serializers.ClubSerializer + + +class AdherentViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `users.models.Adherent` objects. + """ + queryset = users.Adherent.objects.all() + serializer_class = serializers.AdherentSerializer + + +class ServiceUserViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `users.models.ServiceUser` objects. + """ + queryset = users.ServiceUser.objects.all() + serializer_class = serializers.ServiceUserSerializer + + +class SchoolViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `users.models.School` objects. + """ + queryset = users.School.objects.all() + serializer_class = serializers.SchoolSerializer + + +class ListRightViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `users.models.ListRight` objects. + """ + queryset = users.ListRight.objects.all() + serializer_class = serializers.ListRightSerializer + + +class ShellViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `users.models.ListShell` objects. + """ + queryset = users.ListShell.objects.all() + serializer_class = serializers.ShellSerializer + + +class BanViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `users.models.Ban` objects. + """ + queryset = users.Ban.objects.all() + serializer_class = serializers.BanSerializer + + +class WhitelistViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `users.models.Whitelist` objects. + """ + queryset = users.Whitelist.objects.all() + serializer_class = serializers.WhitelistSerializer + + +class EMailAddressViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `users.models.EMailAddress` objects. + """ + serializer_class = serializers.EMailAddressSerializer + queryset = users.EMailAddress.objects.none() + + def get_queryset(self): + if OptionalUser.get_cached_value( + 'local_email_accounts_enabled'): + return (users.EMailAddress.objects + .filter(user__local_email_enabled=True)) + else: + return users.EMailAddress.objects.none() + + +