<commit_before>from lxml import etree

from django.contrib.contenttypes.fields import GenericRelation
from django.db import models
from django.core.exceptions import ValidationError, ObjectDoesNotExist

from mezzanine.pages.page_processors import processor_for

import os

from hs_core.models import BaseResource, ResourceManager, resource_processor, \
    AbstractMetaDataElement

from hs_modelinstance.models import ModelInstanceMetaData, ModelOutput, ExecutedBy


def delete_if_empty(term_obj, non_standard_elements):
    all_blank = True
    for attr, val in vars(term_obj).iteritems():
        if attr in non_standard_elements and val != '':
            all_blank = False
            break
    if all_blank:
        term_obj.delete()


def uncouple(choices):
    uncoupled = [c[0] for c in choices]
    return uncoupled


def validate_choice(value, choices):
    choices = choices if isinstance(choices[0], basestring) else uncouple(choices)
    if 'Choose' in value:
        return ''
    if value not in choices:
        raise ValidationError('Invalid parameter: {} not in {}'.format(value, ", ".join(choices)))
    else:
        return value


class ModelOutput(ModelOutput):
    class Meta:
        proxy = True


class ExecutedBy(ExecutedBy):
    class Meta:
        proxy = True


# extended metadata elements for MODFLOW Model Instance resource type
class StudyArea(AbstractMetaDataElement):
    term = 'StudyArea'
    totalLength = models.CharField(max_length=100, null=True, blank=True,
                                   verbose_name='Total length in meters')
    totalWidth = models.CharField(max_length=100, null=True, blank=True,
                                  verbose_name='Total width in meters')
    maximumElevation = models.CharField(max_length=100, null=True, blank=True,
                                        verbose_name='Maximum elevation in meters')
    minimumElevation = models.CharField(max_length=100, null=True, blank=True,
                                        verbose_name='Minimum elevation in meters')

    def __unicode__(self):
        return self.totalLength

    class Meta:
        # StudyArea element is not repeatable
        unique_together = ("content_type", "object_id")

    @classmethod
    def update(cls, element_id, **kwargs):
        study_area = super(StudyArea, cls).update(element_id, **kwargs)
        delete_if_empty(study_area,
                        ['totalLength', 'totalWidth', 'maximumElevation', 'minimumElevation'])



class GridDimensions(AbstractMetaDataElement):
    term = 'GridDimensions'
    gridTypeChoices = (('Regular', 'Regular'), ('Irregular', 'Irregular'),)

    numberOfLayers = models.CharField(max_length=100, null=True, blank=True,
                                      verbose_name='Number of layers')
    typeOfRows = models.CharField(max_length=100, choices=gridTypeChoices, null=True, blank=True,
                                  verbose_name='Type of rows')
    numberOfRows = models.CharField(max_length=100, null=True, blank=True,
                                    verbose_name='Number of rows')<commit_msg>[#1550] fixed pylint issue<commit_after>from lxml import etree

from django.contrib.contenttypes.fields import GenericRelation
from django.db import models
from django.core.exceptions import ValidationError, ObjectDoesNotExist

from mezzanine.pages.page_processors import processor_for

import os

from hs_core.models import BaseResource, ResourceManager, resource_processor, \
    AbstractMetaDataElement

from hs_modelinstance.models import ModelInstanceMetaData, ModelOutput, ExecutedBy


def delete_if_empty(term_obj, non_standard_elements):
    all_blank = True
    for attr, val in vars(term_obj).iteritems():
        if attr in non_standard_elements and val != '':
            all_blank = False
            break
    if all_blank:
        term_obj.delete()


def uncouple(choices):
    uncoupled = [c[0] for c in choices]
    return uncoupled


def validate_choice(value, choices):
    choices = choices if isinstance(choices[0], basestring) else uncouple(choices)
    if 'Choose' in value:
        return ''
    if value not in choices:
        raise ValidationError('Invalid parameter: {} not in {}'.format(value, ", ".join(choices)))
    else:
        return value


class ModelOutput(ModelOutput):
    class Meta:
        proxy = True


class ExecutedBy(ExecutedBy):
    class Meta:
        proxy = True


# extended metadata elements for MODFLOW Model Instance resource type
class StudyArea(AbstractMetaDataElement):
    term = 'StudyArea'
    totalLength = models.CharField(max_length=100, null=True, blank=True,
                                   verbose_name='Total length in meters')
    totalWidth = models.CharField(max_length=100, null=True, blank=True,
                                  verbose_name='Total width in meters')
    maximumElevation = models.CharField(max_length=100, null=True, blank=True,
                                        verbose_name='Maximum elevation in meters')
    minimumElevation = models.CharField(max_length=100, null=True, blank=True,
                                        verbose_name='Minimum elevation in meters')

    def __unicode__(self):
        return self.totalLength

    class Meta:
        # StudyArea element is not repeatable
        unique_together = ("content_type", "object_id")

    @classmethod
    def update(cls, element_id, **kwargs):
        study_area = super(StudyArea, cls).update(element_id, **kwargs)
        delete_if_empty(study_area,
                        ['totalLength', 'totalWidth', 'maximumElevation', 'minimumElevation'])


class GridDimensions(AbstractMetaDataElement):
    term = 'GridDimensions'
    gridTypeChoices = (('Regular', 'Regular'), ('Irregular', 'Irregular'),)

    numberOfLayers = models.CharField(max_length=100, null=True, blank=True,
                                      verbose_name='Number of layers')
    typeOfRows = models.CharField(max_length=100, choices=gridTypeChoices, null=True, blank=True,
                                  verbose_name='Type of rows')
    numberOfRows = models.CharField(max_length=100, null=True, blank=True,
                                    verbose_name='Number of rows')
{'fraction_non_alphanumeric': 0.08756426735218509, 'fraction_numerical': 0.008033419023136246, 'mean_word_length': 2.9224952741020793, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7300663', 'n_tokens_mistral': 1769, 'n_tokens_neox': 1649, 'n_words': 430}