diff --git a/tailbone/views/bouncer.py b/tailbone/views/bouncer.py index 72674890..bcbd7131 100644 --- a/tailbone/views/bouncer.py +++ b/tailbone/views/bouncer.py @@ -33,14 +33,11 @@ from rattail.db import model from rattail.bouncer import get_handler from rattail.bouncer.config import get_profile_keys -import formalchemy from pyramid.response import FileResponse -from webhelpers2.html import literal +from webhelpers2.html import HTML, tags from tailbone import grids -from tailbone.db import Session -from tailbone.views import MasterView2 as MasterView -from tailbone.forms.renderers.bouncer import BounceMessageFieldRenderer +from tailbone.views import MasterView3 as MasterView class EmailBouncesView(MasterView): @@ -53,6 +50,12 @@ class EmailBouncesView(MasterView): creatable = False editable = False + labels = { + 'config_key': "Source", + 'bounce_recipient_address': "Bounced To", + 'intended_recipient_address': "Intended For", + } + grid_columns = [ 'config_key', 'bounced', @@ -90,38 +93,59 @@ class EmailBouncesView(MasterView): g.set_link('bounced') g.set_link('intended_recipient_address') - def configure_fieldset(self, fs): - bounce = fs.model - handler = self.get_handler(bounce) - fs.append(formalchemy.Field('message', - value=handler.msgpath(bounce), - renderer=BounceMessageFieldRenderer.new(self.request, handler))) - fs.append(formalchemy.Field('links', - value=list(handler.make_links(Session(), bounce.intended_recipient_address)), - renderer=LinksFieldRenderer)) - fs.configure( - include=[ - fs.config_key.label("Source"), - fs.message, - fs.bounced, - fs.bounce_recipient_address.label("Bounced To"), - fs.intended_recipient_address.label("Intended For"), - fs.links, - fs.processed, - fs.processed_by, - ], - readonly=True) + def configure_form(self, f): + super(EmailBouncesView, self).configure_form(f) + bounce = f.model_instance + f.set_renderer('message', self.render_message_file) + f.set_renderer('links', self.render_links) + f.fields = [ + 'config_key', + 'message', + 'bounced', + 'bounce_recipient_address', + 'intended_recipient_address', + 'links', + 'processed', + 'processed_by', + ] if not bounce.processed: - del fs.processed - del fs.processed_by + f.remove_field('processed') + f.remove_field('processed_by') + + def render_links(self, bounce, field): + handler = self.get_handler(bounce) + value = list(handler.make_links(self.Session(), bounce.intended_recipient_address)) + if not value: + return "n/a" + + links = [] + for link in value: + label = HTML.literal("{}: ".format(link.type)) + anchor = tags.link_to(link.title, link.url, target='_blank') + links.append(HTML.tag('li', label + anchor)) + + return HTML.tag('ul', HTML.literal('').join(links)) + + def render_message_file(self, bounce, field): + handler = self.get_handler(bounce) + path = handler.msgpath(bounce) + if not path: + return "" + + url = self.get_action_url('download', bounce) + return self.render_file_field(path, url) def template_kwargs_view(self, **kwargs): bounce = kwargs['instance'] kwargs['bounce'] = bounce handler = self.get_handler(bounce) kwargs['handler'] = handler - with open(handler.msgpath(bounce), 'rb') as f: - kwargs['message'] = f.read() + path = handler.msgpath(bounce) + if os.path.exists(path): + with open(path, 'rb') as f: + kwargs['message'] = f.read() + else: + kwargs['message'] = "(file not found)" return kwargs def process(self): @@ -185,19 +209,5 @@ class EmailBouncesView(MasterView): cls._defaults(config) -class LinksFieldRenderer(formalchemy.FieldRenderer): - - def render_readonly(self, **kwargs): - value = self.raw_value - if not value: - return 'n/a' - html = literal('