# Path: sfm/ui/rabbit.py
# class RabbitWorker:
#     def __init__(self):
#         self.exchange = Exchange(name=EXCHANGE,
#                                  type="topic",
#                                  durable=True)
# 
#     @staticmethod
#     def get_connection():
#         return Connection(transport="librabbitmq",
#                           hostname=settings.RABBITMQ_HOST,
#                           userid=settings.RABBITMQ_USER,
#                           password=settings.RABBITMQ_PASSWORD,
#                           port=settings.RABBITMQ_PORT,
#                           transport_options={'max_retries': 3})
# 
#     def declare_exchange(self):
#         try:
#             with self.get_connection() as connection:
#                 log.debug("Declaring %s exchange", self.exchange.name)
#                 self.exchange(connection).declare()
#         except Exception:
#             log.error("Error connecting to RabbitMQ to declare exchange")
# 
#     def send_message(self, message, routing_key):
#         with self.get_connection() as connection:
#             log.debug("Sending message to %s: %s", routing_key, json.dumps(message, indent=4))
#             connection.Producer(exchange=self.exchange).publish(message, routing_key=routing_key)
# 
#     def send_messages(self, messages, routing_key):
#         with self.get_connection() as connection:
#             for message in messages:
#                 log.debug("Sending message to %s: %s", routing_key, json.dumps(message, indent=4))
#                 connection.Producer(exchange=self.exchange).publish(message, routing_key=routing_key)


from django.apps import AppConfig
from .rabbit import RabbitWorker
from django.db.models.signals import post_save, pre_delete, m2m_changed, post_delete
from django.conf import settings
        from .models import Collection, Export, CollectionSet, Warc
        from .sched import start_sched, schedule_harvest_receiver, unschedule_harvest_receiver
        from .export import export_receiver, export_m2m_receiver
        from .notifications import send_user_harvest_emails, send_free_space_emails, send_queue_warn_emails
        from .serialize import serialize_all
        from .models import delete_collection_set_receiver, delete_collection_receiver, delete_warc_receiver, \
            delete_export_receiver, move_collection_receiver
import logging

class UIConfig(AppConfig):
    name = 'ui'
    verbose_name = "ui"

    def ready(self):
        RabbitWorker().declare_exchange()

        if settings.SCHEDULE_HARVESTS:
            log.debug("Setting receivers for collections.")
            post_save.connect(schedule_harvest_receiver, sender=Collection)
            pre_delete.connect(unschedule_harvest_receiver, sender=Collection)

        # Export
        if settings.PERFORM_EXPORTS:
            log.debug("Setting receiver for exports.")
            post_save.connect(export_receiver, sender=Export)
            m2m_changed.connect(export_m2m_receiver, sender=Export.seeds.through)

        # Add 5 minute interval
        if settings.FIVE_MINUTE_SCHEDULE:
            log.debug("Adding 5 minute timer")
            Collection.SCHEDULE_CHOICES.append((5, "Every 5 minutes"))

        # Add 5 minute interval
        if settings.HUNDRED_ITEM_SEGMENT:
            log.debug("Adding 10 item export segment")
            Export.SEGMENT_CHOICES.append((100, "100"))

        # Add weibo search collection

# Path: sfm/ui/rabbit.py
# class RabbitWorker:
#     def __init__(self):
#         self.exchange = Exchange(name=EXCHANGE,
#                                  type="topic",
#                                  durable=True)
# 
#     @staticmethod
#     def get_connection():
#         return Connection(transport="librabbitmq",
#                           hostname=settings.RABBITMQ_HOST,
#                           userid=settings.RABBITMQ_USER,
#                           password=settings.RABBITMQ_PASSWORD,
#                           port=settings.RABBITMQ_PORT,
#                           transport_options={'max_retries': 3})
# 
#     def declare_exchange(self):
#         try:
#             with self.get_connection() as connection:
#                 log.debug("Declaring %s exchange", self.exchange.name)
#                 self.exchange(connection).declare()
#         except Exception:
#             log.error("Error connecting to RabbitMQ to declare exchange")
# 
#     def send_message(self, message, routing_key):
#         with self.get_connection() as connection:
#             log.debug("Sending message to %s: %s", routing_key, json.dumps(message, indent=4))
#             connection.Producer(exchange=self.exchange).publish(message, routing_key=routing_key)
# 
#     def send_messages(self, messages, routing_key):
#         with self.get_connection() as connection:
#             for message in messages:
#                 log.debug("Sending message to %s: %s", routing_key, json.dumps(message, indent=4))
#                 connection.Producer(exchange=self.exchange).publish(message, routing_key=routing_key)


# Path: sfm/ui/config.py
from django.apps import AppConfig
from .rabbit import RabbitWorker
from django.db.models.signals import post_save, pre_delete, m2m_changed, post_delete
from django.conf import settings
        from .models import Collection, Export, CollectionSet, Warc
        from .sched import start_sched, schedule_harvest_receiver, unschedule_harvest_receiver
        from .export import export_receiver, export_m2m_receiver
        from .notifications import send_user_harvest_emails, send_free_space_emails, send_queue_warn_emails
        from .serialize import serialize_all
        from .models import delete_collection_set_receiver, delete_collection_receiver, delete_warc_receiver, \
            delete_export_receiver, move_collection_receiver
import logging


class UIConfig(AppConfig):
    name = 'ui'
    verbose_name = "ui"

    def ready(self):
        RabbitWorker().declare_exchange()

        if settings.SCHEDULE_HARVESTS:
            log.debug("Setting receivers for collections.")
            post_save.connect(schedule_harvest_receiver, sender=Collection)
            pre_delete.connect(unschedule_harvest_receiver, sender=Collection)

        # Export
        if settings.PERFORM_EXPORTS:
            log.debug("Setting receiver for exports.")
            post_save.connect(export_receiver, sender=Export)
            m2m_changed.connect(export_m2m_receiver, sender=Export.seeds.through)

        # Add 5 minute interval
        if settings.FIVE_MINUTE_SCHEDULE:
            log.debug("Adding 5 minute timer")
            Collection.SCHEDULE_CHOICES.append((5, "Every 5 minutes"))

        # Add 5 minute interval
        if settings.HUNDRED_ITEM_SEGMENT:
            log.debug("Adding 10 item export segment")
            Export.SEGMENT_CHOICES.append((100, "100"))

        # Add weibo search collection

        if settings.WEIBO_SEARCH_OPTION: