2012-05-01 12:58:37 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-12-06 14:56:32 +00:00
|
|
|
from openerp.osv import fields, osv
|
2012-05-01 12:58:37 +00:00
|
|
|
import random
|
2012-08-29 12:38:33 +00:00
|
|
|
import re
|
2012-05-01 12:58:37 +00:00
|
|
|
import string
|
2012-05-15 11:19:28 +00:00
|
|
|
import urllib2
|
2012-10-29 13:25:27 +00:00
|
|
|
import logging
|
2014-08-26 13:35:47 +00:00
|
|
|
from openerp import SUPERUSER_ID
|
2012-12-06 14:56:32 +00:00
|
|
|
from openerp.tools.translate import _
|
2012-11-06 12:34:42 +00:00
|
|
|
from openerp.tools import html2plaintext
|
2012-09-20 10:57:13 +00:00
|
|
|
from py_etherpad import EtherpadLiteClient
|
2012-05-01 12:58:37 +00:00
|
|
|
|
2012-10-29 13:19:38 +00:00
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
2012-05-01 12:58:37 +00:00
|
|
|
class pad_common(osv.osv_memory):
|
|
|
|
_name = 'pad.common'
|
2012-08-29 09:36:06 +00:00
|
|
|
|
2014-03-14 13:18:49 +00:00
|
|
|
def pad_is_configured(self, cr, uid, context=None):
|
|
|
|
user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
|
|
|
|
return bool(user.company_id.pad_server)
|
|
|
|
|
2012-07-21 19:25:51 +00:00
|
|
|
def pad_generate_url(self, cr, uid, context=None):
|
2014-08-26 13:35:47 +00:00
|
|
|
company = self.pool.get('res.users').browse(cr, SUPERUSER_ID, uid, context=context).company_id
|
2012-09-20 10:57:13 +00:00
|
|
|
|
|
|
|
pad = {
|
|
|
|
"server" : company.pad_server,
|
2012-11-09 06:30:02 +00:00
|
|
|
"key" : company.pad_key,
|
2012-09-20 10:57:13 +00:00
|
|
|
}
|
|
|
|
|
2012-08-29 12:38:33 +00:00
|
|
|
# make sure pad server in the form of http://hostname
|
2012-09-20 10:57:13 +00:00
|
|
|
if not pad["server"]:
|
2013-01-17 17:40:27 +00:00
|
|
|
return pad
|
2012-09-20 10:57:13 +00:00
|
|
|
if not pad["server"].startswith('http'):
|
|
|
|
pad["server"] = 'http://' + pad["server"]
|
|
|
|
pad["server"] = pad["server"].rstrip('/')
|
2012-08-29 12:38:33 +00:00
|
|
|
# generate a salt
|
2012-05-11 04:58:56 +00:00
|
|
|
s = string.ascii_uppercase + string.digits
|
2015-07-27 16:27:21 +00:00
|
|
|
salt = ''.join([s[random.SystemRandom().randint(0, len(s) - 1)] for i in range(10)])
|
2012-09-20 10:57:13 +00:00
|
|
|
#path
|
2014-12-08 13:46:11 +00:00
|
|
|
# etherpad hardcodes pad id length limit to 50
|
|
|
|
path = '-%s-%s' % (self._name, salt)
|
|
|
|
path = '%s%s' % (cr.dbname.replace('_','-')[0:50 - len(path)], path)
|
2012-08-29 12:38:33 +00:00
|
|
|
# contruct the url
|
2012-09-20 10:57:13 +00:00
|
|
|
url = '%s/p/%s' % (pad["server"], path)
|
|
|
|
|
|
|
|
#if create with content
|
|
|
|
if "field_name" in context and "model" in context and "object_id" in context:
|
|
|
|
myPad = EtherpadLiteClient( pad["key"], pad["server"]+'/api')
|
2013-05-29 06:50:12 +00:00
|
|
|
try:
|
|
|
|
myPad.createPad(path)
|
|
|
|
except urllib2.URLError:
|
2013-11-20 14:43:18 +00:00
|
|
|
raise osv.except_osv(_("Error"), _("Pad creation failed, \
|
2013-05-29 06:50:12 +00:00
|
|
|
either there is a problem with your pad server URL or with your connection."))
|
2012-09-20 10:57:13 +00:00
|
|
|
|
|
|
|
#get attr on the field model
|
2013-03-29 14:37:20 +00:00
|
|
|
model = self.pool[context["model"]]
|
[IMP] use model._fields instead of model._all_columns to cover all fields
The old-api model._all_columns contains information about model._columns and
inherited columns. This dictionary is missing new-api computed non-stored
fields, and the new field objects provide a more readable api...
This commit contains the following changes:
- adapt several methods of BaseModel to use fields instead of columns and
_all_columns
- copy all semantic-free attributes of related fields from their source
- add attribute 'group_operator' on integer and float fields
- base, base_action_rule, crm, edi, hr, mail, mass_mailing, pad,
payment_acquirer, share, website, website_crm, website_mail: simply use
_fields instead of _all_columns
- base, decimal_precision, website: adapt qweb rendering methods to use fields
instead of columns
2014-11-03 15:00:50 +00:00
|
|
|
field = model._fields[context['field_name']]
|
|
|
|
real_field = field.pad_content_field
|
2012-09-20 10:57:13 +00:00
|
|
|
|
|
|
|
#get content of the real field
|
|
|
|
for record in model.browse(cr, uid, [context["object_id"]]):
|
|
|
|
if record[real_field]:
|
2014-08-06 12:16:46 +00:00
|
|
|
myPad.setText(path, (html2plaintext(record[real_field]).encode('utf-8')))
|
2012-09-20 12:06:16 +00:00
|
|
|
#Etherpad for html not functional
|
|
|
|
#myPad.setHTML(path, record[real_field])
|
2012-09-20 07:50:31 +00:00
|
|
|
|
|
|
|
return {
|
2012-09-20 10:57:13 +00:00
|
|
|
"server": pad["server"],
|
|
|
|
"path": path,
|
2012-09-20 07:50:31 +00:00
|
|
|
"url": url,
|
|
|
|
}
|
2012-05-11 04:58:56 +00:00
|
|
|
|
2012-08-29 12:38:33 +00:00
|
|
|
def pad_get_content(self, cr, uid, url, context=None):
|
|
|
|
content = ''
|
|
|
|
if url:
|
2012-10-29 13:19:38 +00:00
|
|
|
try:
|
|
|
|
page = urllib2.urlopen('%s/export/html'%url).read()
|
|
|
|
mo = re.search('<body>(.*)</body>',page)
|
|
|
|
if mo:
|
|
|
|
content = mo.group(1)
|
|
|
|
except:
|
|
|
|
_logger.warning("No url found '%s'.", url)
|
2012-08-29 12:38:33 +00:00
|
|
|
return content
|
|
|
|
|
2012-08-29 09:36:06 +00:00
|
|
|
# TODO
|
|
|
|
# reverse engineer protocol to be setHtml without using the api key
|
2012-08-29 12:38:33 +00:00
|
|
|
|
|
|
|
def write(self, cr, uid, ids, vals, context=None):
|
2012-09-19 14:35:53 +00:00
|
|
|
self._set_pad_value(cr, uid, vals, context)
|
|
|
|
return super(pad_common, self).write(cr, uid, ids, vals, context=context)
|
|
|
|
|
|
|
|
def create(self, cr, uid, vals, context=None):
|
|
|
|
self._set_pad_value(cr, uid, vals, context)
|
|
|
|
return super(pad_common, self).create(cr, uid, vals, context=context)
|
|
|
|
|
|
|
|
# Set the pad content in vals
|
|
|
|
def _set_pad_value(self, cr, uid, vals, context=None):
|
2012-08-29 12:38:33 +00:00
|
|
|
for k,v in vals.items():
|
[IMP] use model._fields instead of model._all_columns to cover all fields
The old-api model._all_columns contains information about model._columns and
inherited columns. This dictionary is missing new-api computed non-stored
fields, and the new field objects provide a more readable api...
This commit contains the following changes:
- adapt several methods of BaseModel to use fields instead of columns and
_all_columns
- copy all semantic-free attributes of related fields from their source
- add attribute 'group_operator' on integer and float fields
- base, base_action_rule, crm, edi, hr, mail, mass_mailing, pad,
payment_acquirer, share, website, website_crm, website_mail: simply use
_fields instead of _all_columns
- base, decimal_precision, website: adapt qweb rendering methods to use fields
instead of columns
2014-11-03 15:00:50 +00:00
|
|
|
field = self._fields[k]
|
2012-08-29 12:38:33 +00:00
|
|
|
if hasattr(field,'pad_content_field'):
|
2012-09-19 14:35:53 +00:00
|
|
|
vals[field.pad_content_field] = self.pad_get_content(cr, uid, v, context=context)
|
2012-08-29 09:36:06 +00:00
|
|
|
|
2012-05-01 12:58:37 +00:00
|
|
|
def copy(self, cr, uid, id, default=None, context=None):
|
|
|
|
if not default:
|
|
|
|
default = {}
|
[IMP] use model._fields instead of model._all_columns to cover all fields
The old-api model._all_columns contains information about model._columns and
inherited columns. This dictionary is missing new-api computed non-stored
fields, and the new field objects provide a more readable api...
This commit contains the following changes:
- adapt several methods of BaseModel to use fields instead of columns and
_all_columns
- copy all semantic-free attributes of related fields from their source
- add attribute 'group_operator' on integer and float fields
- base, base_action_rule, crm, edi, hr, mail, mass_mailing, pad,
payment_acquirer, share, website, website_crm, website_mail: simply use
_fields instead of _all_columns
- base, decimal_precision, website: adapt qweb rendering methods to use fields
instead of columns
2014-11-03 15:00:50 +00:00
|
|
|
for k, field in self._fields.iteritems():
|
2012-08-29 12:38:33 +00:00
|
|
|
if hasattr(field,'pad_content_field'):
|
2012-09-20 07:50:31 +00:00
|
|
|
pad = self.pad_generate_url(cr, uid, context)
|
2013-01-17 17:40:27 +00:00
|
|
|
default[k] = pad.get('url')
|
2012-05-01 12:58:37 +00:00
|
|
|
return super(pad_common, self).copy(cr, uid, id, default, context)
|
2012-07-17 22:14:08 +00:00
|
|
|
|
2012-05-01 12:58:37 +00:00
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|