<commit_before>"""
jobber.view_helpers
~~~~~~~~~~~~~~~~~~~

Utility functions for views.

"""
from jobber.core.models import Job, Company, Location, Tag
from jobber.core.forms import JobForm
from jobber.core.utils import insert_email_token
from jobber.functions import send_admin_review_email
from jobber.conf import settings


REVIEWER_ROBOT = settings.MAIL_REVIEWER_ROBOT


def get_location_context():
    """Returns location data for the create/edit form."""
    return [
        dict(id=location.id, city=location.city, country_code=location.country_code)
        for location in Location.query.all()
    ]


def get_tag_context():
    """Returns tags data for the create/edit form."""
    return [
        dict(slug=tag.slug, tag=tag.tag) for tag in Tag.query.all()
    ]


def populate_form(job):
    """Populates a `JobForm` object from a `Job` model.

    :param job: A `Job` instance.

    """
    location = job.location
    company = job.company

    related_data = {
        'location__id': location.id,
        'location__city': location.city,
        'location__country_code': location.country_code,
        'company__id': company.id,
        'company__name': company.name,
        'company__website': company.website_with_protocol
    }

    return JobForm(obj=job, **related_data)


def populate_company(job, form_data):
    """Populates the company relation from `job` and `form_data`.

    :param job: A `Job` instance.
    :param form_data: Form data as a `dict`.

    """
    company = None
    company_id = form_data['company__id']

    if job.company_id is not None and (job.company_id == company_id):
        return

    if company_id:
        company = Company.query.get(company_id)

    if not company:
        name = form_data['company__name']
        website = form_data.get('company__website')
        company = Company(name=name, website=website)

    job.company = company


def populate_location(job, form_data):
    """Populates the location relation from `job` and `form_data`.

    :param job: A `Job` instance.
    :param form_data: Form data as a `dict`.

    """
    location = None
    location_id = form_data['location__id']

    if job.location_id is not None and (job.location_id == location_id):
        return

    if location_id:
        location = Location.query.get(location_id)
<commit_msg>Use new session in view helpers<commit_after>"""
jobber.view_helpers
~~~~~~~~~~~~~~~~~~~

Utility functions for views.

"""
from jobber.core.models import Job, Company, Location, Tag
from jobber.core.forms import JobForm
from jobber.core.utils import insert_email_token
from jobber.functions import send_admin_review_email
from jobber.conf import settings
from jobber.database import db


REVIEWER_ROBOT = settings.MAIL_REVIEWER_ROBOT


def get_location_context():
    """Returns location data for the create/edit form."""
    return [
        dict(id=location.id, city=location.city, country_code=location.country_code)
        for location in db.session.query(Location).all()
    ]


def get_tag_context():
    """Returns tags data for the create/edit form."""
    return [
        dict(slug=tag.slug, tag=tag.tag) for tag in db.session.query(Tag).all()
    ]


def populate_form(job):
    """Populates a `JobForm` object from a `Job` model.

    :param job: A `Job` instance.

    """
    location = job.location
    company = job.company

    related_data = {
        'location__id': location.id,
        'location__city': location.city,
        'location__country_code': location.country_code,
        'company__id': company.id,
        'company__name': company.name,
        'company__website': company.website_with_protocol
    }

    return JobForm(obj=job, **related_data)


def populate_company(job, form_data):
    """Populates the company relation from `job` and `form_data`.

    :param job: A `Job` instance.
    :param form_data: Form data as a `dict`.

    """
    company = None
    company_id = form_data['company__id']

    if job.company_id is not None and (job.company_id == company_id):
        return

    if company_id:
        company = db.session.query(Company).get(company_id)

    if not company:
        name = form_data['company__name']
        website = form_data.get('company__website')
        company = Company(name=name, website=website)

    job.company = company


def populate_location(job, form_data):
    """Populates the location relation from `job` and `form_data`.

    :param job: A `Job` instance.
    :param form_data: Form data as a `dict`.

    """
    location = None
    location_id = form_data['location__id']

    if job.location_id is not None and (job.location_id == location_id):
        return

    if location_id:
        location = db.session.query(Location).get(location_id)

{'fraction_non_alphanumeric': 0.11297421924124922, 'fraction_numerical': 0.0, 'mean_word_length': 3.2645218945487042, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7211291', 'n_tokens_mistral': 1483, 'n_tokens_neox': 1430, 'n_words': 391}