<commit_before>"""
resourceview.py

Contains administrative views for working with resources.
"""
from datetime import date, datetime

from admin_helpers import *

from sqlalchemy import or_, not_, and_, func
from flask import current_app, redirect, flash, request, url_for
from flask.ext.admin import BaseView, expose
from flask.ext.admin.form import rules
from flask.ext.admin.helpers import get_redirect_target
from flask.ext.admin.actions import action
from flask.ext.admin.contrib.sqla import ModelView
from flask.ext.admin.contrib.sqla.filters import FilterEmpty
from wtforms import DecimalField, validators

import geopy
from geopy.exc import *

from remedy.remedyblueprint import group_active_populations, group_active_categories
from remedy.rad.models import Resource, Category, Population, Review
from remedy.rad.geocoder import Geocoder
from remedy.rad.nullablebooleanfield import NullableBooleanField
from remedy.rad.plaintextfield import PlainTextField
from remedy.rad.statichtmlfield import StaticHtmlField


def scaffold_resource_form(form_class):
    """
    Scaffolds the provided resource form class by ensuring
    location fields are optional and that nullable flag fields
    are actually handled as nullable.

    Args:
        form_class: The form class to update.
        column_labels: The column labels to use.

    Returns:
        The updated form class.
    """
    # Override the latitude/longitude fields to be optional
    form_class.latitude = DecimalField(validators=[validators.Optional()])
    form_class.longitude = DecimalField(validators=[validators.Optional()])

    # Override the nullable flag fields to actually be nullable -
    # otherwise, Flask-Admin treats them as standard Boolean fields
    # (which is bad - we want the N/A option)
    form_class.is_wpath = NullableBooleanField(
        label=resource_column_labels['is_wpath'])
    form_class.is_icath = NullableBooleanField(
        label=resource_column_labels['is_icath'])
    form_class.is_accessible = NullableBooleanField(
        label=resource_column_labels['is_accessible'])
    form_class.has_sliding_scale = NullableBooleanField(
        label=resource_column_labels['has_sliding_scale'])

    return form_class


class ResourceView(AdminAuthMixin, ModelView):
    """
    An administrative view for working with resources.
    """
    can_view_details = True

    column_details_exclude_list = ('latitude', 'longitude', 
        'location', 'category_text')

    # Allow exporting
    can_export = True
    export_max_rows = 0
    column_export_list = ('name', 'organization', 'address',
        'url', 'email', 'phone', 'fax', 'hours', 'hospital_affiliation',
        'description', 'npi', 'categories', 'populations',
        'is_icath', 'is_wpath', 'is_accessible', 'has_sliding_scale', 'visible',
        'is_approved', 'submitted_user', 'submitted_date', 'submitted_ip',
        'source', 'notes', 'date_created', 'last_updated', 'date_verified', 'id')
    column_formatters_export = resource_export_formatters

    column_list = ('name', 'organization', 
        'address', 'url', 
        'source', 'last_updated')

    column_default_sort = 'name'

    column_searchable_list = ('name','description','organization','notes',)

    # By default, Flask-Admin isn't going to pick up on the fact
    # that our flags are nullable. Therefore, we need to manually
    # add FilterEmpty options. These use names identical to the
    # column labels for the normal filters so that they are appropriately grouped.
    column_filters = ('visible', 'is_approved', 'source', 'npi', 'date_verified',
        FilterEmpty(Resource.is_icath, 'Informed Consent/ICATH'), 'is_icath', 
        FilterEmpty(Resource.is_wpath, 'WPATH'), 'is_wpath', 
        FilterEmpty(Resource.is_accessible, 'ADA/Wheelchair Accessible'), 'is_accessible', 
        FilterEmpty(Resource.has_sliding_scale, 'Sliding Scale'), 'has_sliding_scale',
    )

    form_excluded_columns = ('date_created', 'last_updated', 
        'category_text', 'reviews', 'aggregateratings', 'submitted_user', 'submitted_ip',
        'submitted_date', 'is_approved')

    create_template = 'admin/resource_create.html'<commit_msg>adding form_rules for standard resource forms<commit_after>"""
resourceview.py

Contains administrative views for working with resources.
"""
from datetime import date, datetime

from admin_helpers import *

from sqlalchemy import or_, not_, and_, func
from flask import current_app, redirect, flash, request, url_for
from flask.ext.admin import BaseView, expose
from flask.ext.admin.form import rules
from flask.ext.admin.helpers import get_redirect_target
from flask.ext.admin.actions import action
from flask.ext.admin.contrib.sqla import ModelView
from flask.ext.admin.contrib.sqla.filters import FilterEmpty
from wtforms import DecimalField, validators

