# Path: disaster_recovery/utils.py
# def shield(message, redirect=''):
#     """decorator to reduce boilerplate try except blocks for horizon functions
#     :param message: a str error message
#     :param redirect: a str with the redirect namespace without including
#                      horizon:disaster_recovery:
#                      eg. @shield('error', redirect='jobs:index')
#     """
#     def wrap(function):
# 
#         @wraps(function)
#         def wrapped_function(view, *args, **kwargs):
# 
#             try:
#                 return function(view, *args, **kwargs)
#             except Exception as error:
#                 LOG.error(error.message)
#                 namespace = "horizon:disaster_recovery:"
#                 r = reverse("{0}{1}".format(namespace, redirect))
# 
#                 if view.request.path == r:
#                     # To avoid an endless loop, we must not redirect to the
#                     # same page on which the error happened
#                     user_home = get_user_home(view.request.user)
#                     exceptions.handle(view.request, _(error.message),
#                                       redirect=user_home)
#                 else:
#                     exceptions.handle(view.request, _(error.message),
#                                       redirect=r)
# 
#         return wrapped_function
#     return wrap
# 
# Path: disaster_recovery/utils.py
# def timestamp_to_string(ts):
#     return django_date(
#         datetime.datetime.fromtimestamp(int(ts)),
#         'SHORT_DATETIME_FORMAT')


from django.urls import reverse
from django.utils import safestring
from django.utils.translation import ungettext_lazy
from django.utils.translation import ugettext_lazy as _
from horizon import tables
from horizon.utils import functions as utils
from disaster_recovery.utils import shield
from disaster_recovery.utils import timestamp_to_string
import disaster_recovery.api.api as freezer_api
class Restore(tables.LinkAction):
    name = "restore"
    verbose_name = _("Restore")
    classes = ("ajax-modal", "btn-launch")
    ajax = True

    def get_link_url(self, datum=None):
        return reverse("horizon:disaster_recovery:backups:restore",
                       kwargs={'backup_id': datum.id})


class DeleteBackup(tables.DeleteAction):
    help_text = _("Delete backups is not recoverable.")

    @staticmethod
    def action_present(count):
        return ungettext_lazy(
            u"Delete Backup",
            u"Delete Backups",
            count
        )

    @staticmethod
    def action_past(count):
        return ungettext_lazy(
            u"Deleted Backup",
            u"Deleted Backups",
            count
        )


# Path: disaster_recovery/utils.py
# def shield(message, redirect=''):
#     """decorator to reduce boilerplate try except blocks for horizon functions
#     :param message: a str error message
#     :param redirect: a str with the redirect namespace without including
#                      horizon:disaster_recovery:
#                      eg. @shield('error', redirect='jobs:index')
#     """
#     def wrap(function):
# 
#         @wraps(function)
#         def wrapped_function(view, *args, **kwargs):
# 
#             try:
#                 return function(view, *args, **kwargs)
#             except Exception as error:
#                 LOG.error(error.message)
#                 namespace = "horizon:disaster_recovery:"
#                 r = reverse("{0}{1}".format(namespace, redirect))
# 
#                 if view.request.path == r:
#                     # To avoid an endless loop, we must not redirect to the
#                     # same page on which the error happened
#                     user_home = get_user_home(view.request.user)
#                     exceptions.handle(view.request, _(error.message),
#                                       redirect=user_home)
#                 else:
#                     exceptions.handle(view.request, _(error.message),
#                                       redirect=r)
# 
#         return wrapped_function
#     return wrap
# 
# Path: disaster_recovery/utils.py
# def timestamp_to_string(ts):
#     return django_date(
#         datetime.datetime.fromtimestamp(int(ts)),
#         'SHORT_DATETIME_FORMAT')


# Path: disaster_recovery/backups/tables.py
from django.urls import reverse
from django.utils import safestring
from django.utils.translation import ungettext_lazy
from django.utils.translation import ugettext_lazy as _
from horizon import tables
from horizon.utils import functions as utils
from disaster_recovery.utils import shield
from disaster_recovery.utils import timestamp_to_string
import disaster_recovery.api.api as freezer_api

class Restore(tables.LinkAction):
    name = "restore"
    verbose_name = _("Restore")
    classes = ("ajax-modal", "btn-launch")
    ajax = True

    def get_link_url(self, datum=None):
        return reverse("horizon:disaster_recovery:backups:restore",
                       kwargs={'backup_id': datum.id})


class DeleteBackup(tables.DeleteAction):
    help_text = _("Delete backups is not recoverable.")

    @staticmethod
    def action_present(count):
        return ungettext_lazy(
            u"Delete Backup",
            u"Delete Backups",
            count
        )

    @staticmethod
    def action_past(count):
        return ungettext_lazy(
            u"Deleted Backup",
            u"Deleted Backups",
            count
        )


    @shield("Unable to delete backup", redirect="backups:index")