<commit_before>from django.utils.translation import gettext as _
from django.utils.html import format_html
from django.utils.safestring import mark_safe

from couchdbkit.exceptions import ResourceNotFound

from couchforms.analytics import get_form_analytics_metadata
from dimagi.utils.couch import IncompatibleDocument, get_cached_property
from dimagi.utils.couch.safe_index import safe_index

from corehq.apps.hqcase.utils import SYSTEM_FORM_XMLNS_MAP
from corehq.apps.users.models import CouchUser
from corehq.const import USER_DATETIME_FORMAT_WITH_SEC
from corehq.util.dates import iso_string_to_datetime
from corehq.util.timezones.conversions import PhoneTime, ServerTime
from corehq.util.view_utils import absolute_reverse


class StringWithAttributes(str):

    def replace(self, *args):
        string = super(StringWithAttributes, self).replace(*args)
        return StringWithAttributes(string)


class FormDisplay(object):

    def __init__(self, form_doc, report, lang=None):
        self.form = form_doc
        self.report = report
        self.lang = lang

    @property
    def form_data_link(self):
        return format_html(
            "<a class='ajax_dialog' target='_new' href='{url}'>{text}</a>",
            url=absolute_reverse('render_form_data', args=[self.report.domain, self.form['_id']]),
            text=_("View Form")
        )

    @property
    def username(self):
        uid = self.form["form"]["meta"]["userID"]
        username = self.form["form"]["meta"].get("username")
        try:
            if username not in ['demo_user', 'admin']:
                full_name = get_cached_property(CouchUser, uid, 'full_name', expiry=7*24*60*60)
                name = '"%s"' % full_name if full_name else ""
            else:
                name = ""
        except (ResourceNotFound, IncompatibleDocument):
            name = mark_safe('<b>[unregistered]</b>')  # nosec: no user input
        username = username or _('No data for username')<commit_msg>FormDisplay cleanup<commit_after>from django.utils.translation import gettext as _
from django.utils.html import format_html
from django.utils.safestring import mark_safe

from couchdbkit.exceptions import ResourceNotFound

from couchforms.analytics import get_form_analytics_metadata
from dimagi.utils.couch import IncompatibleDocument, get_cached_property
from dimagi.utils.couch.safe_index import safe_index

from corehq.apps.hqcase.utils import SYSTEM_FORM_XMLNS_MAP
from corehq.apps.users.models import CouchUser
from corehq.const import USER_DATETIME_FORMAT_WITH_SEC
from corehq.util.dates import iso_string_to_datetime
from corehq.util.timezones.conversions import PhoneTime, ServerTime
from corehq.util.view_utils import absolute_reverse


ONE_WEEK = 7 * 24 * 60 * 60


class StringWithAttributes(str):

    def replace(self, *args):
        string = super(StringWithAttributes, self).replace(*args)
        return StringWithAttributes(string)


class FormDisplay():

    def __init__(self, form_doc, report, lang=None):
        self.form = form_doc
        self.report = report
        self.lang = lang

    @property
    def form_data_link(self):
        return format_html(
            "<a class='ajax_dialog' target='_new' href='{url}'>{text}</a>",
            url=absolute_reverse('render_form_data', args=[self.report.domain, self.form['_id']]),
            text=_("View Form")
        )

    @property
    def username(self):
        uid = self.form["form"]["meta"]["userID"]
        username = self.form["form"]["meta"].get("username")
        try:
            if username not in ['demo_user', 'admin']:
                full_name = get_cached_property(CouchUser, uid, 'full_name', expiry=ONE_WEEK)
                name = '"%s"' % full_name if full_name else ""
            else:
                name = ""
        except (ResourceNotFound, IncompatibleDocument):
            name = mark_safe('<b>[unregistered]</b>')  # nosec: no user input
        username = username or _('No data for username')
{'fraction_non_alphanumeric': 0.10335335335335336, 'fraction_numerical': 0.0035035035035035035, 'mean_word_length': 3.455964325529543, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '5912737', 'n_tokens_mistral': 1195, 'n_tokens_neox': 1169, 'n_words': 275}