Ce serveur Gitlab sera éteint le 30 juin 2020, pensez à migrer vos projets vers les serveurs gitlab-research.centralesupelec.fr et gitlab-student.centralesupelec.fr !

views.py 31.1 KB
Newer Older
Maël Kervella's avatar
Maël Kervella committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
# 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.views

The views for the API app. They should all return JSON data and not fallback on
HTML pages such as the login and index pages for a better integration.
"""
26

27
import datetime
28

29 30 31 32
from django.conf import settings

from rest_framework.authtoken.views import ObtainAuthToken
from rest_framework.authtoken.models import Token
Maël Kervella's avatar
Maël Kervella committed
33
from rest_framework.response import Response
34
from rest_framework import viewsets, status
Maël Kervella's avatar
Maël Kervella committed
35

Maël Kervella's avatar
Maël Kervella committed
36 37 38 39 40
import cotisations.models as cotisations
import machines.models as machines
import preferences.models as preferences
import topologie.models as topologie
import users.models as users
41

Maël Kervella's avatar
Maël Kervella committed
42
from . import serializers
43

Maël Kervella's avatar
Maël Kervella committed
44

45
# COTISATIONS APP
46 47 48


class FactureViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
49 50
    queryset = cotisations.Facture.objects.all()
    serializer_class = serializers.FactureSerializer
51 52 53


class VenteViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
54 55
    queryset = cotisations.Vente.objects.all()
    serializer_class = serializers.VenteSerializer
56 57 58


class ArticleViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
59 60
    queryset = cotisations.Article.objects.all()
    serializer_class = serializers.ArticleSerializer
61 62 63


class BanqueViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
64 65
    queryset = cotisations.Banque.objects.all()
    serializer_class = serializers.BanqueSerializer
66 67 68


class PaiementViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
69 70
    queryset = cotisations.Paiement.objects.all()
    serializer_class = serializers.PaiementSerializer
71 72 73


class CotisationViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
74 75
    queryset = cotisations.Cotisation.objects.all()
    serializer_class = serializers.CotisationSerializer
76 77


78 79 80 81
# MACHINES APP


class MachineViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
82 83
    queryset = machines.Machine.objects.all()
    serializer_class = serializers.MachineSerializer
84 85 86


class MachineTypeViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
87 88
    queryset = machines.MachineType.objects.all()
    serializer_class = serializers.MachineTypeSerializer
89 90 91


class IpTypeViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
92 93
    queryset = machines.IpType.objects.all()
    serializer_class = serializers.IpTypeSerializer
94 95 96


class VlanViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
97 98
    queryset = machines.Vlan.objects.all()
    serializer_class = serializers.VlanSerializer
99 100 101


class NasViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
102 103
    queryset = machines.Nas.objects.all()
    serializer_class = serializers.NasSerializer
104 105 106


class SOAViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
107 108
    queryset = machines.SOA.objects.all()
    serializer_class = serializers.SOASerializer
109 110 111


class ExtensionViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
112 113
    queryset = machines.Extension.objects.all()
    serializer_class = serializers.ExtensionSerializer
114 115 116


class MxViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
117 118
    queryset = machines.Mx.objects.all()
    serializer_class = serializers.MxSerializer
119 120 121


class NsViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
122 123
    queryset = machines.Ns.objects.all()
    serializer_class = serializers.NsSerializer
124 125 126


class TxtViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
127 128
    queryset = machines.Txt.objects.all()
    serializer_class = serializers.TxtSerializer
129 130 131


class SrvViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
132 133
    queryset = machines.Srv.objects.all()
    serializer_class = serializers.SrvSerializer
134 135 136


class InterfaceViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
137 138
    queryset = machines.Interface.objects.all()
    serializer_class = serializers.InterfaceSerializer
139 140 141


class Ipv6ListViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
142 143
    queryset = machines.Ipv6List.objects.all()
    serializer_class = serializers.Ipv6ListSerializer
144 145 146


class DomainViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
147 148
    queryset = machines.Domain.objects.all()
    serializer_class = serializers.DomainSerializer
149 150 151


class IpListViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
152 153
    queryset = machines.IpList.objects.all()
    serializer_class = serializers.IpListSerializer
154 155 156


class ServiceViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
157 158
    queryset = machines.Service.objects.all()
    serializer_class = serializers.ServiceSerializer
159 160 161


class ServiceLinkViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
162 163
    queryset = machines.Service_link.objects.all()
    serializer_class = serializers.ServiceLinkSerializer
164 165 166


class OuverturePortListViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
167 168
    queryset = machines.OuverturePortList.objects.all()
    serializer_class = serializers.OuverturePortListSerializer
169 170 171


class OuverturePortViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
172 173
    queryset = machines.OuverturePort.objects.all()
    serializer_class = serializers.OuverturePortSerializer
174 175


176 177 178
# PREFERENCES APP

# class OptionalUserViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
179 180
#     queryset = preferences.OptionalUser.objects.all()
#     serializer_class = serializers.OptionalUserSerializer
181 182 183
# 
# 
# class OptionalMachineViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
184 185
#     queryset = preferences.OptionalMachine.objects.all()
#     serializer_class = serializers.OptionalMachineSerializer
186 187 188
# 
# 
# class OptionalTopologieViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
189 190
#     queryset = preferences.OptionalTopologie.objects.all()
#     serializer_class = serializers.OptionalTopologieSerializer
191 192 193
# 
# 
# class GeneralOptionViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
194 195
#     queryset = preferences.GeneralOption.objects.all()
#     serializer_class = serializers.GeneralOptionSerializer
196 197 198
# 
# 
# class ServiceOptionViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
199 200
#     queryset = preferences.ServiceOption.objects.all()
#     serializer_class = serializers.ServiceOptionSerializer
201 202 203
# 
# 
# class AssoOptionViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
204 205
#     queryset = preferences.AssoOption.objects.all()
#     serializer_class = serializers.AssoOptionSerializer
206 207 208
# 
# 
# class HomeOptionViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
209 210
#     queryset = preferences.HomeOption.objects.all()
#     serializer_class = serializers.HomeOptionSerializer
211 212 213
# 
# 
# class MailMessageOptionViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
214 215
#     queryset = preferences.MailMessageOption.objects.all()
#     serializer_class = serializers.MailMessageOptionSerializer
216 217


218 219 220 221
# TOPOLOGIE APP


class StackViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
222 223
    queryset = topologie.Stack.objects.all()
    serializer_class = serializers.StackSerializer
224 225 226


class AccessPointViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
227 228
    queryset = topologie.AccessPoint.objects.all()
    serializer_class = serializers.AccessPointSerializer
229 230 231


class SwitchViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
232 233
    queryset = topologie.Switch.objects.all()
    serializer_class = serializers.SwitchSerializer
234 235 236


class ModelSwitchViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
237 238
    queryset = topologie.ModelSwitch.objects.all()
    serializer_class = serializers.ModelSwitchSerializer
239 240 241


class ConstructorSwitchViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
242 243
    queryset = topologie.ConstructorSwitch.objects.all()
    serializer_class = serializers.ConstructorSwitchSerializer
244 245 246


class SwitchBayViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
247 248
    queryset = topologie.SwitchBay.objects.all()
    serializer_class = serializers.SwitchBaySerializer
249 250 251


class BuildingViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
252 253
    queryset = topologie.Building.objects.all()
    serializer_class = serializers.BuildingSerializer
254 255 256


class SwitchPortViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
257 258
    queryset = topologie.Port.objects.all()
    serializer_class = serializers.SwitchPortSerializer
259 260 261


class RoomViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
262 263
    queryset = topologie.Room.objects.all()
    serializer_class = serializers.RoomSerializer
264 265


266 267 268
# USER APP


Maël Kervella's avatar
Maël Kervella committed
269
class UserViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
270 271
    queryset = users.User.objects.all()
    serializer_class = serializers.UserSerializer
Maël Kervella's avatar
Maël Kervella committed
272 273 274


class ClubViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
275 276
    queryset = users.Club.objects.all()
    serializer_class = serializers.ClubSerializer
Maël Kervella's avatar
Maël Kervella committed
277 278 279


class AdherentViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
280 281
    queryset = users.Adherent.objects.all()
    serializer_class = serializers.AdherentSerializer
Maël Kervella's avatar
Maël Kervella committed
282 283 284


class ServiceUserViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
285 286
    queryset = users.ServiceUser.objects.all()
    serializer_class = serializers.ServiceUserSerializer
Maël Kervella's avatar
Maël Kervella committed
287 288 289


class SchoolViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
290 291
    queryset = users.School.objects.all()
    serializer_class = serializers.SchoolSerializer
Maël Kervella's avatar
Maël Kervella committed
292 293 294


class ListRightViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
295 296
    queryset = users.ListRight.objects.all()
    serializer_class = serializers.ListRightSerializer
Maël Kervella's avatar
Maël Kervella committed
297 298 299


class ShellViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
300 301
    queryset = users.ListShell.objects.all()
    serializer_class = serializers.ShellSerializer
Maël Kervella's avatar
Maël Kervella committed
302 303 304


class BanViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
305 306
    queryset = users.Ban.objects.all()
    serializer_class = serializers.BanSerializer
Maël Kervella's avatar
Maël Kervella committed
307 308 309


class WhitelistViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
310 311
    queryset = users.Whitelist.objects.all()
    serializer_class = serializers.WhitelistSerializer
Maël Kervella's avatar
Maël Kervella committed
312

313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
# Subclass the standard rest_framework.auth_token.views.ObtainAuthToken
# in order to renew the lease of the token and add expiration time
class ObtainExpiringAuthToken(ObtainAuthToken):
    def post(self, request, *args, **kwargs):
        serializer = self.serializer_class(data=request.data)
        serializer.is_valid(raise_exception=True)
        user = serializer.validated_data['user']
        token, created = Token.objects.get_or_create(user=user)

        token_duration = datetime.timedelta(
            seconds=settings.API_TOKEN_DURATION
        )
        utc_now = datetime.datetime.now(datetime.timezone.utc)
        if not created and token.created < utc_now - token_duration:
            token.delete()
            token = Token.objects.create(user=user)
            token.created = datetime.datetime.utcnow()
            token.save()

        return Response({
            'token': token.key,
            'expiration_date': token.created + token_duration
        })

Maël Kervella's avatar
Maël Kervella committed
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
# 
# @csrf_exempt
# @login_required
# @permission_required('machines.serveur')
# @accept_method(['GET'])
# def services(_request):
#     """The list of the different services and servers couples
# 
#     Return:
#         GET:
#             A JSONSuccess response with a field `data` containing:
#             * a list of dictionnaries (one for each service-server couple)
#               containing:
#               * a field `server`: the server name
#               * a field `service`: the service name
#               * a field `need_regen`: does the service need a regeneration ?
#     """
# 
#     service_link = (Service_link.objects.all()
#                     .select_related('server__domain')
#                     .select_related('service'))
#     seria = ServicesSerializer(service_link, many=True)
#     return JSONSuccess(seria.data)
# 
# 
# @csrf_exempt
# @login_required
# @permission_required('machines.serveur')
# @accept_method(['GET', 'POST'])
# def services_server_service_regen(request, server_name, service_name):
#     """The status of a particular service linked to a particular server.
#     Mark the service as regenerated if POST used.
# 
#     Returns:
#         GET:
#             A JSONSucess response with a field `data` containing:
#             * a field `need_regen`: does the service need a regeneration ?
# 
#         POST:
#             An empty JSONSuccess response.
#     """
# 
#     query = Service_link.objects.filter(
#         service__in=Service.objects.filter(service_type=service_name),
#         server__in=Interface.objects.filter(
#             domain__in=Domain.objects.filter(name=server_name)
#         )
#     )
#     if not query:
#         return JSONError("This service is not active for this server")
# 
#     service = query.first()
#     if request.method == 'GET':
#         return JSONSuccess({'need_regen': service.need_regen()})
#     else:
#         service.done_regen()
#         return JSONSuccess()
# 
# 
# @csrf_exempt
# @login_required
# @permission_required('machines.serveur')
# @accept_method(['GET'])
# def services_server(_request, server_name):
#     """The list of services attached to a specific server
# 
#     Returns:
#         GET:
#             A JSONSuccess response with a field `data` containing:
#             * a list of dictionnaries (one for each service) containing:
#               * a field `name`: the name of a service
#     """
# 
#     query = Service_link.objects.filter(
#         server__in=Interface.objects.filter(
#             domain__in=Domain.objects.filter(name=server_name)
#         )
#     )
#     if not query:
#         return JSONError("This service is not active for this server")
# 
#     services_objects = query.all()
#     seria = ServiceLinkSerializer(services_objects, many=True)
#     return JSONSuccess(seria.data)
# 
# 
# @csrf_exempt
# @login_required
# @permission_required('machines.serveur')
# @accept_method(['GET'])
# 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)
# 
#     Returns:
#         GET:
#             A JSON Success response with a field `data` containing:
#             * a list of dictionnaries (one for each interface) containing:
#               * a field `ipv4` containing:
#                 * a field `ipv4`: the ip for this interface
#                 * a field `ip_type`: the name of the IpType of this interface
#               * a field `ipv6` containing `null` if ipv6 is deactivated else:
#                 * a field `ipv6`: the ip for this interface
#                 * a field `ip_type`: the name of the IpType of this interface
#               * a field `mac_address`: the MAC of this interface
#               * a field `domain`: the DNS name for this interface
#               * a field `extension`: the extension for the DNS zone of this
#                 interface
#     """
# 
#     interfaces = all_active_assigned_interfaces(full=True)
#     seria = FullInterfaceSerializer(interfaces, many=True)
#     return JSONSuccess(seria.data)
# 
# 
# @csrf_exempt
# @login_required
# @permission_required('machines.serveur')
# @accept_method(['GET'])
# def dns_alias(_request):
#     """The list of all the alias used and the DNS info associated
# 
#     Returns:
#         GET:
#             A JSON Success response with a field `data` containing:
#             * a list of dictionnaries (one for each alias) containing:
#               * a field `name`: the alias used
#               * a field `cname`: the target of the alias (real name of the
#                 interface)
#               * a field `cname_entry`: the entry to write in the DNS to have
#                 the alias
#               * a field `extension`: the extension for the DNS zone of this
#                 interface
#     """
# 
#     alias = (Domain.objects
#              .filter(interface_parent=None)
#              .filter(
#                  cname__in=Domain.objects.filter(
#                      interface_parent__in=Interface.objects.exclude(ipv4=None)
#                  )
#              )
#              .select_related('extension')
#              .select_related('cname__extension'))
#     seria = DomainSerializer(alias, many=True)
#     return JSONSuccess(seria.data)
# 
# 
# @csrf_exempt
# @login_required
# @permission_required('machines.serveur')
# @accept_method(['GET'])
# 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)
# 
#     Only display access points. Use to gen unifi controler names
# 
#     Returns:
#         GET:
#             A JSON Success response with a field `data` containing:
#             * a list of dictionnaries (one for each interface) containing:
#               * a field `ipv4` containing:
#                 * a field `ipv4`: the ip for this interface
#                 * a field `ip_type`: the name of the IpType of this interface
#               * a field `ipv6` containing `null` if ipv6 is deactivated else:
#                 * a field `ipv6`: the ip for this interface
#                 * a field `ip_type`: the name of the IpType of this interface
#               * a field `mac_address`: the MAC of this interface
#               * a field `domain`: the DNS name for this interface
#               * a field `extension`: the extension for the DNS zone of this
#                 interface
#     """
# 
#     interfaces = (all_active_assigned_interfaces(full=True)
#                   .filter(machine__accesspoint__isnull=False))
#     seria = FullInterfaceSerializer(interfaces, many=True)
#     return JSONSuccess(seria.data)
# 
# 
# @csrf_exempt
# @login_required
# @permission_required('machines.serveur')
# @accept_method(['GET'])
# def dns_corresp(_request):
#     """The list of the IpTypes possible with the infos about each
# 
#     Returns:
#         GET:
#             A JSON Success response with a field `data` containing:
#             * a list of dictionnaries (one for each IpType) containing:
#               * a field `type`: the name of the type
#               * a field `extension`: the DNS extension associated
#               * a field `domain_ip_start`: the first ip to use for this type
#               * a field `domain_ip_stop`: the last ip to use for this type
#               * a field `prefix_v6`: `null` if IPv6 is deactivated else the
#                 prefix to use
#               * a field `ouverture_ports_tcp_in`: the policy for TCP IN ports
#               * a field `ouverture_ports_tcp_out`: the policy for TCP OUT ports
#               * a field `ouverture_ports_udp_in`: the policy for UDP IN ports
#               * a field `ouverture_ports_udp_out`: the policy for UDP OUT ports
#     """
# 
#     ip_type = IpType.objects.all().select_related('extension')
#     seria = TypeSerializer(ip_type, many=True)
#     return JSONSuccess(seria.data)
# 
# 
# @csrf_exempt
# @login_required
# @permission_required('machines.serveur')
# @accept_method(['GET'])
# def dns_mx(_request):
#     """The list of MX record to add to the DNS
# 
#     Returns:
#         GET:
#             A JSON Success response with a field `data` containing:
#             * a list of dictionnaries (one for each MX record) containing:
#               * a field `zone`: the extension for the concerned zone
#               * a field `priority`: the priority to use
#               * a field `name`: the name of the target
#               * a field `mx_entry`: the full entry to add in the DNS for this
#                 MX record
#     """
# 
#     mx = (Mx.objects.all()
#           .select_related('zone')
#           .select_related('name__extension'))
#     seria = MxSerializer(mx, many=True)
#     return JSONSuccess(seria.data)
# 
# 
# @csrf_exempt
# @login_required
# @permission_required('machines.serveur')
# @accept_method(['GET'])
# def dns_ns(_request):
#     """The list of NS record to add to the DNS
# 
#     Returns:
#         GET:
#             A JSON Success response with a field `data` containing:
#             * a list of dictionnaries (one for each NS record) containing:
#               * a field `zone`: the extension for the concerned zone
#               * a field `ns`: the DNS name for the NS server targeted
#               * a field `ns_entry`: the full entry to add in the DNS for this
#                 NS record
#     """
# 
#     ns = (Ns.objects
#           .exclude(
#               ns__in=Domain.objects.filter(
#                   interface_parent__in=Interface.objects.filter(ipv4=None)
#               )
#           )
#           .select_related('zone')
#           .select_related('ns__extension'))
#     seria = NsSerializer(ns, many=True)
#     return JSONSuccess(seria.data)
# 
# 
# @csrf_exempt
# @login_required
# @permission_required('machines.serveur')
# @accept_method(['GET'])
# def dns_txt(_request):
#     """The list of TXT record to add to the DNS
# 
#     Returns:
#         GET:
#             A JSON Success response with a field `data` containing:
#             * a list of dictionnaries (one for each TXT record) containing:
#               * a field `zone`: the extension for the concerned zone
#               * a field `field1`: the first field in the record (target)
#               * a field `field2`: the second field in the record (value)
#               * a field `txt_entry`: the full entry to add in the DNS for this
#                 TXT record
#     """
# 
#     txt = Txt.objects.all().select_related('zone')
#     seria = TxtSerializer(txt, many=True)
#     return JSONSuccess(seria.data)
# 
# 
# @csrf_exempt
# @login_required
# @permission_required('machines.serveur')
# @accept_method(['GET'])
# def dns_srv(_request):
#     """The list of SRV record to add to the DNS
# 
#     Returns:
#         GET:
#             A JSON Success response with a field `data` containing:
#             * a list of dictionnaries (one for each SRV record) containing:
#               * a field `extension`: the extension for the concerned zone
#               * a field `service`: the name of the service concerned
#               * a field `protocole`: the name of the protocol to use
#               * a field `ttl`: the Time To Live to use
#               * a field `priority`: the priority for this service
#               * a field `weight`: the weight for same priority entries
#               * a field `port`: the port targeted
#               * a field `target`: the interface targeted by this service
#               * a field `srv_entry`: the full entry to add in the DNS for this
#                 SRV record
#     """
# 
#     srv = (Srv.objects.all()
#            .select_related('extension')
#            .select_related('target__extension'))
#     seria = SrvSerializer(srv, many=True)
#     return JSONSuccess(seria.data)
# 
# 
# @csrf_exempt
# @login_required
# @permission_required('machines.serveur')
# @accept_method(['GET'])
# def dns_zones(_request):
#     """The list of the zones managed
# 
#     Returns:
#         GET:
#             A JSON Success response with a field `data` containing:
#             * a list of dictionnaries (one for each zone) containing:
#               * a field `name`: the extension for the zone
#               * a field `origin`: the server IPv4 for the orgin of the zone
#               * a field `origin_v6`: `null` if ipv6 is deactivated else the
#                 server IPv6 for the origin of the zone
#               * a field `soa` containing:
#                 * a field `mail` containing the mail to contact in case of
#                   problem with the zone
#                 * a field `param` containing the full soa paramters to use
#                   in the DNS for this zone
#               * a field `zone_entry`: the full entry to add in the DNS for the
#                 origin of the zone
#     """
# 
#     zones = Extension.objects.all().select_related('origin')
#     seria = ExtensionSerializer(zones, many=True)
#     return JSONSuccess(seria.data)
# 
# 
# @csrf_exempt
# @login_required
# @permission_required('machines.serveur')
# @accept_method(['GET'])
# def firewall_ouverture_ports(_request):
#     """The list of the ports authorized to be openned by the firewall
# 
#     Returns:
#         GET:
#             A JSONSuccess response with a `data` field containing:
#             * a field `ipv4` containing:
#               * a field `tcp_in` containing:
#                 * a list of port number where ipv4 tcp in should be ok
#               * a field `tcp_out` containing:
#                 * a list of port number where ipv4 tcp ou should be ok
#               * a field `udp_in` containing:
#                 * a list of port number where ipv4 udp in should be ok
#               * a field `udp_out` containing:
#                 * a list of port number where ipv4 udp out should be ok
#             * a field `ipv6` containing:
#               * a field `tcp_in` containing:
#                 * a list of port number where ipv6 tcp in should be ok
#               * a field `tcp_out` containing:
#                 * a list of port number where ipv6 tcp ou should be ok
#               * a field `udp_in` containing:
#                 * a list of port number where ipv6 udp in should be ok
#               * a field `udp_out` containing:
#                 * a list of port number where ipv6 udp out should be ok
#     """
# 
#     r = {'ipv4': {}, 'ipv6': {}}
#     for o in (OuverturePortList.objects.all()
#               .prefetch_related('ouvertureport_set')
#               .prefetch_related('interface_set', 'interface_set__ipv4')):
#         pl = {
#             "tcp_in": set(map(
#                 str,
#                 o.ouvertureport_set.filter(
#                     protocole=OuverturePort.TCP,
#                     io=OuverturePort.IN
#                 )
#             )),
#             "tcp_out": set(map(
#                 str,
#                 o.ouvertureport_set.filter(
#                     protocole=OuverturePort.TCP,
#                     io=OuverturePort.OUT
#                 )
#             )),
#             "udp_in": set(map(
#                 str,
#                 o.ouvertureport_set.filter(
#                     protocole=OuverturePort.UDP,
#                     io=OuverturePort.IN
#                 )
#             )),
#             "udp_out": set(map(
#                 str,
#                 o.ouvertureport_set.filter(
#                     protocole=OuverturePort.UDP,
#                     io=OuverturePort.OUT
#                 )
#             )),
#         }
#         for i in filter_active_interfaces(o.interface_set):
#             if i.may_have_port_open():
#                 d = r['ipv4'].get(i.ipv4.ipv4, {})
#                 d["tcp_in"] = (d.get("tcp_in", set())
#                                .union(pl["tcp_in"]))
#                 d["tcp_out"] = (d.get("tcp_out", set())
#                                 .union(pl["tcp_out"]))
#                 d["udp_in"] = (d.get("udp_in", set())
#                                .union(pl["udp_in"]))
#                 d["udp_out"] = (d.get("udp_out", set())
#                                 .union(pl["udp_out"]))
#                 r['ipv4'][i.ipv4.ipv4] = d
#             if i.ipv6():
#                 for ipv6 in i.ipv6():
#                     d = r['ipv6'].get(ipv6.ipv6, {})
#                     d["tcp_in"] = (d.get("tcp_in", set())
#                                    .union(pl["tcp_in"]))
#                     d["tcp_out"] = (d.get("tcp_out", set())
#                                     .union(pl["tcp_out"]))
#                     d["udp_in"] = (d.get("udp_in", set())
#                                    .union(pl["udp_in"]))
#                     d["udp_out"] = (d.get("udp_out", set())
#                                     .union(pl["udp_out"]))
#                     r['ipv6'][ipv6.ipv6] = d
#     return JSONSuccess(r)
# 
# 
# @csrf_exempt
# @login_required
# @permission_required('machines.serveur')
# @accept_method(['GET'])
# 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)
# 
#     Returns:
#         GET:
#             A JSON Success response with a field `data` containing:
#             * a list of dictionnaries (one for each interface) containing:
#               * a field `ipv4` containing:
#                 * a field `ipv4`: the ip for this interface
#                 * a field `ip_type`: the name of the IpType of this interface
#               * a field `mac_address`: the MAC of this interface
#               * a field `domain`: the DNS name for this interface
#               * a field `extension`: the extension for the DNS zone of this
#                 interface
#     """
# 
#     interfaces = all_active_assigned_interfaces()
#     seria = InterfaceSerializer(interfaces, many=True)
#     return JSONSuccess(seria.data)
# 
# 
# @csrf_exempt
# @login_required
# @permission_required('machines.serveur')
# @accept_method(['GET'])
# def mailing_standard(_request):
#     """All the available standard mailings.
# 
#     Returns:
#         GET:
#             A JSONSucess response with a field `data` containing:
#             * a list of dictionnaries (one for each mailing) containing:
#               * a field `name`: the name of a mailing
#     """
# 
#     return JSONSuccess([
#         {'name': 'adherents'}
#     ])
# 
# 
# @csrf_exempt
# @login_required
# @permission_required('machines.serveur')
# @accept_method(['GET'])
# def mailing_standard_ml_members(_request, ml_name):
#     """All the members of a specific standard mailing
# 
#     Returns:
#         GET:
#             A JSONSucess response with a field `data` containing:
#             * a list if dictionnaries (one for each member) containing:
#               * a field `email`:   the email of the member
#               * a field `name`:    the name of the member
#               * a field `surname`: the surname of the member
#               * a field `pseudo`:  the pseudo of the member
#     """
# 
#     # All with active connextion
#     if ml_name == 'adherents':
#         members = all_has_access().values('email').distinct()
#     # Unknown mailing
#     else:
#         return JSONError("This mailing does not exist")
#     seria = MailingMemberSerializer(members, many=True)
#     return JSONSuccess(seria.data)
# 
# 
# @csrf_exempt
# @login_required
# @permission_required('machines.serveur')
# @accept_method(['GET'])
# def mailing_club(_request):
#     """All the available club mailings.
# 
#     Returns:
#         GET:
#             A JSONSucess response with a field `data` containing:
#             * a list of dictionnaries (one for each mailing) containing:
#               * a field `name` indicating the name of a mailing
#     """
# 
#     clubs = Club.objects.filter(mailing=True).values('pseudo')
#     seria = MailingSerializer(clubs, many=True)
#     return JSONSuccess(seria.data)
# 
# 
# @csrf_exempt
# @login_required
# @permission_required('machines.serveur')
# @accept_method(['GET'])
# def mailing_club_ml_members(_request, ml_name):
#     """All the members of a specific club mailing
# 
#     Returns:
#         GET:
#             A JSONSucess response with a field `data` containing:
#             * a list if dictionnaries (one for each member) containing:
#               * a field `email`:   the email of the member
#               * a field `name`:    the name of the member
#               * a field `surname`: the surname of the member
#               * a field `pseudo`:  the pseudo of the member
#     """
# 
#     try:
#         club = Club.objects.get(mailing=True, pseudo=ml_name)
#     except Club.DoesNotExist:
#         return JSONError("This mailing does not exist")
#     members = club.administrators.all().values('email').distinct()
#     seria = MailingMemberSerializer(members, many=True)
#     return JSONSuccess(seria.data)