# Path: hyperkitty/lib/mailman.py
# def sync_with_mailman():
#     from hyperkitty.models import MailingList, Sender
#     for mlist in MailingList.objects.all():
#         mlist.update_from_mailman()
#     # Now sync Sender.mailman_id with Mailman's User.user_id
#     # There can be thousands of senders, break into smaller chuncks to avoid
#     # hogging up the memory
#     buffer_size = 1000
#     query = Sender.objects.filter(mailman_id__isnull=True)
#     prev_count = query.count()
#     lower_bound = 0
#     upper_bound = buffer_size
#     while True:
#         try:
#             for sender in query[lower_bound:upper_bound]:
#                 sender.set_mailman_id()
#         except MailmanConnectionError:
#             break # Can't refresh at this time
#         count = query.count()
#         if count == 0:
#             break # all done
#         if count == prev_count:
#             # no improvement...
#             if count < upper_bound:
#                 break # ...and all users checked
#             else:
#                 # there may be some more left
#                 lower_bound = upper_bound
#                 upper_bound += buffer_size
#         prev_count = count
#         logger.info("%d emails left to refresh", count)


import logging
from django.core.management.base import BaseCommand, CommandError
from hyperkitty.lib.mailman import sync_with_mailman
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.
#
# Author: Aurelien Bompard <abompard@fedoraproject.org>

"""
Sync properties from Mailman into HyperKitty
"""

from __future__ import absolute_import, print_function, unicode_literals



class Command(BaseCommand):
    help = "Sync properties from Mailman into HyperKitty"

    def handle(self, *args, **options):
        options["verbosity"] = int(options.get("verbosity", "1"))
        # logging
        if options["verbosity"] >= 3:
            debuglevel = logging.DEBUG
        else:
            debuglevel = logging.INFO
        logging.basicConfig(format='%(message)s', level=debuglevel)
        if args:
            raise CommandError("no arguments allowed")

# Path: hyperkitty/lib/mailman.py
# def sync_with_mailman():
#     from hyperkitty.models import MailingList, Sender
#     for mlist in MailingList.objects.all():
#         mlist.update_from_mailman()
#     # Now sync Sender.mailman_id with Mailman's User.user_id
#     # There can be thousands of senders, break into smaller chuncks to avoid
#     # hogging up the memory
#     buffer_size = 1000
#     query = Sender.objects.filter(mailman_id__isnull=True)
#     prev_count = query.count()
#     lower_bound = 0
#     upper_bound = buffer_size
#     while True:
#         try:
#             for sender in query[lower_bound:upper_bound]:
#                 sender.set_mailman_id()
#         except MailmanConnectionError:
#             break # Can't refresh at this time
#         count = query.count()
#         if count == 0:
#             break # all done
#         if count == prev_count:
#             # no improvement...
#             if count < upper_bound:
#                 break # ...and all users checked
#             else:
#                 # there may be some more left
#                 lower_bound = upper_bound
#                 upper_bound += buffer_size
#         prev_count = count
#         logger.info("%d emails left to refresh", count)


# Path: hyperkitty/management/commands/mailman_sync.py
import logging
from django.core.management.base import BaseCommand, CommandError
from hyperkitty.lib.mailman import sync_with_mailman

# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.
#
# Author: Aurelien Bompard <abompard@fedoraproject.org>

"""
Sync properties from Mailman into HyperKitty
"""

from __future__ import absolute_import, print_function, unicode_literals



class Command(BaseCommand):
    help = "Sync properties from Mailman into HyperKitty"

    def handle(self, *args, **options):
        options["verbosity"] = int(options.get("verbosity", "1"))
        # logging
        if options["verbosity"] >= 3:
            debuglevel = logging.DEBUG
        else:
            debuglevel = logging.INFO
        logging.basicConfig(format='%(message)s', level=debuglevel)
        if args:
            raise CommandError("no arguments allowed")

        sync_with_mailman()