# Path: ngnotifier/management/utils.py
# def query_yes_no(question, default="yes"):
#     """Ask a yes/no question via raw_input() and return their answer.
# 
#     "question" is a string that is presented to the user.
#     "default" is the presumed answer if the user just hits <Enter>.
#         It must be "yes" (the default), "no" or None (meaning
#         an answer is required of the user).
# 
#     The "answer" return value is one of "yes" or "no".
#     """
#     valid = {"yes": True, "y": True, "ye": True,
#              "no": False, "n": False}
#     if default == None:
#         prompt = " [y/n] "
#     elif default == "yes":
#         prompt = " [Y/n] "
#     elif default == "no":
#         prompt = " [y/N] "
#     else:
#         raise ValueError("invalid default answer: '%s'" % default)
# 
#     while True:
#         stdout.write(question + prompt)
#         choice = input().lower()
#         if default is not None and choice == '':
#             return valid[default]
#         elif choice in valid:
#             return valid[choice]
#         else:
#             stdout.write("Please respond with 'yes' or 'no' "
#                          "(or 'y' or 'n').\n")
# 
# Path: ngnotifier/fixtures.py
# class NGFixtures():
#     @staticmethod
#     def create_hosts():
#         for host in hosts:
#             print_msg('host', hosts[host]['host'])
#             try:
#                 NGHost.objects.get(host=hosts[host]['host'])
#                 print_exists()
#             except ObjectDoesNotExist:
#                 try:
#                     current_host = NGHost()
#                     current_host.host = hosts[host]['host']
#                     current_host.port = hosts[host]['port']
#                     current_host.ssl = hosts[host]['ssl']
#                     current_host.user = hosts[host]['user']
#                     current_host.password = hosts[host]['pass']
#                     current_host.timeout = hosts[host]['timeout']
#                     current_host.save()
#                     print_done()
#                 except Exception as e:
#                     print_fail(e)
# 
# 
#     @staticmethod
#     def create_groups():
#         for host in NGHost.objects.all():
#             host.update_groups(hosts[host.host]['groups'], verbose=True)
# 
# 
#     @staticmethod
#     def clean_all():
#         for group in NGGroup.objects.all():
#             group.delete()
#         for host in NGHost.objects.all():
#             host.delete()
# 
# class UserFixtures():
#     @staticmethod
#     def create_users():
#         for user in users:
#             try:
#                 User.objects.get(
#                     email=users[user]['mail']
#                 )
#             except ObjectDoesNotExist:
#                 new_user = User()
#                 new_user.email = users[user]['mail']
#                 new_user.set_password(users[user]['password'])
#                 new_user.is_active = True
#                 new_user.is_admin = users[user]['admin']
#                 new_user.pushbullet_api_key = users[user]['pushbullet_api_key']
#                 new_user.send_emails = users[user]['notifs']['mail']
#                 new_user.send_pushbullets = users[user]['notifs']['pushbullet']
#                 new_user.save()
# 
#                 for host in users[user]['subscriptions']:
#                     ng_host = NGHost.objects.get(
#                         host=hosts[host]['host']
#                     )
#                     for group in hosts[host]['groups']:
#                         ng_group = NGGroup.objects.get(
#                             host=ng_host,
#                             name=group
#                         )
#                         new_user.add_ng_group(ng_group)
# 
# Path: ngnotifier/utils.py
# class bcolors:
#     HEADER = '\033[95m'
#     OKBLUE = '\033[94m'
#     OKGREEN = '\033[92m'
#     WARNING = '\033[93m'
#     FAIL = '\033[91m'
#     ENDC = '\033[0m'
#     BOLD = '\033[1m'
#     UNDERLINE = '\033[4m'
# 
# Path: ngnotifier/tasks.py
# def update_news(verbose=False, notif=True):
#     notif_count_before = Log.objects.filter(type='N').count()
#     for host in NGHost.objects.all():
#         if verbose:
#             print('Checking host {}:'.format(host.host), flush=False)
#         groups = NGGroup.objects.filter(host=host)
#         tmp_co = host.get_co()
#         new_news_list_all = []
#         for group in groups:
#             new_news_list = group.update_news(tmp_co, verbose=verbose)
#             new_news_list_all += new_news_list
#             if notif:
#                 send_notif(new_news_list, group)
# 
#         print("    Found {} news".format(len(new_news_list_all)))
# 
#         print("\    Updating kinship links...")
#         kinship_updater()
#         for nn in new_news_list_all:
#             nn.add_answer_count()
#     if verbose and notif:
#         notif_count_after = Log.objects.filter(type='N').count()
#         print('    Sent {} notifications!'.format(notif_count_after-notif_count_before))
#     Log.objects.create(type='UN')


from subprocess import call
from ngnotifier.management.utils import query_yes_no
from django.core.management import call_command
from django.core.management.base import BaseCommand
from django.conf import settings
from ngnotifier.fixtures import NGFixtures, UserFixtures
from ngnotifier.utils import bcolors
from ngnotifier.tasks import update_news



