<commit_before>'''The definition of the participant object and DB marshalling.
'''

import api_util

import data_access_object
import identifier

from protorpc import messages
from google.appengine.ext import ndb
from google.appengine.ext.ndb import msgprop

DATE_OF_BIRTH_FORMAT = '%Y-%m-%d'

class PhysicalExamStatus(messages.Enum):
  """The state of the participant's physical exam"""
  SCHEDULED = 1
  COMPLETED = 2
  RESULT_READY = 3


class MembershipTier(messages.Enum):
  """The state of the participant"""
  REGISTERED = 1
  VOLUNTEER = 2
  FULL_PARTICIPANT = 3
  ENROLLEE = 4
  # Note that these are out of order; ENROLEE was added after FULL_PARTICIPANT.


class GenderIdentity(messages.Enum):
  """The gender identity of the participant."""
  FEMALE = 1
  MALE = 2
  NEITHER = 3
  OTHER = 4
  PREFER_NOT_TO_SAY = 5


class RecruitmentSource(messages.Enum):
  HPO = 1
  DIRECT_VOLUNTEER = 2


class Participant(ndb.Model):
  """The participant resource definition"""
  participant_id = ndb.StringProperty()
  biobank_id = ndb.StringProperty()
  first_name = ndb.StringProperty()
  first_name_search = ndb.ComputedProperty(
      lambda self: api_util.searchable_representation(self.first_name))
  middle_name = ndb.StringProperty()
  last_name = ndb.StringProperty()
  last_name_search = ndb.ComputedProperty(
      lambda self: api_util.searchable_representation(self.last_name))
  zip_code = ndb.StringProperty()
  date_of_birth = ndb.DateProperty()
  gender_identity = msgprop.EnumProperty(GenderIdentity)
  membership_tier = msgprop.EnumProperty(MembershipTier)
  physical_exam_status = msgprop.EnumProperty(PhysicalExamStatus)
  sign_up_time = ndb.DateTimeProperty()
  consent_time = ndb.DateTimeProperty()
  hpo_id = ndb.StringProperty()
  recruitment_source = msgprop.EnumProperty(RecruitmentSource)


class ParticipantDAO(data_access_object.DataAccessObject):
  def __init__(self):
    super(ParticipantDAO, self).__init__(Participant)

  def properties_from_json(self, dict_, ancestor_id, id_):
    if id_:
      dict_['participant_id'] = id_

    api_util.parse_json_date(dict_, 'date_of_birth', DATE_OF_BIRTH_FORMAT)
    api_util.parse_json_date(dict_, 'sign_up_time')
    api_util.parse_json_date(dict_, 'consent_time')
    api_util.parse_json_enum(dict_, 'gender_identity', GenderIdentity)
    api_util.parse_json_enum(dict_, 'membership_tier', MembershipTier)
    api_util.parse_json_enum(dict_, 'physical_exam_status', PhysicalExamStatus)
    api_util.parse_json_enum(dict_, 'recruitment_source', RecruitmentSource)
    return dict_

  def properties_to_json(self, dict_):
    api_util.format_json_date(dict_, 'date_of_birth', DATE_OF_BIRTH_FORMAT)
    api_util.format_json_date(dict_, 'sign_up_time')
    api_util.format_json_date(dict_, 'consent_time')
    api_util.format_json_enum(dict_, 'gender_identity')
    api_util.format_json_enum(dict_, 'membership_tier')
    api_util.format_json_enum(dict_, 'physical_exam_status')
    api_util.format_json_enum(dict_, 'recruitment_source')
    api_util.remove_field(dict_, 'first_name_search')
    api_util.remove_field(dict_, 'last_name_search')
    return dict_


  def list(self, first_name, last_name, dob_string, zip_code):
    date_of_birth = api_util.parse_date(dob_string, DATE_OF_BIRTH_FORMAT)
    query = Participant.query(
        Participant.last_name_search == api_util.searchable_representation(last_name),
        Participant.date_of_birth == date_of_birth)
    if first_name:
      query = query.filter(
          Participant.first_name_search == api_util.searchable_representation(first_name))

    if zip_code:
      query = query.filter(Participant.zip_code == zip_code)

    items = []
    for p in query.fetch():
      items.append(self.to_json(p))
    return {"items": items}

  def allocate_id(self):
    _id = identifier.get_id()
    return 'P{:d}'.format(_id).zfill(9)
<commit_msg>Changing "exam" to "evaluation" in participant.<commit_after>'''The definition of the participant object and DB marshalling.
'''

import api_util

import data_access_object
import identifier

