import web
from decorators import i18n
from user.userdecorators import admin

class UninstallView:

    @admin
    def GET(self, name):
        plugin = web.ctx.plugin_manager.get_plugin_by_name(name)
        return web.ctx.render.plugin_manager.uninstall(plugin)

    @admin
    @i18n
    def POST(self, name):
        uninstall = web.input().uninstall

        disable_plugins = web.ctx.plugin_manager.disable_plugin(name)
        plugins_name = (p.name for p in disable_plugins if not p.name == name)
        if plugins_name:
            string_name = ', '.join(plugins_name)
            web.ctx.notify(_('The next plugins has been disabled: %s'
                % string_name), 'notification')

        delete_files = True if uninstall == 'A' else False

        web.ctx.plugin_manager.uninstall_plugin(name, delete_files)

        web.ctx.notify(_('The %s plugin has been uninstall.' % name),
                'notification')

        raise web.seeother('/pluginmanager')
