Mercurial > libervia-backend
comparison plugins/plugin_misc_cs.py @ 107:5ae370c71803
CS: message sending is now working
- xmltools/xmlui: buttons can now send fields of the ui when used
- xmltools/xmlui: new textbox element, to write a large text (used for messages in CS plugin)
- xmltools/xmlui: list have now an attribute multi for selecting several options
- xmltools/xmlui: window title can now be specified in the xml (attribute title)
- CS_plugin: message sending interface & management
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 28 Jun 2010 15:18:59 +0800 |
parents | 138d82f64b6f |
children | e24e080e6b16 |
comparison
equal
deleted
inserted
replaced
106:138d82f64b6f | 107:5ae370c71803 |
---|---|
72 #parameters | 72 #parameters |
73 host.memory.importParams(CS_Plugin.params) | 73 host.memory.importParams(CS_Plugin.params) |
74 #menu | 74 #menu |
75 host.importMenu(_("Plugin"), "CouchSurfing", self.menuSelected, help_string = _("Launch CoushSurfing mangement interface")) | 75 host.importMenu(_("Plugin"), "CouchSurfing", self.menuSelected, help_string = _("Launch CoushSurfing mangement interface")) |
76 self.data=self.host.memory.getPrivate('plugin_cs_data') or {} #TODO: delete cookies/data after a while | 76 self.data=self.host.memory.getPrivate('plugin_cs_data') or {} #TODO: delete cookies/data after a while |
77 self.host.registerGeneralCB("plugin_CS_sendMessage", self.sendMessage) | |
77 | 78 |
78 def erroCB(self, e, id): | 79 def erroCB(self, e, id): |
79 """Called when something is going wrong when contacting CS website""" | 80 """Called when something is going wrong when contacting CS website""" |
81 #pdb.set_trace() | |
80 message_data={"reason": "connection error", "message":_(u"Impossible to contact CS website, please check your login/password, connection or try again later")} | 82 message_data={"reason": "connection error", "message":_(u"Impossible to contact CS website, please check your login/password, connection or try again later")} |
81 self.host.bridge.actionResult("ERROR", id, message_data) | 83 self.host.bridge.actionResult("ERROR", id, message_data) |
82 | 84 |
83 def menuSelected(self, id, profile): | 85 def menuSelected(self, id, profile): |
84 """Called when the couchsurfing menu item is selected""" | 86 """Called when the couchsurfing menu item is selected""" |
99 | 101 |
100 | 102 |
101 | 103 |
102 | 104 |
103 #tmp | 105 #tmp |
104 f = open('/home/goffi/tmp/CS_principale.html','r') | 106 """f = open('/home/goffi/tmp/CS_principale.html','r') |
105 html = f.read() | 107 html = f.read() |
106 self.__connectionCB(html, id, profile) | 108 self.__connectionCB(html, id, profile)""" |
107 | 109 |
108 """d = getPage('http://www.couchsurfing.org/login.html', method='POST', postdata=post_data, headers={'Content-Type':'application/x-www-form-urlencoded'} , agent=AGENT, cookies=self.data[profile]['cookies']) | 110 d = getPage('http://www.couchsurfing.org/login.html', method='POST', postdata=post_data, headers={'Content-Type':'application/x-www-form-urlencoded'} , agent=AGENT, cookies=self.data[profile]['cookies']) |
109 d.addCallback(self.__connectionCB, id, profile) | 111 d.addCallback(self.__connectionCB, id, profile) |
110 d.addErrback(self.erroCB, id)""" | 112 d.addErrback(self.erroCB, id) |
111 | 113 |
112 | 114 |
113 #self.host.bridge.actionResult("SUPPRESS", id, {}) | 115 #self.host.bridge.actionResult("SUPPRESS", id, {}) |
114 | 116 |
115 | 117 |
157 user_name = data['user_name'] | 159 user_name = data['user_name'] |
158 unread_mess = data['unread_messages'] | 160 unread_mess = data['unread_messages'] |
159 unread_CR_mess = data['unread_CR_messages'] | 161 unread_CR_mess = data['unread_CR_messages'] |
160 friends_list = data['friends'].keys() | 162 friends_list = data['friends'].keys() |
161 friends_list.sort() | 163 friends_list.sort() |
162 interface = XMLUI('window','tabs') | 164 interface = XMLUI('window','tabs', title='CouchSurfing management') |
163 interface.addCategory(_("Messages"), "vertical") | 165 interface.addCategory(_("Messages"), "vertical") |
164 interface.addText(_("G'day %(name)s, you have %(nb_message)i unread message%(plural_mess)s and %(unread_CR_mess)s unread couch request message%(plural_CR)s") % {'name':user_name, 'nb_message':unread_mess, 'plural_mess':'s' if unread_mess>1 else '', 'unread_CR_mess': unread_CR_mess, 'plural_CR':'s' if unread_CR_mess>1 else ''}) | 166 interface.addText(_("G'day %(name)s, you have %(nb_message)i unread message%(plural_mess)s and %(unread_CR_mess)s unread couch request message%(plural_CR)s\nIf you want to send a message, select the recipient(s) in the list below") % {'name':user_name, 'nb_message':unread_mess, 'plural_mess':'s' if unread_mess>1 else '', 'unread_CR_mess': unread_CR_mess, 'plural_CR':'s' if unread_CR_mess>1 else ''}) |
165 interface.addList(friends_list, 'friends') | 167 interface.addList(friends_list, 'friends', style=['multi']) |
168 interface.changeLayout('pairs') | |
169 interface.addLabel(_("Subject")) | |
170 interface.addString('subject') | |
171 interface.changeLayout('vertical') | |
172 interface.addLabel(_("Message")) | |
173 interface.addText("(use %name% for contact name and %firstname% for guessed first name)") | |
174 interface.addTextBox('message') | |
175 interface.addButton('plugin_CS_sendMessage', 'sendMessage', _('send'), fields_back=['friends','subject','message']) | |
166 interface.addCategory(_("Events"), "vertical") | 176 interface.addCategory(_("Events"), "vertical") |
167 interface.addCategory(_("Couch search"), "vertical") | 177 interface.addCategory(_("Couch search"), "vertical") |
168 return interface.toXml() | 178 return interface.toXml() |
169 | 179 |
170 def __meetingPageCB(self, html): | 180 def __meetingPageCB(self, html): |
171 """Called when the meeting page has been received""" | 181 """Called when the meeting page has been received""" |
172 | 182 |
173 def __friendsPageCB(self, html, id, profile): | 183 def __friendsPageCB(self, html, id, profile): |
174 """Called when the friends list page has been received""" | 184 """Called when the friends list page has been received""" |
175 self.savePage('friends',html) | 185 self.savePage('friends',html) |
176 soup = BeautifulSoup(html.replace('"formtable width="400','"formtable" width="400"')) | 186 soup = BeautifulSoup(html.replace('"formtable width="400','"formtable" width="400"')) #CS html fix #TODO: report the bug to CS dev team |
177 friends = self.data[profile]['friends'] | 187 friends = self.data[profile]['friends'] |
178 for _tr in soup.findAll('tr', {'class':re.compile("^msgRow*")}): #we parse the row with friends infos | 188 for _tr in soup.findAll('tr', {'class':re.compile("^msgRow*")}): #we parse the row with friends infos |
179 _nobr = _tr.find('nobr') #contain the friend name | 189 _nobr = _tr.find('nobr') #contain the friend name |
180 friend_name = unicode(_nobr.string) | 190 friend_name = unicode(_nobr.string) |
181 friend_link = u'http://www.couchsurfing.org'+_nobr.parent['href'] | 191 friend_link = u'http://www.couchsurfing.org'+_nobr.parent['href'] |
194 else: | 204 else: |
195 #no, we show the result | 205 #no, we show the result |
196 self.host.bridge.actionResult("XMLUI", id, {"type":"window", "xml":self.__buildUI(self.data[profile])}) | 206 self.host.bridge.actionResult("XMLUI", id, {"type":"window", "xml":self.__buildUI(self.data[profile])}) |
197 #and save the data | 207 #and save the data |
198 self.host.memory.setPrivate('plugin_cs_data', self.data) | 208 self.host.memory.setPrivate('plugin_cs_data', self.data) |
199 | 209 |
200 | 210 def __sendMessage(self, answer, subject, message, data, recipient_list, id, profile): |
201 | 211 """Send actually the message |
202 | 212 @param subject: subject of the message |
203 | 213 @param message: body of the message |
214 @param data: data of the profile | |
215 @param recipient_list: list of friends names, names are removed once message is sent | |
216 @param id: id of the action | |
217 @param profile: profile who launched the action | |
218 """ | |
219 if answer: | |
220 if not 'Here is a copy of the email that was sent' in answer: | |
221 error(_("INTERNAL ERROR: no confirmation of message sent by CS, maybe the site has been modified ?")) | |
222 #TODO: throw a warning to the frontend, saying that maybe the message has not been sent and to contact dev of this plugin | |
223 #debug(_('HTML answer: %s') % answer) | |
224 if recipient_list: | |
225 recipient = recipient_list.pop() | |
226 try: | |
227 friend_id = data['friends'][recipient]['id'] | |
228 except KeyError: | |
229 error('INTERNAL ERROR: unknown friend') | |
230 return #send an error to the frontend | |
231 mess = message.replace('%name%',recipient).replace('%firstname%',recipient.split(' ')[0]) | |
232 info(_('Sending message to %s') % recipient) | |
233 debug(_("\nsubject: %(subject)s\nmessage: \n---\n%(message)s\n---\n\n") % {'subject':subject,'message':mess}) | |
234 post_data = urllib.urlencode({'email[subject]':subject.encode('utf-8'),'email[body]':mess.encode('utf-8'),'email[id]':friend_id,'email[action]':'Send Message','email[replied_id]':'','email[couchsurf]':'','email[directions_to_add]':''}) | |
235 d = getPage("http://www.couchsurfing.org/send_message.html", method='POST', postdata=post_data, headers={'Content-Type':'application/x-www-form-urlencoded'} , agent=AGENT, cookies=data['cookies']) | |
236 d.addCallback(self.__sendMessage, subject, message, data, recipient_list, id, profile) | |
237 d.addErrback(self.erroCB, id) | |
238 else: | |
239 interface = XMLUI('window', title=_('Message sent')) #TODO: create particular actionResult for alerts ? | |
240 interface.addText(_('The message has been sent to every recipients')) | |
241 self.host.bridge.actionResult("XMLUI", id, {"type":"window", "xml":interface.toXml()}) | |
242 | |
243 | |
244 | |
245 | |
246 | |
247 def sendMessage(self, id, data, profile): | |
248 """Called to send a message to a friend | |
249 @param data: dict with the following keys: | |
250 friend: name of the recipient | |
251 subject: subject of the message | |
252 message: body of the message, with the following special keywords: | |
253 - %name%: name of the friend | |
254 - %firstname%: guessed first name of the friend (currently the first part of the name) | |
255 """ | |
256 if not data['friends']: | |
257 message_data={"reason": "bad data", "message":_(u"There is not recipient selected for this message !")} | |
258 self.host.bridge.actionResult("ERROR", id, message_data) | |
259 return | |
260 friends = data['friends'].split('\t') | |
261 subject = data['subject'] | |
262 message = data['message'] | |
263 print "send message \o/ :) :) :)" | |
264 info(_("sending message to %(friends)s with subject [%(subject)s]" % {'friends':friends, 'subject':subject})) | |
265 self.__sendMessage(None, subject, message, self.data[profile], friends, id, profile) | |
266 |