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


import hashlib
import re
from copy import copy

from django.conf import settings
from django.contrib.gis.geos import Point, MultiPolygon
from django.core.mail import send_mail
from django.core.exceptions import (ValidationError, MultipleObjectsReturned,
                                    ObjectDoesNotExist)
from django.core import validators
from django.http import Http404
from django.contrib.gis.db import models
from django.contrib.gis.measure import D
from django.db import IntegrityError, transaction
from django.db.models.signals import post_save, post_delete
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import (UserManager, AbstractBaseUser,
                                        PermissionsMixin)
from django.template.loader import get_template

from treemap.species.codes import ITREE_REGIONS, get_itree_code
from treemap.audit import Auditable, Role, Dictable, Audit, PendingAuditable
# Import this even though it's not referenced, so Django can find it
from treemap.audit import UserTrackable, FieldPermission  # NOQA
from treemap.util import leaf_models_of_class, to_object_name
from treemap.decorators import classproperty
from treemap.images import save_uploaded_image
from treemap.units import Convertible
from treemap.udf import UDFModel
from treemap.instance import Instance
from treemap.lib.object_caches import invalidate_adjuncts


def _action_format_string_for_location(action):
    """A helper that allows multiple auditable models to return the
    same action format string for a field value that should be displayed
    as a location"""
    lang = {
        Audit.Type.Insert: _('set the location'),
        Audit.Type.Update: _('updated the location'),
        Audit.Type.Delete: _('deleted the location'),
        Audit.Type.PendingApprove: _('approved an edit of the location'),
        Audit.Type.PendingReject: _('rejected an edit of the location')
    }
    return lang[action]


def _action_format_string_for_readonly(action, readonly):
    """A helper that allows multiple auditable models to return the
    the state of a readonly boolean"""
    if readonly:
        value = _("read only")
    else:
        value = _("editable")
    lang = {
        Audit.Type.Insert: _('made the tree %(value)s'),
        Audit.Type.Update: _('made the tree %(value)s'),
        Audit.Type.Delete: _('made the tree %(value)s'),
        Audit.Type.PendingApprove: _('approved making the tree %(value)s'),
        Audit.Type.PendingReject: _('approved making the tree %(value)s')
    }
    return lang[action] % {'value': value}


class StaticPage(models.Model):
    instance = models.ForeignKey(Instance)
    name = models.CharField(max_length=100)
    content = models.TextField()

    DEFAULT_CONTENT = {
        'resources': get_template('treemap/partials/Resources.html').render(),
        'about': get_template('treemap/partials/About.html').render(),
        'faq': get_template('treemap/partials/FAQ.html').render()
    }

    @staticmethod
    def built_in_names():
        return ['Resources', 'FAQ', 'About', 'Partners']

    @staticmethod
    def get_or_new(instance, page_name, only_create_built_ins=True):
        '''
        If static page exists, return it;
        otherwise construct one (without saving).
        Make sure the returned object's name is cased correctly
        if it matches an existing object name or built-in name.
        '''
        try:
            static_page = StaticPage.objects.get(name__iexact=page_name,
                                                 instance=instance)
        except StaticPage.DoesNotExist:
            built_in_name = StaticPage._get_built_in_name(page_name)
            if built_in_name:
                page_name = built_in_name
            elif only_create_built_ins:
                raise Http404('Static page does not exist')

            static_page = StaticPage(
                instance=instance, name=page_name,
                content=StaticPage.DEFAULT_CONTENT.get(
                    page_name.lower(),
                    ('There is no content for this page yet.')))
        return static_page

    @staticmethod
    def _get_built_in_name(page_name):
        page_name_lower = page_name.lower()
        for name in StaticPage.built_in_names():
            if page_name_lower == name.lower():
                return name
        return None

    def __unicode__(self):
        return self.name


class BenefitCurrencyConversion(Dictable, models.Model):<commit_msg>Defer Template.render usage to avoid circular reference at module load<commit_after># -*- coding: utf-8 -*-
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division


import hashlib
import re
from copy import copy