from protorpc import messages
from google.appengine.ext import ndb
from google.appengine.ext.ndb import msgprop

DATE_OF_BIRTH_FORMAT = '%Y-%m-%d'

class PhysicalEvaluationStatus(messages.Enum):
  """The state of the participant's physical evaluation"""
  SCHEDULED = 1
  COMPLETED = 2
  RESULT_READY = 3


class MembershipTier(messages.Enum):
  """The state of the participant"""
  REGISTERED = 1
  VOLUNTEER = 2
  FULL_PARTICIPANT = 3
  ENROLLEE = 4
  # Note that these are out of order; ENROLEE was added after FULL_PARTICIPANT.


class GenderIdentity(messages.Enum):
  """The gender identity of the participant."""
  FEMALE = 1
  MALE = 2
  NEITHER = 3
  OTHER = 4
  PREFER_NOT_TO_SAY = 5


class RecruitmentSource(messages.Enum):
  HPO = 1
  DIRECT_VOLUNTEER = 2


class Participant(ndb.Model):
  """The participant resource definition"""
  participant_id = ndb.StringProperty()
  biobank_id = ndb.StringProperty()
  first_name = ndb.StringProperty()
  first_name_search = ndb.ComputedProperty(
      lambda self: api_util.searchable_representation(self.first_name))
  middle_name = ndb.StringProperty()
  last_name = ndb.StringProperty()
  last_name_search = ndb.ComputedProperty(
      lambda self: api_util.searchable_representation(self.last_name))
  zip_code = ndb.StringProperty()
  date_of_birth = ndb.DateProperty()
  gender_identity = msgprop.EnumProperty(GenderIdentity)
  membership_tier = msgprop.EnumProperty(MembershipTier)
  physical_evaluation_status = msgprop.EnumProperty(PhysicalEvaluationStatus)
  sign_up_time = ndb.DateTimeProperty()
  consent_time = ndb.DateTimeProperty()
  hpo_id = ndb.StringProperty()
  recruitment_source = msgprop.EnumProperty(RecruitmentSource)


class ParticipantDAO(data_access_object.DataAccessObject):
  def __init__(self):
    super(ParticipantDAO, self).__init__(Participant)

  def properties_from_json(self, dict_, ancestor_id, id_):
    if id_:
      dict_['participant_id'] = id_

    api_util.parse_json_date(dict_, 'date_of_birth', DATE_OF_BIRTH_FORMAT)
    api_util.parse_json_date(dict_, 'sign_up_time')
    api_util.parse_json_date(dict_, 'consent_time')
    api_util.parse_json_enum(dict_, 'gender_identity', GenderIdentity)
    api_util.parse_json_enum(dict_, 'membership_tier', MembershipTier)
    api_util.parse_json_enum(dict_, 'physical_evaluation_status', PhysicalEvaluationStatus)
    api_util.parse_json_enum(dict_, 'recruitment_source', RecruitmentSource)
    return dict_

  def properties_to_json(self, dict_):
    api_util.format_json_date(dict_, 'date_of_birth', DATE_OF_BIRTH_FORMAT)
    api_util.format_json_date(dict_, 'sign_up_time')
    api_util.format_json_date(dict_, 'consent_time')
    api_util.format_json_enum(dict_, 'gender_identity')
    api_util.format_json_enum(dict_, 'membership_tier')
    api_util.format_json_enum(dict_, 'physical_evaluation_status')
    api_util.format_json_enum(dict_, 'recruitment_source')
    api_util.remove_field(dict_, 'first_name_search')
    api_util.remove_field(dict_, 'last_name_search')
    return dict_


  def list(self, first_name, last_name, dob_string, zip_code):
    date_of_birth = api_util.parse_date(dob_string, DATE_OF_BIRTH_FORMAT)
    query = Participant.query(
        Participant.last_name_search == api_util.searchable_representation(last_name),
        Participant.date_of_birth == date_of_birth)
    if first_name:
      query = query.filter(
          Participant.first_name_search == api_util.searchable_representation(first_name))

    if zip_code:
      query = query.filter(Participant.zip_code == zip_code)

    items = []
    for p in query.fetch():
      items.append(self.to_json(p))
    return {"items": items}

  def allocate_id(self):
    _id = identifier.get_id()
    return 'P{:d}'.format(_id).zfill(9)

{'fraction_non_alphanumeric': 0.09560888435026806, 'fraction_numerical': 0.0038294613224406433, 'mean_word_length': 5.135473766640564, 'pattern_counts': {'":': 2, '<': 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': '4404682', 'n_tokens_mistral': 2747, 'n_tokens_neox': 2621, 'n_words': 485}