Skip to content

Commit

Permalink
global: PostgreSQL Text type compatibility fix
Browse files Browse the repository at this point in the history
* Changes Text in models because from SQLAlchemy>=1.0 it's arised a
  exception if the length is specified. (addresses inveniosoftware#3037)

* PEP8/257 code style improvements.

Signed-off-by: Leonardo Rossi <[email protected]>
  • Loading branch information
Leonardo Rossi committed Apr 21, 2015
1 parent 9b1992c commit a5762d1
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 27 deletions.
29 changes: 22 additions & 7 deletions invenio/modules/collections/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,22 @@ 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'
id = db.Column(db.MediumInteger(9, unsigned=True),
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):
Expand Down Expand Up @@ -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']))
Expand All @@ -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)).\
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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),
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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"
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -591,6 +603,7 @@ class Externalcollection(db.Model):

@property
def engine(self):
"""Engine."""
from invenio.legacy.websearch_external_collections.searcher import (
external_collections_dictionary
)
Expand All @@ -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),
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion invenio/modules/knowledge/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 11 additions & 7 deletions invenio/modules/records/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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):
Expand Down
4 changes: 3 additions & 1 deletion invenio/modules/search/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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:
Expand Down
24 changes: 13 additions & 11 deletions invenio/modules/submit/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -292,18 +294,18 @@ 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):

"""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):
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit a5762d1

Please sign in to comment.