# Path: app/config.py
# WTF_CSRF_ENABLED = True
# SECRET_KEY = 'you-will-never-guess'
# CURRENT_TICKET_LIFETIME = 720
# QUEUED_TICKET_LIFETIME = 2
# 
# Path: app/util.py
# def data_from_request():
# def is_unblocked_by_rule(lt, h, d):
# def get_by_list(dic, keys):
# def to_iso_date_string(date):
# def from_iso_date_string(date):
# 
# Path: app/gates.py
# def generate_info(config):
# def test_against_global_rules(rules, env, lt):
# def are_gates_open(config, env):
# def gate_is_closed(entry, config, env, ticket_id=None):
# def gate_is_manually_closed(entry, env):
# 
# Path: app/errors.py
# class EnvironmentNotFound(Exception):
#     def __init__(self, environment):
#         self.message = "Environment " + environment + " not found."
#         self.status_code = 404
# 
# Path: app/errors.py
# class GateStateNotValid(Exception):
#     def __init__(self):
#         self.message = "state must be open or closed"
# 
# Path: app/errors.py
# class JsonStructureError(Exception):
#     def __init__(self, message=""):
#         self.message = "Your json contains invalid information. " + message
# 
# Path: app/errors.py
# class JsonValidationError(Exception):
#     def __init__(self):
#         self.message = "Json was not valid"
# 
# Path: app/errors.py
# class NotFound(Exception):
#     def __init__(self):
#         self.message = "Service or Group not found"
#         self.status_code = 404
# 
# Path: app/errors.py
# class NotMasterError(pymongo.errors.NotMasterError):
#     def __init__(self, message):
#         super(NotMasterError, self).__init__(message)
#         self.message = "Can not write to database. Please wait until reconnected to master or start with proper configuration"
#         self.status_code = 500
# 
# Path: app/errors.py
# class ServiceAlreadyExists(Exception):
#     def __init__(self):
#         self.message = "Service with this name already exists"
# 
# Path: app/errors.py
# class ServiceNameNotValid(Exception):
#     def __init__(self):
#         self.message = "Service name not valid. Should not empty or contains dots"
# 
# Path: app/errors.py
# class TicketNotFound(Exception):
#     def __init__(self):
#         self.message = "Ticket does not exist"
#         self.status_code = 404


import json
import uuid
from delorean import Delorean
from flask import Response, request, Blueprint
from app import config
from app import util
from app import gates
from app.errors import EnvironmentNotFound
from app.errors import GateStateNotValid
from app.errors import JsonStructureError
from app.errors import JsonValidationError
from app.errors import NotFound
from app.errors import NotMasterError
from app.errors import ServiceAlreadyExists
from app.errors import ServiceNameNotValid
from app.errors import TicketNotFound

@blueprint.route('/api/gates/<string:name>', methods=['DELETE'])
@blueprint.route('/api/gates/<string:group>/<string:name>', methods=['DELETE'])
def api_remove_gate(group, name):
    try:
        blueprint.mongo.remove_gate(group, name)
        return Response('{"status": "ok"}', status=200, mimetype='application/json')
    except(NotFound, NotMasterError) as error:
        return error_response(error)


@blueprint.route('/api/tickets/<string:ticket_id>', methods=['DELETE'])
def api_release(ticket_id):
    try:
        blueprint.mongo.remove_ticket(ticket_id)
        return Response('{"status": "ok"}', status=200, mimetype='application/json')
    except (NotMasterError, ServiceAlreadyExists, ServiceNameNotValid, JsonValidationError, JsonStructureError,
            TicketNotFound) as error:
        return error_response(error)


@blueprint.route('/api/holidays/', methods=['POST'])
def api_replace_holidays():
    data = util.data_from_request()
    holidays = data['holidays']
    try:
        blueprint.mongo.clear_holidays()
        for holiday in holidays:
            blueprint.mongo.add_holiday(holiday)
        return Response('{"status": "ok"}', status=200, mimetype='application/json')

