def selected_related_choices(queryset): for order in queryset: verbose = '<a href="{order_url}">{description}</a> ' verbose += '<a class="account" href="{account_url}">{account}</a>' verbose = verbose.format( order_url=change_url(order), description=order.description, account_url=change_url(order.account), account=str(order.account) ) yield (order.pk, mark_safe(verbose))
def selected_related_choices(queryset): for order in queryset: verbose = '<a href="{order_url}">{description}</a> ' verbose += '<a class="account" href="{account_url}">{account}</a>' if order.ignore: verbose += ' (ignored)' verbose = verbose.format( order_url=change_url(order), description=order.description, account_url=change_url(order.account), account=str(order.account) ) yield (order.pk, mark_safe(verbose))
def retry_backend(modeladmin, request, queryset): related_operations = queryset.values_list('operations__id', flat=True).distinct() related_operations = BackendOperation.objects.filter(pk__in=related_operations) related_operations = related_operations.select_related('log__server').prefetch_related('instance') if request.POST.get('post') == 'generic_confirmation': operations = [] for operation in related_operations: if operation.instance: op = Operation.load(operation) operations.append(op) if not operations: messages.warning(request, _("No backend operation has been executed.")) else: logs = Operation.execute(operations) message_user(request, logs) for backendlog in queryset: modeladmin.log_change(request, backendlog, 'Retried') return opts = modeladmin.model._meta display_objects = [] deleted_objects = [] for op in related_operations: if not op.instance: deleted_objects.append(op) else: context = { 'backend': op.log.backend, 'action': op.action, 'instance': op.instance, 'instance_url': change_url(op.instance), 'server': op.log.server, 'server_url': change_url(op.log.server), } display_objects.append(mark_safe( '%(backend)s.%(action)s(<a href="%(instance_url)s">%(instance)s</a>) @ <a href="%(server_url)s">%(server)s</a>' % context )) context = { 'title': _("Are you sure to execute the following backends?"), 'action_name': _('Retry backend'), 'action_value': 'retry_backend', 'display_objects': display_objects, 'deleted_objects': deleted_objects, 'queryset': queryset, 'opts': opts, 'app_label': opts.app_label, 'action_checkbox_name': helpers.ACTION_CHECKBOX_NAME, 'obj': get_object_from_url(modeladmin, request), } return render(request, 'admin/orchestration/backends/retry.html', context)
def confirmation(self, request): form = BillSelectConfirmationForm(initial=self.options) if int(request.POST.get('step')) >= 3: bills = self.queryset.bill(commit=True, **self.options) for order in self.queryset: self.modeladmin.log_change(request, order, _("Billed")) if not bills: msg = _("Selected orders do not have pending billing") self.modeladmin.message_user(request, msg, messages.WARNING) else: num = len(bills) if num == 1: url = change_url(bills[0]) else: url = reverse('admin:bills_bill_changelist') ids = ','.join(map(str, bills)) url += '?id__in=%s' % ids num = len(bills) msg = ungettext( '<a href="{url}">One bill</a> has been created.', '<a href="{url}">{num} bills</a> have been created.', num).format(url=url, num=num) msg = mark_safe(msg) self.modeladmin.message_user(request, msg, messages.INFO) return bills = self.queryset.bill(commit=False, **self.options) self.context.update({ 'title': _("Confirmation for billing selected orders"), 'step': 3, 'form': form, 'bills': bills, }) return render(request, self.template, self.context)
def swap_user(self, username, username2, dbname): database = Database.objects.get(name=dbname, type=self.db_type) url = self.live_server_url + change_url(database) self.selenium.get(url) # remove user "username" user = DatabaseUser.objects.get(username=username, type=self.db_type) users_to = self.selenium.find_element_by_id('id_users_to') users_select = Select(users_to) users_select.select_by_value(str(user.pk)) remove_user = self.selenium.find_element_by_id('id_users_remove_link') remove_user.click() time.sleep(0.2) # add user "username2" user = DatabaseUser.objects.get(username=username2, type=self.db_type) users_from = self.selenium.find_element_by_id('id_users_from') users_select = Select(users_from) users_select.select_by_value(str(user.pk)) add_user = self.selenium.find_element_by_id('id_users_add_link') add_user.click() time.sleep(0.2) save = self.selenium.find_element_by_name('_save') save.submit() self.assertNotEqual(url, self.selenium.current_url)
def save(self, username): user = SystemUser.objects.get(username=username) url = self.live_server_url + change_url(user) self.selenium.get(url) save = self.selenium.find_element_by_name('_save') save.submit() self.assertNotEqual(url, self.selenium.current_url)
def __init__(self, *args, **kwargs): super(SaaSBaseForm, self).__init__(*args, **kwargs) self.is_change = bool(self.instance and self.instance.pk) if self.is_change: site_domain = self.instance.get_site_domain() if self.instance.custom_url: try: website = self.instance.service_instance.get_website() except ObjectDoesNotExist: link = ( '<br><span style="color:red"><b>Warning:</b> ' 'Related website directive does not exist for %s URL !</span>' % self.instance.custom_url) else: url = change_url(website) link = '<br>Related website: <a href="%s">%s</a>' % ( url, website.name) self.fields['custom_url'].help_text += link else: site_domain = self.plugin.site_domain context = { 'site_name': '<site_name>', 'name': '<site_name>', } site_domain = site_domain % context if '<site_name>' in site_domain: site_link = site_domain else: site_link = '<a href="http://%s">%s</a>' % (site_domain, site_domain) self.fields['site_url'].widget.display = site_link self.fields['name'].label = _( "Site name") if '%(' in self.plugin.site_domain else _("Username")
def __init__(self, *args, **kwargs): super(SaaSBaseForm, self).__init__(*args, **kwargs) self.is_change = bool(self.instance and self.instance.pk) if self.is_change: site_domain = self.instance.get_site_domain() if self.instance.custom_url: try: website = self.instance.service_instance.get_website() except ObjectDoesNotExist: link = ('<br><span style="color:red"><b>Warning:</b> ' 'Related website directive does not exist for %s URL !</span>' % self.instance.custom_url) else: url = change_url(website) link = '<br>Related website: <a href="%s">%s</a>' % (url, website.name) self.fields['custom_url'].help_text += link else: site_domain = self.plugin.site_domain context = { 'site_name': '<site_name>', 'name': '<site_name>', } site_domain = site_domain % context if '<site_name>' in site_domain: site_link = site_domain else: site_link = '<a href="http://%s">%s</a>' % (site_domain, site_domain) self.fields['site_url'].widget.display = site_link self.fields['name'].label = _("Site name") if '%(' in self.plugin.site_domain else _("Username")
def get_instance_url(operation): try: url = change_url(operation.instance) except NoReverseMatch: alt_repr = '%s-%s' % (operation.content_type, operation.object_id) return _("Deleted {0}").format(operation.instance_repr or alt_repr) return orchestra_settings.ORCHESTRA_SITE_URL + url
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 display_addresses(self, mailbox): # Get from forwards cache = caches.get_request_cache() cached_forwards = cache.get('forwards') if cached_forwards is None: cached_forwards = {} qs = Address.objects.filter( forward__regex=r'(^|.*\s)[^@]+(\s.*|$)') qs = qs.annotate(email=Concat('name', V('@'), 'domain__name')) qs = qs.values_list('id', 'email', 'forward') for addr_id, email, mbox in qs: url = reverse('admin:mailboxes_address_change', args=(addr_id, )) link = '<a href="%s">%s</a>' % (url, email) try: cached_forwards[mbox].append(link) except KeyError: cached_forwards[mbox] = [link] cache.set('forwards', cached_forwards) try: forwards = cached_forwards[mailbox.name] except KeyError: forwards = [] # Get from mailboxes addresses = [] for addr in mailbox.addresses.all(): url = change_url(addr) addresses.append('<a href="%s">%s</a>' % (url, addr.email)) return '<br>'.join(addresses + forwards)
def display_addresses(self, mailbox): # Get from forwards cache = caches.get_request_cache() cached_forwards = cache.get('forwards') if cached_forwards is None: cached_forwards = {} qs = Address.objects.filter(forward__regex=r'(^|.*\s)[^@]+(\s.*|$)') qs = qs.annotate(email=Concat('name', V('@'), 'domain__name')) qs = qs.values_list('id', 'email', 'forward') for addr_id, email, mbox in qs: url = reverse('admin:mailboxes_address_change', args=(addr_id,)) link = '<a href="%s">%s</a>' % (url, email) try: cached_forwards[mbox].append(link) except KeyError: cached_forwards[mbox] = [link] cache.set('forwards', cached_forwards) try: forwards = cached_forwards[mailbox.name] except KeyError: forwards = [] # Get from mailboxes addresses = [] for addr in mailbox.addresses.all(): url = change_url(addr) addresses.append('<a href="%s">%s</a>' % (url, addr.email)) return '<br>'.join(addresses+forwards)
def get_instance_url(operation): try: url = change_url(operation.instance) except NoReverseMatch: return _("Deleted {0}").format(operation.instance_repr or '-'.join( (escape(operation.content_type), escape(operation.object_id)))) return orchestra_settings.ORCHESTRA_SITE_URL + url
def get_instance_url(operation): try: url = change_url(operation.instance) except NoReverseMatch: alt_repr = '%s-%i' % (operation.content_type, operation.object_id) return _("Deleted {0}").format(operation.instance_repr or alt_repr) return orchestra_settings.ORCHESTRA_SITE_URL + url
def _format_display_objects(modeladmin, request, queryset, related): objects = [] opts = modeladmin.model._meta for obj in queryset: objects.append( mark_safe('{0}: <a href="{1}">{2}</a>'.format( capfirst(opts.verbose_name), change_url(obj), obj)) ) subobjects = [] attr, verb = related for trans in getattr(obj.transactions, attr)(): subobjects.append( mark_safe('Transaction: <a href="{}">{}</a> will be marked as {}'.format( change_url(trans), trans, verb)) ) objects.append(subobjects) return {'display_objects': objects}
def _format_display_objects(modeladmin, request, queryset, related): objects = [] opts = modeladmin.model._meta for obj in queryset: objects.append( mark_safe('{0}: <a href="{1}">{2}</a>'.format( capfirst(opts.verbose_name), change_url(obj), obj)) ) subobjects = [] attr, verb = related for related in getattr(obj.transactions, attr)(): subobjects.append( mark_safe('Transaction: <a href="{}">{}</a> will be marked as {}'.format( change_url(related), related, verb)) ) objects.append(subobjects) return {'display_objects': objects}
def display_webapps(self, website): webapps = [] for content in website.content_set.all(): webapp = content.webapp url = change_url(webapp) name = "%s on %s" % (webapp.name, content.path or "/") webapps.append('<a href="%s">%s</a>' % (url, name)) return "<br>".join(webapps)
def close_bills(modeladmin, request, queryset, action='close_bills'): # Validate bills for bill in queryset: if not validate_contact(request, bill): return False if not bill.is_open: messages.warning(request, _("Selected bills should be in open state")) return False SelectSourceFormSet = modelformset_factory(modeladmin.model, form=SelectSourceForm, formset=AdminFormSet, extra=0) formset = SelectSourceFormSet(queryset=queryset) if request.POST.get('post') == 'generic_confirmation': formset = SelectSourceFormSet(request.POST, request.FILES, queryset=queryset) if formset.is_valid(): transactions = [] for form in formset.forms: source = form.cleaned_data['source'] transaction = form.instance.close(payment=source) if transaction: transactions.append(transaction) for bill in queryset: modeladmin.log_change(request, bill, 'Closed') messages.success(request, _("Selected bills have been closed")) if transactions: num = len(transactions) if num == 1: url = change_url(transactions[0]) else: url = reverse('admin:payments_transaction_changelist') url += 'id__in=%s' % ','.join([str(t.id) for t in transactions]) context = { 'url': url, 'num': num, } message = ungettext( _('<a href="%(url)s">One related transaction</a> has been created') % context, _('<a href="%(url)s">%(num)i related transactions</a> have been created') % context, num) messages.success(request, mark_safe(message)) return opts = modeladmin.model._meta context = { 'title': _("Are you sure about closing the following bills?"), 'content_message': _("Once a bill is closed it can not be further modified.</p>" "<p>Please select a payment source for the selected bills"), 'action_name': 'Close bills', 'action_value': action, 'display_objects': [], 'queryset': queryset, 'opts': opts, 'app_label': opts.app_label, 'action_checkbox_name': helpers.ACTION_CHECKBOX_NAME, 'formset': formset, 'obj': get_object_from_url(modeladmin, request), } template = 'admin/orchestra/generic_confirmation.html' if action == 'close_send_download_bills': template = 'admin/bills/bill/close_send_download_bills.html' return render(request, template, context)
def display_total(self, line): if line.pk: total = line.compute_total() sublines = line.sublines.all() url = change_url(line) if sublines: content = '\n'.join(['%s: %s' % (sub.description, sub.total) for sub in sublines]) img = static('admin/img/icon-alert.svg') return '<a href="%s" title="%s">%s <img src="%s"></img></a>' % (url, content, total, img) return '<a href="%s">%s</a>' % (url, total)
def add_group(self, username, groupname): user = SystemUser.objects.get(username=username) url = self.live_server_url + change_url(user) self.selenium.get(url) groups = self.selenium.find_element_by_id('id_groups_add_all_link') groups.click() time.sleep(0.5) save = self.selenium.find_element_by_name('_save') save.submit() self.assertNotEqual(url, self.selenium.current_url)
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 set_context_emails(modeladmin, request, queryset): opts = modeladmin.model._meta bills = [] for bill in queryset: emails = ', '.join(bill.get_billing_contact_emails()) bills.append( format_html('{0}: <a href="{1}">{2}</a> <i>{3}</i>', capfirst(opts.verbose_name), change_url(bill), bill, emails)) return {'display_objects': bills}
def content_object_link(self, order): if order.content_object: try: url = change_url(order.content_object) except NoReverseMatch: # Does not has admin return order.content_object_repr description = str(order.content_object) return '<a href="{url}">{description}</a>'.format( url=url, description=description) return order.content_object_repr
def set_context_emails(modeladmin, request, queryset): opts = modeladmin.model._meta bills = [] for bill in queryset: emails = ', '.join(bill.get_billing_contact_emails()) bills.append(format_html('{0}: <a href="{1}">{2}</a> <i>{3}</i>', capfirst(opts.verbose_name), change_url(bill), bill, emails) ) return { 'display_objects': bills }
def update_address_name(self, name, address_name): mail_list = List.objects.get(name=name) url = self.live_server_url + change_url(mail_list) self.selenium.get(url) address_name_field = self.selenium.find_element_by_id("id_address_name") address_name_field.clear() address_name_field.send_keys(address_name) save = self.selenium.find_element_by_name("_save") save.submit() self.assertNotEqual(url, self.selenium.current_url)
def update_domain(self, name, domain_name): mail_list = List.objects.get(name=name) url = self.live_server_url + change_url(mail_list) self.selenium.get(url) domain = Domain.objects.get(name=domain_name) domain_input = self.selenium.find_element_by_id('id_address_domain') domain_select = Select(domain_input) domain_select.select_by_value(str(domain.pk)) save = self.selenium.find_element_by_name('_save') save.submit() self.assertNotEqual(url, self.selenium.current_url)
def update_address_name(self, name, address_name): mail_list = List.objects.get(name=name) url = self.live_server_url + change_url(mail_list) self.selenium.get(url) address_name_field = self.selenium.find_element_by_id( 'id_address_name') address_name_field.clear() address_name_field.send_keys(address_name) save = self.selenium.find_element_by_name('_save') save.submit() self.assertNotEqual(url, self.selenium.current_url)
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_websites(self, webapp): websites = [] for content in webapp.content_set.all(): website = content.website url = change_url(website) name = "%s on %s" % (website.name, content.path) websites.append('<a href="%s">%s</a>' % (url, name)) if not websites: add_url = reverse('admin:websites_website_add') add_url += '?account=%s' % webapp.account_id plus = '<strong style="color:green; font-size:12px">+</strong>' websites.append('<a href="%s">%s%s</a>' % (add_url, plus, ugettext("Add website"))) return '<br>'.join(websites)
def update_domain(self, name, domain_name): mail_list = List.objects.get(name=name) url = self.live_server_url + change_url(mail_list) self.selenium.get(url) domain = Domain.objects.get(name=domain_name) domain_input = self.selenium.find_element_by_id("id_address_domain") domain_select = Select(domain_input) domain_select.select_by_value(str(domain.pk)) save = self.selenium.find_element_by_name("_save") save.submit() self.assertNotEqual(url, self.selenium.current_url)
def display_webapps(self, website): webapps = [] for content in website.content_set.all(): site_link = get_on_site_link(content.get_absolute_url()) webapp = content.webapp detail = _("Edit Webapp") + ' ' + webapp.get_type_display() try: detail += ' ' + webapp.type_instance.get_detail() except KeyError: pass url = change_url(webapp) name = "%s on %s" % (webapp.name, content.path or '/') webapps.append('<a href="%s" title="%s">%s %s</a>' % (url, detail, name, site_link)) return '<br>'.join(webapps)
def add_user_to_db(self, username, dbname): database = Database.objects.get(name=dbname, type=self.db_type) url = self.live_server_url + change_url(database) self.selenium.get(url) user = DatabaseUser.objects.get(username=username, type=self.db_type) users_from = self.selenium.find_element_by_id('id_users_from') users_select = Select(users_from) users_select.select_by_value(str(user.pk)) add_user = self.selenium.find_element_by_id('id_users_add_link') add_user.click() save = self.selenium.find_element_by_name('_save') save.submit() self.assertNotEqual(url, self.selenium.current_url)
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_webapps(self, website): webapps = [] for content in website.content_set.all(): site_link = get_on_site_link(content.get_absolute_url()) webapp = content.webapp detail = _("Edit Webapp") + ' ' + webapp.get_type_display() try: detail += ' ' + webapp.type_instance.get_detail() except KeyError: pass url = change_url(webapp) name = "%s on %s" % (webapp.name, content.path or '/') webapp_info = format_html('<a href="{}" title="{}">{}</a> {}', url, detail, name, site_link) webapps.append(webapp_info) return '<br>'.join(webapps)
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 confirmation(self, request): form = BillSelectConfirmationForm(initial=self.options) if int(request.POST.get('step')) >= 3: bills = self.queryset.bill(commit=True, **self.options) for order in self.queryset: self.modeladmin.log_change(request, order, _("Billed")) if not bills: msg = _("Selected orders do not have pending billing") self.modeladmin.message_user(request, msg, messages.WARNING) else: num = len(bills) if num == 1: url = change_url(bills[0]) else: url = reverse('admin:bills_bill_changelist') ids = ','.join([str(b.id) for b in bills]) url += '?id__in=%s' % ids msg = ungettext( '<a href="{url}">One bill</a> has been created.', '<a href="{url}">{num} bills</a> have been created.', num).format(url=url, num=num) msg = mark_safe(msg) self.modeladmin.message_user(request, msg, messages.INFO) return bills = self.queryset.bill(commit=False, **self.options) bills_with_total = [] for account, lines in bills: total = 0 for line in lines: discount = sum([discount.total for discount in line.discounts]) total += line.subtotal + discount bills_with_total.append((account, total, lines)) self.context.update({ 'title': _("Confirmation for billing selected orders"), 'step': 3, 'form': form, 'bills': sorted(bills_with_total, key=lambda i: -i[1]), }) return render(request, self.template, self.context)
def confirmation(self, request): form = BillSelectConfirmationForm(initial=self.options) if int(request.POST.get("step")) >= 3: bills = self.queryset.bill(commit=True, **self.options) for order in self.queryset: self.modeladmin.log_change(request, order, _("Billed")) if not bills: msg = _("Selected orders do not have pending billing") self.modeladmin.message_user(request, msg, messages.WARNING) else: num = len(bills) if num == 1: url = change_url(bills[0]) else: url = reverse("admin:bills_bill_changelist") ids = ",".join([str(b.id) for b in bills]) url += "?id__in=%s" % ids msg = ungettext( '<a href="{url}">One bill</a> has been created.', '<a href="{url}">{num} bills</a> have been created.', num, ).format(url=url, num=num) msg = mark_safe(msg) self.modeladmin.message_user(request, msg, messages.INFO) return bills = self.queryset.bill(commit=False, **self.options) bills_with_total = [] for account, lines in bills: total = 0 for line in lines: discount = sum([discount.total for discount in line.discounts]) total += line.subtotal + discount bills_with_total.append((account, total, lines)) self.context.update( { "title": _("Confirmation for billing selected orders"), "step": 3, "form": form, "bills": sorted(bills_with_total, key=lambda i: -i[1]), } ) return render(request, self.template, self.context)
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 get_view_on_site_url(self, obj=None): if obj: return change_url(obj)
def get_absolute_url(self): return change_url(self)
def account_link(self, instance): account = instance.account if instance.pk else self.account url = change_url(account) return '<a href="%s">%s</a>' % (url, str(account))
def display_databases(self, user): links = [] for db in user.databases.all(): link = '<a href="%s">%s</a>' % (change_url(db), db.name) links.append(link) return '<br>'.join(links)
def admin_link(obj): try: url = change_url(obj) except NoReverseMatch: return str(obj) return mark_safe('<a href="%s">%s</a>' % (url, obj))
def admin_url(obj): return change_url(obj)
def display_all_mailboxes(self, address): boxes = [] for mailbox in address.get_mailboxes(): url = change_url(mailbox) boxes.append('<a href="%s">%s</a>' % (url, mailbox.name)) return '<br>'.join(boxes)
def display_forwards(self, mailbox): forwards = [] for addr in mailbox.get_forwards(): url = change_url(addr) forwards.append('<a href="%s">%s</a>' % (url, addr.email)) return '<br>'.join(forwards)
def display_users(self, db): links = [] for user in db.users.all(): link = '<a href="%s">%s</a>' % (change_url(user), user.username) links.append(link) return '<br>'.join(links)
def display_all_mailboxes(self, address): boxes = address.get_mailboxes() return format_html_join('<br>', '<a href="{}">{}</a>', [(change_url(mailbox), mailbox.name) for mailbox in boxes])
def edit_records(modeladmin, request, queryset): selected_ids = queryset.values_list('id', flat=True) # Include subodmains queryset = queryset.model.objects.filter( Q(top__id__in=selected_ids) | Q(id__in=selected_ids) ).annotate( structured_id=Coalesce('top__id', 'id'), structured_name=Concat('top__name', 'name') ).order_by('-structured_id', 'structured_name') formsets = [] for domain in queryset.prefetch_related('records'): modeladmin_copy = copy.copy(modeladmin) modeladmin_copy.model = Record prefix = '' if domain.is_top else ' '*8 context = { 'url': change_url(domain), 'name': prefix+domain.name, 'title': '', } if domain.id not in selected_ids: context['name'] += '*' context['title'] = _("This subdomain was not explicitly selected " "but has been automatically added to this list.") link = '<a href="%(url)s" title="%(title)s">%(name)s</a>' % context modeladmin_copy.verbose_name_plural = mark_safe(link) RecordFormSet = modelformset_factory( Record, form=RecordForm, formset=RecordEditFormSet, extra=1, can_delete=True) formset = RecordFormSet(queryset=domain.records.all(), prefix=domain.id) formset.instance = domain formset.cls = RecordFormSet formsets.append(formset) if request.POST.get('post') == 'generic_confirmation': posted_formsets = [] all_valid = True for formset in formsets: instance = formset.instance formset = formset.cls( request.POST, request.FILES, queryset=formset.queryset, prefix=instance.id) formset.instance = instance if not formset.is_valid(): all_valid = False posted_formsets.append(formset) formsets = posted_formsets if all_valid: for formset in formsets: for form in formset.forms: form.instance.domain_id = formset.instance.id formset.save() fake_form = AttrDict({ 'changed_data': False }) change_message = modeladmin.construct_change_message(request, fake_form, [formset]) modeladmin.log_change(request, formset.instance, change_message) num = len(formsets) message = ungettext( _("Records for one selected domain have been updated."), _("Records for %i selected domains have been updated.") % num, num) modeladmin.message_user(request, message) return opts = modeladmin.model._meta context = { 'title': _("Edit records"), 'action_name': _("Edit records"), 'action_value': 'edit_records', 'display_objects': [], 'queryset': queryset, 'opts': opts, 'app_label': opts.app_label, 'action_checkbox_name': helpers.ACTION_CHECKBOX_NAME, 'formsets': formsets, 'obj': get_object_from_url(modeladmin, request), } return render(request, 'admin/domains/domain/edit_records.html', context)
def display_forwards(self, mailbox): forwards = mailbox.get_forwards() return format_html_join('<br>', '<a href="{}">{}</a>', [(change_url(addr), addr.email) for addr in forwards])