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])
def create_thread_ndb(actor_id, recipients, text, subject=''): """Creates a new thread with given recipients, body text, and subject. TODO(kanat): check if recipient is a valid account. :param actor_id: (int) ID of the account acting. :param recipients: (list) list of account IDs in the thread. :param text: (str) body text of the initial message. :param subject: (str) optional subject line of the thread. :return: (kinds.messages.Thread) """ asserts.valid_id_type(actor_id) asserts.not_empty(recipients) asserts.type_of(text, basestring) asserts.type_of(subject, basestring) thd = Thread() for account_id in set(recipients): if account_id != actor_id: thd.add_member(account_id) if not len(thd.members): raise exp.BadRequestExp('Recipients not specified.') thd.add_member(actor_id) thd.put() # Populate MessageDto. message_dto = MessageDto() message_dto.thread_id = thd.id message_dto.text = text send_ndb(actor_id, message_dto) return thd
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
def POST_login(self): """Log-in POST request. TODO(kanat): Handle errors properly. """ email = self.request_json['email'] pass_raw = self.request_json['password'] auth_id = 'local:' + email.lower() try: self.auth.get_user_by_password(auth_id=auth_id, password=pass_raw, remember=True) self.write_json({'status': 'success'}) except auth.InvalidAuthIdError: logging.info('Invalid email: %s' % email) raise exp.BadRequestExp('We do not recognize that email.') except auth.InvalidPasswordError: logging.info('Invalid password. email: %s' % email) raise exp.BadRequestExp('You entered a wrong password.')
def apply_job(actor_id, job_id, message=None): """Apply for a job. TODO(kanat): Notify job owner. :param actor_id: (int) ID of the account acting. :param job_id: (int) ID of the job applying. :param message: (str) Optional message. :return: (None) """ asserts.valid_id_type(actor_id) asserts.valid_id_type(job_id) job = get_by_id_ndb(job_id) if job.status != JobStatus.Open: raise exp.BadRequestExp('Job is not open.') if actor_id == job.account_id: raise exp.BadRequestExp('Can\'t apply to own job.') apps = _get_applicants(job_id) if apps.find_applicant(actor_id) is not None: logging.warning('Already applied.') return apps.add_applicant(actor_id, message) apps.put()
def caregiver_by_account(account_id, _dto=True): """Returns the caregiver details that belongs to the given account_id. :param account_id: (int) ID of the account. :return: (dto.accounts.CaregiverDto) if _dto is True (kinds.accounts.Caregiver) if _dto is False (None) if account or caregiver does not exist. """ asserts.valid_id_type(account_id) account = account_by_id(account_id, _dto=False) if not asserts.is_valid_id_type(account.caregiver_id): raise exp.BadRequestExp() caregiver = Caregiver.get_by_id(account.caregiver_id) if not _dto: return caregiver return CaregiverDto.from_caregiver_ndb(caregiver)
def verify_email(email, token, _dto=True): """Verifies the given email verification token. :param email: (str) Email address. :param token: (str) Verification token. :return: Updated account. """ asserts.type_of(email, basestring) asserts.type_of(token, basestring) account = account_by_email(email, _dto=False) if account.verification_token != token: raise exp.BadRequestExp('Invalid verification token.') if not account.email_verified: account.email_verified = True account.put() if _dto: return AccountDto.from_account_ndb(account) return account
def _create_new_account(email, password_raw, auth_id): """Performs validation and stores a new account. :return: (kinds.accounts.Account) """ # Minimum password length. pass_min_len = 6 if not services.email.is_valid_email(email): raise exp.ValueExp('Invalid email.') if password_raw[0] == ' ' or password_raw[-1] == ' ': raise exp.ValueExp('Password must not start or end with white a space.') if len(password_raw) < pass_min_len: raise exp.ValueExp('Password must be at least {} characters.' .format(pass_min_len)) success, account = Account.create_user( auth_id=auth_id, unique_properties=['email'], email=email, password_raw=password_raw) if not success: logging.info('Signup failed. email: %s' % email) raise exp.BadRequestExp('The email you entered is already registered.') return account