class Command(BaseCommand):
    help = 'Installs the app: checks requirements, creates DB, '\
           'and adds its fixtures'

    def handle(self, *args, **options):
        print(
            'Welcome to the %s installation procedure...' % settings.SITE_NAME)
        print(
            'Before installation please check twice your configuration in '
            '\'web/ngnotifier/settings.py\' file.')
        print('Your settings are :')
        print('-> MODE = ' + bcolors.WARNING + (

# Path: ngnotifier/management/utils.py
# def query_yes_no(question, default="yes"):
#     """Ask a yes/no question via raw_input() and return their answer.
# 
#     "question" is a string that is presented to the user.
#     "default" is the presumed answer if the user just hits <Enter>.
#         It must be "yes" (the default), "no" or None (meaning
#         an answer is required of the user).
# 
#     The "answer" return value is one of "yes" or "no".
#     """
#     valid = {"yes": True, "y": True, "ye": True,
#              "no": False, "n": False}
#     if default == None:
#         prompt = " [y/n] "
#     elif default == "yes":
#         prompt = " [Y/n] "
#     elif default == "no":
#         prompt = " [y/N] "
#     else:
#         raise ValueError("invalid default answer: '%s'" % default)
# 
#     while True:
#         stdout.write(question + prompt)
#         choice = input().lower()
#         if default is not None and choice == '':
#             return valid[default]
#         elif choice in valid:
#             return valid[choice]
#         else:
#             stdout.write("Please respond with 'yes' or 'no' "
#                          "(or 'y' or 'n').\n")
# 
# Path: ngnotifier/fixtures.py
# class NGFixtures():
#     @staticmethod
#     def create_hosts():
#         for host in hosts:
#             print_msg('host', hosts[host]['host'])
#             try:
#                 NGHost.objects.get(host=hosts[host]['host'])
#                 print_exists()
#             except ObjectDoesNotExist:
#                 try:
#                     current_host = NGHost()
#                     current_host.host = hosts[host]['host']
#                     current_host.port = hosts[host]['port']
#                     current_host.ssl = hosts[host]['ssl']
#                     current_host.user = hosts[host]['user']
#                     current_host.password = hosts[host]['pass']
#                     current_host.timeout = hosts[host]['timeout']
#                     current_host.save()
#                     print_done()
#                 except Exception as e:
#                     print_fail(e)
# 
# 
#     @staticmethod
#     def create_groups():
#         for host in NGHost.objects.all():
#             host.update_groups(hosts[host.host]['groups'], verbose=True)
# 
# 
#     @staticmethod
#     def clean_all():
#         for group in NGGroup.objects.all():
#             group.delete()
#         for host in NGHost.objects.all():
#             host.delete()
# 
# class UserFixtures():
#     @staticmethod
#     def create_users():
#         for user in users:
#             try:
#                 User.objects.get(
#                     email=users[user]['mail']
#                 )
#             except ObjectDoesNotExist:
#                 new_user = User()
#                 new_user.email = users[user]['mail']
#                 new_user.set_password(users[user]['password'])
#                 new_user.is_active = True
#                 new_user.is_admin = users[user]['admin']
#                 new_user.pushbullet_api_key = users[user]['pushbullet_api_key']
#                 new_user.send_emails = users[user]['notifs']['mail']
#                 new_user.send_pushbullets = users[user]['notifs']['pushbullet']
#                 new_user.save()
# 
#                 for host in users[user]['subscriptions']:
#                     ng_host = NGHost.objects.get(
#                         host=hosts[host]['host']
#                     )
#                     for group in hosts[host]['groups']:
#                         ng_group = NGGroup.objects.get(
#                             host=ng_host,
#                             name=group
#                         )
#                         new_user.add_ng_group(ng_group)
# 
# Path: ngnotifier/utils.py
# class bcolors:
#     HEADER = '\033[95m'
#     OKBLUE = '\033[94m'
#     OKGREEN = '\033[92m'
#     WARNING = '\033[93m'
#     FAIL = '\033[91m'
#     ENDC = '\033[0m'
#     BOLD = '\033[1m'
#     UNDERLINE = '\033[4m'
# 
# Path: ngnotifier/tasks.py
# def update_news(verbose=False, notif=True):
#     notif_count_before = Log.objects.filter(type='N').count()
#     for host in NGHost.objects.all():
#         if verbose:
#             print('Checking host {}:'.format(host.host), flush=False)
#         groups = NGGroup.objects.filter(host=host)
#         tmp_co = host.get_co()
#         new_news_list_all = []
#         for group in groups:
#             new_news_list = group.update_news(tmp_co, verbose=verbose)
#             new_news_list_all += new_news_list
#             if notif:
#                 send_notif(new_news_list, group)
# 
#         print("    Found {} news".format(len(new_news_list_all)))
# 
#         print("\    Updating kinship links...")
#         kinship_updater()
#         for nn in new_news_list_all:
#             nn.add_answer_count()
#     if verbose and notif:
#         notif_count_after = Log.objects.filter(type='N').count()
#         print('    Sent {} notifications!'.format(notif_count_after-notif_count_before))
#     Log.objects.create(type='UN')


# Path: ngnotifier/management/commands/install.py
from subprocess import call
from ngnotifier.management.utils import query_yes_no
from django.core.management import call_command
from django.core.management.base import BaseCommand
from django.conf import settings
from ngnotifier.fixtures import NGFixtures, UserFixtures
from ngnotifier.utils import bcolors
from ngnotifier.tasks import update_news




class Command(BaseCommand):
    help = 'Installs the app: checks requirements, creates DB, '\
           'and adds its fixtures'

    def handle(self, *args, **options):
        print(
            'Welcome to the %s installation procedure...' % settings.SITE_NAME)
        print(
            'Before installation please check twice your configuration in '
            '\'web/ngnotifier/settings.py\' file.')
        print('Your settings are :')
        print('-> MODE = ' + bcolors.WARNING + (

        'DEBUG' if settings.DEBUG else 'PRODUCTION') + bcolors.ENDC)