| 1 | import logging |
|---|
| 2 | import cgi |
|---|
| 3 | |
|---|
| 4 | from pylons.i18n import _ |
|---|
| 5 | |
|---|
| 6 | import adhocracy.model as model |
|---|
| 7 | from .. import helpers as h |
|---|
| 8 | |
|---|
| 9 | log = logging.getLogger(__name__) |
|---|
| 10 | DT_FORMAT = "%Y%m%d%H%M%S" |
|---|
| 11 | |
|---|
| 12 | class ObjectFormatter(object): |
|---|
| 13 | |
|---|
| 14 | def unicode(self, value): |
|---|
| 15 | return value |
|---|
| 16 | |
|---|
| 17 | def html(self, value): |
|---|
| 18 | return value |
|---|
| 19 | |
|---|
| 20 | class DelegateableFormatter(ObjectFormatter): |
|---|
| 21 | |
|---|
| 22 | def unicode(self, delegateable): |
|---|
| 23 | return delegateable.full_title |
|---|
| 24 | |
|---|
| 25 | def html(self, delegateable): |
|---|
| 26 | return h.delegateable.link(delegateable) |
|---|
| 27 | |
|---|
| 28 | class ProposalFormatter(DelegateableFormatter): |
|---|
| 29 | pass |
|---|
| 30 | |
|---|
| 31 | class PageFormatter(DelegateableFormatter): |
|---|
| 32 | pass |
|---|
| 33 | |
|---|
| 34 | class PollFormatter(ObjectFormatter): |
|---|
| 35 | |
|---|
| 36 | SELECT_PATTERN = lambda s, v, p: _("variant %(variant)s of %(page)s") % \ |
|---|
| 37 | {'variant': v, |
|---|
| 38 | 'page': p} |
|---|
| 39 | |
|---|
| 40 | def _get_formatter(self, poll): |
|---|
| 41 | if isinstance(poll.subject, model.Comment): |
|---|
| 42 | return CommentFormatter() |
|---|
| 43 | if isinstance(poll.subject, model.Delegateable): |
|---|
| 44 | return DelegateableFormatter() |
|---|
| 45 | else: |
|---|
| 46 | return unicode(poll.subject) |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | def unicode(self, poll): |
|---|
| 50 | if poll.action == poll.SELECT and poll.selection: |
|---|
| 51 | text = poll.selection.page.variant_head(poll.variant) |
|---|
| 52 | title = _("Status quo") if text.variant == text.HEAD else text.variant |
|---|
| 53 | return self.SELECT_PATTERN(title, poll.selection.page.title) |
|---|
| 54 | else: |
|---|
| 55 | fmt = self._get_formatter(poll) |
|---|
| 56 | return fmt.unicode(poll.subject) |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | def html(self, poll): |
|---|
| 60 | if poll.action == poll.SELECT: |
|---|
| 61 | text = poll.selection.page.variant_head(poll.variant) |
|---|
| 62 | title = _("Status quo") if text.variant == text.HEAD else text.variant |
|---|
| 63 | variant_link = "<a href='%s'>%s</a>" % (h.text.url(text), |
|---|
| 64 | cgi.escape(title)) |
|---|
| 65 | page_link = h.page.link(poll.selection.page, icon=True, icon_size=16) |
|---|
| 66 | return self.SELECT_PATTERN(variant_link, page_link) |
|---|
| 67 | else: |
|---|
| 68 | fmt = self._get_formatter(poll) |
|---|
| 69 | return fmt.html(poll.subject) |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | class InstanceFormatter(ObjectFormatter): |
|---|
| 73 | |
|---|
| 74 | def unicode(self, instance): |
|---|
| 75 | return instance.label |
|---|
| 76 | |
|---|
| 77 | def html(self, instance): |
|---|
| 78 | return u"<a class='event_instance' href='%s'>%s</a>" % ( |
|---|
| 79 | h.entity_url(instance), |
|---|
| 80 | instance.label) |
|---|
| 81 | |
|---|
| 82 | class UserFormatter(ObjectFormatter): |
|---|
| 83 | |
|---|
| 84 | def unicode(self, user): |
|---|
| 85 | return user.name |
|---|
| 86 | |
|---|
| 87 | def html(self, user): |
|---|
| 88 | return h.user.link(user) |
|---|
| 89 | |
|---|
| 90 | class GroupFormatter(ObjectFormatter): |
|---|
| 91 | |
|---|
| 92 | def unicode(self, group): |
|---|
| 93 | return _(group.group_name) |
|---|
| 94 | |
|---|
| 95 | def html(self, group): |
|---|
| 96 | return self.unicode(group) |
|---|
| 97 | |
|---|
| 98 | class VoteFormatter(ObjectFormatter): |
|---|
| 99 | |
|---|
| 100 | def unicode(self, vote): |
|---|
| 101 | return {1: _("is for"), |
|---|
| 102 | 0: _("abstains on"), |
|---|
| 103 | -1: _("is against")}[vote.orientation] |
|---|
| 104 | |
|---|
| 105 | def html(self, value): |
|---|
| 106 | return self.unicode(value) |
|---|
| 107 | |
|---|
| 108 | class CommentFormatter(ObjectFormatter): |
|---|
| 109 | |
|---|
| 110 | def unicode(self, comment): |
|---|
| 111 | return comment.latest.title |
|---|
| 112 | |
|---|
| 113 | def html(self, comment): |
|---|
| 114 | if comment.delete_time: |
|---|
| 115 | return self.unicode(comment) |
|---|
| 116 | return "<a href='%s'>%s</a>" % (h.entity_url(comment), |
|---|
| 117 | cgi.escape(self.unicode(comment))) |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | class FormattedEvent(object): |
|---|
| 121 | |
|---|
| 122 | FORMATTERS = {model.Vote: VoteFormatter(), |
|---|
| 123 | model.Group: GroupFormatter(), |
|---|
| 124 | model.User: UserFormatter(), |
|---|
| 125 | model.Instance: InstanceFormatter(), |
|---|
| 126 | model.Proposal: ProposalFormatter(), |
|---|
| 127 | model.Poll: PollFormatter(), |
|---|
| 128 | model.Page: PageFormatter(), |
|---|
| 129 | model.Comment: CommentFormatter()} |
|---|
| 130 | |
|---|
| 131 | def __init__(self, event, decoder): |
|---|
| 132 | self.event = event |
|---|
| 133 | self.decoder = decoder |
|---|
| 134 | |
|---|
| 135 | def __getitem__(self, item): |
|---|
| 136 | try: |
|---|
| 137 | value = self.event[item] |
|---|
| 138 | for cls in self.FORMATTERS.keys(): |
|---|
| 139 | if isinstance(value, cls): |
|---|
| 140 | return self.decoder(self.FORMATTERS[cls], value) |
|---|
| 141 | return value |
|---|
| 142 | except AttributeError, ae: |
|---|
| 143 | log.exception(ae) |
|---|
| 144 | return _("(Undefined)") |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | def as_unicode(event): |
|---|
| 148 | if not event.event: |
|---|
| 149 | return _("(Undefined)") |
|---|
| 150 | fe = FormattedEvent(event, lambda f, value: f.unicode(value)) |
|---|
| 151 | return event.event.event_msg() % fe |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | def as_html(event): |
|---|
| 155 | if not event.event: |
|---|
| 156 | return _("(Undefined)") |
|---|
| 157 | fe = FormattedEvent(event, lambda f, value: f.html(value)) |
|---|
| 158 | return event.event.event_msg() % fe |
|---|
| 159 | |
|---|