Skip to content
Snippets Groups Projects
Commit e9572e1c authored by Dan Michael O. Heggø's avatar Dan Michael O. Heggø
Browse files

Update email method to use API

parent 083394be
No related branches found
No related tags found
No related merge requests found
...@@ -310,23 +310,39 @@ class Site(object): ...@@ -310,23 +310,39 @@ class Site(object):
# Actions # Actions
def email(self, user, text, subject, cc=False): def email(self, user, text, subject, cc=False):
"""Sends email to a specified user on the wiki.""" """
# TODO: Use api! Send email to a specified user on the wiki.
postdata = {}
postdata['wpSubject'] = subject >>> try:
postdata['wpText'] = text ... site.email('SomeUser', 'Some message', 'Some subject')
if cc: ... except mwclient.errors.NoSpecifiedEmailError, e:
postdata['wpCCMe'] = '1' ... print 'The user does not accept email, or has not specified an email address.'
postdata['wpEditToken'] = self.tokens['edit']
postdata['uselang'] = 'en' Args:
postdata['title'] = u'Special:Emailuser/' + user user (str): User name of the recipient
text (str): Body of the email
data = self.raw_index('submit', **postdata) subject (str): Subject of the email
if 'var wgAction = "success";' not in data: cc (bool): True to send a copy of the email to yourself (default is False)
if 'This user has not specified a valid e-mail address' in data:
# Dirty hack Returns:
raise errors.NoSpecifiedEmailError, user Dictionary of the JSON response
raise errors.EmailError, data
Raises:
NoSpecifiedEmailError (mwclient.errors.NoSpecifiedEmailError): if recipient does not accept email
EmailError (mwclient.errors.EmailError): on other errors
"""
token = self.get_token('email')
try:
info = self.api('emailuser', target=user, subject=subject,
text=text, ccme=cc, token=token)
except errors.APIError, e:
if e[0] == u'noemail':
raise errors.NoSpecifiedEmail(user, e[1])
raise errors.EmailError(*e)
return info
def login(self, username=None, password=None, cookies=None, domain=None): def login(self, username=None, password=None, cookies=None, domain=None):
"""Login to the wiki.""" """Login to the wiki."""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment