Re: Comments merging, pass 2
Steven Armstrong <[email protected]>
| Newsgroups | gmane.comp.web.pyblosxom.devel |
|---|---|
| Message-ID | <[email protected]> |
Another patch for comments.py
It adds the following:
1. property: comment_fixlink
if True: fixes urls that are missing the protocol
e.g. www.example.com becomes http://www.example.com
if False: drops urls without protocols
e.g. www.example.com becomes ''
if not set: does nothing
2. property: comment_trigger
if set, only shows comments if there is eather a form field
or a querystring variable that matches the trigger.
e.g. comment_trigger = "cmt"
-> ?cmt=1 or <input type="hidden" name="cmt" value="1" />
3. property: comment_nohtmlmail
if set to True sends mail in plain text instead of html format
4. creates a template variable $cmt_author_link which holds:
the author wrapped with <a href ...> if a url is given.
otherwise just the author as text.
5. fixed a bug in write_comment:
> # don't log message here. it may not exist which raises another,
unhandled, exception.
< tools.log("Error sending mail: %s" % message)
> tools.log("Error sending mail for comment: %s" % cfn)
6. few small changes to build_preview_comment:
it still behaves the same as before. just hacked it to work with
comment_fixlink and $cmt_author_link. (hope that's ok Bill)
As Will has introduced the new cb_comment_reject callback, I have moved
all the "nospam image thingy" stuff to such a callback in the nospam
plugin. I'm still testing a few things and cleaning up on this. The
changed plugin will be on my site by tommorow.
cheers
Steven
comments.py.diff
(text/plain, 5.3 KB)
11a12
> Steven Armstrong
35a37
>
39a42,55
> comment_fixlink - True or False:
> if True: fixes urls that are missing the protocol
> e.g. www.example.com becomes http://www.example.com
> if False: drops urls without protocols
> e.g. www.example.com becomes ''
> if not set: does nothing
>
> comment_trigger - if set, only shows comments if there is eather a form field
> or a querystring variable that matches the trigger.
> e.g. comment_trigger = "cmt"
> -> ?cmt=1 or <input type="hidden" name="cmt" value="1" />
>
> comment_nohtmlmail - True: if set sends mail in plain text instead of html format
>
99c115,116
< optional_keys = ['comment_dir', 'comment_ext', 'comment_draft_ext']
---
> optional_keys = ['comment_dir', 'comment_ext', 'comment_draft_ext', \
> 'comment_fixlink', 'comment_trigger', 'comment_nohtmlmail']
228a246,250
> # added new variable cmt_author_link.
> if cmt['cmt_link'] != '':
> cmt['cmt_author_link'] = '<a href="%s">%s</a>' % (cmt['cmt_link'], cmt['cmt_author'])
> else:
> cmt['cmt_author_link'] = cmt['cmt_author']
343,344c365,371
< html = """%s<br />\n%s<br />\n<a href="%s">%s</a>\n""" % (description, cfn, curl, curl)
< message = createhtmlmail(html, headers)
---
> if request.getConfiguration().has_key('comment_nohtmlmail'):
> headers.append("")
> headers.append("%s\n%s\n%s\n" % (description, cfn, curl))
> message = "\n".join(headers)
> else:
> html = """%s<br />\n%s<br />\n<a href="%s">%s</a>\n""" % (description, cfn, curl, curl)
> message = createhtmlmail(html, headers)
350c377,379
< tools.log("Error sending mail: %s" % message)
---
> # don't log message here. it may not exist which raises another, unhandled, exception.
> #tools.log("Error sending mail: %s" % message)
> tools.log("Error sending mail for comment: %s" % cfn)
465,466c494,521
<
<
---
>
>
>
> def _fixlink(config, form, raiseError=False):
> """
> If the config property comment_fixlink is set to True:
> makes sure that the url is absolute (includes the protocol).
> If it's set to False, drops url's without protocol.
> Does nothing otherwise.
>
> @param config: pyblosxom config dict
> @type config: C{dict}
> @param form: dict containig the GET/POST form fields
> @type form: C{dict}
> @return: the fixed url
> @rtype boolean: C{str}
> """
> url = (form.has_key('url') and [form['url'].value] or [''])[0]
> if url != '' and config.has_key('comment_fixlink'):
> if not "://" in url:
> url = (config['comment_fixlink'] and ["http://%s" % url] or [''])[0]
> else:
> # raise KeyError to keep Bill's build_preview_comment happy
> if raiseError:
> raise KeyError("url")
> return url
>
>
478c533
<
---
>
486,488c541,543
< # Check if the form has a URL
< url = (form.has_key('url') and [form['url'].value] or [''])[0]
<
---
> # Check if the form has a URL and if it's absolute
> url = _fixlink(config, form)
>
497c552
<
---
>
522a578
> form = request.getHttp()['form']
523a580
>
526c583,585
< and not entry.has_key("nocomments"):
---
> and not entry.has_key("nocomments") \
> and (config.has_key('comment_trigger') and \
> [form.has_key(config.get('comment_trigger'))] or [True])[0]:
534c593
< def build_preview_comment(form, entry):
---
> def build_preview_comment(request, entry):
536a596,597
> form = request.getHttp()['form']
> config = request.getConfiguration()
542,543c603,607
< c['cmt_link'] = form['url'].value
< c['cmt_item'] = sanitize(form['body'].value)
---
> # set raiseError=False to make cmt_link optional
> c['cmt_link'] = _fixlink(config, form, raiseError=True)
> # sanitize only once
> description = sanitize(form['body'].value)
> c['cmt_item'] = description
545c609
< c['cmt_description'] = sanitize(form['body'].value)
---
> c['cmt_description'] = description
547a612
>
549a615,623
>
> # added new variable cmt_author_link.
> if c.has_key('cmt_author') and c.has_key('cmt_link'):
> if c['cmt_link'] != "":
> entry['cmt_url'] = c['cmt_link'] # keep Steven's template happy
> c['cmt_author_link'] = '<a href="%s">%s</a>' % (c['cmt_link'], c['cmt_author'])
> else:
> c['cmt_author_link'] = c['cmt_author']
>
550a625,628
> # store a non-sanitized version of the body to put in the textarea
> if form.has_key('body'):
> entry["cmt_body"] = form['body'].value
>
552a631
>
559a639
>
561a642,643
> and (config.has_key('comment_trigger') and \
> [form.has_key(config.get('comment_trigger'))] or [True])[0] \
568c650
< if form.has_key('preview')\
---
> if form.has_key('preview') \
570c652
< com = build_preview_comment(form, entry)
---
> com = build_preview_comment(request, entry)
576a659,660
>
>