#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright 2017 EMBL - European Bioinformatics Institute
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import django_filters

from . import models as emg_models


class PublicationFilter(django_filters.FilterSet):

    data_origination = django_filters.CharFilter(
        name='studies__data_origination',
        distinct=True)

    class Meta:
        model = emg_models.Publication
        fields = (
            'data_origination',
        )


class StudyFilter(django_filters.FilterSet):

    biome = django_filters.CharFilter(
        name='biome__lineage',
        distinct=True)

    biome_name = django_filters.CharFilter(
        name='biome__biome_name',
        distinct=True)

    # species = django_filters.CharFilter(
    #     name='samples__species',
    #     distinct=True)

    class Meta:
        model = emg_models.Study
        fields = (
            'biome',
            'biome_name',
            'project_id',
            # 'species',
            'centre_name',
        )


class SampleFilter(django_filters.FilterSet):

    experiment_type = django_filters.CharFilter(
        name='runs__experiment_type__experiment_type',
        distinct=True)

    pipeline_version = django_filters.CharFilter(
        name='runs__pipeline__release_version',
        distinct=True)

    biome = django_filters.CharFilter(
        name='biome__lineage',
        distinct=True)

    biome_name = django_filters.CharFilter(
        name='biome__biome_name',
        distinct=True)

    instrument_model = django_filters.CharFilter(
        name='runs__instrument_platform',
        distinct=True)

    instrument_platform = django_filters.CharFilter(
        name='runs__instrument_model',
        distinct=True)

    metadata_key = django_filters.CharFilter(
        name='metadata__var__var_name',
        distinct=True)

    metadata_value = django_filters.CharFilter(
        name='metadata__var_val_ucv',
        distinct=True)

    class Meta:
        model = emg_models.Sample
        fields = (
            'experiment_type',
            'biome',
            'biome_name',
            'pipeline_version',
            'geo_loc_name',
            'species',
            'instrument_model',
            'instrument_platform',
            'metadata_key',
            'metadata_value',
        )


class PipelineFilter(django_filters.FilterSet):

    experiment_type = django_filters.CharFilter(
        name='runs__experiment_type__experiment_type',
        distinct=True)

    class Meta:
        model = emg_models.Pipeline
        fields = (
            'experiment_type',
        )


class RunFilter(django_filters.FilterSet):

    analysis_status = django_filters.CharFilter(
        name='analysis_status__analysis_status',
        distinct=True)

    experiment_type = django_filters.CharFilter(
        name='experiment_type__experiment_type',
        distinct=True)

    pipeline_version = django_filters.CharFilter(
        name='pipeline__release_version',
        distinct=True)

    biome = django_filters.CharFilter(
        name='sample__biome__lineage',
        distinct=True)

    biome_name = django_filters.CharFilter(
        name='sample__biome__biome_name',
        distinct=True)

    class Meta:
        model = emg_models.Run
        fields = (
            'biome',
            'biome_name',
            'analysis_status',
            'experiment_type',
            'pipeline_version',
            'instrument_platform',
            'instrument_model',
        )

@@ -663,16 +663,64 @@
 ilters%0A%0A
+from django.shortcuts import get_object_or_404%0A%0A
 from . i
@@ -746,16 +746,16 @@
 _models%0A
-
 %0A%0Aclass 
@@ -1122,39 +1122,154 @@
 ter(
-%0A        name='b
+method='filter_biome', distinct=True)%0A%0A    def filter_biome(self, qs, name, value):%0A        b = get_object_or_404(emg_models.B
 iome
-__
+, 
 lineage
-',
+=value)
 %0A   
@@ -1269,37 +1269,89 @@
 ue)%0A        
-distinct=True
+return qs.filter(biome__lft__gte=b.lft-1, biome__rgt__lte=b.rgt+1
 )%0A%0A    biome
@@ -2091,39 +2091,154 @@
 ter(
-%0A        name='b
+method='filter_biome', distinct=True)%0A%0A    def filter_biome(self, qs, name, value):%0A        b = get_object_or_404(emg_models.B
 iome
-__
+, 
 lineage
-',
+=value)
 %0A   
@@ -2238,37 +2238,89 @@
 ue)%0A        
-distinct=True
+return qs.filter(biome__lft__gte=b.lft-1, biome__rgt__lte=b.rgt+1
 )%0A%0A    biome
@@ -3014,32 +3014,34 @@
 me',%0A           
+ #
  'pipeline_versi
@@ -3970,69 +3970,269 @@
 ter(
-%0A        name='sample__biome__lineage',%0A        distinct=True
+method='filter_biome', distinct=True)%0A%0A    def filter_biome(self, qs, name, value):%0A        b = get_object_or_404(emg_models.Biome, lineage=value)%0A        return qs.filter(%0A            sample__biome__lft__gte=b.lft-1,%0A            sample__biome__rgt__lte=b.rgt+1
 )%0A%0A 
@@ -4507,32 +4507,32 @@
 periment_type',%0A
-
             'pip
@@ -4526,16 +4526,18 @@
         
+ #
  'pipeli

fix biome filter to load subtree with all children and descendants