import geopy
from geopy.exc import *

from remedy.remedyblueprint import group_active_populations, group_active_categories
from remedy.rad.models import Resource, Category, Population, Review
from remedy.rad.geocoder import Geocoder
from remedy.rad.nullablebooleanfield import NullableBooleanField
from remedy.rad.plaintextfield import PlainTextField
from remedy.rad.statichtmlfield import StaticHtmlField


def scaffold_resource_form(form_class):
    """
    Scaffolds the provided resource form class by ensuring
    location fields are optional and that nullable flag fields
    are actually handled as nullable.

    Args:
        form_class: The form class to update.
        column_labels: The column labels to use.

    Returns:
        The updated form class.
    """
    # Override the latitude/longitude fields to be optional
    form_class.latitude = DecimalField(validators=[validators.Optional()])
    form_class.longitude = DecimalField(validators=[validators.Optional()])

    # Override the nullable flag fields to actually be nullable -
    # otherwise, Flask-Admin treats them as standard Boolean fields
    # (which is bad - we want the N/A option)
    form_class.is_wpath = NullableBooleanField(
        label=resource_column_labels['is_wpath'])
    form_class.is_icath = NullableBooleanField(
        label=resource_column_labels['is_icath'])
    form_class.is_accessible = NullableBooleanField(
        label=resource_column_labels['is_accessible'])
    form_class.has_sliding_scale = NullableBooleanField(
        label=resource_column_labels['has_sliding_scale'])

    return form_class


class ResourceView(AdminAuthMixin, ModelView):
    """
    An administrative view for working with resources.
    """
    can_view_details = True

    column_details_exclude_list = ('latitude', 'longitude', 
        'location', 'category_text')

    # Allow exporting
    can_export = True
    export_max_rows = 0
    column_export_list = ('name', 'organization', 'address',
        'url', 'email', 'phone', 'fax', 'hours', 'hospital_affiliation',
        'description', 'npi', 'categories', 'populations',
        'is_icath', 'is_wpath', 'is_accessible', 'has_sliding_scale', 'visible',
        'is_approved', 'submitted_user', 'submitted_date', 'submitted_ip',
        'source', 'notes', 'date_created', 'last_updated', 'date_verified', 'id')
    column_formatters_export = resource_export_formatters

    column_list = ('name', 'organization', 
        'address', 'url', 
        'source', 'last_updated')

    column_default_sort = 'name'

    column_searchable_list = ('name','description','organization','notes',)

    # By default, Flask-Admin isn't going to pick up on the fact
    # that our flags are nullable. Therefore, we need to manually
    # add FilterEmpty options. These use names identical to the
    # column labels for the normal filters so that they are appropriately grouped.
    column_filters = ('visible', 'is_approved', 'source', 'npi', 'date_verified',
        FilterEmpty(Resource.is_icath, 'Informed Consent/ICATH'), 'is_icath', 
        FilterEmpty(Resource.is_wpath, 'WPATH'), 'is_wpath', 
        FilterEmpty(Resource.is_accessible, 'ADA/Wheelchair Accessible'), 'is_accessible', 
        FilterEmpty(Resource.has_sliding_scale, 'Sliding Scale'), 'has_sliding_scale',
    )

    form_excluded_columns = ('date_created', 'last_updated', 
        'category_text', 'reviews', 'aggregateratings', 'submitted_user', 'submitted_ip',
        'submitted_date', 'is_approved')

    form_rules = [
        rules.FieldSet((rules.HTML('<hr />'), 'name', 'organization', 'description', 
            'categories', 'populations', 'visible'), 'Basic Information'),

        rules.FieldSet((rules.HTML('<hr />'), 'address', 'latitude', 'longitude', 'location',
            'phone', 'fax', 'email', 'url', 'hours'), 'Contact Information'),

        rules.FieldSet((rules.HTML('<hr />'), 'hospital_affiliation', 'is_icath', 'is_wpath',
            'is_accessible', 'has_sliding_scale')),

        rules.FieldSet((rules.HTML('<hr />'), 'npi', 'date_verified', 'notes', 'source'), 'Other')
    ]

    create_template = 'admin/resource_create.html'
{'fraction_non_alphanumeric': 0.10473235065942592, 'fraction_numerical': 0.0002216557685913776, 'mean_word_length': 4.3144876325088335, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '5812205', 'n_tokens_mistral': 2661, 'n_tokens_neox': 2527, 'n_words': 692}