from django.core.management.base import BaseCommand, CommandError
from products.models import *
from products.sendmsg import send_user_message, admin_broadcast
from django.db import transaction

class Command(BaseCommand):
    help = 'Send a message to admin/tenant'

    def add_arguments(self, parser):
        parser.add_argument('--all-admin', '-A', action="store_true")
        parser.add_argument('--admin', '-a')
        parser.add_argument('--tenant', '-t')
        parser.add_argument('--subject', '-s', required=True)
        parser.add_argument('--content', '-c', default='')

    def handle(self, *args, **options):
        admin = options['admin']
        tenant = options['tenant']
        subject = options['subject']
        content = options['content']
        whom = None
        if options.get('all_admin'):
            admin_broadcast(subject, content)
            return
        if admin:
            whom = AdminInfo.objects.get(user__username=admin)
        elif tenant:
            whom = Tenant.objects.get(username=tenant)
        else:
            raise Exception("Must specify either admin or tenant name")
        send_user_message(whom, subject, content)
