<s> import jinja2 <EOL> from core . models import TenantPrivilege <EOL> from plus import PlusSerializerMixin <EOL> from rest_framework import serializers <EOL> from services . openvpn . models import OpenVPNService , OpenVPNTenant <EOL> from xos . apibase import XOSListCreateAPIView <EOL> if hasattr ( serializers , "<STR_LIT>" ) : <EOL> ReadOnlyField = serializers . ReadOnlyField <EOL> else : <EOL> ReadOnlyField = serializers . Field <EOL> def get_default_openvpn_service ( ) : <EOL> openvpn_services = OpenVPNService . get_service_objects ( ) . all ( ) <EOL> if openvpn_services : <EOL> return openvpn_services [ <NUM_LIT:0> ] . id <EOL> return None <EOL> class OpenVPNTenantSerializer ( serializers . ModelSerializer , PlusSerializerMixin ) : <EOL> """<STR_LIT>""" <EOL> id = ReadOnlyField ( ) <EOL> server_network = ReadOnlyField ( ) <EOL> vpn_subnet = ReadOnlyField ( ) <EOL> script_text = serializers . SerializerMethodField ( ) <EOL> class Meta : <EOL> model = OpenVPNTenant <EOL> fields = ( '<STR_LIT:id>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> def get_script_text ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> env = jinja2 . Environment ( <EOL> loader = jinja2 . FileSystemLoader ( "<STR_LIT>" ) ) <EOL> template = env . get_template ( "<STR_LIT>" ) <EOL> client_name = self . context [ '<STR_LIT>' ] . user . email + "<STR_LIT:->" + str ( obj . id ) <EOL> remote_ids = list ( obj . failover_server_ids ) <EOL> remote_ids . insert ( <NUM_LIT:0> , obj . id ) <EOL> remotes = OpenVPNTenant . get_tenant_objects ( ) . filter ( pk__in = remote_ids ) <EOL> pki_dir = OpenVPNService . get_pki_dir ( obj ) <EOL> fields = { "<STR_LIT>" : client_name , <EOL> "<STR_LIT>" : remotes , <EOL> "<STR_LIT>" : obj . is_persistent , <EOL> "<STR_LIT>" : obj . get_ca_crt ( pki_dir ) , <EOL> "<STR_LIT>" : obj . get_client_cert ( client_name , pki_dir ) , <EOL> "<STR_LIT>" : obj . get_client_key ( client_name , pki_dir ) <EOL> } <EOL> return template . render ( fields ) <EOL> class OpenVPNTenantList ( XOSListCreateAPIView ) : <EOL> """<STR_LIT>""" <EOL> serializer_class = OpenVPNTenantSerializer <EOL> method_kind = "<STR_LIT:list>" <EOL> method_name = "<STR_LIT>" <EOL> def get_queryset ( self ) : <EOL> tenants_privs = TenantPrivilege . objects . all ( ) . filter ( <EOL> user = self . request . user ) <EOL> vpn_tenants = [ ] <EOL> for priv in tenants_privs : <EOL> vpn_tenants . append ( <EOL> OpenVPNTenant . get_tenant_objects ( ) . filter ( pk = priv . tenant . pk ) [ <NUM_LIT:0> ] ) <EOL> return vpn_tenants </s>
