Exemple #1
0
def send_request(actor_id, to_id):
    """Sends a request from one account to another.

    TODO(kanat): Send email notification.

    :param actor_id: (int) ID of the account sending.
    :param to_id: (int) ID of the account receiving.
    :return: (kinds.connections.ConnRequest)
        Connection request that was created.
    """
    asserts.valid_id_type(actor_id)
    asserts.valid_id_type(to_id)

    if actor_id == to_id:
        raise exp.BadRequestExp('Cannot send a request to self.')
    if not services.accounts.account_by_id(to_id):
        logging.warning('to_id={} does not exist.'.format(to_id))
        raise exp.NotFoundExp('Account does not exist.')
    # Maybe one or the other already sent a request.
    req = (_conn_request_get(from_id=actor_id, to_id=to_id)
           or _conn_request_get(from_id=to_id, to_id=actor_id))
    if req is not None:
        logging.info('Connection request already exists.')
        return
    req = ConnRequest(from_id=actor_id, to_id=to_id)
    req.put()
    return req
Exemple #2
0
def select_applicant(actor_id, job_id, caregiver_id):
    """Select an applicant for the job.

    TODO(kanat): Notify selected applicant.

    :param actor_id: (int) ID of the account acting.
    :param caregiver_id: (int) ID of the job applicant's account.
    :return: (None)
    """
    asserts.valid_id_type(caregiver_id)
    _assert_job_owner(actor_id, job_id)

    job = get_by_id_ndb(job_id)
    if job.status != JobStatus.Open:
        raise exp.BadRequestExp('Job is not open')
    if actor_id == caregiver_id:
        raise exp.BadRequestExp('Can\'t select yourself.')
    apps = _get_applicants(job_id)
    applicant = apps.find_applicant(caregiver_id)
    if applicant is None:
        raise exp.NotFoundExp('Applicant not found.')
    # Set up job invoice.
    invoice = JobInvoice(job_id=job.id,
                         start_date=job.start_date,
                         end_date=job.end_date)
    invoice.put()
    apps.selected = applicant
    job.status = JobStatus.Accepted
    job.invoice_id = invoice.id
    ndb.put_multi([apps, job])
Exemple #3
0
def remove_connection(actor_id, other_id):
    """Removes a connection from the account.

    :param actor_id: (int) ID of the account removing the connection.
    :param other_id: ID of the account to be removed.
    :return: (None)
    """
    asserts.valid_id_type(actor_id)
    asserts.valid_id_type(other_id)

    if not is_connected(actor_id, other_id):
        logging.warning('Trying to remove a connection that doesn\'t exist: '
                        'actor={}, other={}'.format(actor_id, other_id))
        raise exp.NotFoundExp('Not connected.')

    connlistA = _get_connlist(actor_id)
    connlistB = _get_connlist(other_id)
    # Find the connection request.
    req = (_conn_request_get(from_id=actor_id, to_id=other_id)
           or _conn_request_get(from_id=other_id, to_id=actor_id))
    try:
        connlistA.accepted_reqs.remove(req.id)
        connlistB.accepted_reqs.remove(req.id)
    except ValueError:
        pass
    ndb.put_multi([connlistA, connlistB])
    req.key.delete()
Exemple #4
0
def get_by_id_ndb(job_id):
    """Returns the job associated with the given job_id.

    :param job_id: (int) ID of the job.
    :return: (kinds.jobs.Job)
    """
    asserts.valid_id_type(job_id)

    job = Job.get_by_id(job_id)
    if job is None:
        logging.warning('Job not found. id={}'.format(job_id))
        raise exp.NotFoundExp()
    return job
Exemple #5
0
def account_by_email(email, _dto=True, _throw=True):
    """Returns the account associated with the given email address.

    :param email: (int) ID of the account.
    :return: (dto.accounts.AccountDto) if _dto is True.
             (kinds.accounts.Account) if _dto is False.
             (None) if account does not exist.
    """
    asserts.type_of(email, basestring)

    account = Account.query(Account.email == email.lower()).get()
    if account is None and _throw:
        logging.warning('account not found. email={}'.format(email))
        raise exp.NotFoundExp('Account not found.')
    if not _dto:
        return account
    return AccountDto.from_account_ndb(account)
Exemple #6
0
def account_by_id(account_id, _dto=True, _throw=True):
    """Returns the account associated with the given account_id.

    :param account_id: (int) ID of the account.
    :return: (dto.accounts.AccountDto) if _dto is True.
             (kinds.accounts.Account) if _dto is False.
             (None) if account does not exist.
    :raise: (exp.NotFoundExp) if `_throw` is True and account is not found.
    """
    asserts.valid_id_type(account_id)

    account = Account.get_by_id(account_id)
    if account is None and _throw:
        logging.warning('account not found. id={}'.format(account_id))
        raise exp.NotFoundExp('Account not found.')
    if not _dto:
        return account
    return AccountDto.from_account_ndb(account)
Exemple #7
0
def patient_by_id(actor_id, patient_id, _dto=True):
    """Returns the patient associated with the given account and patient_id.

    :param actor_id: (int) ID of the account performing the action.
    :param patient_id: (int) ID of the patient to retrieve.
    :return: (dto.accounts.PatientDto) if _dto is True
             (kinds.accounts.Patient) if _dto is False
    """
    asserts.valid_id_type(actor_id)
    asserts.valid_id_type(patient_id)

    account = account_by_id(actor_id, _dto=False)
    if patient_id not in account.patient_ids:
        raise exp.NotFoundExp('Care recipient not found.')
    patient = Patient.get_by_id(patient_id)
    if not _dto:
        return patient
    return PatientDto.from_patient_ndb(patient)
Exemple #8
0
def decline_request(actor_id, from_id):
    """Declines a connection request from the given account.

    :param actor_id: (int) ID of the account.
    :param from_id: (int) ID of the account that sent the connection request.
    :return: (None)
    """
    asserts.valid_id_type(actor_id)
    asserts.valid_id_type(from_id)

    req = _conn_request_get(to_id=actor_id,
                            from_id=from_id,
                            status=ConnStatus.Pending)
    if req is None:
        logging.warning(
            'Pending request does not exist: to={}, from={}'.format(
                actor_id, from_id))
        raise exp.NotFoundExp('Request does not exist.')
    req.key.delete()
Exemple #9
0
def accept_request(actor_id, from_id):
    """Accepts a connection request from the given account.

    TODO(kanat): Send email notification.

    :param actor_id: (int) ID of the account.
    :param from_id: (int) ID of the account that sent the connection request.
    :return: (kinds.connections.ConnRequest) Accepted connection request.
    """
    asserts.valid_id_type(actor_id)
    asserts.valid_id_type(from_id)

    req = _conn_request_get(to_id=actor_id,
                            from_id=from_id,
                            status=ConnStatus.Pending)
    if req is None:
        logging.warning(
            'Pending request does not exist: to={}, from={}'.format(
                actor_id, from_id))
        raise exp.NotFoundExp('Request does not exist.')
    _accept_request(req)
    return req