from django.conf import settings
from django.contrib.gis.geos import Point, MultiPolygon
from django.core.mail import send_mail
from django.core.exceptions import (ValidationError, MultipleObjectsReturned,
                                    ObjectDoesNotExist)
from django.core import validators
from django.http import Http404
from django.contrib.gis.db import models
from django.contrib.gis.measure import D
from django.db import IntegrityError, transaction
from django.db.models.signals import post_save, post_delete
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import (UserManager, AbstractBaseUser,
                                        PermissionsMixin)
from django.template.loader import get_template

from treemap.species.codes import ITREE_REGIONS, get_itree_code
from treemap.audit import Auditable, Role, Dictable, Audit, PendingAuditable
# Import this even though it's not referenced, so Django can find it
from treemap.audit import UserTrackable, FieldPermission  # NOQA
from treemap.util import leaf_models_of_class, to_object_name
from treemap.decorators import classproperty
from treemap.images import save_uploaded_image
from treemap.units import Convertible
from treemap.udf import UDFModel
from treemap.instance import Instance
from treemap.lib.object_caches import invalidate_adjuncts


def _action_format_string_for_location(action):
    """A helper that allows multiple auditable models to return the
    same action format string for a field value that should be displayed
    as a location"""
    lang = {
        Audit.Type.Insert: _('set the location'),
        Audit.Type.Update: _('updated the location'),
        Audit.Type.Delete: _('deleted the location'),
        Audit.Type.PendingApprove: _('approved an edit of the location'),
        Audit.Type.PendingReject: _('rejected an edit of the location')
    }
    return lang[action]


def _action_format_string_for_readonly(action, readonly):
    """A helper that allows multiple auditable models to return the
    the state of a readonly boolean"""
    if readonly:
        value = _("read only")
    else:
        value = _("editable")
    lang = {
        Audit.Type.Insert: _('made the tree %(value)s'),
        Audit.Type.Update: _('made the tree %(value)s'),
        Audit.Type.Delete: _('made the tree %(value)s'),
        Audit.Type.PendingApprove: _('approved making the tree %(value)s'),
        Audit.Type.PendingReject: _('approved making the tree %(value)s')
    }
    return lang[action] % {'value': value}


class StaticPage(models.Model):
    instance = models.ForeignKey(Instance)
    name = models.CharField(max_length=100)
    content = models.TextField()

    DEFAULT_CONTENT = {
        'resources': 'treemap/partials/Resources.html',
        'about': 'treemap/partials/About.html',
        'faq': 'treemap/partials/FAQ.html'
    }

    @staticmethod
    def built_in_names():
        return ['Resources', 'FAQ', 'About', 'Partners']

    @staticmethod
    def get_or_new(instance, page_name, only_create_built_ins=True):
        '''
        If static page exists, return it;
        otherwise construct one (without saving).
        Make sure the returned object's name is cased correctly
        if it matches an existing object name or built-in name.
        '''
        try:
            static_page = StaticPage.objects.get(name__iexact=page_name,
                                                 instance=instance)
        except StaticPage.DoesNotExist:
            built_in_name = StaticPage._get_built_in_name(page_name)
            if built_in_name:
                page_name = built_in_name
            elif only_create_built_ins:
                raise Http404('Static page does not exist')

            if page_name.lower() in StaticPage.DEFAULT_CONTENT:
                template = get_template(StaticPage.DEFAULT_CONTENT[page_name])
                content = template.render()
            else:
                content = 'There is no content for this page yet.'

            static_page = StaticPage(instance=instance, name=page_name,
                                     content=content)
        return static_page

    @staticmethod
    def _get_built_in_name(page_name):
        page_name_lower = page_name.lower()
        for name in StaticPage.built_in_names():
            if page_name_lower == name.lower():
                return name
        return None

    def __unicode__(self):
        return self.name


class BenefitCurrencyConversion(Dictable, models.Model):
{'fraction_non_alphanumeric': 0.07960981749528005, 'fraction_numerical': 0.0020977554017201595, 'mean_word_length': 2.972916666666667, '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': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4486317', 'n_tokens_mistral': 2680, 'n_tokens_neox': 2603, 'n_words': 774}