# Path: app/config.py
# WTF_CSRF_ENABLED = True
# SECRET_KEY = 'you-will-never-guess'
# CURRENT_TICKET_LIFETIME = 720
# QUEUED_TICKET_LIFETIME = 2
# 
# Path: app/util.py
# def data_from_request():
# def is_unblocked_by_rule(lt, h, d):
# def get_by_list(dic, keys):
# def to_iso_date_string(date):
# def from_iso_date_string(date):
# 
# Path: app/gates.py
# def generate_info(config):
# def test_against_global_rules(rules, env, lt):
# def are_gates_open(config, env):
# def gate_is_closed(entry, config, env, ticket_id=None):
# def gate_is_manually_closed(entry, env):
# 
# Path: app/errors.py
# class EnvironmentNotFound(Exception):
#     def __init__(self, environment):
#         self.message = "Environment " + environment + " not found."
#         self.status_code = 404
# 
# Path: app/errors.py
# class GateStateNotValid(Exception):
#     def __init__(self):
#         self.message = "state must be open or closed"
# 
# Path: app/errors.py
# class JsonStructureError(Exception):
#     def __init__(self, message=""):
#         self.message = "Your json contains invalid information. " + message
# 
# Path: app/errors.py
# class JsonValidationError(Exception):
#     def __init__(self):
#         self.message = "Json was not valid"
# 
# Path: app/errors.py
# class NotFound(Exception):
#     def __init__(self):
#         self.message = "Service or Group not found"
#         self.status_code = 404
# 
# Path: app/errors.py
# class NotMasterError(pymongo.errors.NotMasterError):
#     def __init__(self, message):
#         super(NotMasterError, self).__init__(message)
#         self.message = "Can not write to database. Please wait until reconnected to master or start with proper configuration"
#         self.status_code = 500
# 
# Path: app/errors.py
# class ServiceAlreadyExists(Exception):
#     def __init__(self):
#         self.message = "Service with this name already exists"
# 
# Path: app/errors.py
# class ServiceNameNotValid(Exception):
#     def __init__(self):
#         self.message = "Service name not valid. Should not empty or contains dots"
# 
# Path: app/errors.py
# class TicketNotFound(Exception):
#     def __init__(self):
#         self.message = "Ticket does not exist"
#         self.status_code = 404


# Path: app/api.py
import json
import uuid
from delorean import Delorean
from flask import Response, request, Blueprint
from app import config
from app import util
from app import gates
from app.errors import EnvironmentNotFound
from app.errors import GateStateNotValid
from app.errors import JsonStructureError
from app.errors import JsonValidationError
from app.errors import NotFound
from app.errors import NotMasterError
from app.errors import ServiceAlreadyExists
from app.errors import ServiceNameNotValid
from app.errors import TicketNotFound


@blueprint.route('/api/gates/<string:name>', methods=['DELETE'])
@blueprint.route('/api/gates/<string:group>/<string:name>', methods=['DELETE'])
def api_remove_gate(group, name):
    try:
        blueprint.mongo.remove_gate(group, name)
        return Response('{"status": "ok"}', status=200, mimetype='application/json')
    except(NotFound, NotMasterError) as error:
        return error_response(error)


@blueprint.route('/api/tickets/<string:ticket_id>', methods=['DELETE'])
def api_release(ticket_id):
    try:
        blueprint.mongo.remove_ticket(ticket_id)
        return Response('{"status": "ok"}', status=200, mimetype='application/json')
    except (NotMasterError, ServiceAlreadyExists, ServiceNameNotValid, JsonValidationError, JsonStructureError,
            TicketNotFound) as error:
        return error_response(error)


@blueprint.route('/api/holidays/', methods=['POST'])
def api_replace_holidays():
    data = util.data_from_request()
    holidays = data['holidays']
    try:
        blueprint.mongo.clear_holidays()
        for holiday in holidays:
            blueprint.mongo.add_holiday(holiday)
        return Response('{"status": "ok"}', status=200, mimetype='application/json')

    except (NotMasterError, ServiceAlreadyExists, ServiceNameNotValid, JsonValidationError, JsonStructureError,