def dao_get_notifications_by_recipient_or_reference( service_id, search_term, notification_type=None, statuses=None, page=1, page_size=None, ): if notification_type == SMS_TYPE: normalised = try_validate_and_format_phone_number(search_term) for character in {'(', ')', ' ', '-'}: normalised = normalised.replace(character, '') normalised = normalised.lstrip('+0') elif notification_type == EMAIL_TYPE: try: normalised = validate_and_format_email_address(search_term) except InvalidEmailError: normalised = search_term.lower() elif notification_type in {LETTER_TYPE, None}: # For letters, we store the address without spaces, so we need # to removes spaces from the search term to match. We also do # this when a notification type isn’t provided (this will # happen if a user doesn’t have permission to see the dashboard) # because email addresses and phone numbers will never be stored # with spaces either. normalised = ''.join(search_term.split()).lower() else: raise TypeError( f'Notification type must be {EMAIL_TYPE}, {SMS_TYPE}, {LETTER_TYPE} or None' ) normalised = escape_special_characters(normalised) search_term = escape_special_characters(search_term) filters = [ Notification.service_id == service_id, or_( Notification.normalised_to.like("%{}%".format(normalised)), Notification.client_reference.ilike("%{}%".format(search_term)), ), Notification.key_type != KEY_TYPE_TEST, ] if statuses: filters.append(Notification.status.in_(statuses)) if notification_type: filters.append(Notification.notification_type == notification_type) results = db.session.query(Notification)\ .filter(*filters)\ .order_by(desc(Notification.created_at))\ .paginate(page=page, per_page=page_size) return results
def dao_get_notifications_by_to_field(service_id, search_term, notification_type=None, statuses=None): if notification_type is None: notification_type = guess_notification_type(search_term) if notification_type == SMS_TYPE: normalised = try_validate_and_format_phone_number(search_term) for character in {'(', ')', ' ', '-'}: normalised = normalised.replace(character, '') normalised = normalised.lstrip('+0') elif notification_type == EMAIL_TYPE: try: normalised = validate_and_format_email_address(search_term) except InvalidEmailError: normalised = search_term.lower() else: raise InvalidRequest("Only email and SMS can use search by recipient", 400) normalised = escape_special_characters(normalised) filters = [ Notification.service_id == service_id, Notification.normalised_to.like("%{}%".format(normalised)), Notification.key_type != KEY_TYPE_TEST, ] if statuses: filters.append(Notification.status.in_(statuses)) if notification_type: filters.append(Notification.notification_type == notification_type) results = db.session.query(Notification).filter(*filters).order_by( desc(Notification.created_at)).all() return results
def get_services_by_partial_name(service_name): service_name = escape_special_characters(service_name) return Service.query.filter(Service.name.ilike("%{}%".format(service_name))).all()
def get_users_by_partial_email(email): email = escape_special_characters(email) return User.query.filter(User.email_address.ilike( "%{}%".format(email))).all()
def dao_services_by_partial_smtp_name(smtp_name): smtp_name = escape_special_characters(smtp_name) return Service.query.filter(Service.smtp_user.ilike("%{}%".format(smtp_name))).one()