views.py 7.69 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 28 29

from django.contrib.auth.decorators import login_required, permission_required
from django.views.decorators.csrf import csrf_exempt

30
from re2o.utils import all_has_access, all_active_assigned_interfaces
31 32

from users.models import Club
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
from machines.models import Service_link, Service, Interface, Domain

from .serializers import *
from .utils import JSONError, JSONSuccess, accept_method


@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 = query.all()
    seria = ServiceLinkSerializer(services, many=True)
    return JSONSuccess(seria.data)
115 116


117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
@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)


141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
@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):
    """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):
    """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)