<commit_before># -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
from rest_framework import viewsets, serializers, status
from rest_framework.response import Response
from dbaas.middleware import UserMiddleware
from logical import models
from physical.models import Plan, Environment
from account.models import Team
from .credential import CredentialSerializer
from django.contrib.sites.models import Site
from notification.tasks import TaskRegister
import logging

LOG = logging.getLogger(__name__)


class DatabaseSerializer(serializers.HyperlinkedModelSerializer):
    plan = serializers.HyperlinkedRelatedField(
        source='plan', view_name='plan-detail',
        queryset=Plan.objects.filter(is_active=True)
    )
    environment = serializers.HyperlinkedRelatedField(
        source='environment', view_name='environment-detail',
        queryset=Environment.objects
    )
    team = serializers.HyperlinkedRelatedField(
        source='team', view_name='team-detail', queryset=Team.objects
    )
    endpoint = serializers.Field(source='endpoint')
    quarantine_dt = serializers.Field(source='quarantine_dt')
    total_size_in_bytes = serializers.Field(source='total_size')
    credentials = CredentialSerializer(many=True, read_only=True)
    status = serializers.Field(source='status')
    used_size_in_bytes = serializers.Field(source='used_size_in_bytes')
    engine = serializers.CharField(source='infra.engine', read_only=True)

    class Meta:
        model = models.Database
        fields = (
            'url', 'id', 'name', 'endpoint', 'plan', 'environment',
            'project', 'team', 'quarantine_dt', 'total_size_in_bytes',
            'credentials', 'description', 'status',
            'used_size_in_bytes', 'subscribe_to_email_events',
            'created_at', 'engine'
        )
        read_only = ('credentials', 'status', 'used_size_in_bytes')

    def __init__(self, *args, **kwargs):
        super(DatabaseSerializer, self).__init__(*args, **kwargs)

        request = self.context.get('request', None)
        if request:
            creating = request.method == 'POST'

            self.fields['plan'].read_only = not creating
            self.fields['environment'].read_only = not creating
            self.fields['name'].read_only = not creating
            self.fields['credentials'].read_only = True


class DatabaseAPI(viewsets.ModelViewSet):

    """
    *   ### __List databases__
        __GET__ /api/database/
    *   ### __To create a new database__
        __POST__ /api/database/
            {
                "name": "{name}",
                "plan": "{api_url}/plan/{plan_id}/",
                "environment": "{api_url}/environment/{environment_id}/",
                "project": "{api_url}/project/{project_id}/",<commit_msg>Add replication_topology_id on Database API<commit_after># -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
from rest_framework import viewsets, serializers, status
from rest_framework.response import Response
from dbaas.middleware import UserMiddleware
from logical import models
from physical.models import Plan, Environment
from account.models import Team
from .credential import CredentialSerializer
from django.contrib.sites.models import Site
from notification.tasks import TaskRegister
import logging

LOG = logging.getLogger(__name__)


class DatabaseSerializer(serializers.HyperlinkedModelSerializer):
    plan = serializers.HyperlinkedRelatedField(
        source='plan', view_name='plan-detail',
        queryset=Plan.objects.filter(is_active=True)
    )
    replication_topology_id = serializers.Field(source='databaseinfra.plan.replication_topology.id')
    environment = serializers.HyperlinkedRelatedField(
        source='environment', view_name='environment-detail',
        queryset=Environment.objects
    )
    team = serializers.HyperlinkedRelatedField(
        source='team', view_name='team-detail', queryset=Team.objects
    )
    endpoint = serializers.Field(source='endpoint')
    quarantine_dt = serializers.Field(source='quarantine_dt')
    total_size_in_bytes = serializers.Field(source='total_size')
    credentials = CredentialSerializer(many=True, read_only=True)
    status = serializers.Field(source='status')
    used_size_in_bytes = serializers.Field(source='used_size_in_bytes')
    engine = serializers.CharField(source='infra.engine', read_only=True)

    class Meta:
        model = models.Database
        fields = (
            'url', 'id', 'name', 'endpoint', 'plan', 'environment',
            'project', 'team', 'quarantine_dt', 'total_size_in_bytes',
            'credentials', 'description', 'status',
            'used_size_in_bytes', 'subscribe_to_email_events',
            'created_at', 'engine', 'replication_topology_id'
        )
        read_only = ('credentials', 'status', 'used_size_in_bytes')

    def __init__(self, *args, **kwargs):
        super(DatabaseSerializer, self).__init__(*args, **kwargs)

        request = self.context.get('request', None)
        if request:
            creating = request.method == 'POST'

            self.fields['plan'].read_only = not creating
            self.fields['environment'].read_only = not creating
            self.fields['name'].read_only = not creating
            self.fields['credentials'].read_only = True


class DatabaseAPI(viewsets.ModelViewSet):

    """
    *   ### __List databases__
        __GET__ /api/database/
    *   ### __To create a new database__
        __POST__ /api/database/
            {
                "name": "{name}",
                "plan": "{api_url}/plan/{plan_id}/",
                "environment": "{api_url}/environment/{environment_id}/",
                "project": "{api_url}/project/{project_id}/",
{'fraction_non_alphanumeric': 0.11639571082670357, 'fraction_numerical': 0.0003459010722933241, 'mean_word_length': 3.4178762414056534, 'pattern_counts': {'":': 8, '<': 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': '1419153', 'n_tokens_mistral': 1609, 'n_tokens_neox': 1566, 'n_words': 320}