diff --git a/invenio/ext/sqlalchemy/engines/mysql.py b/invenio/ext/sqlalchemy/engines/mysql.py index 62c75b807f..bc888a9d34 100644 --- a/invenio/ext/sqlalchemy/engines/mysql.py +++ b/invenio/ext/sqlalchemy/engines/mysql.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # # This file is part of Invenio. -# Copyright (C) 2011, 2012, 2013, 2014 CERN. +# Copyright (C) 2011, 2012, 2013, 2014, 2015 CERN. # # Invenio is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as @@ -21,86 +21,11 @@ import base64 -# SQLAlchemy import sqlalchemy -import sqlalchemy.types as types +from sqlalchemy import types as types from sqlalchemy.ext.compiler import compiles - -from sqlalchemy.schema import CreateIndex, PrimaryKeyConstraint - - -@compiles(CreateIndex, 'mysql') -def visit_create_index(element, compiler, **kw): - """Return create index statement with defined length for text field. - - example: - CREATE TABLE tableA - ... - description TEXT(40) - ... - INDEX ix_tableA_description ON (description(40)) - """ - index = element.element - preparer = compiler.preparer - table = preparer.format_table(index.table) - name = preparer.quote(index.name, index.name.quote) - - text = "ALTER TABLE %s ADD " % (table, ) - if index.unique: - text += "UNIQUE " - text += "INDEX %s" % (name, ) - - lst = index.kwargs.get('mysql_length', None) - - columns = [] - for i, c in enumerate(index.columns): - cname = c.name - suffix = '' - if isinstance(lst, (list, tuple)) and len(lst) > i \ - and lst[i] is not None: - suffix = '(%d)' % lst[i] - elif c.type != types.NULLTYPE \ - and str(c.type).startswith('TEXT') \ - and (c.type.length is not None): - suffix = '(%d)' % c.type.length - columns.append(cname + suffix) - - text += '(' + ', '.join(columns) + ')' - - if 'mysql_using' in index.kwargs: - using = index.kwargs['mysql_using'] - if using is not None: - text += " USING %s" % (preparer.quote(using, index.quote)) - - return text - - -@compiles(PrimaryKeyConstraint, 'mysql') -def visit_primary_key_constraint(*element): - """ - Return create primary key constrains. - - Return create primary key constrains with defined length - for text field. - """ - constraint, compiler = element - if len(constraint) == 0: - return '' - text = "" - if constraint.name is not None: - text += "CONSTRAINT %s " % \ - compiler.preparer.format_constraint(constraint) - text += "PRIMARY KEY " - text += "(%s)" % ', '.join(c.name + - (c.type != types.NULLTYPE - and (str(c.type).startswith('TEXT') - and (c.type.length is not None)) - and '(%d)' % c.type.length - or '') - for c in constraint) - text += compiler.define_constraint_deferrability(constraint) - return text +from sqlalchemy.types import TypeDecorator @compiles(types.Text, 'sqlite') @@ -140,8 +65,6 @@ def compile_largebinary(element, compiler, **kw): """Redefine LargeBinary filed type for MySQL.""" return 'LONGBLOB' -from sqlalchemy.types import TypeDecorator - class iBinary(TypeDecorator): diff --git a/invenio/modules/accounts/models.py b/invenio/modules/accounts/models.py index cd417570ee..67b069d973 100644 --- a/invenio/modules/accounts/models.py +++ b/invenio/modules/accounts/models.py @@ -24,14 +24,14 @@ from flask_login import current_user -from sqlalchemy.ext.hybrid import hybrid_property - -from sqlalchemy_utils.types.choice import ChoiceType - from invenio.ext.passlib import password_context from invenio.ext.passlib.hash import invenio_aes_encrypted_email from invenio.ext.sqlalchemy import db +from sqlalchemy.ext.hybrid import hybrid_property + +from sqlalchemy_utils.types.choice import ChoiceType + from .errors import AccountSecurityError, IntegrityUsergroupError from .helpers import send_account_activation_email from .signals import profile_updated @@ -252,8 +252,8 @@ def __str__(self): nullable=False, server_default='INTERNAL') # FIXME Unique(login_method(70), name) - __table_args__ = (db.Index('login_method_name', login_method, name, - mysql_length=[60, None]), + __table_args__ = (db.Index('login_method_name', 'login_method', 'name', + mysql_length={'login_method': 60, 'name': 255}), db.Model.__table_args__) @classmethod diff --git a/invenio/modules/baskets/models.py b/invenio/modules/baskets/models.py index ff57a6b3d7..dfc9f198c3 100644 --- a/invenio/modules/baskets/models.py +++ b/invenio/modules/baskets/models.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # # This file is part of Invenio. -# Copyright (C) 2011, 2012 CERN. +# Copyright (C) 2011, 2012, 2015 CERN. # # Invenio is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as @@ -17,154 +17,165 @@ # along with Invenio; if not, write to the Free Software Foundation, Inc., # 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. -""" -webbasket database models. -""" +"""webbasket database models.""" -# General imports. from invenio.ext.sqlalchemy import db - -# Create your models here. - from invenio.modules.accounts.models import User, Usergroup from invenio.modules.collections.models import Collection + class BskBASKET(db.Model): + """Represents a BskBASKET record.""" + __tablename__ = 'bskBASKET' id = db.Column(db.Integer(15, unsigned=True), nullable=False, - primary_key=True) - id_owner = db.Column(db.Integer(15, unsigned=True), - db.ForeignKey(User.id), nullable=False, server_default='0') + primary_key=True) + id_owner = db.Column( + db.Integer(15, unsigned=True), + db.ForeignKey(User.id), nullable=False, server_default='0') name = db.Column(db.String(50), nullable=False, server_default='', - index=True) + index=True) date_modification = db.Column(db.DateTime, nullable=False, - server_default='1900-01-01 00:00:00') + server_default='1900-01-01 00:00:00') nb_views = db.Column(db.Integer(15), nullable=False, - server_default='0') + server_default='0') owner = db.relationship(User, backref='baskets') class BskEXTREC(db.Model): + """Represents a BskEXTREC record.""" + __tablename__ = 'bskEXTREC' id = db.Column(db.Integer(15, unsigned=True), nullable=False, - primary_key=True) + primary_key=True) external_id = db.Column(db.Integer(15), nullable=False, - server_default='0') + server_default='0') collection_id = db.Column(db.MediumInteger(9, unsigned=True), - db.ForeignKey(Collection.id), nullable=False, - server_default='0') + db.ForeignKey(Collection.id), nullable=False, + server_default='0') original_url = db.Column(db.Text, nullable=True) creation_date = db.Column(db.DateTime, nullable=False, - server_default='1900-01-01 00:00:00') + server_default='1900-01-01 00:00:00') modification_date = db.Column(db.DateTime, nullable=False, - server_default='1900-01-01 00:00:00') + server_default='1900-01-01 00:00:00') collection = db.relationship(Collection, backref='EXTRECs') class BskEXTFMT(db.Model): + """Represents a BskEXTFMT record.""" + __tablename__ = 'bskEXTFMT' id = db.Column(db.Integer(15, unsigned=True), nullable=False, - primary_key=True) + primary_key=True) id_bskEXTREC = db.Column(db.Integer(15, unsigned=True), - db.ForeignKey(BskEXTREC.id), nullable=False, - server_default='0') + db.ForeignKey(BskEXTREC.id), nullable=False, + server_default='0') format = db.Column(db.String(10), nullable=False, index=True, - server_default='') + server_default='') last_updated = db.Column(db.DateTime, nullable=False, - server_default='1900-01-01 00:00:00') + server_default='1900-01-01 00:00:00') value = db.Column(db.iLargeBinary, nullable=True) EXTREC = db.relationship(BskEXTREC, backref='EXTFMTs') class BskREC(db.Model): + """Represents a BskREC record.""" + __tablename__ = 'bskREC' - id_bibrec_or_bskEXTREC = db.Column(db.Integer(16), nullable=False, - server_default='0', primary_key=True, index=True) + id_bibrec_or_bskEXTREC = db.Column( + db.Integer(16), nullable=False, + server_default='0', primary_key=True, index=True) id_bskBASKET = db.Column(db.Integer(15, unsigned=True), - db.ForeignKey(BskBASKET.id), nullable=False, - server_default='0', primary_key=True) + db.ForeignKey(BskBASKET.id), nullable=False, + server_default='0', primary_key=True) id_user_who_added_item = db.Column(db.Integer(15, unsigned=True), - db.ForeignKey(User.id), - nullable=False, server_default='0') + db.ForeignKey(User.id), + nullable=False, server_default='0') score = db.Column(db.Integer(15), nullable=False, - server_default='0') + server_default='0') date_added = db.Column(db.DateTime, nullable=False, index=True, - server_default='1900-01-01 00:00:00') + server_default='1900-01-01 00:00:00') basket = db.relationship(BskBASKET, backref='RECs') user_who_added_item = db.relationship(User) class BskRECORDCOMMENT(db.Model): + """Represents a BskRECORDCOMMENT record.""" + __tablename__ = 'bskRECORDCOMMENT' id = db.Column(db.Integer(15, unsigned=True), nullable=False, - primary_key=True, autoincrement=True) + primary_key=True, autoincrement=True) id_bibrec_or_bskEXTREC = db.Column(db.Integer(16), nullable=False, - server_default='0') + server_default='0') id_bskBASKET = db.Column(db.Integer(15, unsigned=True), - db.ForeignKey(BskBASKET.id), nullable=False, - server_default='0') + db.ForeignKey(BskBASKET.id), nullable=False, + server_default='0') id_user = db.Column(db.Integer(15, unsigned=True), db.ForeignKey(User.id), - nullable=False, server_default='0') + nullable=False, server_default='0') title = db.Column(db.String(255), nullable=False, - server_default='') + server_default='') body = db.Column(db.Text, nullable=False) date_creation = db.Column(db.DateTime, nullable=False, - server_default='1900-01-01 00:00:00', index=True) + server_default='1900-01-01 00:00:00', index=True) priority = db.Column(db.Integer(15), nullable=False, - server_default='0') - in_reply_to_id_bskRECORDCOMMENT = db.Column(db.Integer(15, unsigned=True), - db.ForeignKey(id), nullable=False, server_default='0') + server_default='0') + in_reply_to_id_bskRECORDCOMMENT = db.Column( + db.Integer(15, unsigned=True), + db.ForeignKey(id), nullable=False, server_default='0') reply_order_cached_data = db.Column(db.Binary, nullable=True) in_reply_to = db.relationship('BskRECORDCOMMENT') basket = db.relationship(BskBASKET, backref='RECORDCOMMENTs') user = db.relationship(User) __table_args__ = (db.Index('bskRECORDCOMMENT_reply_order_cached_data', - reply_order_cached_data, mysql_length=[40]), + reply_order_cached_data, mysql_length=40), db.Model.__table_args__) class UserBskBASKET(db.Model): + """Represents a UserBskBASKET record.""" + __tablename__ = 'user_bskBASKET' id_user = db.Column(db.Integer(15, unsigned=True), db.ForeignKey(User.id), - nullable=False, server_default='0', primary_key=True) + nullable=False, server_default='0', primary_key=True) id_bskBASKET = db.Column(db.Integer(15, unsigned=True), - db.ForeignKey(BskBASKET.id), nullable=False, - server_default='0', primary_key=True) + db.ForeignKey(BskBASKET.id), nullable=False, + server_default='0', primary_key=True) topic = db.Column(db.String(50), nullable=False, server_default='') user = db.relationship(User, backref='user_baskets') user_basket = db.relationship(BskBASKET, backref='users') class UsergroupBskBASKET(db.Model): + """Represents a UsergroupBskBASKET record.""" + __tablename__ = 'usergroup_bskBASKET' id_usergroup = db.Column(db.Integer(15, unsigned=True), - db.ForeignKey(Usergroup.id), nullable=False, - server_default='0', primary_key=True) + db.ForeignKey(Usergroup.id), nullable=False, + server_default='0', primary_key=True) id_bskBASKET = db.Column(db.Integer(15, unsigned=True), - db.ForeignKey(BskBASKET.id), nullable=False, - server_default='0', primary_key=True) + db.ForeignKey(BskBASKET.id), nullable=False, + server_default='0', primary_key=True) topic = db.Column(db.String(50), nullable=False, - server_default='') + server_default='') date_shared = db.Column(db.DateTime, nullable=False, - server_default='1900-01-01 00:00:00') + server_default='1900-01-01 00:00:00') share_level = db.Column(db.Char(2), nullable=False, server_default='') usergroup = db.relationship(Usergroup, backref='usergroup_baskets') usergroup_basket = db.relationship(BskBASKET, backref='usergroups') - -__all__ = ['BskBASKET', +__all__ = ('BskBASKET', 'BskEXTREC', 'BskEXTFMT', 'BskREC', 'BskRECORDCOMMENT', 'UserBskBASKET', - 'UsergroupBskBASKET'] + 'UsergroupBskBASKET') diff --git a/invenio/modules/collections/models.py b/invenio/modules/collections/models.py index 905b872dbd..e378029747 100644 --- a/invenio/modules/collections/models.py +++ b/invenio/modules/collections/models.py @@ -38,9 +38,10 @@ from sqlalchemy.ext.associationproxy import association_proxy from sqlalchemy.ext.orderinglist import ordering_list from sqlalchemy.orm.collections import attribute_mapped_collection +from sqlalchemy.schema import Index from werkzeug.utils import cached_property -# Create your models here. + external_collection_mapper = attribute_multi_dict_collection( creator=lambda k, v: CollectionExternalcollection(type=k, @@ -59,19 +60,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) + name = db.Column(db.String(255), unique=True, index=True, nullable=False) + dbquery = db.Column( + db.Text().with_variant(db.Text(20), 'mysql'), + nullable=True) @property def nbrecs(self): @@ -136,6 +140,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 +156,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 +165,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 +269,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: @@ -423,6 +428,9 @@ def breadcrumbs(self, builder=None, ln=None): return breadcrumbs +Index('ix_collection_dbquery', Collection.dbquery, mysql_length=20) + + class Collectionname(db.Model): """Represent a Collectionname record.""" @@ -440,10 +448,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 +490,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 +504,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 +526,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 +537,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 +553,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 +562,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 +576,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 +598,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 +607,7 @@ class Externalcollection(db.Model): @property def engine(self): + """Engine.""" from invenio.legacy.websearch_external_collections.searcher import ( external_collections_dictionary ) @@ -601,6 +618,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 +652,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/comments/models.py b/invenio/modules/comments/models.py index 2072d3b7e5..577db30a27 100644 --- a/invenio/modules/comments/models.py +++ b/invenio/modules/comments/models.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # # This file is part of Invenio. -# Copyright (C) 2011, 2012, 2013 CERN. +# Copyright (C) 2011, 2012, 2013, 2015 CERN. # # Invenio is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as @@ -17,22 +17,21 @@ # along with Invenio; if not, write to the Free Software Foundation, Inc., # 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. -""" -WebComment database models. -""" +"""WebComment database models.""" -# General imports. -from sqlalchemy import event +from invenio.base.signals import record_after_update from invenio.ext.sqlalchemy import db - -# Create your models here. -from invenio.modules.records.models import Record as Bibrec +from invenio.ext.sqlalchemy.utils import session_manager from invenio.modules.accounts.models import User -from invenio.base.signals import record_after_update +from invenio.modules.records.models import Record as Bibrec + +from sqlalchemy import event class CmtRECORDCOMMENT(db.Model): + """Represents a CmtRECORDCOMMENT record.""" + __tablename__ = 'cmtRECORDCOMMENT' id = db.Column(db.Integer(15, unsigned=True), nullable=False, @@ -69,40 +68,39 @@ class CmtRECORDCOMMENT(db.Model): @property def is_deleted(self): + """Check if is deleted.""" return self.status != 'ok' def is_collapsed(self, id_user): - """Returns true if the comment is collapsed by user.""" + """Return true if the comment is collapsed by user.""" return CmtCOLLAPSED.query.filter(db.and_( CmtCOLLAPSED.id_bibrec == self.id_bibrec, CmtCOLLAPSED.id_cmtRECORDCOMMENT == self.id, CmtCOLLAPSED.id_user == id_user)).count() > 0 + @session_manager def collapse(self, id_user): - """Collapses comment beloging to user.""" + """Collapse comment beloging to user.""" c = CmtCOLLAPSED(id_bibrec=self.id_bibrec, id_cmtRECORDCOMMENT=self.id, id_user=id_user) - try: - db.session.add(c) - db.session.commit() - except: - db.session.rollback() + db.session.add(c) + db.session.commit() def expand(self, id_user): - """Expands comment beloging to user.""" + """Expand comment beloging to user.""" CmtCOLLAPSED.query.filter(db.and_( CmtCOLLAPSED.id_bibrec == self.id_bibrec, CmtCOLLAPSED.id_cmtRECORDCOMMENT == self.id, CmtCOLLAPSED.id_user == id_user)).delete(synchronize_session=False) __table_args__ = (db.Index('cmtRECORDCOMMENT_reply_order_cached_data', - reply_order_cached_data, mysql_length=[40]), + reply_order_cached_data, mysql_length=40), db.Model.__table_args__) @event.listens_for(CmtRECORDCOMMENT, 'after_insert') def after_insert(mapper, connection, target): - """Updates reply order cache and send record-after-update signal.""" + """Update reply order cache and send record-after-update signal.""" record_after_update.send(CmtRECORDCOMMENT, recid=target.id_bibrec) from .api import get_reply_order_cache_data @@ -114,14 +112,17 @@ def after_insert(mapper, connection, target): parent_reply_order = parent.reply_order_cached_data \ if parent.reply_order_cached_data else '' parent_reply_order += get_reply_order_cache_data(target.id) - connection.execute(db.update(CmtRECORDCOMMENT.__table__). - where(CmtRECORDCOMMENT.id == parent.id). - values(reply_order_cached_data=parent_reply_order)) + connection.execute( + db.update(CmtRECORDCOMMENT.__table__). + where(CmtRECORDCOMMENT.id == parent.id). + values(reply_order_cached_data=parent_reply_order)) trans.commit() class CmtACTIONHISTORY(db.Model): + """Represents a CmtACTIONHISTORY record.""" + __tablename__ = 'cmtACTIONHISTORY' id_cmtRECORDCOMMENT = db.Column(db.Integer(15, unsigned=True), db.ForeignKey(CmtRECORDCOMMENT.id), @@ -141,7 +142,9 @@ class CmtACTIONHISTORY(db.Model): class CmtSUBSCRIPTION(db.Model): + """Represents a CmtSUBSCRIPTION record.""" + __tablename__ = 'cmtSUBSCRIPTION' id_bibrec = db.Column(db.MediumInteger(8, unsigned=True), @@ -157,6 +160,7 @@ class CmtSUBSCRIPTION(db.Model): class CmtCOLLAPSED(db.Model): + """Represents a CmtCOLLAPSED record.""" __tablename__ = 'cmtCOLLAPSED' @@ -170,7 +174,7 @@ class CmtCOLLAPSED(db.Model): primary_key=True) -__all__ = ['CmtRECORDCOMMENT', +__all__ = ('CmtRECORDCOMMENT', 'CmtACTIONHISTORY', 'CmtSUBSCRIPTION', - 'CmtCOLLAPSED'] + 'CmtCOLLAPSED') diff --git a/invenio/modules/knowledge/models.py b/invenio/modules/knowledge/models.py index 85dffa6ffb..9eb613c6e3 100644 --- a/invenio/modules/knowledge/models.py +++ b/invenio/modules/knowledge/models.py @@ -27,9 +27,10 @@ from invenio.modules.collections.models import Collection from invenio.utils.text import slugify +from sqlalchemy.dialects import mysql from sqlalchemy.event import listens_for - from sqlalchemy.orm.collections import attribute_mapped_collection +from sqlalchemy.schema import Index class KnwKB(db.Model): @@ -281,7 +282,9 @@ 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(mysql.TEXT(30), 'mysql'), + nullable=False) id_knwKB = db.Column(db.MediumInteger(8), db.ForeignKey(KnwKB.id), nullable=False, server_default='0', primary_key=True) @@ -343,4 +346,7 @@ def to_dict(self): 'kbid': self.kb.id if self.kb else None, 'kbname': self.kb.name if self.kb else None} + +Index('ix_knwKBRVAL_m_value', KnwKBRVAL.m_value, mysql_length=30) + __all__ = ('KnwKB', 'KnwKBDDEF', 'KnwKBRVAL') diff --git a/invenio/modules/pages/models.py b/invenio/modules/pages/models.py index 8b9822cc38..f98a648ec2 100644 --- a/invenio/modules/pages/models.py +++ b/invenio/modules/pages/models.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # # This file is part of Invenio. -# Copyright (C) 2014 CERN. +# Copyright (C) 2014, 2015 CERN. # # Invenio is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as @@ -17,19 +17,17 @@ # along with Invenio; if not, write to the Free Software Foundation, Inc., # 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. -""" - invenio.modules.pages.models - ---------------------------- +"""Contains page model.""" - Contains page model. -""" +from datetime import datetime from invenio.ext.sqlalchemy import db -from datetime import datetime class Page(db.Model): + """Represents a page.""" + __tablename__ = 'pages' id = db.Column(db.Integer(15, unsigned=True), nullable=False, @@ -37,11 +35,13 @@ class Page(db.Model): autoincrement=True) url = db.Column(db.String(100), unique=True, nullable=False) title = db.Column(db.String(200), nullable=True) - content = db.Column(db.TEXT(length=2**32-2), nullable=True) + content = db.Column( + db.Text().with_variant(db.Text(length=2**32-2), 'mysql'), + nullable=True) # Default is pages/templates/default.html template_name = db.Column(db.String(70), nullable=True) created = db.Column(db.DateTime(), nullable=False, default=datetime.now) last_modified = db.Column(db.DateTime(), nullable=False, default=datetime.now, onupdate=datetime.now) -__all__ = ['Page'] +__all__ = ('Page',) diff --git a/invenio/modules/records/models.py b/invenio/modules/records/models.py index 4e385cf450..bc7c89cbf8 100644 --- a/invenio/modules/records/models.py +++ b/invenio/modules/records/models.py @@ -20,12 +20,16 @@ """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 sqlalchemy.schema import Index + +from werkzeug import cached_property + class Record(db.Model): @@ -59,8 +63,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 +113,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 +168,9 @@ 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) class BibrecBibxxxMixin(utils.TableFromCamelNameMixin): @@ -202,13 +208,17 @@ def bibxxx(cls): for idx in range(100): Bibxxx = "Bib{0:02d}x".format(idx) - globals()[Bibxxx] = type(Bibxxx, (db.Model, BibxxxMixin), {}) + globals_Bibxxx = type(Bibxxx, (db.Model, BibxxxMixin), {}) + globals()[Bibxxx] = globals_Bibxxx BibrecBibxxx = "BibrecBib{0:02d}x".format(idx) globals()[BibrecBibxxx] = type(BibrecBibxxx, (db.Model, BibrecBibxxxMixin), {}) models += [Bibxxx, BibrecBibxxx] + Index('ix_{0:02d}x_value'.format(idx), + globals_Bibxxx.value, mysql_length=35) + __all__ = tuple([ 'Record', 'RecordMetadata', diff --git a/invenio/modules/search/models.py b/invenio/modules/search/models.py index 0fd97b8385..41cc8c4ca4 100644 --- a/invenio/modules/search/models.py +++ b/invenio/modules/search/models.py @@ -29,6 +29,8 @@ from invenio.ext.sqlalchemy import db from invenio.modules.accounts.models import User +from sqlalchemy.schema import Index + class Field(db.Model): @@ -168,7 +170,12 @@ 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('ix_query_urlargs', WebQuery.urlargs, mysql_length=100) class UserQuery(db.Model): @@ -191,6 +198,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/setup.py b/setup.py index 86fe67f0ef..013e6ae202 100644 --- a/setup.py +++ b/setup.py @@ -77,7 +77,7 @@ def run(self): "feedparser>=5.1", "fixture>=1.5", "Flask>=0.10.1", - "Flask-Admin>=1.0.9", + "Flask-Admin>=1.1.0", "Flask-Assets>=0.10", "Flask-Babel>=0.9", "Flask-Breadcrumbs>=0.2",