Mathias Markl 7 years ago
parent
commit
fd59a0b7cf
  1. 25
      muk_fields_lobject/fields.py
  2. 89
      muk_fields_lobject/static/description/index.html
  3. BIN
      muk_fields_lobject/static/description/logo.png

25
muk_fields_lobject/fields.py

@ -23,6 +23,7 @@ import re
import hashlib
import logging
import psycopg2
import tempfile
from odoo import _
from odoo import models, api, fields
@ -46,7 +47,11 @@ class LargeObject(fields.Field):
if isinstance(value, bytes):
lobject.write(value)
else:
lobject.write(value.read())
while True:
chunk = value.read(4096)
if not chunk:
break
lobject.write(chunk)
return lobject.oid
def convert_to_record(self, value, record):
@ -56,6 +61,22 @@ class LargeObject(fields.Field):
return human_size(lobject.seek(0, 2))
elif record._context.get('oid'):
return lobject.oid
elif record._context.get('stream'):
file = tempfile.TemporaryFile()
while True:
chunk = lobject.read(4096)
if not chunk:
file.seek(0)
return file
file.write(chunk)
else:
return lobject.read()
return False
return False
def convert_to_export(self, value, record):
if value:
lobject = record.env.cr._cnx.lobject(value, 'rb')
if record._context.get('export_raw_data'):
return lobject.read()
return base64.b64encode(lobject.read())
return ''

89
muk_fields_lobject/static/description/index.html

@ -0,0 +1,89 @@
<section class="oe_container">
<div class="oe_row oe_spaced">
<h2 class="oe_slogan">Web Client</h2>
<h3 class="oe_slogan">Web Client Extension</h3>
<h4 class="oe_slogan" style="font-size: 23px;">MuK IT GmbH -
www.mukit.at</h4>
</div>
</section>
<section class="oe_container">
<div class="oe_row oe_spaced">
<div class="oe_picture">
<h3 class="oe_slogan">Overview</h3>
<div class="oe_mt32"
style="padding-bottom: 10px; text-align: justify;">
<p>Provides a field to store bytes as PostgreSQL large objects.
PostgreSQL offers support for large objects, which provide
stream-style access to user data that is stored in a special
large-object structure. They are useful with data values too large
to be manipulated conveniently as a whole.</p>
<h5 style="padding-top: 10px;">Psycopg2 for Python</h5>
<p>
Psycopg allows access to the large object using the
<code>lobject</code>
class. Objects are generated using the
<code>connection.lobject()</code>
factory method. Data can be retrieved either as bytes or as Unicode
strings.
</p>
<p>
Psycopg large object support efficient import/export with file
system files using the
<code>lo_import()</code>
and
<code>lo_export()</code>
libpq functions.
</p>
<p>Changed in version 2.6: added support for large objects
greated than 2GB. Note that the support is enabled only if all the
following conditions are verified:</p>
<ul>
<li>the Python build is 64 bits;</li>
<li>the extension was built against at least libpq 9.3;</li>
<li>the server version is at least PostgreSQL 9.3
(server_version must be >= 90300).</li>
</ul>
<p>
If Psycopg was built with 64 bits large objects support (i.e. the
first two contidions above are verified), the
<code>psycopg2.__version__</code>
constant will contain the lo64 flag. If any of the contition is not
met several lobject methods will fail if the arguments exceed 2GB.
</p>
</div>
<pre>
<code>
from odoo.addons.muk_fields_lobject import fields as lobject_fields
class LargeObjectModel(models.Model):
data_content = lobject_fields.LargeObject(string="Data")
@api.multi
def data(self):
for record in self:
bytes = record.data_content
oid = record.with_context({'oid': True}).data_content
size = record.with_context({'bin_size': True}).data_content
stream = record.with_context({'stream': True}).data_content
</code>
</pre>
</div>
</div>
</section>
<section class="oe_container oe_dark" style="padding-top: 300px;">
<h3 class="oe_slogan">Help and Support</h3>
<div class="oe_slogan">
<a class="btn btn-primary btn-lg mt8" href="mailto:sale@mukit.at">
<i class="fa fa-envelope"></i> Email
</a> <a class="btn btn-primary btn-lg mt8"
href="https://mukit.at/page/contactus"> <i class="fa fa-phone"></i>
Contact
</a>
</div>
<img src="logo.png" style="width: 200px; margin-bottom: 20px;"
class="center-block">
</section>

BIN
muk_fields_lobject/static/description/logo.png

After

Width: 500  |  Height: 500  |  Size: 37 KiB

Loading…
Cancel
Save