<commit_before>from sqlalchemy import or_
from werkzeug.exceptions import NotFound

from geonature.utils.env import DB
from geonature.core.gn_meta.models import TDatasets

from geonature.utils.utilssqlalchemy import testDataType
from geonature.utils.errors import GeonatureApiError
from .utils import get_nomenclature_filters

from .models import (
    TRelevesOccurrence,
    TOccurrencesOccurrence,
    CorCountingOccurrence,
    corRoleRelevesOccurrence,
    ViewExportDLB
)
from geonature.core.gn_meta.models import TDatasets, CorDatasetsActor



class ReleveRepository():
    """
        Repository: classe permettant l'acces au données
        d'un modèle de type 'releve'
        """
    def __init__(self, model):
        self.model = model

    def get_one(self, id_releve, info_user):
        """Return one releve
        params:
         - id_releve: integer
         - info_user: TRole object model
        """
        try:
            releve = DB.session.query(self.model).get(id_releve)
        except Exception:
            DB.session.rollback()
            raise
        if releve:
            return releve.get_releve_if_allowed(info_user)

        raise NotFound(
            'The releve "{}" does not exist'.format(id_releve)
        )

    def update(self, releve, info_user):
        """ Update the current releve if allowed
        params:
        - releve: a Releve object model
        - info_user: Trole object model
        """
        releve = releve.get_releve_if_allowed(info_user)
        DB.session.merge(releve)
        DB.session.commit()
        DB.session.rollback()
        return releve

    def delete(self, id_releve, info_user):
        """Delete a releve
        params:
         - id_releve: integer
         - info_user: TRole object model"""

        releve = DB.session.query(self.model).get(id_releve)
        if releve:
            releve = releve.get_releve_if_allowed(info_user)
            DB.session.delete(releve)
            DB.session.commit()
            DB.session.rollback()
            return releve
        raise NotFound('The releve "{}" does not exist'.format(id_releve))

    def filter_query_with_autorization(self, user):
        q = DB.session.query(self.model)
        if user.tag_object_code == '2':
            allowed_datasets = TDatasets.get_user_datasets(user)
            q = q.filter(
                or_(
                    self.model.id_dataset.in_(tuple(allowed_datasets)),
                    self.model.observers.any(id_role=user.id_role),
                    self.model.id_digitiser == user.id_role
                )
            )
        elif user.tag_object_code == '1':
            q = q.filter(
                or_(
                    self.model.observers.any(id_role=user.id_role),
                    self.model.id_digitiser == user.id_role
                )
            )
        return q

    def filter_query_generic_table(self, user):
        """
        Return a prepared query filter with cruved authorization
        from a generic_table (a view)
        """
        print('ENTER PAR LA')
        q = DB.session.query(self.model.tableDef)
        print('et la ?')
        if user.tag_object_code in ('1', 2):
            q.join(corRoleRelevesOccurrence, self.model.tableDef.id_releve_occtax == corRoleRelevesOccurrence.columns.id_releve_occtax)
            if user.tag_object_code == '2':
                allowed_datasets = TDatasets.get_user_datasets(user)
                q = q.filter(
                    or_(
                        self.model.tableDef.columns.id_dataset.in_(tuple(allowed_datasets)),
                        corRoleRelevesOccurrence.columns.id_role == user.id_role,<commit_msg>relecture<commit_after>from sqlalchemy import or_
from werkzeug.exceptions import NotFound

from geonature.utils.env import DB
from geonature.core.gn_meta.models import TDatasets

from geonature.utils.utilssqlalchemy import testDataType
from geonature.utils.errors import GeonatureApiError
from .utils import get_nomenclature_filters

from .models import (
    TRelevesOccurrence,
    TOccurrencesOccurrence,
    CorCountingOccurrence,
    corRoleRelevesOccurrence,
    ViewExportDLB
)
from geonature.core.gn_meta.models import TDatasets, CorDatasetsActor



class ReleveRepository():
    """
        Repository: classe permettant l'acces au données
        d'un modèle de type 'releve'
        """
    def __init__(self, model):
        self.model = model

    def get_one(self, id_releve, info_user):
        """Return one releve
        params:
         - id_releve: integer
         - info_user: TRole object model
        """
        try:
            releve = DB.session.query(self.model).get(id_releve)
        except Exception:
            DB.session.rollback()
            raise
        if releve:
            return releve.get_releve_if_allowed(info_user)

        raise NotFound(
            'The releve "{}" does not exist'.format(id_releve)
        )

    def update(self, releve, info_user):
        """ Update the current releve if allowed
        params:
        - releve: a Releve object model
        - info_user: Trole object model
        """
        releve = releve.get_releve_if_allowed(info_user)
        DB.session.merge(releve)
        DB.session.commit()
        DB.session.rollback()
        return releve

    def delete(self, id_releve, info_user):
        """Delete a releve
        params:
         - id_releve: integer
         - info_user: TRole object model"""

        releve = DB.session.query(self.model).get(id_releve)
        if releve:
            releve = releve.get_releve_if_allowed(info_user)
            DB.session.delete(releve)
            DB.session.commit()
            DB.session.rollback()
            return releve
        raise NotFound('The releve "{}" does not exist'.format(id_releve))

    def filter_query_with_autorization(self, user):
        q = DB.session.query(self.model)
        if user.tag_object_code == '2':
            allowed_datasets = TDatasets.get_user_datasets(user)
            q = q.filter(
                or_(
                    self.model.id_dataset.in_(tuple(allowed_datasets)),
                    self.model.observers.any(id_role=user.id_role),
                    self.model.id_digitiser == user.id_role
                )
            )
        elif user.tag_object_code == '1':
            q = q.filter(
                or_(
                    self.model.observers.any(id_role=user.id_role),
                    self.model.id_digitiser == user.id_role
                )
            )
        return q

    def filter_query_generic_table(self, user):
        """
        Return a prepared query filter with cruved authorization
        from a generic_table (a view)
        """
        q = DB.session.query(self.model.tableDef)
        if user.tag_object_code in ('1', '2'):
            q.join(corRoleRelevesOccurrence, self.model.tableDef.id_releve_occtax == corRoleRelevesOccurrence.columns.id_releve_occtax)
            if user.tag_object_code == '2':
                allowed_datasets = TDatasets.get_user_datasets(user)
                q = q.filter(
                    or_(
                        self.model.tableDef.columns.id_dataset.in_(tuple(allowed_datasets)),
                        corRoleRelevesOccurrence.columns.id_role == user.id_role,
{'fraction_non_alphanumeric': 0.08746077227452585, 'fraction_numerical': 0.0013644426251876109, 'mean_word_length': 1.9979550102249488, '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': '5380449', 'n_tokens_mistral': 2257, 'n_tokens_neox': 2181, 'n_words': 432}