From a5762d1937adb7375d79512205af03524618ca66 Mon Sep 17 00:00:00 2001 From: Leonardo Rossi Date: Tue, 21 Apr 2015 09:38:09 +0000 Subject: [PATCH] global: PostgreSQL Text type compatibility fix * Changes Text in models because from SQLAlchemy>=1.0 it's arised a exception if the length is specified. (addresses #3037) * PEP8/257 code style improvements. Signed-off-by: Leonardo Rossi --- invenio/modules/collections/models.py | 29 ++++++++++++++++++++------- invenio/modules/knowledge/models.py | 3 ++- invenio/modules/records/models.py | 18 ++++++++++------- invenio/modules/search/models.py | 4 +++- invenio/modules/submit/models.py | 24 ++++++++++++---------- 5 files changed, 51 insertions(+), 27 deletions(-) diff --git a/invenio/modules/collections/models.py b/invenio/modules/collections/models.py index 905b872dbd..d1f72833a5 100644 --- a/invenio/modules/collections/models.py +++ b/invenio/modules/collections/models.py @@ -59,10 +59,13 @@ def __repr__(self): 'nbrecs: {0.nbrecs}>'.format(self) def __unicode__(self): + """Unicode.""" suffix = ' ({0})'.format(_('default')) if self.id == 1 else '' return u"{0.id}. {0.name}{1}".format(self, suffix) def __str__(self): + """Str.""" + # TODO it's compatible with python 3? return unicode(self).encode('utf-8') __tablename__ = 'collection' @@ -70,8 +73,8 @@ def __str__(self): primary_key=True) name = db.Column(db.String(255), unique=True, index=True, nullable=False) - dbquery = db.Column(db.Text(20), nullable=True, - index=True) + dbquery = db.Column(db.Text().with_variant(db.Text(20), 'mysql'), + nullable=True, index=True) @property def nbrecs(self): @@ -136,6 +139,7 @@ def examples(self): @property def name_ln(self): + """Name ln.""" from invenio.legacy.search_engine import get_coll_i18nname return get_coll_i18nname(self.name, getattr(g, 'ln', cfg['CFG_SITE_LANG'])) @@ -151,6 +155,7 @@ def name_ln(self): @property # @cache.memoize(make_name=lambda fname: fname + '::' + g.ln) def portalboxes_ln(self): + """Get Portalboxes ln.""" return db.object_session(self).query(CollectionPortalbox).\ with_parent(self).\ options(db.joinedload_all(CollectionPortalbox.portalbox)).\ @@ -159,6 +164,7 @@ def portalboxes_ln(self): @property def most_specific_dad(self): + """Most specific dad.""" results = sorted( db.object_session(self).query(Collection).join( Collection.sons @@ -262,9 +268,7 @@ def _make_field_fieldvalue(type_): @property # @cache.memoize(make_name=lambda fname: fname + '::' + g.ln) def search_within(self): - """ - Collect search within options. - """ + """Collect search within options.""" default = [('', _('any field'))] found = [(o.field.code, o.field.name_ln) for o in self._search_within] if not found: @@ -440,10 +444,12 @@ class Collectionname(db.Model): @db.hybrid_property def ln_type(self): + """Get ln type.""" return (self.ln, self.type) @ln_type.setter def set_ln_type(self, value): + """Set ln type.""" (self.ln, self.type) = value @@ -480,6 +486,7 @@ def set_ln_type(self, value): class Collectiondetailedrecordpagetabs(db.Model): """Represent a Collectiondetailedrecordpagetabs record.""" + __tablename__ = 'collectiondetailedrecordpagetabs' id_collection = db.Column(db.MediumInteger(9, unsigned=True), db.ForeignKey(Collection.id), @@ -493,6 +500,7 @@ class Collectiondetailedrecordpagetabs(db.Model): class CollectionCollection(db.Model): """Represent a CollectionCollection record.""" + __tablename__ = 'collection_collection' id_dad = db.Column(db.MediumInteger(9, unsigned=True), db.ForeignKey(Collection.id), primary_key=True) @@ -514,6 +522,7 @@ class CollectionCollection(db.Model): class Example(db.Model): """Represent a Example record.""" + __tablename__ = 'example' id = db.Column(db.MediumInteger(9, unsigned=True), primary_key=True, autoincrement=True) @@ -524,6 +533,7 @@ class Example(db.Model): class CollectionExample(db.Model): """Represent a CollectionExample record.""" + __tablename__ = 'collection_example' id_collection = db.Column(db.MediumInteger(9, unsigned=True), db.ForeignKey(Collection.id), primary_key=True) @@ -539,6 +549,7 @@ class CollectionExample(db.Model): class Portalbox(db.Model): """Represent a Portalbox record.""" + __tablename__ = 'portalbox' id = db.Column(db.MediumInteger(9, unsigned=True), autoincrement=True, primary_key=True) @@ -547,8 +558,7 @@ class Portalbox(db.Model): def get_pbx_pos(): - """Returns a list of all the positions for a portalbox""" - + """Return a list of all the positions for a portalbox.""" position = {} position["rt"] = "Right Top" position["lt"] = "Left Top" @@ -562,6 +572,7 @@ def get_pbx_pos(): class CollectionPortalbox(db.Model): """Represent a CollectionPortalbox record.""" + __tablename__ = 'collection_portalbox' id_collection = db.Column(db.MediumInteger(9, unsigned=True), db.ForeignKey(Collection.id), primary_key=True) @@ -583,6 +594,7 @@ class CollectionPortalbox(db.Model): class Externalcollection(db.Model): """Represent a Externalcollection record.""" + __tablename__ = 'externalcollection' id = db.Column(db.MediumInteger(9, unsigned=True), primary_key=True) @@ -591,6 +603,7 @@ class Externalcollection(db.Model): @property def engine(self): + """Engine.""" from invenio.legacy.websearch_external_collections.searcher import ( external_collections_dictionary ) @@ -601,6 +614,7 @@ def engine(self): class CollectionExternalcollection(db.Model): """Represent a CollectionExternalcollection record.""" + __tablename__ = 'collection_externalcollection' id_collection = db.Column(db.MediumInteger(9, unsigned=True), @@ -634,6 +648,7 @@ def _collection_type(type_): class CollectionFormat(db.Model): """Represent a CollectionFormat record.""" + __tablename__ = 'collection_format' id_collection = db.Column(db.MediumInteger(9, unsigned=True), db.ForeignKey(Collection.id), primary_key=True) diff --git a/invenio/modules/knowledge/models.py b/invenio/modules/knowledge/models.py index 85dffa6ffb..f841c9a8c3 100644 --- a/invenio/modules/knowledge/models.py +++ b/invenio/modules/knowledge/models.py @@ -281,7 +281,8 @@ class KnwKBRVAL(db.Model): __tablename__ = 'knwKBRVAL' m_key = db.Column(db.String(255), nullable=False, primary_key=True, index=True) - m_value = db.Column(db.Text(30), nullable=False, index=True) + m_value = db.Column(db.Text().with_variant(db.Text(30), 'mysql'), + nullable=False, index=True) id_knwKB = db.Column(db.MediumInteger(8), db.ForeignKey(KnwKB.id), nullable=False, server_default='0', primary_key=True) diff --git a/invenio/modules/records/models.py b/invenio/modules/records/models.py index 4e385cf450..be033b2e9d 100644 --- a/invenio/modules/records/models.py +++ b/invenio/modules/records/models.py @@ -20,12 +20,15 @@ """Record models.""" from flask import current_app + from intbitset import intbitset -from sqlalchemy.ext.declarative import declared_attr -from werkzeug import cached_property from invenio.ext.sqlalchemy import db, utils +from sqlalchemy.ext.declarative import declared_attr + +from werkzeug import cached_property + class Record(db.Model): @@ -59,8 +62,8 @@ def deleted(self): dbcollids = get_fieldvalues(self.id, "980__%") return ("DELETED" in dbcollids) or \ - (current_app.config.get('CFG_CERN_SITE') - and "DUMMY" in dbcollids) + (current_app.config.get('CFG_CERN_SITE') and + "DUMMY" in dbcollids) @staticmethod def _next_merged_recid(recid): @@ -109,7 +112,8 @@ def is_restricted(self): @cached_property def is_processed(self): """Return True is recods is processed (not in any collection).""" - from invenio.modules.collections.cache import is_record_in_any_collection + from invenio.modules.collections.cache import \ + is_record_in_any_collection return not is_record_in_any_collection(self.id, recreate_cache_if_needed=False) @@ -163,8 +167,8 @@ class BibxxxMixin(utils.TableNameMixin): autoincrement=True) tag = db.Column(db.String(6), nullable=False, index=True, server_default='') - value = db.Column(db.Text(35), nullable=False, - index=True) + value = db.Column(db.Text().with_variant(db.Text(35), 'mysql'), + nullable=False, index=True) class BibrecBibxxxMixin(utils.TableFromCamelNameMixin): diff --git a/invenio/modules/search/models.py b/invenio/modules/search/models.py index 0fd97b8385..07cbb32c93 100644 --- a/invenio/modules/search/models.py +++ b/invenio/modules/search/models.py @@ -168,7 +168,8 @@ class WebQuery(db.Model): id = db.Column(db.Integer(15, unsigned=True), primary_key=True, autoincrement=True) type = db.Column(db.Char(1), nullable=False, server_default='r') - urlargs = db.Column(db.Text(100), nullable=False, index=True) + urlargs = db.Column(db.Text().with_variant(db.Text(100), 'mysql'), + nullable=False, index=True) class UserQuery(db.Model): @@ -191,6 +192,7 @@ class UserQuery(db.Model): @classmethod def log(cls, urlargs=None, id_user=None): + """Log.""" id_user = id_user if not None else current_user.get_id() urlargs = urlargs or request.query_string if id_user < 0: diff --git a/invenio/modules/submit/models.py b/invenio/modules/submit/models.py index 0092bc08a4..045b7ea62f 100644 --- a/invenio/modules/submit/models.py +++ b/invenio/modules/submit/models.py @@ -135,13 +135,15 @@ class SbmCOLLECTIONSbmCOLLECTION(db.Model): SbmCOLLECTION, backref=db.backref('father', uselist=False), single_parent=True, - primaryjoin="and_(SbmCOLLECTIONSbmCOLLECTION.id_son==SbmCOLLECTION.id) " + primaryjoin="and_(" + "SbmCOLLECTIONSbmCOLLECTION.id_son==SbmCOLLECTION.id) " ) father = db.relationship( SbmCOLLECTION, backref=db.backref('son', uselist=False), single_parent=True, - primaryjoin="and_(SbmCOLLECTIONSbmCOLLECTION.id_father==SbmCOLLECTION.id) " + primaryjoin="and_(" + "SbmCOLLECTIONSbmCOLLECTION.id_father==SbmCOLLECTION.id) " ) @db.hybrid_property @@ -292,7 +294,7 @@ class SbmFIELDDESC(db.Model): fddfi2 = db.Column(db.Text, nullable=True) cookie = db.Column(db.Integer(11), nullable=True, server_default='0') - #field = db.relationship(SbmFIELD, backref='fielddescs') + # field = db.relationship(SbmFIELD, backref='fielddescs') class SbmFORMATEXTENSION(db.Model): @@ -300,10 +302,10 @@ class SbmFORMATEXTENSION(db.Model): """Represents a SbmFORMATEXTENSION record.""" __tablename__ = 'sbmFORMATEXTENSION' - FILE_FORMAT = db.Column(db.Text(50), nullable=False, - primary_key=True) - FILE_EXTENSION = db.Column(db.Text(10), nullable=False, - primary_key=True) + FILE_FORMAT = db.Column(db.Text().with_variant(db.Text(50), 'mysql'), + nullable=False, primary_key=True) + FILE_EXTENSION = db.Column(db.Text().with_variant(db.Text(10), 'mysql'), + nullable=False, primary_key=True) class SbmFUNCTIONS(db.Model): @@ -338,10 +340,10 @@ class SbmGFILERESULT(db.Model): """Represents a SbmGFILERESULT record.""" __tablename__ = 'sbmGFILERESULT' - FORMAT = db.Column(db.Text(50), nullable=False, - primary_key=True) - RESULT = db.Column(db.Text(50), nullable=False, - primary_key=True) + FORMAT = db.Column(db.Text().with_variant(db.Text(50), 'mysql'), + nullable=False, primary_key=True) + RESULT = db.Column(db.Text().with_variant(db.Text(50), 'mysql'), + nullable=False, primary_key=True) class SbmIMPLEMENT(db.Model):