diff --git a/api/serializers.py b/api/serializers.py index 8e4c49aa..f2604068 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -29,20 +29,20 @@ from machines.models import ( IpType, Extension, IpList, - MachineType, Domain, Txt, Mx, Srv, Service_link, Ns, - OuverturePortList, OuverturePort, Ipv6List ) class ServiceLinkSerializer(serializers.ModelSerializer): + """ Serializer for the ServiceLink objects """ + name = serializers.CharField(source='service.service_type') class Meta: @@ -51,6 +51,8 @@ class ServiceLinkSerializer(serializers.ModelSerializer): class MailingSerializer(serializers.ModelSerializer): + """ Serializer to build Mailing objects """ + name = serializers.CharField(source='pseudo') class Meta: @@ -59,20 +61,27 @@ class MailingSerializer(serializers.ModelSerializer): class MailingMemberSerializer(serializers.ModelSerializer): + """ Serializer fot the Adherent objects (who belong to a + Mailing) """ + class Meta: model = Adherent - fields = ('email', 'name', 'surname', 'pseudo',) + fields = ('email',) class IpTypeField(serializers.RelatedField): - """Serialisation d'une iptype, renvoie son evaluation str""" + """ 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): - """Serialisation d'une iplist, ip_type etant une foreign_key, - on evalue sa methode str""" + """ Serializer for an Ipv4List obejct using the IpType serialization """ + ip_type = IpTypeField(read_only=True) class Meta: @@ -81,16 +90,19 @@ class IpListSerializer(serializers.ModelSerializer): class Ipv6ListSerializer(serializers.ModelSerializer): + """ Serializer for an Ipv6List object """ + class Meta: model = Ipv6List fields = ('ipv6', 'slaac_ip') class InterfaceSerializer(serializers.ModelSerializer): - """Serialisation d'une interface, ipv4, domain et extension sont - des foreign_key, on les override et on les evalue avec des fonctions - get_...""" + """ 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') @@ -99,20 +111,29 @@ class InterfaceSerializer(serializers.ModelSerializer): model = Interface fields = ('ipv4', 'mac_address', 'domain', 'extension') - def get_dns(self, obj): + @staticmethod + def get_dns(obj): + """ The name of the associated DNS object """ return obj.domain.name - def get_interface_extension(self, obj): + @staticmethod + def get_interface_extension(obj): + """ The name of the associated Interface object """ return obj.domain.extension.name - def get_macaddress(self, obj): + @staticmethod + def get_macaddress(obj): + """ The string representation of the associated MAC address """ return str(obj.mac_address) class FullInterfaceSerializer(serializers.ModelSerializer): - """Serialisation complete d'une interface avec les ipv6 en plus""" + """ 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') @@ -121,26 +142,36 @@ class FullInterfaceSerializer(serializers.ModelSerializer): model = Interface fields = ('ipv4', 'ipv6', 'mac_address', 'domain', 'extension') - def get_dns(self, obj): + @staticmethod + def get_dns(obj): + """ The name of the associated DNS object """ return obj.domain.name - def get_interface_extension(self, obj): + @staticmethod + def get_interface_extension(obj): + """ The name of the associated Extension object """ return obj.domain.extension.name - def get_macaddress(self, obj): + @staticmethod + def get_macaddress(obj): + """ The string representation of the associated MAC address """ return str(obj.mac_address) class ExtensionNameField(serializers.RelatedField): - """Evaluation str d'un objet extension (.example.org)""" + """ Serializer for Extension object field """ + def to_representation(self, value): return value.name + def to_internal_value(self, data): + pass + class TypeSerializer(serializers.ModelSerializer): - """Serialisation d'un iptype : extension et la liste des - ouvertures de port son evalués en get_... etant des - foreign_key ou des relations manytomany""" + """ 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') @@ -158,7 +189,10 @@ class TypeSerializer(serializers.ModelSerializer): 'ouverture_ports_tcp_in', 'ouverture_ports_tcp_out', 'ouverture_ports_udp_in', 'ouverture_ports_udp_out',) - def get_port_policy(self, obj, protocole, io): + @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( @@ -196,13 +230,19 @@ class ExtensionSerializer(serializers.ModelSerializer): model = Extension fields = ('name', 'origin', 'origin_v6', 'zone_entry', 'soa') - def get_origin_ip(self, obj): + @staticmethod + def get_origin_ip(obj): + """ The IP of the associated origin for the zone """ return obj.origin.ipv4 - def get_zone_name(self, obj): + @staticmethod + def get_zone_name(obj): + """ The name of the associated zone """ return str(obj.dns_entry) - def get_soa_data(self, obj): + @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} @@ -217,13 +257,19 @@ class MxSerializer(serializers.ModelSerializer): model = Mx fields = ('zone', 'priority', 'name', 'mx_entry') - def get_entry_name(self, obj): + @staticmethod + def get_entry_name(obj): + """ The name of the DNS MX entry """ return str(obj.name) - def get_zone_name(self, obj): + @staticmethod + def get_zone_name(obj): + """ The name of the associated zone of the MX record """ return obj.zone.name - def get_mx_name(self, obj): + @staticmethod + def get_mx_name(obj): + """ The string representation of the entry to add to the DNS """ return str(obj.dns_entry) @@ -237,10 +283,14 @@ class TxtSerializer(serializers.ModelSerializer): model = Txt fields = ('zone', 'txt_entry', 'field1', 'field2') - def get_zone_name(self, obj): + @staticmethod + def get_zone_name(obj): + """ The name of the associated zone """ return str(obj.zone.name) - def get_txt_name(self, obj): + @staticmethod + def get_txt_name(obj): + """ The string representation of the entry to add to the DNS """ return str(obj.dns_entry) @@ -263,10 +313,14 @@ class SrvSerializer(serializers.ModelSerializer): 'srv_entry' ) - def get_extension_name(self, obj): + @staticmethod + def get_extension_name(obj): + """ The name of the associated extension """ return str(obj.extension.name) - def get_srv_name(self, obj): + @staticmethod + def get_srv_name(obj): + """ The string representation of the entry to add to the DNS """ return str(obj.dns_entry) @@ -281,13 +335,19 @@ class NsSerializer(serializers.ModelSerializer): model = Ns fields = ('zone', 'ns', 'ns_entry') - def get_zone_name(self, obj): + @staticmethod + def get_zone_name(obj): + """ The name of the associated zone """ return obj.zone.name - def get_domain_name(self, obj): + @staticmethod + def get_domain_name(obj): + """ The name of the associated NS target """ return str(obj.ns) - def get_text_name(self, obj): + @staticmethod + def get_text_name(obj): + """ The string representation of the entry to add to the DNS """ return str(obj.dns_entry) @@ -302,13 +362,19 @@ class DomainSerializer(serializers.ModelSerializer): model = Domain fields = ('name', 'extension', 'cname', 'cname_entry') - def get_zone_name(self, obj): + @staticmethod + def get_zone_name(obj): + """ The name of the associated zone """ return obj.extension.name - def get_alias_name(self, obj): + @staticmethod + def get_alias_name(obj): + """ The name of the associated alias """ return str(obj.cname) - def get_cname_name(self, obj): + @staticmethod + def get_cname_name(obj): + """ The name of the associated CNAME target """ return str(obj.dns_entry) @@ -322,13 +388,19 @@ class ServicesSerializer(serializers.ModelSerializer): model = Service_link fields = ('server', 'service', 'need_regen') - def get_server_name(self, obj): + @staticmethod + def get_server_name(obj): + """ The name of the associated server """ return str(obj.server.domain.name) - def get_service_name(self, obj): + @staticmethod + def get_service_name(obj): + """ The name of the service name """ return str(obj.service) - def get_regen_status(self, obj): + @staticmethod + def get_regen_status(obj): + """ The string representation of the regen status """ return obj.need_regen() @@ -337,7 +409,19 @@ class OuverturePortsSerializer(serializers.Serializer): 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()], @@ -348,7 +432,9 @@ class OuverturePortsSerializer(serializers.Serializer): 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()], diff --git a/api/tests.py b/api/tests.py index 21fa6d24..bfcda28f 100644 --- a/api/tests.py +++ b/api/tests.py @@ -19,7 +19,10 @@ # 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.tests +The tests for the API module. +""" -from django.test import TestCase +# from django.test import TestCase # Create your tests here. diff --git a/api/utils.py b/api/utils.py index 79aba0a1..d65cff44 100644 --- a/api/utils.py +++ b/api/utils.py @@ -106,7 +106,11 @@ def accept_method(methods): """ 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: diff --git a/api/views.py b/api/views.py index 47a34baf..eda4dd59 100644 --- a/api/views.py +++ b/api/views.py @@ -27,18 +27,42 @@ HTML pages such as the login and index pages for a better integration. from django.contrib.auth.decorators import login_required, permission_required from django.views.decorators.csrf import csrf_exempt -from re2o.utils import all_has_access, all_active_assigned_interfaces - +from re2o.utils import ( + all_has_access, + all_active_assigned_interfaces, + filter_active_interfaces +) from users.models import Club from machines.models import ( Service_link, Service, Interface, Domain, - OuverturePortList + IpType, + Mx, + Ns, + Txt, + Srv, + Extension, + OuverturePortList, + OuverturePort ) -from .serializers import * +from .serializers import ( + ServicesSerializer, + ServiceLinkSerializer, + FullInterfaceSerializer, + DomainSerializer, + TypeSerializer, + MxSerializer, + NsSerializer, + TxtSerializer, + SrvSerializer, + ExtensionSerializer, + InterfaceSerializer, + MailingMemberSerializer, + MailingSerializer +) from .utils import JSONError, JSONSuccess, accept_method @@ -46,7 +70,7 @@ from .utils import JSONError, JSONSuccess, accept_method @login_required @permission_required('machines.serveur') @accept_method(['GET']) -def services(request): +def services(_request): """The list of the different services and servers couples Return: @@ -104,7 +128,7 @@ def services_server_service_regen(request, server_name, service_name): @login_required @permission_required('machines.serveur') @accept_method(['GET']) -def services_server(request, server_name): +def services_server(_request, server_name): """The list of services attached to a specific server Returns: @@ -122,8 +146,8 @@ def services_server(request, server_name): if not query: return JSONError("This service is not active for this server") - services = query.all() - seria = ServiceLinkSerializer(services, many=True) + services_objects = query.all() + seria = ServiceLinkSerializer(services_objects, many=True) return JSONSuccess(seria.data) @@ -131,7 +155,7 @@ def services_server(request, server_name): @login_required @permission_required('machines.serveur') @accept_method(['GET']) -def dns_mac_ip_dns(request): +def dns_mac_ip_dns(_request): """The list of all active interfaces with all the associated infos (MAC, IP, IpType, DNS name and associated zone extension) @@ -160,7 +184,7 @@ def dns_mac_ip_dns(request): @login_required @permission_required('machines.serveur') @accept_method(['GET']) -def dns_alias(request): +def dns_alias(_request): """The list of all the alias used and the DNS info associated Returns: @@ -193,7 +217,7 @@ def dns_alias(request): @login_required @permission_required('machines.serveur') @accept_method(['GET']) -def accesspoint_ip_dns(request): +def accesspoint_ip_dns(_request): """The list of all active interfaces with all the associated infos (MAC, IP, IpType, DNS name and associated zone extension) @@ -225,7 +249,7 @@ def accesspoint_ip_dns(request): @login_required @permission_required('machines.serveur') @accept_method(['GET']) -def dns_corresp(request): +def dns_corresp(_request): """The list of the IpTypes possible with the infos about each Returns: @@ -253,7 +277,7 @@ def dns_corresp(request): @login_required @permission_required('machines.serveur') @accept_method(['GET']) -def dns_mx(request): +def dns_mx(_request): """The list of MX record to add to the DNS Returns: @@ -278,7 +302,7 @@ def dns_mx(request): @login_required @permission_required('machines.serveur') @accept_method(['GET']) -def dns_ns(request): +def dns_ns(_request): """The list of NS record to add to the DNS Returns: @@ -307,7 +331,7 @@ def dns_ns(request): @login_required @permission_required('machines.serveur') @accept_method(['GET']) -def dns_txt(request): +def dns_txt(_request): """The list of TXT record to add to the DNS Returns: @@ -330,7 +354,7 @@ def dns_txt(request): @login_required @permission_required('machines.serveur') @accept_method(['GET']) -def dns_srv(request): +def dns_srv(_request): """The list of SRV record to add to the DNS Returns: @@ -360,7 +384,7 @@ def dns_srv(request): @login_required @permission_required('machines.serveur') @accept_method(['GET']) -def dns_zones(request): +def dns_zones(_request): """The list of the zones managed Returns: @@ -389,7 +413,7 @@ def dns_zones(request): @login_required @permission_required('machines.serveur') @accept_method(['GET']) -def firewall_ouverture_ports(request): +def firewall_ouverture_ports(_request): """The list of the ports authorized to be openned by the firewall Returns: @@ -480,7 +504,7 @@ def firewall_ouverture_ports(request): @login_required @permission_required('machines.serveur') @accept_method(['GET']) -def dhcp_mac_ip(request): +def dhcp_mac_ip(_request): """The list of all active interfaces with all the associated infos (MAC, IP, IpType, DNS name and associated zone extension) @@ -506,7 +530,7 @@ def dhcp_mac_ip(request): @login_required @permission_required('machines.serveur') @accept_method(['GET']) -def mailing_standard(request): +def mailing_standard(_request): """All the available standard mailings. Returns: @@ -525,7 +549,7 @@ def mailing_standard(request): @login_required @permission_required('machines.serveur') @accept_method(['GET']) -def mailing_standard_ml_members(request): +def mailing_standard_ml_members(_request, ml_name): """All the members of a specific standard mailing Returns: @@ -552,7 +576,7 @@ def mailing_standard_ml_members(request): @login_required @permission_required('machines.serveur') @accept_method(['GET']) -def mailing_club(request): +def mailing_club(_request): """All the available club mailings. Returns: @@ -571,7 +595,7 @@ def mailing_club(request): @login_required @permission_required('machines.serveur') @accept_method(['GET']) -def mailing_club_ml_members(request): +def mailing_club_ml_members(_request, ml_name): """All the members of a specific club mailing Returns: