def save_model(self, request, obj, form, change): if not change: form.save_model(obj) form.save_related(obj) else: if isinstalled('orchestra.contrib.orders') and isinstalled('orchestra.contrib.services'): if 'type' in form.changed_data: old_type = Account.objects.get(pk=obj.pk).type new_type = form.cleaned_data['type'] context = { 'from': old_type.lower(), 'to': new_type.lower(), 'url': reverse('admin:orders_order_changelist'), } msg = '' if old_type in SERVICES_IGNORE_ACCOUNT_TYPE and new_type not in SERVICES_IGNORE_ACCOUNT_TYPE: context['url'] += '?account=%i&ignore=1' % obj.pk msg = _("Account type has been changed from <i>%(from)s</i> to <i>%(to)s</i>. " "You may want to mark <a href='%(url)s'>existing ignored orders</a> as not ignored.") elif old_type not in SERVICES_IGNORE_ACCOUNT_TYPE and new_type in SERVICES_IGNORE_ACCOUNT_TYPE: context['url'] += '?account=%i&ignore=0' % obj.pk msg = _("Account type has been changed from <i>%(from)s</i> to <i>%(to)s</i>. " "You may want to ignore <a href='%(url)s'>existing not ignored orders</a>.") if msg: messages.warning(request, mark_safe(msg % context)) super(AccountAdmin, self).save_model(request, obj, form, change)
def save_model(self, request, obj, form, change): if not change: form.save_model(obj) form.save_related(obj) else: if isinstalled('orchestra.contrib.orders') and isinstalled( 'orchestra.contrib.services'): if 'type' in form.changed_data: old_type = Account.objects.get(pk=obj.pk).type new_type = form.cleaned_data['type'] context = { 'from': old_type.lower(), 'to': new_type.lower(), 'url': reverse('admin:orders_order_changelist'), } msg = '' if old_type in SERVICES_IGNORE_ACCOUNT_TYPE and new_type not in SERVICES_IGNORE_ACCOUNT_TYPE: context['url'] += '?account=%i&ignore=1' % obj.pk msg = _( "Account type has been changed from <i>%(from)s</i> to <i>%(to)s</i>. " "You may want to mark <a href='%(url)s'>existing ignored orders</a> as not ignored." ) elif old_type not in SERVICES_IGNORE_ACCOUNT_TYPE and new_type in SERVICES_IGNORE_ACCOUNT_TYPE: context['url'] += '?account=%i&ignore=0' % obj.pk msg = _( "Account type has been changed from <i>%(from)s</i> to <i>%(to)s</i>. " "You may want to ignore <a href='%(url)s'>existing not ignored orders</a>." ) if msg: messages.warning(request, mark_safe(msg % context)) super(AccountAdmin, self).save_model(request, obj, form, change)
def get_queryset(self, request): """ Order by structured name and imporve performance """ qs = super(DomainAdmin, self).get_queryset(request) qs = qs.select_related('top', 'account') if request.method == 'GET': qs = qs.annotate( structured_id=Coalesce('top__id', 'id'), structured_name=Concat('top__name', 'name') ).order_by('-structured_id', 'structured_name') if apps.isinstalled('orchestra.contrib.websites'): qs = qs.prefetch_related('websites__domains') if apps.isinstalled('orchestra.contrib.mailboxes'): qs = qs.annotate(models.Count('addresses')) return qs
def get_queryset(self, request): """ Order by structured name and imporve performance """ qs = super(DomainAdmin, self).get_queryset(request) qs = qs.select_related('top', 'account') if request.method == 'GET': qs = qs.annotate(structured_id=Coalesce('top__id', 'id'), structured_name=Concat('top__name', 'name')).order_by( '-structured_id', 'structured_name') if apps.isinstalled('orchestra.contrib.websites'): qs = qs.prefetch_related('websites__domains') if apps.isinstalled('orchestra.contrib.mailboxes'): qs = qs.annotate(models.Count('addresses')) return qs
def validator(enabled, app=app): if enabled and isinstalled('orchestra.contrib.websites'): from orchestra.contrib.websites import settings if app not in settings.WEBSITES_SAAS_DIRECTIVES: raise ValidationError( _("Allow custom URL is enabled for '%s', " "but has no associated WEBSITES_SAAS_DIRECTIVES" % app))
def display_websites(self, domain): if apps.isinstalled('orchestra.contrib.websites'): websites = domain.websites.all() if websites: links = [] for website in websites: site_link = get_on_site_link(website.get_absolute_url()) admin_url = change_url(website) title = _("Edit website") link = format_html('<a href="{}" title="{}">{} {}</a>', admin_url, title, website.name, site_link) links.append(link) return '<br>'.join(links) add_url = reverse('admin:websites_website_add') add_url += '?account=%i&domains=%i' % (domain.account_id, domain.pk) add_link = format_html( '<a href="{}" title="{}"><img src="{}" /></a>', add_url, _("Add website"), static('orchestra/images/add.png'), ) return _("No website %s") % (add_link) return '---'
def save(self): # pre instance.save() if isinstalled('orchestra.contrib.websites'): if self.instance.custom_url: self.create_or_update_directive() elif self.instance.pk: self.delete_directive()
def get_accounts(): childrens=[] if isinstalled('orchestra.contrib.payments'): url = reverse('admin:payments_transactionprocess_changelist') childrens.append(items.MenuItem(_("Transaction processes"), url)) if isinstalled('orchestra.contrib.issues'): url = reverse('admin:issues_ticket_changelist') childrens.append(items.MenuItem(_("Tickets"), url)) for model, options in accounts.get().items(): if options.get('menu', True): opts = model._meta url = reverse('admin:{}_{}_changelist'.format( opts.app_label, opts.model_name)) name = capfirst(options.get('verbose_name_plural')) childrens.append(items.MenuItem(name, url)) return sorted(childrens, key=lambda i: i.title)
def clean(self): if self.allow_custom_url: if self.instance.custom_url: if isinstalled('orchestra.contrib.websites'): helpers.clean_custom_url(self) elif self.instance.custom_url: raise ValidationError({ 'custom_url': _("Custom URL not allowed for this service."), })
def display_websites(self, domain): if apps.isinstalled('orchestra.contrib.websites'): webs = domain.websites.all() if webs: links = [] for web in webs: url = change_url(web) links.append('<a href="%s">%s</a>' % (url, web.name)) return '<br>'.join(links) return _("No website")
def display_websites(self, domain): if apps.isinstalled('orchestra.contrib.websites'): websites = domain.websites.all() if websites: links = [] for website in websites: site_link = get_on_site_link(website.get_absolute_url()) admin_url = change_url(website) link = '<a href="%s">%s %s</a>' % (admin_url, website.name, site_link) links.append(link) return '<br>'.join(links) site_link = get_on_site_link('http://%s' % domain.name) return _("No website %s") % site_link
def display_url(self, saas): site_domain = saas.get_site_domain() 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)
def display_addresses(self, domain): if apps.isinstalled('orchestra.contrib.mailboxes'): add_url = reverse('admin:mailboxes_address_add') add_url += '?account=%i&domain=%i' % (domain.account_id, domain.pk) image = '<img src="%s"></img>' % static('orchestra/images/add.png') add_link = '<a href="%s" title="%s">%s</a>' % ( add_url, _("Add address"), image ) addresses = domain.addresses.all() if addresses: url = reverse('admin:mailboxes_address_changelist') url += '?domain=%i' % addresses[0].domain_id title = '\n'.join([address.email for address in addresses]) return '<a href="%s" title="%s">%s</a> %s' % (url, title, len(addresses), add_link) return _("No address %s") % (add_link) return '---'
def display_addresses(self, domain): if apps.isinstalled('orchestra.contrib.mailboxes'): add_url = reverse('admin:mailboxes_address_add') add_url += '?account=%i&domain=%i' % (domain.account_id, domain.pk) image = '<img src="%s"></img>' % static('orchestra/images/add.png') add_link = '<a href="%s" title="%s">%s</a>' % ( add_url, _("Add address"), image) addresses = domain.addresses.all() if addresses: url = reverse('admin:mailboxes_address_changelist') url += '?domain=%i' % addresses[0].domain_id title = '\n'.join([address.email for address in addresses]) return '<a href="%s" title="%s">%s</a> %s' % ( url, title, len(addresses), add_link) return _("No address %s") % (add_link) return '---'
def get_queryset(self, request): """ Order by structured name and imporve performance """ qs = super(DomainAdmin, self).get_queryset(request) qs = qs.select_related('top', 'account') # Order by structured name if request.method == 'GET': # For some reason if we do this we know for sure that join table will be called T4 query = str(qs.query) table = re.findall(r'(T\d+)\."account_id"', query)[0] qs = qs.extra( select={ 'structured_name': 'CONCAT({table}.name, domains_domain.name)'.format(table=table) }, ).order_by('structured_name') if apps.isinstalled('orchestra.contrib.websites'): qs = qs.prefetch_related('websites') return qs
def display_url(self, saas): site_domain = saas.get_site_domain() 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)
def get_administration_items(): childrens = [] if isinstalled('orchestra.contrib.settings'): url = reverse('admin:settings_setting_change') childrens.append(items.MenuItem(_("Settings"), url)) if isinstalled('orchestra.contrib.services'): url = reverse('admin:services_service_changelist') childrens.append(items.MenuItem(_("Services"), url)) url = reverse('admin:plans_plan_changelist') childrens.append(items.MenuItem(_("Plans"), url)) if isinstalled('orchestra.contrib.orchestration'): route = reverse('admin:orchestration_route_changelist') backendlog = reverse('admin:orchestration_backendlog_changelist') server = reverse('admin:orchestration_server_changelist') childrens.append(items.MenuItem(_("Orchestration"), route, children=[ items.MenuItem(_("Routes"), route), items.MenuItem(_("Backend logs"), backendlog), items.MenuItem(_("Servers"), server), ])) if isinstalled('orchestra.contrib.resources'): resource = reverse('admin:resources_resource_changelist') data = reverse('admin:resources_resourcedata_changelist') monitor = reverse('admin:resources_monitordata_changelist') childrens.append(items.MenuItem(_("Resources"), resource, children=[ items.MenuItem(_("Resources"), resource), items.MenuItem(_("Data"), data), items.MenuItem(_("Monitoring"), monitor), ])) if isinstalled('orchestra.contrib.miscellaneous'): url = reverse('admin:miscellaneous_miscservice_changelist') childrens.append(items.MenuItem(_("Miscellaneous"), url)) if isinstalled('orchestra.contrib.issues'): url = reverse('admin:issues_queue_changelist') childrens.append(items.MenuItem(_("Ticket queues"), url)) if isinstalled('djcelery'): task = reverse('admin:djcelery_taskstate_changelist') periodic = reverse('admin:djcelery_periodictask_changelist') worker = reverse('admin:djcelery_workerstate_changelist') childrens.append(items.MenuItem(_("Celery"), task, children=[ items.MenuItem(_("Tasks"), task), items.MenuItem(_("Periodic tasks"), periodic), items.MenuItem(_("Workers"), worker), ])) return childrens
def display_websites(self, domain): if apps.isinstalled('orchestra.contrib.websites'): websites = domain.websites.all() if websites: links = [] for website in websites: site_link = get_on_site_link(website.get_absolute_url()) admin_url = change_url(website) title = _("Edit website") link = '<a href="%s" title="%s">%s %s</a>' % ( admin_url, title, website.name, site_link) links.append(link) return '<br>'.join(links) add_url = reverse('admin:websites_website_add') add_url += '?account=%i&domains=%i' % (domain.account_id, domain.pk) image = '<img src="%s"></img>' % static('orchestra/images/add.png') add_link = '<a href="%s" title="%s">%s</a>' % ( add_url, _("Add website"), image ) return _("No website %s") % (add_link) return '---'
def delete(self): if isinstalled('orchestra.contrib.websites'): self.delete_directive()
def app_is_installed(app_name): return isinstalled(app_name)
def validator(enabled, app=app): if enabled and isinstalled('orchestra.contrib.websites'): from orchestra.contrib.websites import settings if app not in settings.WEBSITES_SAAS_DIRECTIVES: raise ValidationError(_("Allow custom URL is enabled for '%s', " "but has no associated WEBSITES_SAAS_DIRECTIVES" % app))