# Path: yithlibraryserver/backups/utils.py
# def get_backup_filename(date):
#     return 'yith-library-backup-%d-%02d-%02d.yith' % (
#         date.year, date.month, date.day)
# 
# Path: yithlibraryserver/backups/utils.py
# def get_user_passwords(db, user):
#     passwords_manager = PasswordsManager(db)
#     return [remove_attrs(password, 'owner', '_id')
#             for password in passwords_manager.retrieve(user)]
# 
# def compress(passwords):
#     buf = BytesIO()
#     gzip_data = gzip.GzipFile(fileobj=buf, mode='wb')
#     data = json.dumps(passwords)
#     gzip_data.write(data.encode('utf-8'))
#     gzip_data.close()
#     return buf.getvalue()
# 
# Path: yithlibraryserver/email.py
# def send_email(request, template, context, subject, recipients,
#                attachments=None, extra_headers=None):
#     message = create_message(request, template, context, subject, recipients,
#                              attachments, extra_headers)
#     return get_mailer(request).send(message)


from pyramid_mailer.message import Attachment
from yithlibraryserver.backups.utils import get_backup_filename
from yithlibraryserver.backups.utils import get_user_passwords, compress
from yithlibraryserver.email import send_email
# 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_day_to_send(user, days_of_month):
    """Sum the ordinal of each character in user._id % days_of_month"""
    return sum([ord(chr) for chr in str(user['_id'])]) % days_of_month


def send_passwords(request, user, preferences_link, backups_link):
    passwords = get_user_passwords(request.db, user)
    if not passwords:
        return False

    context = {
        'user': user,
        'preferences_link': preferences_link,
        'backups_link': backups_link,
    }

    today = request.date_service.today()
    attachment = Attachment(get_backup_filename(today),
                            "application/yith",

# Path: yithlibraryserver/backups/utils.py
# def get_backup_filename(date):
#     return 'yith-library-backup-%d-%02d-%02d.yith' % (
#         date.year, date.month, date.day)
# 
# Path: yithlibraryserver/backups/utils.py
# def get_user_passwords(db, user):
#     passwords_manager = PasswordsManager(db)
#     return [remove_attrs(password, 'owner', '_id')
#             for password in passwords_manager.retrieve(user)]
# 
# def compress(passwords):
#     buf = BytesIO()
#     gzip_data = gzip.GzipFile(fileobj=buf, mode='wb')
#     data = json.dumps(passwords)
#     gzip_data.write(data.encode('utf-8'))
#     gzip_data.close()
#     return buf.getvalue()
# 
# Path: yithlibraryserver/email.py
# def send_email(request, template, context, subject, recipients,
#                attachments=None, extra_headers=None):
#     message = create_message(request, template, context, subject, recipients,
#                              attachments, extra_headers)
#     return get_mailer(request).send(message)


# Path: yithlibraryserver/backups/email.py
from pyramid_mailer.message import Attachment
from yithlibraryserver.backups.utils import get_backup_filename
from yithlibraryserver.backups.utils import get_user_passwords, compress
from yithlibraryserver.email import send_email

# 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_day_to_send(user, days_of_month):
    """Sum the ordinal of each character in user._id % days_of_month"""
    return sum([ord(chr) for chr in str(user['_id'])]) % days_of_month


def send_passwords(request, user, preferences_link, backups_link):
    passwords = get_user_passwords(request.db, user)
    if not passwords:
        return False

    context = {
        'user': user,
        'preferences_link': preferences_link,
        'backups_link': backups_link,
    }

    today = request.date_service.today()
    attachment = Attachment(get_backup_filename(today),
                            "application/yith",

                            compress(passwords))