<commit_before># -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models
from dataloaderinterface.models import SiteRegistration
from accounts.models import User
from django.db.models import Sum, Q
from operator import __or__ as OR


class Macroinvertebrate(models.Model):
    """
    :var: scientific_name: The scientific name for the macroinvertebrate (i.e. 'Ephemeroptera')
    :var: common_name: The common for the macroinvertebrate (i.e. 'mayflies')
    :var: family_of: Establishes an Order-Family one-to-many relationships. For example, in biological classification,
     Diptera is the order of all true flies and includes the families of Anthericidae, Chironomidae, Simuliidae, etc.,
     but all are macroinvertebrate.
    """
    class Meta:
        db_table = 'macroinvertebrate'

    scientific_name = models.CharField(max_length=255, unique=True)
    # 'latin_name' is basically the same as 'scientific_name', but it
    # can be blank. This was added to resolve an issue with displaying
    # names on the website.
    latin_name = models.CharField(max_length=255, default='', blank=True)
    common_name = models.CharField(max_length=255, unique=True)
    family_of = models.ForeignKey('Macroinvertebrate',
                                  on_delete=models.CASCADE,
                                  null=True,
                                  blank=True,
                                  related_name='families')
    pollution_tolerance = models.FloatField(default=0)
    itis_serial_number = models.CharField(max_length=255, null=True, blank=True)
    url = models.CharField(max_length=255, null=True, blank=True)

    """
    'sort_priority' is used to determine the order taxon appear on things like django forms. HIGHER values  
    of sort_priority should appear first, and LOWER values last
    """
    sort_priority = models.FloatField(default=0)

    @property
    def is_ept(self):
        return self.scientific_name.lower() in ['ephemeroptera', 'plecoptera', 'tricoptera']

    @property
    def display_name(self):
        return self.__str__()

    def __str__(self):
        if not len(self.latin_name):
            return '{0}'.format(self.common_name, self.latin_name)
        return '{0} ({1})'.format(self.common_name, self.latin_name)

    def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
        if self.family_of is not None and len(self.families.all()):
            raise AttributeError("'{}' is a parent of another taxon and therefore cannot be a subcategory of '{}'".format(
                self.__str__(), self.family_of.__str__()
            ))

        if self.family_of and self.family_of.family_of is not None:
            raise AttributeError("'{}' is a subcategory of another taxon and therefore cannot be a parent of '{}'".format(
                self.family_of.__str__(), self.__str__()
            ))

        super(Macroinvertebrate, self).save(force_insert=force_insert, force_update=force_update, using=using,
                                            update_fields=update_fields)


