2014-09-26 19:21:09 +00:00
|
|
|
from django.contrib import admin
|
2015-10-01 16:02:26 +00:00
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
2021-05-17 11:20:18 +00:00
|
|
|
from django.utils.safestring import mark_safe
|
2023-11-17 12:25:13 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2014-09-26 19:21:09 +00:00
|
|
|
|
2015-04-02 16:14:55 +00:00
|
|
|
from orchestra.admin import ExtendedModelAdmin, ChangePasswordAdminMixin
|
2016-03-31 16:02:50 +00:00
|
|
|
from orchestra.admin.actions import disable, enable
|
2015-10-01 16:02:26 +00:00
|
|
|
from orchestra.admin.utils import change_url
|
2015-07-21 10:44:32 +00:00
|
|
|
from orchestra.contrib.accounts.actions import list_accounts
|
2015-04-05 10:46:24 +00:00
|
|
|
from orchestra.contrib.accounts.admin import AccountAdminMixin
|
2016-04-15 09:56:10 +00:00
|
|
|
from orchestra.contrib.accounts.filters import IsActiveListFilter
|
2014-11-24 14:39:41 +00:00
|
|
|
from orchestra.plugins.admin import SelectPluginAdminMixin
|
2015-10-01 16:02:26 +00:00
|
|
|
from orchestra.utils.apps import isinstalled
|
|
|
|
from orchestra.utils.html import get_on_site_link
|
2014-09-26 19:21:09 +00:00
|
|
|
|
2015-10-01 16:02:26 +00:00
|
|
|
from .filters import CustomURLListFilter
|
2014-09-26 19:21:09 +00:00
|
|
|
from .models import SaaS
|
|
|
|
from .services import SoftwareService
|
|
|
|
|
|
|
|
|
2015-04-02 16:14:55 +00:00
|
|
|
class SaaSAdmin(SelectPluginAdminMixin, ChangePasswordAdminMixin, AccountAdminMixin, ExtendedModelAdmin):
|
2016-04-15 09:56:10 +00:00
|
|
|
list_display = ('name', 'service', 'display_url', 'account_link', 'display_active')
|
|
|
|
list_filter = ('service', IsActiveListFilter, CustomURLListFilter)
|
2015-04-07 15:14:49 +00:00
|
|
|
search_fields = ('name', 'account__username')
|
2015-03-25 15:45:04 +00:00
|
|
|
change_readonly_fields = ('service',)
|
2014-09-26 19:21:09 +00:00
|
|
|
plugin = SoftwareService
|
|
|
|
plugin_field = 'service'
|
2015-03-04 21:06:16 +00:00
|
|
|
plugin_title = 'Software as a Service'
|
2016-03-31 16:02:50 +00:00
|
|
|
actions = (disable, enable, list_accounts)
|
2021-05-17 11:20:18 +00:00
|
|
|
|
|
|
|
@mark_safe
|
2015-10-01 16:02:26 +00:00
|
|
|
def display_url(self, saas):
|
2015-03-25 15:45:04 +00:00
|
|
|
site_domain = saas.get_site_domain()
|
2015-10-01 16:02:26 +00:00
|
|
|
site_link = '<a href="http://%s">%s</a>' % (site_domain, site_domain)
|
|
|
|
links = [site_link]
|
|
|
|
if saas.custom_url and isinstalled('orchestra.contrib.websites'):
|
|
|
|
try:
|
|
|
|
website = saas.service_instance.get_website()
|
|
|
|
except ObjectDoesNotExist:
|
|
|
|
warning = _("Related website directive does not exist for this custom URL.")
|
|
|
|
link = '<span style="color:red" title="%s">%s</span>' % (warning, saas.custom_url)
|
|
|
|
else:
|
|
|
|
website_link = get_on_site_link(saas.custom_url)
|
|
|
|
admin_url = change_url(website)
|
|
|
|
link = '<a title="Edit website" href="%s">%s %s</a>' % (
|
|
|
|
admin_url, saas.custom_url, website_link
|
|
|
|
)
|
|
|
|
links.append(link)
|
|
|
|
return '<br>'.join(links)
|
|
|
|
display_url.short_description = _("URL")
|
|
|
|
display_url.admin_order_field = 'name'
|
2021-05-17 11:20:18 +00:00
|
|
|
|
2015-10-01 16:02:26 +00:00
|
|
|
def get_fields(self, *args, **kwargs):
|
|
|
|
fields = super(SaaSAdmin, self).get_fields(*args, **kwargs)
|
|
|
|
if not self.plugin_instance.allow_custom_url:
|
|
|
|
return [field for field in fields if field != 'custom_url']
|
|
|
|
return fields
|
2015-03-04 21:06:16 +00:00
|
|
|
|
2014-09-26 19:21:09 +00:00
|
|
|
|
|
|
|
admin.site.register(SaaS, SaaSAdmin)
|