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

View File

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