<commit_before>from core.models import TenantPrivilege
from plus import PlusSerializerMixin
from rest_framework import serializers
from services.vpn.models import VPNService, VPNTenant, VPN_KIND
from xos.apibase import XOSListCreateAPIView

if hasattr(serializers, "ReadOnlyField"):
    # rest_framework 3.x
    ReadOnlyField = serializers.ReadOnlyField
else:
    # rest_framework 2.x
    ReadOnlyField = serializers.Field


def get_default_vpn_service():
    vpn_services = VPNService.get_service_objects().all()
    if vpn_services:
        return vpn_services[0].id
    return None


class VPNTenantSerializer(serializers.ModelSerializer, PlusSerializerMixin):
        id = ReadOnlyField()
        service_specific_attribute = ReadOnlyField()
        server_network = ReadOnlyField()
        vpn_subnet = ReadOnlyField()
        is_persistent = ReadOnlyField()
        clients_can_see_each_other = ReadOnlyField()
        ca_crt = ReadOnlyField()
        port_number = ReadOnlyField()
        protocol = ReadOnlyField()
        failover_servers = ReadOnlyField()
        creator = ReadOnlyField()
        instance = ReadOnlyField()
        script_text = ReadOnlyField()
        provider_service = serializers.PrimaryKeyRelatedField(
            queryset=VPNService.get_service_objects().all(),
            default=get_default_vpn_service)

        humanReadableName = serializers.SerializerMethodField(
                "getHumanReadableName")

        computeNodeName = serializers.SerializerMethodField(
                "getComputeNodeName")

        class Meta:
            model = VPNTenant
            fields = ('humanReadableName', 'id', 'provider_service',
                      'service_specific_attribute', 'vpn_subnet',
                      'server_network', 'creator', 'instance', 'protocol',
                      'computeNodeName', 'is_persistent',
                      'clients_can_see_each_other', 'ca_crt', 'port_number',
                      'script_text', 'failover_servers')

        def getHumanReadableName(self, obj):
            return obj.__unicode__()

        def getComputeNodeName(self, obj):
            instance = obj.instance
            if not instance:
                return None
            return instance.node.name


class VPNTenantList(XOSListCreateAPIView):
    serializer_class = VPNTenantSerializer
    method_kind = "list"
    method_name = "vpntenant"

    def get_queryset(self):
        # Get every privilege for this user
        queryset = TenantPrivilege.objects.all().filter(user=self.request.user)
        queryset = [
            priv.tenant for priv in queryset if priv.tenant.KIND == VPN_KIND]
        for tenant in queryset:
            tenant.script_text = (
                tenant.create_client_script(
                    self.request.user.email + "-" + str(tenant.id)))
        return queryset<commit_msg>Fix method<commit_after>from core.models import TenantPrivilege
from plus import PlusSerializerMixin
from rest_framework import serializers
from services.vpn.models import VPNService, VPNTenant, VPN_KIND
from xos.apibase import XOSListCreateAPIView

if hasattr(serializers, "ReadOnlyField"):
    # rest_framework 3.x
    ReadOnlyField = serializers.ReadOnlyField
else:
    # rest_framework 2.x
    ReadOnlyField = serializers.Field


def get_default_vpn_service():
    vpn_services = VPNService.get_service_objects().all()
    if vpn_services:
        return vpn_services[0].id
    return None


class VPNTenantSerializer(serializers.ModelSerializer, PlusSerializerMixin):
        id = ReadOnlyField()
        service_specific_attribute = ReadOnlyField()
        server_network = ReadOnlyField()
        vpn_subnet = ReadOnlyField()
        is_persistent = ReadOnlyField()
        clients_can_see_each_other = ReadOnlyField()
        ca_crt = ReadOnlyField()
        port_number = ReadOnlyField()
        protocol = ReadOnlyField()
        failover_servers = ReadOnlyField()
        creator = ReadOnlyField()
        instance = ReadOnlyField()
        script_text = ReadOnlyField()
        provider_service = serializers.PrimaryKeyRelatedField(
            queryset=VPNService.get_service_objects().all(),
            default=get_default_vpn_service)

        humanReadableName = serializers.SerializerMethodField(
                "getHumanReadableName")

        computeNodeName = serializers.SerializerMethodField(
                "getComputeNodeName")

        class Meta:
            model = VPNTenant
            fields = ('humanReadableName', 'id', 'provider_service',
                      'service_specific_attribute', 'vpn_subnet',
                      'server_network', 'creator', 'instance', 'protocol',
                      'computeNodeName', 'is_persistent',
                      'clients_can_see_each_other', 'ca_crt', 'port_number',
                      'script_text', 'failover_servers')

        def getHumanReadableName(self, obj):
            return obj.__unicode__()

        def getComputeNodeName(self, obj):
            instance = obj.instance
            if not instance:
                return None
            return instance.node.name


class VPNTenantList(XOSListCreateAPIView):
    serializer_class = VPNTenantSerializer
    method_kind = "list"
    method_name = "vpntenant"

    def get_queryset(self):
        # Get every privilege for this user
        queryset = TenantPrivilege.objects.all().filter(user=self.request.user)
        queryset = [
            priv.tenant for priv in queryset if priv.tenant.kind == VPN_KIND]
        for tenant in queryset:
            tenant.script_text = (
                tenant.create_client_script(
                    self.request.user.email + "-" + str(tenant.id)))
        return queryset
{'fraction_non_alphanumeric': 0.07919065061922205, 'fraction_numerical': 0.0010465724751439038, 'mean_word_length': 2.5748129675810474, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3933704', 'n_tokens_mistral': 1601, 'n_tokens_neox': 1554, 'n_words': 316}