class LeafPack(models.Model):
    """
    Leaf pack data
    """
    class Meta:
        db_table = 'leafpack'

    site_registration = models.ForeignKey(SiteRegistration, on_delete=models.CASCADE)
    placement_date = models.DateField()
    retrieval_date = models.DateField()
    leafpack_placement_count = models.IntegerField()
    leafpack_retrieval_count = models.IntegerField()
    placement_air_temp = models.FloatField(blank=True, null=True)
    placement_water_temp = models.FloatField(blank=True, null=True)
    retrieval_air_temp = models.FloatField(blank=True, null=True)
    retrieval_water_temp = models.FloatField(blank=True, null=True)
    had_storm = models.NullBooleanField()
    had_flood = models.NullBooleanField()
    had_drought = models.NullBooleanField()
    storm_count = models.IntegerField(default=0)
    storm_precipitation = models.FloatField(default=0)
    types = models.ManyToManyField('LeafPackType')
    deployment_type = models.CharField(max_length=255, blank=True, null=True)
    notes = models.TextField(blank=True, null=True)

    def taxon_count(self):
        """
        Gets the total count of taxon for the entire leafpack experiement.

        :return: total taxon count.
        """
        queryset = LeafPackBug.objects.filter(leaf_pack=self)
        taxon_counts = [lpg.bug_count - self.sub_taxon_count(lpg.bug) for lpg in queryset]

        return sum(taxon_counts)

    def sub_taxon_count(self, parent_taxon):
        queryset = LeafPackBug.objects.filter(leaf_pack=self)
        sub_taxon_count = queryset.filter(bug__family_of=parent_taxon).aggregate(Sum('bug_count'))

        if sub_taxon_count['bug_count__sum'] is not None:
            return sub_taxon_count['bug_count__sum']

        return 0

    def percent_EPT(self):
        """<commit_msg>Commenting out notes model field<commit_after># -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models
from dataloaderinterface.models import SiteRegistration
from accounts.models import User
from django.db.models import Sum, Q
from operator import __or__ as OR


class Macroinvertebrate(models.Model):
    """
    :var: scientific_name: The scientific name for the macroinvertebrate (i.e. 'Ephemeroptera')
    :var: common_name: The common for the macroinvertebrate (i.e. 'mayflies')
    :var: family_of: Establishes an Order-Family one-to-many relationships. For example, in biological classification,
     Diptera is the order of all true flies and includes the families of Anthericidae, Chironomidae, Simuliidae, etc.,
     but all are macroinvertebrate.
    """
    class Meta:
        db_table = 'macroinvertebrate'

    scientific_name = models.CharField(max_length=255, unique=True)
    # 'latin_name' is basically the same as 'scientific_name', but it
    # can be blank. This was added to resolve an issue with displaying
    # names on the website.
    latin_name = models.CharField(max_length=255, default='', blank=True)
    common_name = models.CharField(max_length=255, unique=True)
    family_of = models.ForeignKey('Macroinvertebrate',
                                  on_delete=models.CASCADE,
                                  null=True,
                                  blank=True,
                                  related_name='families')
    pollution_tolerance = models.FloatField(default=0)
    itis_serial_number = models.CharField(max_length=255, null=True, blank=True)
    url = models.CharField(max_length=255, null=True, blank=True)

    """
    'sort_priority' is used to determine the order taxon appear on things like django forms. HIGHER values  
    of sort_priority should appear first, and LOWER values last
    """
    sort_priority = models.FloatField(default=0)

    @property
    def is_ept(self):
        return self.scientific_name.lower() in ['ephemeroptera', 'plecoptera', 'tricoptera']

    @property
    def display_name(self):
        return self.__str__()

    def __str__(self):
        if not len(self.latin_name):
            return '{0}'.format(self.common_name, self.latin_name)
        return '{0} ({1})'.format(self.common_name, self.latin_name)

    def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
        if self.family_of is not None and len(self.families.all()):
            raise AttributeError("'{}' is a parent of another taxon and therefore cannot be a subcategory of '{}'".format(
                self.__str__(), self.family_of.__str__()
            ))

        if self.family_of and self.family_of.family_of is not None:
            raise AttributeError("'{}' is a subcategory of another taxon and therefore cannot be a parent of '{}'".format(
                self.family_of.__str__(), self.__str__()
            ))

        super(Macroinvertebrate, self).save(force_insert=force_insert, force_update=force_update, using=using,
                                            update_fields=update_fields)


class LeafPack(models.Model):
    """
    Leaf pack data
    """
    class Meta:
        db_table = 'leafpack'

    site_registration = models.ForeignKey(SiteRegistration, on_delete=models.CASCADE)
    placement_date = models.DateField()
    retrieval_date = models.DateField()
    leafpack_placement_count = models.IntegerField()
    leafpack_retrieval_count = models.IntegerField()
    placement_air_temp = models.FloatField(blank=True, null=True)
    placement_water_temp = models.FloatField(blank=True, null=True)
    retrieval_air_temp = models.FloatField(blank=True, null=True)
    retrieval_water_temp = models.FloatField(blank=True, null=True)
    had_storm = models.NullBooleanField()
    had_flood = models.NullBooleanField()
    had_drought = models.NullBooleanField()
    storm_count = models.IntegerField(default=0)
    storm_precipitation = models.FloatField(default=0)
    types = models.ManyToManyField('LeafPackType')
    deployment_type = models.CharField(max_length=255, blank=True, null=True)
    # notes = models.TextField(blank=True, null=True)

    def taxon_count(self):
        """
        Gets the total count of taxon for the entire leafpack experiement.

        :return: total taxon count.
        """
        queryset = LeafPackBug.objects.filter(leaf_pack=self)
        taxon_counts = [lpg.bug_count - self.sub_taxon_count(lpg.bug) for lpg in queryset]

        return sum(taxon_counts)

    def sub_taxon_count(self, parent_taxon):
        queryset = LeafPackBug.objects.filter(leaf_pack=self)
        sub_taxon_count = queryset.filter(bug__family_of=parent_taxon).aggregate(Sum('bug_count'))

        if sub_taxon_count['bug_count__sum'] is not None:
            return sub_taxon_count['bug_count__sum']

        return 0

    def percent_EPT(self):
        """
{'fraction_non_alphanumeric': 0.09520429889485958, 'fraction_numerical': 0.005475007604177228, 'mean_word_length': 3.1867572156196946, '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': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4001429', 'n_tokens_mistral': 3035, 'n_tokens_neox': 2884, 'n_words': 708}