fixing those two python files

This commit is contained in:
Alex Barcelo 2016-10-24 10:05:41 +02:00
parent 179ceb2f82
commit f017adea63
2 changed files with 47 additions and 46 deletions

View File

@ -15,7 +15,7 @@
"""
A python interface to the `Akismet <http://akismet.com>`_ API.
This is a web service for blocking SPAM comments to blogs - or other online
This is a web service for blocking SPAM comments to blogs - or other online
services.
You will need a Wordpress API key, from `wordpress.com <http://wordpress.com>`_.
@ -31,10 +31,10 @@ The default is : ::
Whatever you pass in, will replace the *Python Interface by Fuzzyman* part.
**0.2.0** will change with the version of this interface.
Usage example::
Usage example:
from akismet import Akismet
api = Akismet(agent='Test Script')
# if apikey.txt is in place,
# the key will automatically be set
@ -70,7 +70,7 @@ __all__ = (
'Akismet',
'AkismetError',
'APIKeyError',
)
)
__author__ = 'Michael Foord <fuzzyman AT voidspace DOT org DOT uk>'
@ -92,7 +92,7 @@ if urllib2 is None:
req = urlfetch.fetch(url=url, payload=data, method=urlfetch.POST, headers=headers)
if req.status_code == 200:
return req.content
raise Exception('Could not fetch Akismet URL: %s Response code: %s' %
raise Exception('Could not fetch Akismet URL: %s Response code: %s' %
(url, req.status_code))
else:
def _fetch_url(url, data, headers):
@ -127,7 +127,7 @@ class Akismet(object):
def _getURL(self):
"""
Fetch the url to make requests to.
This comprises of api key plus the baseurl.
"""
return 'http://%s.%s' % (self.key, self.baseurl)
@ -142,11 +142,11 @@ class Akismet(object):
def setAPIKey(self, key=None, blog_url=None):
"""
Set the wordpress API key for all transactions.
If you don't specify an explicit API ``key`` and ``blog_url`` it will
attempt to load them from a file called ``apikey.txt`` in the current
directory.
This method is *usually* called automatically when you create a new
``Akismet`` instance.
"""
@ -165,14 +165,14 @@ class Akismet(object):
def verify_key(self):
"""
This equates to the ``verify-key`` call against the akismet API.
It returns ``True`` if the key is valid.
The docs state that you *ought* to call this at the start of the
transaction.
It raises ``APIKeyError`` if you have not yet set an API key.
If the connection to akismet fails, it allows the normal ``HTTPError``
or ``URLError`` to be raised.
(*akismet.py* uses `urllib2 <http://docs.python.org/lib/module-urllib2.html>`_)
@ -195,21 +195,21 @@ class Akismet(object):
"""
This function builds the data structure required by ``comment_check``,
``submit_spam``, and ``submit_ham``.
It modifies the ``data`` dictionary you give it in place. (and so
doesn't return anything)
It raises an ``AkismetError`` if the user IP or user-agent can't be
worked out.
"""
data['comment_content'] = comment
if not 'user_ip' in data:
if 'user_ip' not in data:
try:
val = os.environ['REMOTE_ADDR']
except KeyError:
raise AkismetError("No 'user_ip' supplied")
data['user_ip'] = val
if not 'user_agent' in data:
if 'user_agent' not in data:
try:
val = os.environ['HTTP_USER_AGENT']
except KeyError:
@ -234,44 +234,44 @@ class Akismet(object):
def comment_check(self, comment, data=None, build_data=True, DEBUG=False):
"""
This is the function that checks comments.
It returns ``True`` for spam and ``False`` for ham.
If you set ``DEBUG=True`` then it will return the text of the response,
instead of the ``True`` or ``False`` object.
It raises ``APIKeyError`` if you have not yet set an API key.
If the connection to Akismet fails then the ``HTTPError`` or
``URLError`` will be propogated.
As a minimum it requires the body of the comment. This is the
``comment`` argument.
Akismet requires some other arguments, and allows some optional ones.
The more information you give it, the more likely it is to be able to
make an accurate diagnosise.
You supply these values using a mapping object (dictionary) as the
``data`` argument.
If ``build_data`` is ``True`` (the default), then *akismet.py* will
attempt to fill in as much information as possible, using default
values where necessary. This is particularly useful for programs
running in a {acro;CGI} environment. A lot of useful information
can be supplied from evironment variables (``os.environ``). See below.
You *only* need supply values for which you don't want defaults filled
in for. All values must be strings.
There are a few required values. If they are not supplied, and
defaults can't be worked out, then an ``AkismetError`` is raised.
If you set ``build_data=False`` and a required value is missing an
``AkismetError`` will also be raised.
The normal values (and defaults) are as follows : ::
'user_ip': os.environ['REMOTE_ADDR'] (*)
'user_agent': os.environ['HTTP_USER_AGENT'] (*)
'referrer': os.environ.get('HTTP_REFERER', 'unknown') [#]_
@ -287,16 +287,16 @@ class Akismet(object):
'SERVER_SIGNATURE': os.environ.get('SERVER_SIGNATURE', '')
'SERVER_SOFTWARE': os.environ.get('SERVER_SOFTWARE', '')
'HTTP_ACCEPT': os.environ.get('HTTP_ACCEPT', '')
(*) Required values
You may supply as many additional 'HTTP_*' type values as you wish.
These should correspond to the http headers sent with the request.
.. [#] Note the spelling "referrer". This is a required value by the
akismet api - however, referrer information is not always
supplied by the browser or server. In fact the HTTP protocol
forbids relying on referrer information for functionality in
forbids relying on referrer information for functionality in
programs.
.. [#] The `API docs <http://akismet.com/development/api/>`_ state that this value
can be " *blank, comment, trackback, pingback, or a made up value*
@ -330,7 +330,7 @@ class Akismet(object):
"""
This function is used to tell akismet that a comment it marked as ham,
is really spam.
It takes all the same arguments as ``comment_check``, except for
*DEBUG*.
"""
@ -350,7 +350,7 @@ class Akismet(object):
"""
This function is used to tell akismet that a comment it marked as spam,
is really ham.
It takes all the same arguments as ``comment_check``, except for
*DEBUG*.
"""

View File

@ -71,7 +71,7 @@ def api(request, method):
request.user = authenticate(
username=request.POST.get('user', False),
password=request.POST.get('password'),
)
)
if request.user is None:
return api_return(STATUS_ERROR_PERMISSIONS)
@ -109,6 +109,7 @@ def api_return(status, text='', json=False):
class API:
def __init__(self, request):
self.request = request
@ -195,7 +196,7 @@ class API:
comment=message,
user=self.request.user,
title='Comment Added',
)
)
if public:
f.public = True
@ -214,7 +215,7 @@ class API:
recipients=ticket.submitter_email,
sender=ticket.queue.from_address,
fail_silently=True,
)
)
messages_sent_to.append(ticket.submitter_email)
if public:
@ -226,7 +227,7 @@ class API:
recipients=cc.email_address,
sender=ticket.queue.from_address,
fail_silently=True,
)
)
messages_sent_to.append(cc.email_address)
if ticket.queue.updated_ticket_cc and ticket.queue.updated_ticket_cc not in messages_sent_to:
@ -236,7 +237,7 @@ class API:
recipients=ticket.queue.updated_ticket_cc,
sender=ticket.queue.from_address,
fail_silently=True,
)
)
messages_sent_to.append(ticket.queue.updated_ticket_cc)
if (
@ -276,7 +277,7 @@ class API:
user=self.request.user,
title='Resolved',
public=True,
)
)
f.save()
context = safe_template_context(ticket)
@ -293,7 +294,7 @@ class API:
recipients=ticket.submitter_email,
sender=ticket.queue.from_address,
fail_silently=True,
)
)
messages_sent_to.append(ticket.submitter_email)
for cc in ticket.ticketcc_set.all():
@ -304,7 +305,7 @@ class API:
recipients=cc.email_address,
sender=ticket.queue.from_address,
fail_silently=True,
)
)
messages_sent_to.append(cc.email_address)
if ticket.queue.updated_ticket_cc and ticket.queue.updated_ticket_cc not in messages_sent_to:
@ -314,7 +315,7 @@ class API:
recipients=ticket.queue.updated_ticket_cc,
sender=ticket.queue.from_address,
fail_silently=True,
)
)
messages_sent_to.append(ticket.queue.updated_ticket_cc)
if ticket.assigned_to and \
@ -329,7 +330,7 @@ class API:
recipients=ticket.assigned_to.email,
sender=ticket.queue.from_address,
fail_silently=True,
)
)
ticket.resoltuion = f.comment
ticket.status = Ticket.RESOLVED_STATUS