# Path: yithlibraryserver/compat.py
# PY3 = sys.version_info[0] == 3
#     def encode_header(obj):  # pragma: no cover
#     def encode_header(obj):  # pragma: no cover
# 
# Path: yithlibraryserver/email.py
# def create_message(request, template, context, subject, recipients,
#                    attachments=None, extra_headers=None):
#     text_body = render(template + '.txt', context, request=request)
#     # chamaleon txt templates are rendered as utf-8 bytestrings
#     text_body = text_body.decode('utf-8')
# 
#     html_body = render(template + '.pt', context, request=request)
# 
#     extra_headers = extra_headers or {}
# 
#     message = Message(
#         subject=subject,
#         recipients=recipients,
#         body=text_body,
#         html=html_body,
#         extra_headers=extra_headers,
#     )
# 
#     if attachments is not None:
#         for attachment in attachments:
#             message.attach(attachment)
# 
#     return message
# 
# Path: yithlibraryserver/scripts/reports.py
# def get_passwords_map(passwords):
#     # make a password dict keyed by users_id
#     passwords_map = {}
#     for password in passwords:
#         owner = password['owner']
#         if owner in passwords_map:
#             passwords_map[owner] += 1
#         else:
#             passwords_map[owner] = 1
# 
#     return passwords_map
# 
# Path: yithlibraryserver/scripts/utils.py
# def safe_print(value):
#     if PY3:  # pragma: no cover
#         print(value)
#     else:  # pragma: no cover
#         print(value.encode('utf-8'))
# 
# Path: yithlibraryserver/scripts/utils.py
# def get_user_display_name(user):
#     return '%s %s <%s>' % (user.get('first_name', ''),
#                            user.get('last_name', ''),
#                            user.get('email', ''))


import optparse
import textwrap
import sys
import transaction
from pyramid.paster import bootstrap
from pyramid_mailer import get_mailer
from yithlibraryserver.compat import urlparse
from yithlibraryserver.email import create_message
from yithlibraryserver.scripts.reports import get_passwords_map
from yithlibraryserver.scripts.utils import safe_print
from yithlibraryserver.scripts.utils import get_user_display_name
# Yith Library Server is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Yith Library Server.  If not, see <http://www.gnu.org/licenses/>.







def get_all_users_with_passwords_and_email(db):
    all_passwords = list(db.passwords.find())
    passwords_map = get_passwords_map(all_passwords)
    for user in db.users.find({
            'email_verified': True,
            }):
        if not user['email']:
            continue

        if not user['_id'] in passwords_map:
            continue

        yield user


def send_email(request, email_template, user, preferences_link):

# Path: yithlibraryserver/compat.py
# PY3 = sys.version_info[0] == 3
#     def encode_header(obj):  # pragma: no cover
#     def encode_header(obj):  # pragma: no cover
# 
# Path: yithlibraryserver/email.py
# def create_message(request, template, context, subject, recipients,
#                    attachments=None, extra_headers=None):
#     text_body = render(template + '.txt', context, request=request)
#     # chamaleon txt templates are rendered as utf-8 bytestrings
#     text_body = text_body.decode('utf-8')
# 
#     html_body = render(template + '.pt', context, request=request)
# 
#     extra_headers = extra_headers or {}
# 
#     message = Message(
#         subject=subject,
#         recipients=recipients,
#         body=text_body,
#         html=html_body,
#         extra_headers=extra_headers,
#     )
# 
#     if attachments is not None:
#         for attachment in attachments:
#             message.attach(attachment)
# 
#     return message
# 
# Path: yithlibraryserver/scripts/reports.py
# def get_passwords_map(passwords):
#     # make a password dict keyed by users_id
#     passwords_map = {}
#     for password in passwords:
#         owner = password['owner']
#         if owner in passwords_map:
#             passwords_map[owner] += 1
#         else:
#             passwords_map[owner] = 1
# 
#     return passwords_map
# 
# Path: yithlibraryserver/scripts/utils.py
# def safe_print(value):
#     if PY3:  # pragma: no cover
#         print(value)
#     else:  # pragma: no cover
#         print(value.encode('utf-8'))
# 
# Path: yithlibraryserver/scripts/utils.py
# def get_user_display_name(user):
#     return '%s %s <%s>' % (user.get('first_name', ''),
#                            user.get('last_name', ''),
#                            user.get('email', ''))


# Path: yithlibraryserver/scripts/announce.py
import optparse
import textwrap
import sys
import transaction
from pyramid.paster import bootstrap
from pyramid_mailer import get_mailer
from yithlibraryserver.compat import urlparse
from yithlibraryserver.email import create_message
from yithlibraryserver.scripts.reports import get_passwords_map
from yithlibraryserver.scripts.utils import safe_print
from yithlibraryserver.scripts.utils import get_user_display_name

# Yith Library Server is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Yith Library Server.  If not, see <http://www.gnu.org/licenses/>.







def get_all_users_with_passwords_and_email(db):
    all_passwords = list(db.passwords.find())
    passwords_map = get_passwords_map(all_passwords)
    for user in db.users.find({
            'email_verified': True,
            }):
        if not user['email']:
            continue

        if not user['_id'] in passwords_map:
            continue

        yield user


def send_email(request, email_template, user, preferences_link):

    safe_print('Sending email to %s' % get_user_display_name(user))