Re: Comments merging, pass 2

Steven Armstrong <[email protected]>
Newsgroups gmane.comp.web.pyblosxom.devel
Message-ID <[email protected]>
will guaraldi wrote:
> Remember, other people have to maintain this code.
> 

You've got a point there.

Here's the rewritten patch. It is impossible to do an if/else where I 
need the check to happen, so I moved it to a function.

I also changed the ".has_key(...)" to "in" (__contains__) as Bob 
proposed. Didn't know that that is the same.

I see I have yet a few things to learn about Python.

cheers
Steven
comments.py.diff (text/plain, 6 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,548
<         
<         
---
> 
> 
> 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: C{str}
>     """
>     if 'url' in form:
>         url = form['url'].value
>     else:
>         url = ""
> 
>     if url and 'comment_fixlink' in config:
>         if not '://' in url:
>             if config['comment_fixlink']:
>                 url = 'http://%s' % url
>             else:
>                 url = ''
>     else:
>         # raise KeyError to keep Bill's build_preview_comment happy
>         if raiseError:
>             raise KeyError('url')
> 
>     return url
> 
> 
> def _check_comment_trigger(config, form):
>     """
>     Checks if the config property comment_trigger is used.
>     If so checks if the form contains a field matching it.
>     Returns True or False depending on the match.
>     Allwais returns True if the property is not set.
> 
>     @param config: pyblosxom config dict
>     @type config: C{dict}
>     @param form: dict containig the GET/POST form fields
>     @type form: C{dict}
>     @return: True or False
>     @rtype: C{bool}
>     """
>     if 'comment_trigger' in config:
>         if not config['comment_trigger'] in form:
>             return False
>     return True
>     
> 
478c560
<     
---
> 
486,488c568,570
<         # 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)
> 
497c579
< 
---
>         
522a605
>     form = request.getHttp()['form']
523a607
> 
526c610,611
<             and not entry.has_key("nocomments"):
---
>             and not entry.has_key("nocomments") \
>             and _check_comment_trigger(config, form):
534c619
< def build_preview_comment(form, entry):
---
> def build_preview_comment(request, entry):
536a622,623
>     form = request.getHttp()['form']
>     config = request.getConfiguration()
542,543c629,633
<         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
545c635
<         c['cmt_description'] = sanitize(form['body'].value)
---
>         c['cmt_description'] = description
548c638,639
<     if form.has_key('email'):
---
> 
>     if 'email' in form:
549a641,649
> 
>     # added new variable cmt_author_link.
>     if 'cmt_author' in c and 'cmt_link' in c:
>         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']
> 
550a651,654
>     # store a non-sanitized version of the body to put in the textarea
>     if 'body' in form:
>         entry["cmt_body"] = form['body'].value
> 
552a657
> 
559a665
> 
562c668,670
<             and not entry.has_key("nocomments"):
---
>             and not entry.has_key("nocomments") \
>             and _check_comment_trigger(config, form):
> 
568c676
<             if form.has_key('preview')\
---
>             if form.has_key('preview') \
570c678
<                 com = build_preview_comment(form, entry)
---
>                 com = build_preview_comment(request, entry)
576a685,686
> 
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.