8
0
Fork 0
mirror of https://gitlab.federez.net/re2o/re2o synced 2024-05-20 17:42:26 +00:00

Std API for user

This commit is contained in:
Maël Kervella 2018-04-19 22:00:49 +00:00
parent 998d093cac
commit d190ef00ce
5 changed files with 1190 additions and 1116 deletions

View file

@ -23,7 +23,17 @@ Serializers for the API app
"""
from rest_framework import serializers
from users.models import Club, Adherent
from users.models import (
User,
Club,
Adherent,
ServiceUser,
School,
ListRight,
ListShell,
Ban,
Whitelist
)
from machines.models import (
Interface,
IpType,
@ -40,407 +50,524 @@ from machines.models import (
)
class ServiceLinkSerializer(serializers.ModelSerializer):
""" Serializer for the ServiceLink objects """
name = serializers.CharField(source='service.service_type')
class UserSerializer(serializers.HyperlinkedModelSerializer):
access = serializers.BooleanField(source='has_access')
uid = serializers.IntegerField(source='uid_number')
class Meta:
model = Service_link
fields = ('name',)
model = User
fields = ('name', 'pseudo', 'email', 'school', 'shell', 'comment',
'state', 'registered', 'telephone', 'solde', #'room',
'access', 'end_access', 'uid', 'class_name', 'api_url')
extra_kwargs = {
'school': {'view_name': 'api:school-detail'},
'shell': {'view_name': 'api:shell-detail'},
#'room': {'view_name': 'api:room-detail'},
'api_url': {'view_name': 'api:user-detail'}
}
class MailingSerializer(serializers.ModelSerializer):
""" Serializer to build Mailing objects """
name = serializers.CharField(source='pseudo')
class ClubSerializer(serializers.HyperlinkedModelSerializer):
name = serializers.CharField(source='surname')
access = serializers.BooleanField(source='has_access')
uid = serializers.IntegerField(source='uid_number')
class Meta:
model = Club
fields = ('name',)
fields = ('name', 'pseudo', 'email', 'school', 'shell', 'comment',
'state', 'registered', 'telephone', 'solde', #'room',
'access', 'end_access', 'administrators', 'members',
'mailing', 'uid', 'api_url')
extra_kwargs = {
'school': {'view_name': 'api:school-detail'},
'shell': {'view_name': 'api:shell-detail'},
#'room': {'view_name': 'api:room-detail'},
'administrators': {'view_name': 'api:adherent-detail'},
'members': {'view_name': 'api:adherent-detail'},
'api_url': {'view_name': 'api:club-detail'}
}
class MailingMemberSerializer(serializers.ModelSerializer):
""" Serializer fot the Adherent objects (who belong to a
Mailing) """
class AdherentSerializer(serializers.HyperlinkedModelSerializer):
access = serializers.BooleanField(source='has_access')
uid = serializers.IntegerField(source='uid_number')
class Meta:
model = Adherent
fields = ('email',)
class IpTypeField(serializers.RelatedField):
""" Serializer for an IpType object field """
def to_representation(self, value):
return value.type
def to_internal_value(self, data):
pass
class IpListSerializer(serializers.ModelSerializer):
""" Serializer for an Ipv4List obejct using the IpType serialization """
ip_type = IpTypeField(read_only=True)
class Meta:
model = IpList
fields = ('ipv4', 'ip_type')
class Ipv6ListSerializer(serializers.ModelSerializer):
""" Serializer for an Ipv6List object """
class Meta:
model = Ipv6List
fields = ('ipv6', 'slaac_ip')
class InterfaceSerializer(serializers.ModelSerializer):
""" Serializer for an Interface object. Use SerializerMethodField
to get ForeignKey values """
ipv4 = IpListSerializer(read_only=True)
# TODO : use serializer.RelatedField to avoid duplicate code
mac_address = serializers.SerializerMethodField('get_macaddress')
domain = serializers.SerializerMethodField('get_dns')
extension = serializers.SerializerMethodField('get_interface_extension')
class Meta:
model = Interface
fields = ('ipv4', 'mac_address', 'domain', 'extension')
@staticmethod
def get_dns(obj):
""" The name of the associated DNS object """
return obj.domain.name
@staticmethod
def get_interface_extension(obj):
""" The name of the associated Interface object """
return obj.domain.extension.name
@staticmethod
def get_macaddress(obj):
""" The string representation of the associated MAC address """
return str(obj.mac_address)
class FullInterfaceSerializer(serializers.ModelSerializer):
""" Serializer for an Interface obejct. Use SerializerMethodField
to get ForeignKey values """
ipv4 = IpListSerializer(read_only=True)
ipv6 = Ipv6ListSerializer(read_only=True, many=True)
# TODO : use serializer.RelatedField to avoid duplicate code
mac_address = serializers.SerializerMethodField('get_macaddress')
domain = serializers.SerializerMethodField('get_dns')
extension = serializers.SerializerMethodField('get_interface_extension')
class Meta:
model = Interface
fields = ('ipv4', 'ipv6', 'mac_address', 'domain', 'extension')
@staticmethod
def get_dns(obj):
""" The name of the associated DNS object """
return obj.domain.name
@staticmethod
def get_interface_extension(obj):
""" The name of the associated Extension object """
return obj.domain.extension.name
@staticmethod
def get_macaddress(obj):
""" The string representation of the associated MAC address """
return str(obj.mac_address)
class ExtensionNameField(serializers.RelatedField):
""" Serializer for Extension object field """
def to_representation(self, value):
return value.name
def to_internal_value(self, data):
pass
class TypeSerializer(serializers.ModelSerializer):
""" Serializer for an IpType object. Use SerializerMethodField to
get ForeignKey values """
extension = ExtensionNameField(read_only=True)
ouverture_ports_tcp_in = serializers\
.SerializerMethodField('get_port_policy_input_tcp')
ouverture_ports_tcp_out = serializers\
.SerializerMethodField('get_port_policy_output_tcp')
ouverture_ports_udp_in = serializers\
.SerializerMethodField('get_port_policy_input_udp')
ouverture_ports_udp_out = serializers\
.SerializerMethodField('get_port_policy_output_udp')
class Meta:
model = IpType
fields = ('type', 'extension', 'domaine_ip_start', 'domaine_ip_stop',
'prefix_v6',
'ouverture_ports_tcp_in', 'ouverture_ports_tcp_out',
'ouverture_ports_udp_in', 'ouverture_ports_udp_out',)
@staticmethod
def get_port_policy(obj, protocole, io):
""" Generic utility function to get the policy for a given
port, protocole and IN or OUT """
if obj.ouverture_ports is None:
return []
return map(
str,
obj.ouverture_ports.ouvertureport_set.filter(
protocole=protocole
).filter(io=io)
)
def get_port_policy_input_tcp(self, obj):
"""Renvoie la liste des ports ouverts en entrée tcp"""
return self.get_port_policy(obj, OuverturePort.TCP, OuverturePort.IN)
def get_port_policy_output_tcp(self, obj):
"""Renvoie la liste des ports ouverts en sortie tcp"""
return self.get_port_policy(obj, OuverturePort.TCP, OuverturePort.OUT)
def get_port_policy_input_udp(self, obj):
"""Renvoie la liste des ports ouverts en entrée udp"""
return self.get_port_policy(obj, OuverturePort.UDP, OuverturePort.IN)
def get_port_policy_output_udp(self, obj):
"""Renvoie la liste des ports ouverts en sortie udp"""
return self.get_port_policy(obj, OuverturePort.UDP, OuverturePort.OUT)
class ExtensionSerializer(serializers.ModelSerializer):
"""Serialisation d'une extension : origin_ip et la zone sont
des foreign_key donc evalués en get_..."""
origin = serializers.SerializerMethodField('get_origin_ip')
zone_entry = serializers.SerializerMethodField('get_zone_name')
soa = serializers.SerializerMethodField('get_soa_data')
class Meta:
model = Extension
fields = ('name', 'origin', 'origin_v6', 'zone_entry', 'soa')
@staticmethod
def get_origin_ip(obj):
""" The IP of the associated origin for the zone """
return obj.origin.ipv4
@staticmethod
def get_zone_name(obj):
""" The name of the associated zone """
return str(obj.dns_entry)
@staticmethod
def get_soa_data(obj):
""" The representation of the associated SOA """
return {'mail': obj.soa.dns_soa_mail, 'param': obj.soa.dns_soa_param}
class MxSerializer(serializers.ModelSerializer):
"""Serialisation d'un MX, evaluation du nom, de la zone
et du serveur cible, etant des foreign_key"""
name = serializers.SerializerMethodField('get_entry_name')
zone = serializers.SerializerMethodField('get_zone_name')
mx_entry = serializers.SerializerMethodField('get_mx_name')
class Meta:
model = Mx
fields = ('zone', 'priority', 'name', 'mx_entry')
@staticmethod
def get_entry_name(obj):
""" The name of the DNS MX entry """
return str(obj.name)
@staticmethod
def get_zone_name(obj):
""" The name of the associated zone of the MX record """
return obj.zone.name
@staticmethod
def get_mx_name(obj):
""" The string representation of the entry to add to the DNS """
return str(obj.dns_entry)
class TxtSerializer(serializers.ModelSerializer):
"""Serialisation d'un txt : zone cible et l'entrée txt
sont evaluées à part"""
zone = serializers.SerializerMethodField('get_zone_name')
txt_entry = serializers.SerializerMethodField('get_txt_name')
class Meta:
model = Txt
fields = ('zone', 'txt_entry', 'field1', 'field2')
@staticmethod
def get_zone_name(obj):
""" The name of the associated zone """
return str(obj.zone.name)
@staticmethod
def get_txt_name(obj):
""" The string representation of the entry to add to the DNS """
return str(obj.dns_entry)
class SrvSerializer(serializers.ModelSerializer):
"""Serialisation d'un srv : zone cible et l'entrée txt"""
extension = serializers.SerializerMethodField('get_extension_name')
srv_entry = serializers.SerializerMethodField('get_srv_name')
class Meta:
model = Srv
fields = (
'service',
'protocole',
'extension',
'ttl',
'priority',
'weight',
'port',
'target',
'srv_entry'
)
@staticmethod
def get_extension_name(obj):
""" The name of the associated extension """
return str(obj.extension.name)
@staticmethod
def get_srv_name(obj):
""" The string representation of the entry to add to the DNS """
return str(obj.dns_entry)
class NsSerializer(serializers.ModelSerializer):
"""Serialisation d'un NS : la zone, l'entrée ns complète et le serveur
ns sont évalués à part"""
zone = serializers.SerializerMethodField('get_zone_name')
ns = serializers.SerializerMethodField('get_domain_name')
ns_entry = serializers.SerializerMethodField('get_text_name')
class Meta:
model = Ns
fields = ('zone', 'ns', 'ns_entry')
@staticmethod
def get_zone_name(obj):
""" The name of the associated zone """
return obj.zone.name
@staticmethod
def get_domain_name(obj):
""" The name of the associated NS target """
return str(obj.ns)
@staticmethod
def get_text_name(obj):
""" The string representation of the entry to add to the DNS """
return str(obj.dns_entry)
class DomainSerializer(serializers.ModelSerializer):
"""Serialisation d'un domain, extension, cname sont des foreign_key,
et l'entrée complète, sont évalués à part"""
extension = serializers.SerializerMethodField('get_zone_name')
cname = serializers.SerializerMethodField('get_alias_name')
cname_entry = serializers.SerializerMethodField('get_cname_name')
class Meta:
model = Domain
fields = ('name', 'extension', 'cname', 'cname_entry')
@staticmethod
def get_zone_name(obj):
""" The name of the associated zone """
return obj.extension.name
@staticmethod
def get_alias_name(obj):
""" The name of the associated alias """
return str(obj.cname)
@staticmethod
def get_cname_name(obj):
""" The name of the associated CNAME target """
return str(obj.dns_entry)
class ServicesSerializer(serializers.ModelSerializer):
"""Evaluation d'un Service, et serialisation"""
server = serializers.SerializerMethodField('get_server_name')
service = serializers.SerializerMethodField('get_service_name')
need_regen = serializers.SerializerMethodField('get_regen_status')
class Meta:
model = Service_link
fields = ('server', 'service', 'need_regen')
@staticmethod
def get_server_name(obj):
""" The name of the associated server """
return str(obj.server.domain.name)
@staticmethod
def get_service_name(obj):
""" The name of the service name """
return str(obj.service)
@staticmethod
def get_regen_status(obj):
""" The string representation of the regen status """
return obj.need_regen()
class OuverturePortsSerializer(serializers.Serializer):
"""Serialisation de l'ouverture des ports"""
ipv4 = serializers.SerializerMethodField()
ipv6 = serializers.SerializerMethodField()
def create(self, validated_data):
""" Creates a new object based on the un-serialized data.
Used to implement an abstract inherited method """
pass
def update(self, instance, validated_data):
""" Updates an object based on the un-serialized data.
Used to implement an abstract inherited method """
pass
@staticmethod
def get_ipv4():
""" The representation of the policy for the IPv4 addresses """
return {
i.ipv4.ipv4: {
"tcp_in": [j.tcp_ports_in() for j in i.port_lists.all()],
"tcp_out": [j.tcp_ports_out()for j in i.port_lists.all()],
"udp_in": [j.udp_ports_in() for j in i.port_lists.all()],
"udp_out": [j.udp_ports_out() for j in i.port_lists.all()],
}
for i in Interface.objects.all() if i.ipv4
fields = ('name', 'surname', 'pseudo', 'email', 'school', 'shell',
'comment', 'state', 'registered', 'telephone', #'room',
'solde', 'access', 'end_access', 'uid', 'api_url')
extra_kwargs = {
'school': {'view_name': 'api:school-detail'},
'shell': {'view_name': 'api:shell-detail'},
#'room': {'view_name': 'api:room-detail'},
'api_url': {'view_name': 'api:adherent-detail'}
}
@staticmethod
def get_ipv6():
""" The representation of the policy for the IPv6 addresses """
return {
i.ipv6: {
"tcp_in": [j.tcp_ports_in() for j in i.port_lists.all()],
"tcp_out": [j.tcp_ports_out()for j in i.port_lists.all()],
"udp_in": [j.udp_ports_in() for j in i.port_lists.all()],
"udp_out": [j.udp_ports_out() for j in i.port_lists.all()],
}
for i in Interface.objects.all() if i.ipv6
class ServiceUserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = ServiceUser
fields = ('pseudo', 'access_group', 'comment', 'api_url')
extra_kwargs = {
'api_url': {'view_name': 'api:serviceuser-detail'}
}
class SchoolSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = School
fields = ('name', 'api_url')
extra_kwargs = {
'api_url': {'view_name': 'api:school-detail'}
}
class ListRightSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = ListRight
fields = ('unix_name', 'gid', 'critical', 'details', 'api_url')
extra_kwargs = {
'api_url': {'view_name': 'api:listright-detail'}
}
class ShellSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = ListShell
fields = ('shell', 'api_url')
extra_kwargs = {
'api_url': {'view_name': 'api:shell-detail'}
}
class BanSerializer(serializers.HyperlinkedModelSerializer):
active = serializers.BooleanField(source='is_active')
class Meta:
model = Ban
fields = ('user', 'raison', 'date_start', 'date_end', 'state',
'active', 'api_url')
extra_kwargs = {
'user': {'view_name': 'api:user-detail'},
'api_url': {'view_name': 'api:ban-detail'}
}
class WhitelistSerializer(serializers.HyperlinkedModelSerializer):
active = serializers.BooleanField(source='is_active')
class Meta:
model = Whitelist
fields = ('user', 'raison', 'date_start', 'date_end', 'active', 'api_url')
extra_kwargs = {
'user': {'view_name': 'api:user-detail'},
'api_url': {'view_name': 'api:whitelist-detail'}
}
# class ServiceLinkSerializer(serializers.ModelSerializer):
# """ Serializer for the ServiceLink objects """
#
# name = serializers.CharField(source='service.service_type')
#
# class Meta:
# model = Service_link
# fields = ('name',)
#
#
# class MailingSerializer(serializers.ModelSerializer):
# """ Serializer to build Mailing objects """
#
# name = serializers.CharField(source='pseudo')
#
# class Meta:
# model = Club
# fields = ('name',)
#
#
# class MailingMemberSerializer(serializers.ModelSerializer):
# """ Serializer fot the Adherent objects (who belong to a
# Mailing) """
#
# class Meta:
# model = Adherent
# fields = ('email',)
#
#
# class IpTypeField(serializers.RelatedField):
# """ Serializer for an IpType object field """
#
# def to_representation(self, value):
# return value.type
#
# def to_internal_value(self, data):
# pass
#
#
# class IpListSerializer(serializers.ModelSerializer):
# """ Serializer for an Ipv4List obejct using the IpType serialization """
#
# ip_type = IpTypeField(read_only=True)
#
# class Meta:
# model = IpList
# fields = ('ipv4', 'ip_type')
#
#
# class Ipv6ListSerializer(serializers.ModelSerializer):
# """ Serializer for an Ipv6List object """
#
# class Meta:
# model = Ipv6List
# fields = ('ipv6', 'slaac_ip')
#
#
# class InterfaceSerializer(serializers.ModelSerializer):
# """ Serializer for an Interface object. Use SerializerMethodField
# to get ForeignKey values """
#
# ipv4 = IpListSerializer(read_only=True)
# # TODO : use serializer.RelatedField to avoid duplicate code
# mac_address = serializers.SerializerMethodField('get_macaddress')
# domain = serializers.SerializerMethodField('get_dns')
# extension = serializers.SerializerMethodField('get_interface_extension')
#
# class Meta:
# model = Interface
# fields = ('ipv4', 'mac_address', 'domain', 'extension')
#
# @staticmethod
# def get_dns(obj):
# """ The name of the associated DNS object """
# return obj.domain.name
#
# @staticmethod
# def get_interface_extension(obj):
# """ The name of the associated Interface object """
# return obj.domain.extension.name
#
# @staticmethod
# def get_macaddress(obj):
# """ The string representation of the associated MAC address """
# return str(obj.mac_address)
#
#
# class FullInterfaceSerializer(serializers.ModelSerializer):
# """ Serializer for an Interface obejct. Use SerializerMethodField
# to get ForeignKey values """
#
# ipv4 = IpListSerializer(read_only=True)
# ipv6 = Ipv6ListSerializer(read_only=True, many=True)
# # TODO : use serializer.RelatedField to avoid duplicate code
# mac_address = serializers.SerializerMethodField('get_macaddress')
# domain = serializers.SerializerMethodField('get_dns')
# extension = serializers.SerializerMethodField('get_interface_extension')
#
# class Meta:
# model = Interface
# fields = ('ipv4', 'ipv6', 'mac_address', 'domain', 'extension')
#
# @staticmethod
# def get_dns(obj):
# """ The name of the associated DNS object """
# return obj.domain.name
#
# @staticmethod
# def get_interface_extension(obj):
# """ The name of the associated Extension object """
# return obj.domain.extension.name
#
# @staticmethod
# def get_macaddress(obj):
# """ The string representation of the associated MAC address """
# return str(obj.mac_address)
#
#
# class ExtensionNameField(serializers.RelatedField):
# """ Serializer for Extension object field """
#
# def to_representation(self, value):
# return value.name
#
# def to_internal_value(self, data):
# pass
#
#
# class TypeSerializer(serializers.ModelSerializer):
# """ Serializer for an IpType object. Use SerializerMethodField to
# get ForeignKey values """
#
# extension = ExtensionNameField(read_only=True)
# ouverture_ports_tcp_in = serializers\
# .SerializerMethodField('get_port_policy_input_tcp')
# ouverture_ports_tcp_out = serializers\
# .SerializerMethodField('get_port_policy_output_tcp')
# ouverture_ports_udp_in = serializers\
# .SerializerMethodField('get_port_policy_input_udp')
# ouverture_ports_udp_out = serializers\
# .SerializerMethodField('get_port_policy_output_udp')
#
# class Meta:
# model = IpType
# fields = ('type', 'extension', 'domaine_ip_start', 'domaine_ip_stop',
# 'prefix_v6',
# 'ouverture_ports_tcp_in', 'ouverture_ports_tcp_out',
# 'ouverture_ports_udp_in', 'ouverture_ports_udp_out',)
#
# @staticmethod
# def get_port_policy(obj, protocole, io):
# """ Generic utility function to get the policy for a given
# port, protocole and IN or OUT """
# if obj.ouverture_ports is None:
# return []
# return map(
# str,
# obj.ouverture_ports.ouvertureport_set.filter(
# protocole=protocole
# ).filter(io=io)
# )
#
# def get_port_policy_input_tcp(self, obj):
# """Renvoie la liste des ports ouverts en entrée tcp"""
# return self.get_port_policy(obj, OuverturePort.TCP, OuverturePort.IN)
#
# def get_port_policy_output_tcp(self, obj):
# """Renvoie la liste des ports ouverts en sortie tcp"""
# return self.get_port_policy(obj, OuverturePort.TCP, OuverturePort.OUT)
#
# def get_port_policy_input_udp(self, obj):
# """Renvoie la liste des ports ouverts en entrée udp"""
# return self.get_port_policy(obj, OuverturePort.UDP, OuverturePort.IN)
#
# def get_port_policy_output_udp(self, obj):
# """Renvoie la liste des ports ouverts en sortie udp"""
# return self.get_port_policy(obj, OuverturePort.UDP, OuverturePort.OUT)
#
#
# class ExtensionSerializer(serializers.ModelSerializer):
# """Serialisation d'une extension : origin_ip et la zone sont
# des foreign_key donc evalués en get_..."""
# origin = serializers.SerializerMethodField('get_origin_ip')
# zone_entry = serializers.SerializerMethodField('get_zone_name')
# soa = serializers.SerializerMethodField('get_soa_data')
#
# class Meta:
# model = Extension
# fields = ('name', 'origin', 'origin_v6', 'zone_entry', 'soa')
#
# @staticmethod
# def get_origin_ip(obj):
# """ The IP of the associated origin for the zone """
# return obj.origin.ipv4
#
# @staticmethod
# def get_zone_name(obj):
# """ The name of the associated zone """
# return str(obj.dns_entry)
#
# @staticmethod
# def get_soa_data(obj):
# """ The representation of the associated SOA """
# return {'mail': obj.soa.dns_soa_mail, 'param': obj.soa.dns_soa_param}
#
#
# class MxSerializer(serializers.ModelSerializer):
# """Serialisation d'un MX, evaluation du nom, de la zone
# et du serveur cible, etant des foreign_key"""
# name = serializers.SerializerMethodField('get_entry_name')
# zone = serializers.SerializerMethodField('get_zone_name')
# mx_entry = serializers.SerializerMethodField('get_mx_name')
#
# class Meta:
# model = Mx
# fields = ('zone', 'priority', 'name', 'mx_entry')
#
# @staticmethod
# def get_entry_name(obj):
# """ The name of the DNS MX entry """
# return str(obj.name)
#
# @staticmethod
# def get_zone_name(obj):
# """ The name of the associated zone of the MX record """
# return obj.zone.name
#
# @staticmethod
# def get_mx_name(obj):
# """ The string representation of the entry to add to the DNS """
# return str(obj.dns_entry)
#
#
# class TxtSerializer(serializers.ModelSerializer):
# """Serialisation d'un txt : zone cible et l'entrée txt
# sont evaluées à part"""
# zone = serializers.SerializerMethodField('get_zone_name')
# txt_entry = serializers.SerializerMethodField('get_txt_name')
#
# class Meta:
# model = Txt
# fields = ('zone', 'txt_entry', 'field1', 'field2')
#
# @staticmethod
# def get_zone_name(obj):
# """ The name of the associated zone """
# return str(obj.zone.name)
#
# @staticmethod
# def get_txt_name(obj):
# """ The string representation of the entry to add to the DNS """
# return str(obj.dns_entry)
#
#
# class SrvSerializer(serializers.ModelSerializer):
# """Serialisation d'un srv : zone cible et l'entrée txt"""
# extension = serializers.SerializerMethodField('get_extension_name')
# srv_entry = serializers.SerializerMethodField('get_srv_name')
#
# class Meta:
# model = Srv
# fields = (
# 'service',
# 'protocole',
# 'extension',
# 'ttl',
# 'priority',
# 'weight',
# 'port',
# 'target',
# 'srv_entry'
# )
#
# @staticmethod
# def get_extension_name(obj):
# """ The name of the associated extension """
# return str(obj.extension.name)
#
# @staticmethod
# def get_srv_name(obj):
# """ The string representation of the entry to add to the DNS """
# return str(obj.dns_entry)
#
#
# class NsSerializer(serializers.ModelSerializer):
# """Serialisation d'un NS : la zone, l'entrée ns complète et le serveur
# ns sont évalués à part"""
# zone = serializers.SerializerMethodField('get_zone_name')
# ns = serializers.SerializerMethodField('get_domain_name')
# ns_entry = serializers.SerializerMethodField('get_text_name')
#
# class Meta:
# model = Ns
# fields = ('zone', 'ns', 'ns_entry')
#
# @staticmethod
# def get_zone_name(obj):
# """ The name of the associated zone """
# return obj.zone.name
#
# @staticmethod
# def get_domain_name(obj):
# """ The name of the associated NS target """
# return str(obj.ns)
#
# @staticmethod
# def get_text_name(obj):
# """ The string representation of the entry to add to the DNS """
# return str(obj.dns_entry)
#
#
# class DomainSerializer(serializers.ModelSerializer):
# """Serialisation d'un domain, extension, cname sont des foreign_key,
# et l'entrée complète, sont évalués à part"""
# extension = serializers.SerializerMethodField('get_zone_name')
# cname = serializers.SerializerMethodField('get_alias_name')
# cname_entry = serializers.SerializerMethodField('get_cname_name')
#
# class Meta:
# model = Domain
# fields = ('name', 'extension', 'cname', 'cname_entry')
#
# @staticmethod
# def get_zone_name(obj):
# """ The name of the associated zone """
# return obj.extension.name
#
# @staticmethod
# def get_alias_name(obj):
# """ The name of the associated alias """
# return str(obj.cname)
#
# @staticmethod
# def get_cname_name(obj):
# """ The name of the associated CNAME target """
# return str(obj.dns_entry)
#
#
# class ServicesSerializer(serializers.ModelSerializer):
# """Evaluation d'un Service, et serialisation"""
# server = serializers.SerializerMethodField('get_server_name')
# service = serializers.SerializerMethodField('get_service_name')
# need_regen = serializers.SerializerMethodField('get_regen_status')
#
# class Meta:
# model = Service_link
# fields = ('server', 'service', 'need_regen')
#
# @staticmethod
# def get_server_name(obj):
# """ The name of the associated server """
# return str(obj.server.domain.name)
#
# @staticmethod
# def get_service_name(obj):
# """ The name of the service name """
# return str(obj.service)
#
# @staticmethod
# def get_regen_status(obj):
# """ The string representation of the regen status """
# return obj.need_regen()
#
#
# class OuverturePortsSerializer(serializers.Serializer):
# """Serialisation de l'ouverture des ports"""
# ipv4 = serializers.SerializerMethodField()
# ipv6 = serializers.SerializerMethodField()
#
# def create(self, validated_data):
# """ Creates a new object based on the un-serialized data.
# Used to implement an abstract inherited method """
# pass
#
# def update(self, instance, validated_data):
# """ Updates an object based on the un-serialized data.
# Used to implement an abstract inherited method """
# pass
#
# @staticmethod
# def get_ipv4():
# """ The representation of the policy for the IPv4 addresses """
# return {
# i.ipv4.ipv4: {
# "tcp_in": [j.tcp_ports_in() for j in i.port_lists.all()],
# "tcp_out": [j.tcp_ports_out()for j in i.port_lists.all()],
# "udp_in": [j.udp_ports_in() for j in i.port_lists.all()],
# "udp_out": [j.udp_ports_out() for j in i.port_lists.all()],
# }
# for i in Interface.objects.all() if i.ipv4
# }
#
# @staticmethod
# def get_ipv6():
# """ The representation of the policy for the IPv6 addresses """
# return {
# i.ipv6: {
# "tcp_in": [j.tcp_ports_in() for j in i.port_lists.all()],
# "tcp_out": [j.tcp_ports_out()for j in i.port_lists.all()],
# "udp_in": [j.udp_ports_in() for j in i.port_lists.all()],
# "udp_out": [j.udp_ports_out() for j in i.port_lists.all()],
# }
# for i in Interface.objects.all() if i.ipv6
# }

View file

@ -24,48 +24,60 @@ Urls de l'api, pointant vers les fonctions de views
from __future__ import unicode_literals
from django.conf.urls import url
from django.conf.urls import url, include
from rest_framework.routers import DefaultRouter
from . import views
router = DefaultRouter()
router.register(r'users', views.UserViewSet)
router.register(r'clubs', views.ClubViewSet)
router.register(r'adherents', views.AdherentViewSet)
router.register(r'serviceusers', views.ServiceUserViewSet)
router.register(r'schools', views.SchoolViewSet)
router.register(r'listrights', views.ListRightViewSet)
router.register(r'shells', views.ShellViewSet, 'shell')
router.register(r'bans', views.BanViewSet)
router.register(r'whitelists', views.WhitelistViewSet)
urlpatterns = [
# Services
url(r'^services/$', views.services),
url(
r'^services/(?P<server_name>\w+)/(?P<service_name>\w+)/regen/$',
views.services_server_service_regen
),
url(r'^services/(?P<server_name>\w+)/$', views.services_server),
# DNS
url(r'^dns/mac-ip-dns/$', views.dns_mac_ip_dns),
url(r'^dns/alias/$', views.dns_alias),
url(r'^dns/corresp/$', views.dns_corresp),
url(r'^dns/mx/$', views.dns_mx),
url(r'^dns/ns/$', views.dns_ns),
url(r'^dns/txt/$', views.dns_txt),
url(r'^dns/srv/$', views.dns_srv),
url(r'^dns/zones/$', views.dns_zones),
# Unifi controler AP names
url(r'^unifi/ap_names/$', views.accesspoint_ip_dns),
# Firewall
url(r'^firewall/ouverture_ports/$', views.firewall_ouverture_ports),
# DHCP
url(r'^dhcp/mac-ip/$', views.dhcp_mac_ip),
# Mailings
url(r'^mailing/standard/$', views.mailing_standard),
url(
r'^mailing/standard/(?P<ml_name>\w+)/members/$',
views.mailing_standard_ml_members
),
url(r'^mailing/club/$', views.mailing_club),
url(
r'^mailing/club/(?P<ml_name>\w+)/members/$',
views.mailing_club_ml_members
),
url(r'^', include(router.urls)),
# # Services
# url(r'^services/$', views.services),
# url(
# r'^services/(?P<server_name>\w+)/(?P<service_name>\w+)/regen/$',
# views.services_server_service_regen
# ),
# url(r'^services/(?P<server_name>\w+)/$', views.services_server),
#
# # DNS
# url(r'^dns/mac-ip-dns/$', views.dns_mac_ip_dns),
# url(r'^dns/alias/$', views.dns_alias),
# url(r'^dns/corresp/$', views.dns_corresp),
# url(r'^dns/mx/$', views.dns_mx),
# url(r'^dns/ns/$', views.dns_ns),
# url(r'^dns/txt/$', views.dns_txt),
# url(r'^dns/srv/$', views.dns_srv),
# url(r'^dns/zones/$', views.dns_zones),
#
# # Unifi controler AP names
# url(r'^unifi/ap_names/$', views.accesspoint_ip_dns),
#
# # Firewall
# url(r'^firewall/ouverture_ports/$', views.firewall_ouverture_ports),
#
# # DHCP
# url(r'^dhcp/mac-ip/$', views.dhcp_mac_ip),
#
# # Mailings
# url(r'^mailing/standard/$', views.mailing_standard),
# url(
# r'^mailing/standard/(?P<ml_name>\w+)/members/$',
# views.mailing_standard_ml_members
# ),
# url(r'^mailing/club/$', views.mailing_club),
# url(
# r'^mailing/club/(?P<ml_name>\w+)/members/$',
# views.mailing_club_ml_members
# ),
]

View file

@ -1,123 +0,0 @@
# 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.
"""api.utils.
Set of various and usefull functions for the API app
"""
from rest_framework.renderers import JSONRenderer
from django.http import HttpResponse
class JSONResponse(HttpResponse):
"""A JSON response that can be send as an HTTP response.
Usefull in case of REST API.
"""
def __init__(self, data, **kwargs):
"""Initialisz a JSONResponse object.
Args:
data: the data to render as JSON (often made of lists, dicts,
strings, boolean and numbers). See `JSONRenderer.render(data)` for
further details.
Creates:
An HTTPResponse containing the data in JSON format.
"""
content = JSONRenderer().render(data)
kwargs['content_type'] = 'application/json'
super(JSONResponse, self).__init__(content, **kwargs)
class JSONError(JSONResponse):
"""A JSON response when the request failed.
"""
def __init__(self, error_msg, data=None, **kwargs):
"""Initialise a JSONError object.
Args:
error_msg: A message explaining where the error is.
data: An optional field for further data to send along.
Creates:
A JSONResponse containing a field `status` set to `error` and a
field `reason` containing `error_msg`. If `data` argument has been
given, a field `data` containing it is added to the JSON response.
"""
response = {
'status': 'error',
'reason': error_msg
}
if data is not None:
response['data'] = data
super(JSONError, self).__init__(response, **kwargs)
class JSONSuccess(JSONResponse):
"""A JSON response when the request suceeded.
"""
def __init__(self, data=None, **kwargs):
"""Initialise a JSONSucess object.
Args:
error_msg: A message explaining where the error is.
data: An optional field for further data to send along.
Creates:
A JSONResponse containing a field `status` set to `sucess`. If
`data` argument has been given, a field `data` containing it is
added to the JSON response.
"""
response = {
'status': 'success',
}
if data is not None:
response['data'] = data
super(JSONSuccess, self).__init__(response, **kwargs)
def accept_method(methods):
"""Decorator to set a list of accepted request method.
Check if the method used is accepted. If not, send a NotAllowed response.
"""
def decorator(view):
"""The decorator to use on a specific view
"""
def wrapper(request, *args, **kwargs):
"""The wrapper used for a specific request
"""
if request.method in methods:
return view(request, *args, **kwargs)
else:
return JSONError(
'Invalid request method. Request methods authorize are ' +
str(methods)
)
return view(request, *args, **kwargs)
return wrapper
return decorator

File diff suppressed because it is too large Load diff

View file

@ -174,3 +174,8 @@ GRAPH_MODELS = {
'all_applications': True,
'group_models': True,
}
#RestFramework config fot API
REST_FRAMEWORK = {
'URL_FIELD_NAME': 'api_url'
}