From 94429006c0c18b789e60e67a60db03c68e686b78 Mon Sep 17 00:00:00 2001 From: Leonardo Rossi Date: Wed, 22 Apr 2015 13:57:13 +0000 Subject: [PATCH] global: Integer type compatibility fix * Changes Integer in models because from SQLAlchemy>=1.0 it's arised a exceptions on use LegacyInteger. (closes #3037) * PEP8/257 code style improvements. Signed-off-by: Leonardo Rossi --- invenio/ext/logging/models.py | 49 ++--- invenio/ext/sqlalchemy/__init__.py | 17 +- invenio/ext/sqlalchemy/types/__init__.py | 12 +- invenio/modules/access/models.py | 93 ++++++---- invenio/modules/accounts/models.py | 42 +++-- invenio/modules/apikeys/models.py | 166 +++++++++-------- invenio/modules/comments/models.py | 115 +++++++----- invenio/modules/communities/models.py | 36 ++-- invenio/modules/linkbacks/models.py | 105 +++++++---- invenio/modules/messages/models.py | 194 +++++++++++--------- invenio/modules/oaiharvester/models.py | 22 ++- invenio/modules/oauthclient/models.py | 19 +- invenio/modules/pages/models.py | 20 +- invenio/modules/pidstore/models.py | 77 ++++---- invenio/modules/scheduler/models.py | 39 ++-- invenio/modules/search/models.py | 21 ++- invenio/modules/sequencegenerator/models.py | 12 +- invenio/modules/tags/models.py | 53 +++--- invenio/modules/workflows/models.py | 16 +- 19 files changed, 630 insertions(+), 478 deletions(-) diff --git a/invenio/ext/logging/models.py b/invenio/ext/logging/models.py index c3f9891c28..a717564eb2 100644 --- a/invenio/ext/logging/models.py +++ b/invenio/ext/logging/models.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # # This file is part of Invenio. -# Copyright (C) 2011, 2012, 2014 CERN. +# Copyright (C) 2011, 2012, 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,20 +17,18 @@ # along with Invenio; if not, write to the Free Software Foundation, Inc., # 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. -""" -errorlib database models. -""" +"""errorlib database models.""" -# General imports. from datetime import datetime + from invenio.base.globals import cfg from invenio.ext.sqlalchemy import db +from sqlalchemy.dialects import mysql + def _is_pow_of_2(n): - """ - Return True if n is a power of 2 - """ + """Return True if n is a power of 2.""" while n > 1: if n % 2: return False @@ -39,28 +37,35 @@ def _is_pow_of_2(n): class HstEXCEPTION(db.Model): + """Represents a HstEXCEPTION record.""" + __tablename__ = 'hstEXCEPTION' - id = db.Column(db.Integer(15, unsigned=True), nullable=False, - primary_key=True, autoincrement=True) + id = db.Column( + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + nullable=False, primary_key=True, autoincrement=True) name = db.Column(db.String(50), nullable=False) filename = db.Column(db.String(255), nullable=True) - line = db.Column(db.Integer(9), nullable=True) + line = db.Column( + db.Integer().with_variant(mysql.INTEGER(9), 'mysql'), + nullable=True) last_seen = db.Column(db.DateTime, nullable=False, server_default='1900-01-01 00:00:00', index=True) last_notified = db.Column(db.DateTime, nullable=False, server_default='1900-01-01 00:00:00', index=True) - counter = db.Column(db.Integer(15), nullable=False, - server_default='0') - total = db.Column(db.Integer(15), nullable=False, - server_default='0', index=True) + counter = db.Column( + db.Integer().with_variant(mysql.INTEGER(15), 'mysql'), + nullable=False, server_default='0') + total = db.Column( + db.Integer().with_variant(mysql.INTEGER(15), 'mysql'), + nullable=False, server_default='0', index=True) __table_args__ = (db.Index('name', name, filename, line, unique=True), db.Model.__table_args__) @classmethod def get_or_create(cls, name, filename, line): - """Finds or create exception log.""" + """Find or create exception log.""" try: log = cls.query.filter_by(name=name, filename=filename, line=line).one() @@ -72,7 +77,7 @@ def get_or_create(cls, name, filename, line): 'counter': counter, 'total': log.total + 1}, synchronize_settion=False) db.session.add(log) - except: + except Exception: log = HstEXCEPTION(name=name, filename=filename, line=line, @@ -83,16 +88,18 @@ def get_or_create(cls, name, filename, line): db.session.add(log) try: db.session.commit() - except: + except Exception: db.session.rollback() return log @property def exception_should_be_notified(self): + """Exception should be notified.""" return _is_pow_of_2(self.counter) @property def pretty_notification_info(self): + """Pretty notification info.""" return ("This exception has already been seen %s times\n " "last time it was seen: %s\n " "last time it was notified: %s\n" % ( @@ -102,14 +109,12 @@ def pretty_notification_info(self): @classmethod def get_pretty_notification_info(cls, name, filename, line): - """ - Return a sentence describing when this exception was already seen. - """ + """Return a sentence describing when this exception was already seen.""" try: return cls.query.filter_by( name=name, filename=filename, line=line ).one().pretty_notification_info - except: + except Exception: return "It is the first time this exception has been seen.\n" diff --git a/invenio/ext/sqlalchemy/__init__.py b/invenio/ext/sqlalchemy/__init__.py index e5eca7c83d..24dc67ddf3 100644 --- a/invenio/ext/sqlalchemy/__init__.py +++ b/invenio/ext/sqlalchemy/__init__.py @@ -19,20 +19,20 @@ """Initialization and configuration for `flask_sqlalchemy`.""" +import sqlalchemy +import sqlalchemy.dialects.postgresql + from flask_registry import ModuleAutoDiscoveryRegistry, RegistryProxy from flask_sqlalchemy import SQLAlchemy as FlaskSQLAlchemy -import sqlalchemy -import sqlalchemy.dialects.postgresql -from sqlalchemy import event, types -from sqlalchemy.ext.compiler import compiles +from sqlalchemy import event from sqlalchemy.ext.hybrid import hybrid_property from sqlalchemy.pool import Pool from sqlalchemy_utils import JSONType -from invenio.ext.sqlalchemy.types import LegacyBigInteger, LegacyInteger, \ +from invenio.ext.sqlalchemy.types import LegacyBigInteger, \ LegacyMediumInteger, LegacySmallInteger, LegacyTinyInteger from .expressions import AsBINARY @@ -60,19 +60,18 @@ def _include_sqlalchemy(obj, engine=None): setattr(obj, 'Char', engine_types.CHAR) try: setattr(obj, 'TinyText', engine_types.TINYTEXT) - except: + except Exception: setattr(obj, 'TinyText', engine_types.TEXT) setattr(obj, 'hybrid_property', hybrid_property) try: setattr(obj, 'Double', engine_types.DOUBLE) - except: + except Exception: setattr(obj, 'Double', engine_types.FLOAT) setattr(obj, 'Binary', sqlalchemy.types.LargeBinary) setattr(obj, 'iBinary', sqlalchemy.types.LargeBinary) setattr(obj, 'iLargeBinary', sqlalchemy.types.LargeBinary) setattr(obj, 'iMediumBinary', sqlalchemy.types.LargeBinary) setattr(obj, 'UUID', GUID) - setattr(obj, 'Integer', LegacyInteger) setattr(obj, 'MediumInteger', LegacyMediumInteger) setattr(obj, 'SmallInteger', LegacySmallInteger) setattr(obj, 'TinyInteger', LegacyTinyInteger) @@ -117,7 +116,7 @@ def autocommit_on_checkin(dbapi_con, con_record): """Call autocommit on raw mysql connection for fixing bug in MySQL 5.5.""" try: dbapi_con.autocommit(True) - except: + except Exception: pass # FIXME # from invenio.ext.logging import register_exception diff --git a/invenio/ext/sqlalchemy/types/__init__.py b/invenio/ext/sqlalchemy/types/__init__.py index d78e14389e..3a2caad591 100644 --- a/invenio/ext/sqlalchemy/types/__init__.py +++ b/invenio/ext/sqlalchemy/types/__init__.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 @@ -20,15 +20,15 @@ """Implement various custom column types.""" from .guid import GUID -from .marshal_binary import MarshalBinary -from .pickle_binary import PickleBinary -from .legacyinteger import LegacyInteger +from .legacybiginteger import LegacyBigInteger +from .legacyinteger import LegacyInteger # Don't remove, required! from .legacymediuminteger import LegacyMediumInteger from .legacysmallinteger import LegacySmallInteger from .legacytinyinteger import LegacyTinyInteger -from .legacybiginteger import LegacyBigInteger +from .marshal_binary import MarshalBinary +from .pickle_binary import PickleBinary __all__ = ('GUID', 'MarshalBinary', 'PickleBinary', - 'LegacyInteger', 'LegacyMediumInteger', 'LegacySmallInteger', + 'LegacyMediumInteger', 'LegacySmallInteger', 'LegacyTinyInteger', 'LegacyBigInteger') diff --git a/invenio/modules/access/models.py b/invenio/modules/access/models.py index 0b7a62502f..88cc379c13 100644 --- a/invenio/modules/access/models.py +++ b/invenio/modules/access/models.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # # This file is part of Invenio. -# Copyright (C) 2011, 2012, 2014 CERN. +# Copyright (C) 2011, 2012, 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 @@ -19,38 +19,39 @@ """Access database models.""" -# General imports. from cPickle import dumps, loads + from datetime import datetime, timedelta -from random import random -from sqlalchemy import bindparam -from sqlalchemy.orm import validates, column_property, undefer from invenio.base.wrappers import lazy_import from invenio.ext.sqlalchemy import db from invenio.ext.sqlalchemy.utils import session_manager +from invenio.modules.accounts.models import User from invenio.utils.hash import md5 -from .errors import ( - InvenioWebAccessMailCookieError, InvenioWebAccessMailCookieDeletedError) +from random import random + +from sqlalchemy import bindparam +from sqlalchemy.dialects import mysql +from sqlalchemy.orm import validates + +from .errors import InvenioWebAccessMailCookieDeletedError, \ + InvenioWebAccessMailCookieError SUPERADMINROLE = lazy_import( 'invenio.modules.access.local_config.SUPERADMINROLE') CFG_ACC_ACTIVITIES_URLS = lazy_import( 'invenio.modules.access.local_config.CFG_ACC_ACTIVITIES_URLS') -# Create your models here. - -from invenio.modules.accounts.models import User - class AccACTION(db.Model): """Represent an access action.""" __tablename__ = 'accACTION' - id = db.Column(db.Integer(15, unsigned=True), - primary_key=True, autoincrement=True) + id = db.Column( + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + primary_key=True, autoincrement=True) name = db.Column(db.String(32), unique=True, nullable=True) description = db.Column(db.String(255), nullable=True) allowedkeywords = db.Column(db.String(255), nullable=True) @@ -58,6 +59,7 @@ class AccACTION(db.Model): server_default='no') def __repr__(self): + """Repr.""" return "{0.name}".format(self) @@ -66,13 +68,16 @@ class AccARGUMENT(db.Model): """Represent an authorization argument.""" __tablename__ = 'accARGUMENT' - id = db.Column(db.Integer(15), primary_key=True, autoincrement=True) + id = db.Column( + db.Integer().with_variant(mysql.INTEGER(15), 'mysql'), + primary_key=True, autoincrement=True) keyword = db.Column(db.String(32), nullable=True) value = db.Column(db.String(255), nullable=True) __table_args__ = (db.Index('KEYVAL', keyword, value), db.Model.__table_args__) def __repr__(self): + """Repr.""" return "{0.keyword}={0.value}".format(self) @@ -87,8 +92,10 @@ class AccMAILCOOKIE(db.Model): 'comment_msg', 'generic' ) - id = db.Column(db.Integer(15, unsigned=True), primary_key=True, - autoincrement=True) + id = db.Column( + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + primary_key=True, + autoincrement=True) _data = db.Column('data', db.iBinary, nullable=False) expiration = db.Column(db.DateTime, nullable=False, server_default='9999-12-31 23:59:59', index=True) @@ -152,7 +159,7 @@ def create(cls, kind, params, cookie_timeout=timedelta(days=1), @session_manager def gc(cls): """Remove expired items.""" - return cls.query.filter(cls.expiration0 + db.func.count(User.id) > 0 ).join( User.active_roles, UserAccROLE.role, @@ -235,8 +253,9 @@ class UserAccROLE(db.Model): ).scalar() ) -User.has_super_admin_role = property(lambda self: - db.object_session(self).query(db.func.count(User.id)>0).join( +User.has_super_admin_role = property( + lambda self: + db.object_session(self).query(db.func.count(User.id) > 0).join( User.active_roles, UserAccROLE.role ).filter( diff --git a/invenio/modules/accounts/models.py b/invenio/modules/accounts/models.py index cd417570ee..62166e773d 100644 --- a/invenio/modules/accounts/models.py +++ b/invenio/modules/accounts/models.py @@ -24,14 +24,15 @@ 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.dialects import mysql +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 @@ -61,8 +62,9 @@ def __str__(self): __tablename__ = 'user' __mapper_args__ = {'confirm_deleted_rows': False} - id = db.Column(db.Integer(15, unsigned=True), primary_key=True, - autoincrement=True) + id = db.Column(db.Integer().with_variant( + mysql.INTEGER(15, unsigned=True), 'mysql'), primary_key=True, + autoincrement=True) email = db.Column(db.String(255), nullable=False, server_default='', index=True) _password = db.Column(db.String(255), name="password", @@ -237,8 +239,9 @@ def __str__(self): 'EXTERNAL': 'EXTERNAL', } - id = db.Column(db.Integer(15, unsigned=True), nullable=False, - primary_key=True, autoincrement=True) + id = db.Column(db.Integer().with_variant( + mysql.INTEGER(15, unsigned=True), 'mysql'), + nullable=False, primary_key=True, autoincrement=True) name = db.Column(db.String(255), nullable=False, server_default='', unique=True, index=True) description = db.Column(db.Text, nullable=True) @@ -375,14 +378,16 @@ def __str__(self): __tablename__ = 'user_usergroup' - id_user = db.Column(db.Integer(15, unsigned=True), - db.ForeignKey(User.id), - nullable=False, server_default='0', - primary_key=True) - id_usergroup = db.Column(db.Integer(15, unsigned=True), - db.ForeignKey(Usergroup.id), - nullable=False, server_default='0', - primary_key=True) + id_user = db.Column(db.Integer().with_variant( + mysql.INTEGER(15, unsigned=True), 'mysql'), + db.ForeignKey(User.id), + nullable=False, server_default='0', + primary_key=True) + id_usergroup = db.Column(db.Integer().with_variant( + mysql.INTEGER(15, unsigned=True), 'mysql'), + db.ForeignKey(Usergroup.id), + nullable=False, server_default='0', + primary_key=True) user_status = db.Column(db.CHAR(1), nullable=False, server_default='') user_status_date = db.Column(db.DateTime, nullable=False, default=datetime.now, @@ -415,8 +420,9 @@ class UserEXT(db.Model): id = db.Column(db.VARBINARY(255), primary_key=True, nullable=False) method = db.Column(db.String(50), primary_key=True, nullable=False) - id_user = db.Column(db.Integer(15, unsigned=True), - db.ForeignKey(User.id), nullable=False) + id_user = db.Column(db.Integer().with_variant( + mysql.INTEGER(15, unsigned=True), 'mysql'), + db.ForeignKey(User.id), nullable=False) user = db.relationship(User, backref="external_identifiers") diff --git a/invenio/modules/apikeys/models.py b/invenio/modules/apikeys/models.py index 7c508a5531..e8b83baaca 100644 --- a/invenio/modules/apikeys/models.py +++ b/invenio/modules/apikeys/models.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # # This file is part of Invenio. -# Copyright (C) 2012, 2013 CERN. +# Copyright (C) 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,18 +17,22 @@ # along with Invenio; if not, write to the Free Software Foundation, Inc., # 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. -""" -Web API Key database models. -""" -# General imports. -from werkzeug import cached_property -from six.moves.urllib.parse import parse_qs, urlparse, urlunparse -from sqlalchemy.exc import IntegrityError -from sqlalchemy.orm.exc import NoResultFound +"""Web API Key database models.""" import hmac -import time import re +import time + +from invenio.base.globals import cfg +from invenio.ext.sqlalchemy import db +from invenio.modules.accounts.models import User +from invenio.utils.hash import sha1 + +from six.moves.urllib.parse import parse_qs, urlparse, urlunparse + +from sqlalchemy.dialects import mysql +from sqlalchemy.exc import IntegrityError +from sqlalchemy.orm.exc import NoResultFound try: from uuid import uuid4 @@ -36,16 +40,10 @@ import random def uuid4(): + """Home-made definition of uuid4.""" return "%x" % random.getrandbits(16*8) -from urllib import urlencode, basejoin -from invenio.base.globals import cfg -from invenio.utils.hash import sha1 -from invenio.ext.sqlalchemy import db - - -# Create your models here. -from invenio.modules.accounts.models import User +from urllib import basejoin, urlencode def allowed_urls(): @@ -56,11 +54,13 @@ def allowed_urls(): class WebAPIKey(db.Model): + """Represents a Web API Key record.""" + __tablename__ = 'webapikey' - #There are three status key that must be here: OK, REMOVED and REVOKED - #the value doesn't matter at all + # There are three status key that must be here: OK, REMOVED and REVOKED + # the value doesn't matter at all CFG_WEB_API_KEY_STATUS = {'OK': 'OK', 'REMOVED': 'REMOVED', 'REVOKED': 'REVOKED', @@ -69,22 +69,24 @@ class WebAPIKey(db.Model): id = db.Column(db.String(150), primary_key=True, nullable=False) secret = db.Column(db.String(150), nullable=False) - id_user = db.Column(db.Integer(15, unsigned=True), db.ForeignKey(User.id), - nullable=False) + id_user = db.Column( + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + db.ForeignKey(User.id), + nullable=False) status = db.Column(db.String(25), nullable=False, server_default='OK', index=True) description = db.Column(db.String(255), nullable=True) @classmethod def create_new(cls, uid, key_description=None): - """ - Creates a new pair REST API key / secret key for the user. To do that it - uses the uuid4 function. + """Create a new pair REST API key / secret key for the user. - @param uid: User's id for the new REST API key - @type uid: int - @param key_description: User's description for the REST API key - @type key_description: string + To do that it uses the uuid4 function. + + :param uid: User's id for the new REST API key + :type uid: int + :param key_description: User's description for the REST API key + :type key_description: string """ key_id = str(uuid4()) key_secrect = str(uuid4()) @@ -100,18 +102,17 @@ def create_new(cls, uid, key_description=None): @classmethod def show_keys(cls, uid, diff_status=None): - """ - Makes a query to the DB to obtain all the user's REST API keys + """Make a query to the DB to obtain all the user's REST API keys. - @param uid: User's id - @type uid: int - @param diff_status: This string indicates if the query will show + :param uid: User's id + :type uid: int + :param diff_status: This string indicates if the query will show all the REST API keys or only the ones that still active (usable in the admin part) - @type diff_statusparam: string + :type diff_statusparam: string - @return: Tuples with the id, description and status of the user's REST API - keys + :return: Tuples with the id, description and status of the user's REST + API keys """ if diff_status is None: diff_status = cls.CFG_WEB_API_KEY_STATUS['REMOVED'] @@ -122,13 +123,15 @@ def show_keys(cls, uid, diff_status=None): @classmethod def mark_as(cls, key_id, status): - """ - When the user wants to remove one of his key, this functions puts the status - value of that key to remove, this way the user doesn't see the key anymore - but the admin user stills see it, make statistics whit it, etc. + """Put the status value of that key to remove. + + When the user wants to remove one of his key, this functions puts the + status value of that key to remove, this way the user doesn't see the + key anymore but the admin user stills see it, make statistics whit it, + etc. - @param key_id: The id of the REST key that will be "removed" - @type key_id: string + :param key_id: The id of the REST key that will be "removed" + :type key_id: string """ assert status in cls.CFG_WEB_API_KEY_STATUS cls.query.filter_by(id=key_id).\ @@ -136,17 +139,16 @@ def mark_as(cls, key_id, status): @classmethod def get_available(cls, uid=None, apikey=None): - """ - Search for all the available REST keys, it means all the user's keys that are - not marked as REMOVED or REVOKED + """Search for all the available REST keys. - @param uid: The user id - @type uid: int - @param apikey: the apikey/id + it means all the user's keys that are not marked as REMOVED or REVOKED - @return: WebAPIKey objects - """ + :param uid: The user id + :type uid: int + :param apikey: the apikey/id + :return: WebAPIKey objects + """ filters = {} if uid is not None: filters['id_user'] = uid @@ -161,6 +163,7 @@ def get_available(cls, uid=None, apikey=None): @classmethod def get_server_signature(cls, secret, url): + """Get server signature.""" from flask import request secret = str(secret) if request.base_url not in url: @@ -169,14 +172,14 @@ def get_server_signature(cls, secret, url): @classmethod def acc_get_uid_from_request(cls): - """ - Looks in the data base for the secret that matches with the API key in the - request. If the REST API key is found and if the signature is correct - returns the user's id. + """Look for the secret that matches with the API key in the request. - @return: If everything goes well it returns the user's uid, if not -1 - """ + Looks in the data base for the secret that matches with the API key in + the request. If the REST API key is found and if the signature is + correct returns the user's id. + :return: If everything goes well it returns the user's uid, if not -1 + """ from invenio.legacy.webstat.api import register_customevent from flask import request api_key = signature = timestamp = None @@ -250,41 +253,42 @@ def acc_get_uid_from_request(cls): if signature != server_signature: return -1 - #If the signature is fine, log the key activity and return the UID + # If the signature is fine, log the key activity and return the UID register_customevent("apikeyusage", [uid, api_key, path, url_req]) return uid @classmethod - def build_web_request(cls, path, params=None, uid=-1, api_key=None, timestamp=True): - """ - Build a new request that uses REST authentication. + def build_web_request(cls, path, params=None, uid=-1, api_key=None, + timestamp=True): + """Build a new request that uses REST authentication. + 1. Add your REST API key to the params 2. Add the current timestamp to the params, if needed 3. Sort the query string params 4. Merge path and the sorted query string to a single string - 5. Create a HMAC-SHA1 signature of this string using your secret key as the key + 5. Create a HMAC-SHA1 signature of this string using your secret key as + the key 6. Append the hex-encoded signature to your query string - @note: If the api_key parameter is None, then this method performs a search - in the data base using the uid parameter to get on of the user's REST - API key. If the user has one or more usable REST API key this method - uses the first to appear. - - @param path: uri of the request until the "?" (i.e.: /search) - @type path: string - @param params: All the params of the request (i.e.: req.args or a dictionary - with the param name as key) - @type params: string or dict - @param api_key: User REST API key - @type api_key: string - @param uid: User's id to do the search for the REST API key - @type uid: int - @param timestamp: Indicates if timestamp is needed in the request - @type timestamp: boolean - - @return: Signed request string or, in case of error, '' + :note: If the api_key parameter is None, then this method performs a + search in the data base using the uid parameter to get on of the + user's REST API key. If the user has one or more usable REST API + key this method uses the first to appear. + + :param path: uri of the request until the "?" (i.e.: /search) + :type path: string + :param params: All the params of the request (i.e.: req.args or a + dictionary with the param name as key) + :type params: string or dict + :param api_key: User REST API key + :type api_key: string + :param uid: User's id to do the search for the REST API key + :type uid: int + :param timestamp: Indicates if timestamp is needed in the request + :type timestamp: boolean + + :return: Signed request string or, in case of error, '' """ - if params is None: params = {} diff --git a/invenio/modules/comments/models.py b/invenio/modules/comments/models.py index 2072d3b7e5..a3a29594f7 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,50 +17,60 @@ # 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.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 +from sqlalchemy.dialects import mysql class CmtRECORDCOMMENT(db.Model): + """Represents a CmtRECORDCOMMENT record.""" + __tablename__ = 'cmtRECORDCOMMENT' - id = db.Column(db.Integer(15, unsigned=True), nullable=False, - primary_key=True, autoincrement=True) + id = db.Column( + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + nullable=False, + primary_key=True, autoincrement=True) id_bibrec = db.Column(db.MediumInteger(8, unsigned=True), db.ForeignKey(Bibrec.id), nullable=False, server_default='0') - id_user = db.Column(db.Integer(15, unsigned=True), db.ForeignKey(User.id), - nullable=False, server_default='0') + id_user = db.Column( + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + db.ForeignKey(User.id), + nullable=False, server_default='0') title = db.Column(db.String(255), nullable=False, 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') star_score = db.Column(db.TinyInteger(5, unsigned=True), nullable=False, server_default='0') - nb_votes_yes = db.Column(db.Integer(10), nullable=False, server_default='0') - nb_votes_total = db.Column(db.Integer(10, unsigned=True), nullable=False, - server_default='0') - nb_abuse_reports = db.Column(db.Integer(10), nullable=False, - server_default='0') + nb_votes_yes = db.Column( + db.Integer().with_variant(mysql.INTEGER(10), 'mysql'), + nullable=False, server_default='0') + nb_votes_total = db.Column( + db.Integer().with_variant(mysql.INTEGER(10, unsigned=True), 'mysql'), + nullable=False, + server_default='0') + nb_abuse_reports = db.Column( + db.Integer().with_variant(mysql.INTEGER(10), 'mysql'), + nullable=False, + server_default='0') status = db.Column(db.Char(2), nullable=False, index=True, server_default='ok') round_name = db.Column(db.String(255), nullable=False, server_default='') restriction = db.Column(db.String(50), nullable=False, server_default='') - in_reply_to_id_cmtRECORDCOMMENT = db.Column(db.Integer(15, unsigned=True), - db.ForeignKey(id), - nullable=False, - server_default='0') + in_reply_to_id_cmtRECORDCOMMENT = db.Column( + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + db.ForeignKey(id), + nullable=False, + server_default='0') reply_order_cached_data = db.Column(db.Binary, nullable=True) bibrec = db.relationship(Bibrec, backref='recordcomments') user = db.relationship(User, backref='recordcomments') @@ -69,27 +79,28 @@ class CmtRECORDCOMMENT(db.Model): @property def is_deleted(self): + """Property 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 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: + except Exception: db.session.rollback() 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, @@ -102,7 +113,7 @@ def expand(self, id_user): @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,24 +125,32 @@ 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), - nullable=True, primary_key=True) + id_cmtRECORDCOMMENT = db.Column( + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + db.ForeignKey(CmtRECORDCOMMENT.id), + nullable=True, primary_key=True) id_bibrec = db.Column(db.MediumInteger(8, unsigned=True), db.ForeignKey(Bibrec.id), nullable=True, primary_key=True) - id_user = db.Column(db.Integer(15, unsigned=True), db.ForeignKey(User.id), - nullable=True, primary_key=True) - client_host = db.Column(db.Integer(10, unsigned=True), nullable=True) + id_user = db.Column( + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + db.ForeignKey(User.id), + nullable=True, primary_key=True) + client_host = db.Column( + db.Integer().with_variant(mysql.INTEGER(10, unsigned=True), 'mysql'), + nullable=True) action_time = db.Column(db.DateTime, nullable=False, server_default='1900-01-01 00:00:00') action_code = db.Column(db.Char(1), nullable=False, index=True) @@ -141,14 +160,18 @@ class CmtACTIONHISTORY(db.Model): class CmtSUBSCRIPTION(db.Model): + """Represents a CmtSUBSCRIPTION record.""" + __tablename__ = 'cmtSUBSCRIPTION' id_bibrec = db.Column(db.MediumInteger(8, unsigned=True), db.ForeignKey(Bibrec.id), nullable=False, primary_key=True) - id_user = db.Column(db.Integer(15, unsigned=True), db.ForeignKey(User.id), - nullable=False, primary_key=True) + id_user = db.Column( + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + db.ForeignKey(User.id), + nullable=False, primary_key=True) creation_time = db.Column(db.DateTime, nullable=False, server_default='1900-01-01 00:00:00') @@ -157,17 +180,21 @@ class CmtSUBSCRIPTION(db.Model): class CmtCOLLAPSED(db.Model): + """Represents a CmtCOLLAPSED record.""" __tablename__ = 'cmtCOLLAPSED' id_bibrec = db.Column(db.MediumInteger(8, unsigned=True), db.ForeignKey(Bibrec.id), primary_key=True) - id_cmtRECORDCOMMENT = db.Column(db.Integer(15, unsigned=True), - db.ForeignKey(CmtRECORDCOMMENT.id), - primary_key=True) - id_user = db.Column(db.Integer(15, unsigned=True), db.ForeignKey(User.id), - primary_key=True) + id_cmtRECORDCOMMENT = db.Column( + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + db.ForeignKey(CmtRECORDCOMMENT.id), + primary_key=True) + id_user = db.Column( + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + db.ForeignKey(User.id), + primary_key=True) __all__ = ['CmtRECORDCOMMENT', diff --git a/invenio/modules/communities/models.py b/invenio/modules/communities/models.py index 60fc4ef1da..72d381876f 100644 --- a/invenio/modules/communities/models.py +++ b/invenio/modules/communities/models.py @@ -57,11 +57,6 @@ AccACTION, AccARGUMENT, \ AccAuthorization, AccROLE, UserAccROLE from invenio.modules.accounts.models import User -from invenio.modules.communities.signals import before_save_collection, \ - after_save_collection, before_save_collections, after_save_collections, \ - before_delete_collection, after_delete_collection, \ - before_delete_collections, after_delete_collections, \ - pre_curation, post_curation from invenio.modules.collections.models import \ Collection, \ CollectionCollection, \ @@ -70,8 +65,15 @@ Collectiondetailedrecordpagetabs, \ Collectionname, \ Portalbox -from invenio.modules.records.api import get_record +from invenio.modules.communities.signals import after_delete_collection, \ + after_delete_collections, after_save_collection, after_save_collections, \ + before_delete_collection, before_delete_collections, \ + before_save_collection, before_save_collections, post_curation, \ + pre_curation from invenio.modules.oaiharvester.models import OaiREPOSITORY +from invenio.modules.records.api import get_record + +from sqlalchemy.dialects import mysql class Community(db.Model): @@ -92,19 +94,22 @@ class Community(db.Model): """ id_user = db.Column( - db.Integer(15, unsigned=True), db.ForeignKey(User.id), + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + db.ForeignKey(User.id), nullable=False ) """ Owner of the community. """ id_collection = db.Column( - db.Integer(15, unsigned=True), db.ForeignKey(Collection.id), + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + db.ForeignKey(Collection.id), nullable=True, default=None ) """ Invenio collection generated from this community""" id_collection_provisional = db.Column( - db.Integer(15, unsigned=True), db.ForeignKey(Collection.id), + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + db.ForeignKey(Collection.id), nullable=True, default=None ) """ Invenio provisional collection generated from this community""" @@ -141,10 +146,14 @@ class Community(db.Model): default=datetime(2000, 1, 1, 0, 0, 0)) """ Last record acceptance datetime""" - ranking = db.Column(db.Integer(9), nullable=False, default=0) + ranking = db.Column( + db.Integer().with_variant(mysql.INTEGER(9), 'mysql'), + nullable=False, default=0) """ Ranking of community. Updated by ranking deamon""" - fixed_points = db.Column(db.Integer(9), nullable=False, default=0) + fixed_points = db.Column( + db.Integer().with_variant(mysql.INTEGER(9), 'mysql'), + nullable=False, default=0) """ Points which will be always added to overall score of community""" # # Relation ships @@ -808,8 +817,9 @@ class FeaturedCommunity(db.Model): __tablename__ = 'communityFEATURED' - id = db.Column(db.Integer(15, unsigned=True), primary_key=True, - autoincrement=True) + id = db.Column( + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + primary_key=True, autoincrement=True) """Featured community identifier.""" id_community = db.Column( diff --git a/invenio/modules/linkbacks/models.py b/invenio/modules/linkbacks/models.py index ac3e652e34..eeddb2e110 100644 --- a/invenio/modules/linkbacks/models.py +++ b/invenio/modules/linkbacks/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,102 +17,129 @@ # along with Invenio; if not, write to the Free Software Foundation, Inc., # 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. -""" -WebLinkBack database models. -""" +"""WebLinkBack database models.""" -# General imports. from invenio.ext.sqlalchemy import db - -# Create your models here. -from invenio.modules.records.models import Record as Bibrec from invenio.modules.accounts.models import User +from invenio.modules.records.models import Record as Bibrec + +from sqlalchemy.dialects import mysql + class LnkADMINURL(db.Model): + """Represents a LnkADMINURL record.""" + __tablename__ = 'lnkADMINURL' - id = db.Column(db.Integer(15, unsigned=True), primary_key=True, - nullable=False) + id = db.Column( + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + primary_key=True, + nullable=False) url = db.Column(db.String(100), nullable=False, unique=True) list = db.Column(db.String(30), nullable=False, index=True) class LnkENTRY(db.Model): + """Represents a LnkENTRY record.""" + __tablename__ = 'lnkENTRY' - id = db.Column(db.Integer(15, unsigned=True), primary_key=True, nullable=False) + id = db.Column( + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + primary_key=True, nullable=False) origin_url = db.Column(db.String(100), nullable=False) id_bibrec = db.Column(db.MediumInteger(8, unsigned=True), - db.ForeignKey(Bibrec.id), nullable=False) + db.ForeignKey(Bibrec.id), nullable=False) additional_properties = db.Column(db.Binary) type = db.Column(db.String(30), nullable=False, index=True) status = db.Column(db.String(30), nullable=False, server_default='PENDING', - index=True) + index=True) insert_time = db.Column(db.DateTime, server_default='1900-01-01 00:00:00', - index=True) + index=True) @property def title(self): + """Title.""" try: return db.object_session(self).query(LnkENTRYURLTITLE).\ filter(db.and_( - LnkENTRYURLTITLE.url==self.origin_url, - LnkENTRYURLTITLE.title<>"", - LnkENTRYURLTITLE.broken==0)).first().title - except: + LnkENTRYURLTITLE.url == self.origin_url, + LnkENTRYURLTITLE.title != "", + LnkENTRYURLTITLE.broken == 0)).first().title + except Exception: return self.origin_url class LnkENTRYURLTITLE(db.Model): + """Represents a LnkENTRYURLTITLE record.""" + __tablename__ = 'lnkENTRYURLTITLE' - id = db.Column(db.Integer(15, unsigned=True), primary_key=True, - nullable=False) + id = db.Column( + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + primary_key=True, + nullable=False) url = db.Column(db.String(100), nullable=False, unique=True) title = db.Column(db.String(100), nullable=False, index=True) manual_set = db.Column(db.TinyInteger(1), nullable=False, - server_default='0') - broken_count = db.Column(db.Integer(5), server_default='0') + server_default='0') + broken_count = db.Column( + db.Integer().with_variant(mysql.INTEGER(5), 'mysql'), + server_default='0') broken = db.Column(db.TinyInteger(1), nullable=False, server_default='0') class LnkLOG(db.Model): + """Represents a LnkLOG record.""" + __tablename__ = 'lnkLOG' - id = db.Column(db.Integer(15, unsigned=True), primary_key=True, nullable=False) - id_user = db.Column(db.Integer(15, unsigned=True), db.ForeignKey(User.id)) + id = db.Column( + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + primary_key=True, nullable=False) + id_user = db.Column( + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + db.ForeignKey(User.id)) action = db.Column(db.String(30), nullable=False, index=True) log_time = db.Column(db.DateTime, server_default='1900-01-01 00:00:00', - index=True) + index=True) class LnkENTRYLOG(db.Model): + """Represents a LnkENTRYLOG record.""" + __tablename__ = 'lnkENTRYLOG' - id_lnkENTRY = db.Column(db.Integer(15, unsigned=True), - db.ForeignKey(LnkENTRY.id), nullable=False, primary_key=True) - id_lnkLOG = db.Column(db.Integer(15, unsigned=True), - db.ForeignKey(LnkLOG.id), nullable=False, primary_key=True) + id_lnkENTRY = db.Column( + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + db.ForeignKey(LnkENTRY.id), nullable=False, primary_key=True) + id_lnkLOG = db.Column( + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + db.ForeignKey(LnkLOG.id), nullable=False, primary_key=True) class LnkADMINURLLOG(db.Model): + """Represents a LnkADMINURLLOG record.""" + __tablename__ = 'lnkADMINURLLOG' - id_lnkADMINURL = db.Column(db.Integer(15, unsigned=True), - db.ForeignKey(LnkADMINURL.id), primary_key=True, nullable=False) - id_lnkLOG = db.Column(db.Integer(15, unsigned=True), - db.ForeignKey(LnkLOG.id), primary_key=True, nullable=False) + id_lnkADMINURL = db.Column( + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + db.ForeignKey(LnkADMINURL.id), primary_key=True, nullable=False) + id_lnkLOG = db.Column( + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + db.ForeignKey(LnkLOG.id), primary_key=True, nullable=False) -__all__ = [ 'LnkADMINURL', - 'LnkADMINURLLOG', - 'LnkENTRY', - 'LnkENTRYLOG', - 'LnkENTRYURLTITLE', - 'LnkLOG'] +__all__ = ['LnkADMINURL', + 'LnkADMINURLLOG', + 'LnkENTRY', + 'LnkENTRYLOG', + 'LnkENTRYURLTITLE', + 'LnkLOG'] diff --git a/invenio/modules/messages/models.py b/invenio/modules/messages/models.py index 6d096b30e3..303534d1b6 100644 --- a/invenio/modules/messages/models.py +++ b/invenio/modules/messages/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,66 +17,75 @@ # along with Invenio; if not, write to the Free Software Foundation, Inc., # 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. -""" -WebMessage database models. -""" +"""WebMessage database models.""" + +from string import strip -# General imports from invenio.base.globals import cfg +from invenio.base.i18n import _ from invenio.ext.sqlalchemy import db - -# Create your models here. -from string import strip from invenio.modules.accounts.models import User, Usergroup + +from sqlalchemy.dialects import mysql from sqlalchemy.ext.associationproxy import association_proxy class MsgMESSAGE(db.Model): + """Represents a MsgMESSAGE record.""" + def __str__(self): + """Str.""" return "From: %s<%s>, Subject: <%s> %s" % \ (self.user_from.nickname or _('None'), - self.user_from.email or _('unknown'), - self.subject, self.body) + self.user_from.email or _('unknown'), + self.subject, self.body) __tablename__ = 'msgMESSAGE' - id = db.Column(db.Integer(15, unsigned=True), nullable=False, - primary_key=True, - autoincrement=True) - id_user_from = db.Column(db.Integer(15, unsigned=True), - db.ForeignKey(User.id), - nullable=True, server_default='0') + id = db.Column( + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + nullable=False, + primary_key=True, + autoincrement=True) + id_user_from = db.Column( + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + db.ForeignKey(User.id), + nullable=True, server_default='0') _sent_to_user_nicks = db.Column(db.Text, name='sent_to_user_nicks', - nullable=False) + nullable=False) _sent_to_group_names = db.Column(db.Text, name='sent_to_group_names', - nullable=False) + nullable=False) subject = db.Column(db.Text, nullable=False) body = db.Column(db.Text, nullable=True) - sent_date = db.Column(db.DateTime, nullable=False, - server_default='1900-01-01 00:00:00') # db.func.now() -> 'NOW()' + sent_date = db.Column( + db.DateTime, nullable=False, + server_default='1900-01-01 00:00:00') # db.func.now() -> 'NOW()' received_date = db.Column(db.DateTime, - server_default='1900-01-01 00:00:00') + server_default='1900-01-01 00:00:00') user_from = db.relationship(User, backref='sent_messages') - #recipients = db.relationship(User, + # recipients = db.relationship(User, # secondary=lambda: UserMsgMESSAGE.__table__, # collection_class=set) - recipients = association_proxy('sent_to_users', 'user_to', - creator=lambda u:UserMsgMESSAGE(user_to=u)) + recipients = association_proxy( + 'sent_to_users', 'user_to', + creator=lambda u: UserMsgMESSAGE(user_to=u)) @db.hybrid_property def sent_to_user_nicks(self): - """ Alias for column 'sent_to_user_nicks'. """ + """The Alias for column 'sent_to_user_nicks'.""" return self._sent_to_user_nicks @db.hybrid_property def sent_to_group_names(self): - """ Alias for column 'sent_to_group_names'. """ + """The Alias for column 'sent_to_group_names'.""" return self._sent_to_group_names @db.validates('_sent_to_user_nicks') def validate_sent_to_user_nicks(self, key, value): - user_nicks = filter(len, map(strip, - value.split(cfg['CFG_WEBMESSAGE_SEPARATOR']))) + """Validate sent to user nicks.""" + user_nicks = filter( + len, map(strip, + value.split(cfg['CFG_WEBMESSAGE_SEPARATOR']))) assert len(user_nicks) == len(set(user_nicks)) if len(user_nicks) > 0: assert len(user_nicks) == \ @@ -85,108 +94,121 @@ def validate_sent_to_user_nicks(self, key, value): @db.validates('_sent_to_group_names') def validate_sent_to_group_names(self, key, value): - group_names = filter(len, map(strip, - value.split(cfg['CFG_WEBMESSAGE_SEPARATOR']))) + """Validate sent to group names.""" + group_names = filter( + len, map(strip, + value.split(cfg['CFG_WEBMESSAGE_SEPARATOR']))) assert len(group_names) == len(set(group_names)) if len(group_names) > 0: assert len(group_names) == \ Usergroup.query.filter(Usergroup.name.in_(group_names)).count() return cfg['CFG_WEBMESSAGE_SEPARATOR'].join(group_names) - @sent_to_user_nicks.setter def sent_to_user_nicks(self, value): + """Sent to user nicks.""" old_user_nicks = self.user_nicks self._sent_to_user_nicks = value to_add = set(self.user_nicks)-set(old_user_nicks) to_del = set(old_user_nicks)-set(self.user_nicks) if len(self.group_names): - to_del = to_del-set([u.nickname for u in User.query.\ - join(User.usergroups).filter( - Usergroup.name.in_(self.group_names)).\ - all()]) - if len(to_del): - is_to_del = lambda u: u.nickname in to_del - remove_old = filter(is_to_del, self.recipients) - for u in remove_old: - self.recipients.remove(u) - if len(to_add): - for u in User.query.filter(User.nickname.\ - in_(to_add)).all(): - if u not in self.recipients: - self.recipients.append(u) + to_del = to_del - set([u.nickname for u in User.query. + join(User.usergroups).filter( + Usergroup.name.in_(self.group_names)). + all()]) + if len(to_del): + is_to_del = lambda u: u.nickname in to_del + remove_old = filter(is_to_del, self.recipients) + for u in remove_old: + self.recipients.remove(u) + if len(to_add): + for u in User.query.filter(User.nickname. + in_(to_add)).all(): + if u not in self.recipients: + self.recipients.append(u) @sent_to_group_names.setter def sent_to_group_names(self, value): + """Sent to group names.""" old_group_names = self.group_names self._sent_to_group_names = value groups_to_add = set(self.group_names)-set(old_group_names) groups_to_del = set(old_group_names)-set(self.group_names) if len(groups_to_del): - to_del = set([u.nickname for u in User.query.\ - join(User.usergroups).filter( - Usergroup.name.in_(groups_to_del)).\ - all()])-set(self.user_nicks) + to_del = set([u.nickname for u in User.query. + join(User.usergroups).filter( + Usergroup.name.in_(groups_to_del)). + all()])-set(self.user_nicks) is_to_del = lambda u: u.nickname in to_del remove_old = filter(is_to_del, self.recipients) for u in remove_old: self.recipients.remove(u) - if len(groups_to_add): - for u in User.query.join(User.usergroups).filter(db.and_( - Usergroup.name.in_(groups_to_add), - db.not_(User.nickname.in_(self.user_nicks)))).all(): - if u not in self.recipients: - self.recipients.append(u) + if len(groups_to_add): + myquery = User.query.join(User.usergroups).filter(db.and_( + Usergroup.name.in_(groups_to_add), + db.not_(User.nickname.in_(self.user_nicks)))) + for u in myquery.all(): + if u not in self.recipients: + self.recipients.append(u) @property def user_nicks(self): + """User nicks.""" if not self._sent_to_user_nicks: return [] - return filter(len, map(strip, - self._sent_to_user_nicks.split(cfg['CFG_WEBMESSAGE_SEPARATOR']))) + return filter( + len, + map(strip, + self._sent_to_user_nicks.split(cfg['CFG_WEBMESSAGE_SEPARATOR'])) + ) @property def group_names(self): + """Group names.""" if not self._sent_to_group_names: return [] - return filter(len, map(strip, - self.sent_to_group_names.split(cfg['CFG_WEBMESSAGE_SEPARATOR']))) + return filter( + len, map( + strip, + self.sent_to_group_names.split(cfg['CFG_WEBMESSAGE_SEPARATOR'])) + ) -#TODO consider moving following lines to separate file. +# TODO consider moving following lines to separate file. -from invenio.modules.messages.config import CFG_WEBMESSAGE_EMAIL_ALERT -from invenio.config import CFG_WEBCOMMENT_ALERT_ENGINE_EMAIL -from invenio.utils.date import datetext_format from datetime import datetime +from invenio.utils.date import datetext_format + + def email_alert(mapper, connection, target): - """ Sends email alerts to message recipients. """ + """Send email alerts to message recipients.""" from invenio.ext.template import render_template_to_string from invenio.ext.email import send_email, scheduled_send_email m = target - is_reminder = m.received_date is not None \ - and m.received_date > datetime.now() + is_reminder = m.received_date is not None and \ + m.received_date > datetime.now() alert = send_email if is_reminder: - alert = lambda *args, **kwargs: scheduled_send_email(*args, - other_bibtasklet_arguments=[ - m.received_date.strftime(datetext_format)], - **kwargs) + alert = lambda *args, **kwargs: scheduled_send_email( + *args, + other_bibtasklet_arguments=[ + m.received_date.strftime(datetext_format)], + **kwargs) for u in m.recipients: if isinstance(u.settings, dict) and \ - u.settings.get('webmessage_email_alert', True): + u.settings.get('webmessage_email_alert', True): try: alert( cfg['CFG_WEBCOMMENT_ALERT_ENGINE_EMAIL'], u.email, - subject = m.subject, - content = render_template_to_string( - 'messages/email_alert.html', - message=m, user=u)) - except: + subject=m.subject, + content=render_template_to_string( + 'messages/email_alert.html', + message=m, user=u)) + except Exception: # FIXME tests are not in request context pass @@ -203,17 +225,21 @@ def email_alert_register(): class UserMsgMESSAGE(db.Model): + """Represents a UserMsgMESSAGE record.""" + __tablename__ = 'user_msgMESSAGE' - id_user_to = db.Column(db.Integer(15, unsigned=True), - db.ForeignKey(User.id), nullable=False, - server_default='0', primary_key=True) - id_msgMESSAGE = db.Column(db.Integer(15, unsigned=True), - db.ForeignKey(MsgMESSAGE.id), - nullable=False, server_default='0', - primary_key=True) + id_user_to = db.Column( + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + db.ForeignKey(User.id), nullable=False, + server_default='0', primary_key=True) + id_msgMESSAGE = db.Column( + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + db.ForeignKey(MsgMESSAGE.id), + nullable=False, server_default='0', + primary_key=True) status = db.Column(db.Char(1), nullable=False, - server_default='N') + server_default='N') user_to = db.relationship(User, backref='received_messages', collection_class=set) message = db.relationship(MsgMESSAGE, backref='sent_to_users', diff --git a/invenio/modules/oaiharvester/models.py b/invenio/modules/oaiharvester/models.py index 53cddf2e63..896ca6ccc6 100644 --- a/invenio/modules/oaiharvester/models.py +++ b/invenio/modules/oaiharvester/models.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 @@ -19,13 +19,14 @@ """OAI harvest database models.""" -from sqlalchemy import event - from invenio.ext.sqlalchemy import db from invenio.ext.sqlalchemy.utils import session_manager from invenio.modules.records.models import Record as Bibrec from invenio.modules.scheduler.models import SchTASK +from sqlalchemy import event +from sqlalchemy.dialects import mysql + def get_default_arguments(): """Return default values for arguments.""" @@ -54,8 +55,9 @@ class OaiHARVEST(db.Model): baseurl = db.Column(db.String(255), nullable=False, server_default='') metadataprefix = db.Column(db.String(255), nullable=False, server_default='oai_dc') - arguments = db.Column(db.MarshalBinary(default_value=get_default_arguments(), - force_type=dict), nullable=False) + arguments = db.Column( + db.MarshalBinary(default_value=get_default_arguments(), + force_type=dict), nullable=False) comment = db.Column(db.Text, nullable=True) name = db.Column(db.String(255), nullable=False) lastrun = db.Column(db.DateTime, nullable=True) @@ -119,7 +121,7 @@ class OaiREPOSITORY(db.Model): @classmethod def update_setdefinition_listener(cls, mapper, connection, target): - """Update setDefinition attribute on before_insert/before_update events.""" + """Update setDefinition attr on before_insert/before_update events.""" # FIXME: Next two lines op1 = '' op2 = '' @@ -166,9 +168,11 @@ class OaiHARVESTLOG(db.Model): db.MediumInteger(8, unsigned=True), db.ForeignKey(Bibrec.id), nullable=False, server_default='0' ) - bibupload_task_id = db.Column(db.Integer(11), db.ForeignKey(SchTASK.id), - nullable=False, server_default='0', - primary_key=True) + bibupload_task_id = db.Column( + db.Integer().with_variant(mysql.INTEGER(11), 'mysql'), + db.ForeignKey(SchTASK.id), + nullable=False, server_default='0', + primary_key=True) oai_id = db.Column(db.String(40), nullable=False, server_default='', primary_key=True) date_harvested = db.Column(db.DateTime, nullable=False, diff --git a/invenio/modules/oauthclient/models.py b/invenio/modules/oauthclient/models.py index e69eab39be..eb3d412ce6 100644 --- a/invenio/modules/oauthclient/models.py +++ b/invenio/modules/oauthclient/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 @@ -19,15 +19,20 @@ """Models for storing access tokens and links between users and remote apps.""" -from sqlalchemy.ext.mutable import MutableDict -from sqlalchemy_utils.types.encrypted import EncryptedType - from invenio.config import SECRET_KEY as secret_key from invenio.ext.sqlalchemy import db from invenio.modules.accounts.models import User +from sqlalchemy.dialects import mysql +from sqlalchemy.ext.mutable import MutableDict + +from sqlalchemy_utils.types.encrypted import EncryptedType + + class TextEncryptedType(EncryptedType): + """Text encrypted type.""" + impl = db.Text @@ -46,14 +51,14 @@ class RemoteAccount(db.Model): # Fields # id = db.Column( - db.Integer(15, unsigned=True), + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), primary_key=True, autoincrement=True ) """Primary key.""" user_id = db.Column( - db.Integer(15, unsigned=True), + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), db.ForeignKey(User.id), nullable=False ) @@ -124,7 +129,7 @@ class RemoteToken(db.Model): # Fields # id_remote_account = db.Column( - db.Integer(15, unsigned=True), + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), db.ForeignKey(RemoteAccount.id), nullable=False, primary_key=True diff --git a/invenio/modules/pages/models.py b/invenio/modules/pages/models.py index 8b9822cc38..14f90084e9 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,24 +17,24 @@ # 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 + +from sqlalchemy.dialects import mysql class Page(db.Model): + """Represents a page.""" + __tablename__ = 'pages' - id = db.Column(db.Integer(15, unsigned=True), nullable=False, - primary_key=True, - autoincrement=True) + id = db.Column( + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + nullable=False, primary_key=True, 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) diff --git a/invenio/modules/pidstore/models.py b/invenio/modules/pidstore/models.py index f4b49ae3b3..4f32a28514 100644 --- a/invenio/modules/pidstore/models.py +++ b/invenio/modules/pidstore/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,11 +17,7 @@ # along with Invenio; if not, write to the Free Software Foundation, Inc., # 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. -""" -invenio.modules.pid_store.models --------------------------------- - -PersistentIdentifier store and registration. +"""PersistentIdentifier store and registration. Usage example for registering new identifiers:: @@ -57,18 +53,20 @@ """ from datetime import datetime -from sqlalchemy.exc import SQLAlchemyError from invenio.base.globals import cfg from invenio.ext.sqlalchemy import db from invenio.utils.text import to_unicode +from sqlalchemy.dialects import mysql +from sqlalchemy.exc import SQLAlchemyError + from .provider import PidProvider class PersistentIdentifier(db.Model): - """ - Store and register persistent identifiers + + """Store and register persistent identifiers. Assumptions: * Persistent identifiers can be represented as a string of max 255 chars. @@ -83,7 +81,9 @@ class PersistentIdentifier(db.Model): db.Index('idx_object', 'object_type', 'object_value'), ) - id = db.Column(db.Integer(15, unsigned=True), primary_key=True) + id = db.Column( + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + primary_key=True) """ Id of persistent identifier entry """ pid_type = db.Column(db.String(6), nullable=False) @@ -173,7 +173,8 @@ def get(cls, pid_type, pid_value, pid_provider='', provider=None): # Instance methods # def has_object(self, object_type, object_value): - """ + """Check this persistent identifier. + Determine if this persistent identifier is assigned to a specific object. """ @@ -186,9 +187,7 @@ def has_object(self, object_type, object_value): self.object_value == object_value def get_provider(self): - """ - Get the provider for this type of persistent identifier - """ + """Get the provider for this type of persistent identifier.""" if self._provider is None: self._provider = PidProvider.create( self.pid_type, self.pid_value, self.pid_provider @@ -196,8 +195,7 @@ def get_provider(self): return self._provider def assign(self, object_type, object_value, overwrite=False): - """ - Assign this persistent identifier to a given object + """Assign this persistent identifier to a given object. Note, the persistent identifier must first have been reserved. Also, if an exsiting object is already assigned to the pid, it will raise an @@ -247,7 +245,7 @@ def assign(self, object_type, object_value, overwrite=False): return True def update(self, with_deleted=False, *args, **kwargs): - """ Update the persistent identifier with the provider. """ + """Update the persistent identifier with the provider.""" if self.is_new() or self.is_reserved(): raise Exception( "Persistent identifier has not yet been registered." @@ -269,8 +267,7 @@ def update(self, with_deleted=False, *args, **kwargs): return False def reserve(self, *args, **kwargs): - """ - Reserve the persistent identifier with the provider + """Reserve the persistent identifier with the provider. Note, the reserve method may be called multiple times, even if it was already reserved. @@ -292,9 +289,7 @@ def reserve(self, *args, **kwargs): return False def register(self, *args, **kwargs): - """ - Register the persistent identifier with the provider - """ + """Register the persistent identifier with the provider.""" if self.is_registered() or self.is_deleted(): raise Exception( "Persistent identifier has already been registered." @@ -312,9 +307,7 @@ def register(self, *args, **kwargs): return False def delete(self, *args, **kwargs): - """ - Delete the persistent identifier - """ + """Delete the persistent identifier.""" if self.is_new(): # New persistent identifier which haven't been registered yet. Just # delete it completely but keep log) @@ -331,10 +324,10 @@ def delete(self, *args, **kwargs): return True def sync_status(self, *args, **kwargs): - """ - Synchronize persistent identifier status. Used when the provider uses - an external service, which might have been modified outside of our - system. + """Synchronize persistent identifier status. + + Used when the provider uses an external service, which might have been + modified outside of our system. """ provider = self.get_provider() result = provider.sync_status(self, *args, **kwargs) @@ -342,33 +335,33 @@ def sync_status(self, *args, **kwargs): return result def get_assigned_object(self, object_type=None): + """Get assigned object.""" if object_type is not None and self.object_type == object_type: return self.object_value return None def is_registered(self): - """ Returns true if the persistent identifier has been registered """ + """Return true if the persistent identifier has been registered.""" return self.status == cfg['PIDSTORE_STATUS_REGISTERED'] def is_deleted(self): - """ Returns true if the persistent identifier has been deleted """ + """Return true if the persistent identifier has been deleted.""" return self.status == cfg['PIDSTORE_STATUS_DELETED'] def is_new(self): - """ + """Check if has not yet been registered or reserved. + Returns true if the persistent identifier has not yet been registered or reserved """ return self.status == cfg['PIDSTORE_STATUS_NEW'] def is_reserved(self): - """ - Returns true if the persistent identifier has not yet been - reserved. - """ + """Return true if persistent identifier has not yet been reserved.""" return self.status == cfg['PIDSTORE_STATUS_RESERVED'] def log(self, action, message): + """Log.""" if self.pid_type and self.pid_value: message = "[%s:%s] %s" % (self.pid_type, self.pid_value, message) p = PidLog(id_pid=self.id, action=action, message=message) @@ -377,22 +370,26 @@ def log(self, action, message): class PidLog(db.Model): - """ - Audit log of actions happening to persistent identifiers. + + """Audit log of actions happening to persistent identifiers. This model is primarily used through PersistentIdentifier.log and rarely created manually. """ + __tablename__ = 'pidLOG' __table_args__ = ( db.Index('idx_action', 'action'), ) - id = db.Column(db.Integer(15, unsigned=True), primary_key=True) + id = db.Column( + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + primary_key=True) """ Id of persistent identifier entry """ id_pid = db.Column( - db.Integer(15, unsigned=True), db.ForeignKey(PersistentIdentifier.id), + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + db.ForeignKey(PersistentIdentifier.id), nullable=True, ) """ PID """ diff --git a/invenio/modules/scheduler/models.py b/invenio/modules/scheduler/models.py index 52f8046cdc..a6fcf3f28e 100644 --- a/invenio/modules/scheduler/models.py +++ b/invenio/modules/scheduler/models.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # # This file is part of Invenio. -# Copyright (C) 2011, 2012, 2014 CERN. +# Copyright (C) 2011, 2012, 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,23 +17,22 @@ # along with Invenio; if not, write to the Free Software Foundation, Inc., # 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. -""" -bibsched database models. -""" +"""bibsched database models.""" -# General imports. from invenio.ext.sqlalchemy import db - -# Create your models here. from invenio.modules.sequencegenerator.models import SeqSTORE +from sqlalchemy.dialects import mysql + class HstTASK(db.Model): + """Represents a HstTASK record.""" __tablename__ = 'hstTASK' - id = db.Column(db.Integer(15, unsigned=True), nullable=False, - primary_key=True, autoincrement=False) + id = db.Column( + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + nullable=False, primary_key=True, autoincrement=False) proc = db.Column(db.String(255), nullable=False) host = db.Column(db.String(255), nullable=False, server_default='') @@ -45,18 +44,19 @@ class HstTASK(db.Model): progress = db.Column(db.String(255), nullable=True) priority = db.Column(db.TinyInteger(4), nullable=False, server_default='0', index=True) - sequenceid = db.Column(db.Integer(15, unsigned=True), - db.ForeignKey(SeqSTORE.id)) + sequenceid = db.Column( + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + db.ForeignKey(SeqSTORE.id)) class SchTASK(db.Model): + """Represents a SchTASK record.""" - def __init__(self): - pass __tablename__ = 'schTASK' - id = db.Column(db.Integer(15, unsigned=True), nullable=False, - primary_key=True, autoincrement=True) + id = db.Column( + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + nullable=False, primary_key=True, autoincrement=True) proc = db.Column(db.String(255), nullable=False) host = db.Column(db.String(255), nullable=False, server_default='') @@ -68,8 +68,13 @@ def __init__(self): progress = db.Column(db.String(255), nullable=True) priority = db.Column(db.TinyInteger(4), nullable=False, server_default='0', index=True) - sequenceid = db.Column(db.Integer(15, unsigned=True), - db.ForeignKey(SeqSTORE.id)) + sequenceid = db.Column( + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + db.ForeignKey(SeqSTORE.id)) + + def __init__(self): + """Init.""" + pass # FIXME To be moved to redis when available diff --git a/invenio/modules/search/models.py b/invenio/modules/search/models.py index ec2ff4bbd0..0cf83eb702 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.dialects import mysql + class Field(db.Model): @@ -165,8 +167,9 @@ class WebQuery(db.Model): """Represent a WebQuery record.""" __tablename__ = 'query' - id = db.Column(db.Integer(15, unsigned=True), primary_key=True, - autoincrement=True) + id = db.Column( + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + primary_key=True, autoincrement=True) type = db.Column(db.Char(1), nullable=False, server_default='r') urlargs = db.Column(db.String(100), nullable=False, index=True) @@ -176,12 +179,14 @@ class UserQuery(db.Model): """Represent a UserQuery record.""" __tablename__ = 'user_query' - id_user = db.Column(db.Integer(15, unsigned=True), - db.ForeignKey(User.id), primary_key=True, - server_default='0') - id_query = db.Column(db.Integer(15, unsigned=True), - db.ForeignKey(WebQuery.id), primary_key=True, - index=True, server_default='0') + id_user = db.Column( + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + db.ForeignKey(User.id), primary_key=True, + server_default='0') + id_query = db.Column( + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + db.ForeignKey(WebQuery.id), primary_key=True, + index=True, server_default='0') hostname = db.Column(db.String(50), nullable=True, server_default='unknown host') date = db.Column(db.DateTime, nullable=True, diff --git a/invenio/modules/sequencegenerator/models.py b/invenio/modules/sequencegenerator/models.py index 04e207d9a3..e6c569d816 100644 --- a/invenio/modules/sequencegenerator/models.py +++ b/invenio/modules/sequencegenerator/models.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # # This file is part of Invenio. -# Copyright (C) 2012, 2014 CERN. +# Copyright (C) 2012, 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,21 @@ # along with Invenio; if not, write to the Free Software Foundation, Inc., # 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. -""" -SeqUtils database models. -""" +"""SeqUtils database models.""" from invenio.ext.sqlalchemy import db +from sqlalchemy.dialects import mysql + class SeqSTORE(db.Model): + """Represents a SeqSTORE record.""" + __tablename__ = 'seqSTORE' id = db.Column( - db.Integer(15, unsigned=True), + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), primary_key=True, nullable=False, autoincrement=True ) diff --git a/invenio/modules/tags/models.py b/invenio/modules/tags/models.py index 34dbc88782..453956366e 100644 --- a/invenio/modules/tags/models.py +++ b/invenio/modules/tags/models.py @@ -31,6 +31,7 @@ from six import iteritems +from sqlalchemy.dialects import mysql from sqlalchemy.ext.associationproxy import association_proxy from werkzeug import cached_property @@ -119,10 +120,11 @@ class WtgTAG(db.Model, Serializable): ACCESS_GROUP_DEFAULT = ACCESS_LEVELS['View'] # Primary key - id = db.Column(db.Integer(15, unsigned=True), - primary_key=True, - nullable=False, - autoincrement=True) + id = db.Column( + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + primary_key=True, + nullable=False, + autoincrement=True) # Name name = db.Column(db.String(255), @@ -131,32 +133,35 @@ class WtgTAG(db.Model, Serializable): index=True) # Owner - id_user = db.Column(db.Integer(15, unsigned=True), - db.ForeignKey(User.id), - server_default='0') + id_user = db.Column( + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + db.ForeignKey(User.id), + server_default='0') # Access rights of owner - user_access_rights = db.Column(db.Integer(2, unsigned=True), - nullable=False, - default=ACCESS_OWNER_DEFAULT) + user_access_rights = db.Column( + db.Integer().with_variant(mysql.INTEGER(2, unsigned=True), 'mysql'), + nullable=False, + default=ACCESS_OWNER_DEFAULT) # Group # equal to 0 for private tags id_usergroup = db.Column( - db.Integer(15, unsigned=True), + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), db.ForeignKey(Usergroup.id), server_default='0') # Group access rights group_access_rights = db.Column( - db.Integer(2, unsigned=True), + db.Integer().with_variant(mysql.INTEGER(2, unsigned=True), 'mysql'), nullable=False, default=ACCESS_GROUP_DEFAULT) # Access rights of everyone - public_access_rights = db.Column(db.Integer(2, unsigned=True), - nullable=False, - default=ACCESS_LEVELS['Nothing']) + public_access_rights = db.Column( + db.Integer().with_variant(mysql.INTEGER(2, unsigned=True), 'mysql'), + nullable=False, + default=ACCESS_LEVELS['Nothing']) # Visibility in document description show_in_description = db.Column(db.Boolean, @@ -213,16 +218,18 @@ class WtgTAGRecord(db.Model, Serializable): __public__ = set(['id_tag', 'id_bibrec', 'date_added']) # tagTAG.id - id_tag = db.Column(db.Integer(15, unsigned=True), - db.ForeignKey(WtgTAG.id), - nullable=False, - primary_key=True) + id_tag = db.Column( + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + db.ForeignKey(WtgTAG.id), + nullable=False, + primary_key=True) # Bibrec.id - id_bibrec = db.Column(db.Integer(15, unsigned=True), - db.ForeignKey(Bibrec.id), - nullable=False, - primary_key=True) + id_bibrec = db.Column( + db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'), + db.ForeignKey(Bibrec.id), + nullable=False, + primary_key=True) # Annotation annotation = db.Column( diff --git a/invenio/modules/workflows/models.py b/invenio/modules/workflows/models.py index 4491d9abc6..64e1401bdc 100644 --- a/invenio/modules/workflows/models.py +++ b/invenio/modules/workflows/models.py @@ -35,6 +35,7 @@ from six.moves import cPickle from sqlalchemy import desc +from sqlalchemy.dialects import mysql from sqlalchemy.orm.exc import NoResultFound from .logger import BibWorkflowLogHandler, get_logger @@ -314,8 +315,9 @@ class BibWorkflowObject(db.Model): id_workflow = db.Column(db.String(36), db.ForeignKey("bwlWORKFLOW.uuid"), nullable=True) - version = db.Column(db.Integer(3), - default=ObjectVersion.INITIAL, nullable=False) + version = db.Column( + db.Integer().with_variant(mysql.INTEGER(3), 'mysql'), + default=ObjectVersion.INITIAL, nullable=False) id_parent = db.Column(db.Integer, db.ForeignKey("bwlOBJECT.id"), default=None) child_objects = db.relationship("BibWorkflowObject", @@ -727,7 +729,8 @@ def get_log(self, *criteria, **filters): :return: list of BibWorkflowObjectLog """ - criterions = [BibWorkflowObjectLog.id_object == self.id] + list(criteria) + criterions = [BibWorkflowObjectLog.id_object == self.id] + \ + list(criteria) res = BibWorkflowObjectLog.query.filter( *criterions ).filter_by(**filters) @@ -842,9 +845,10 @@ class BibWorkflowObjectLog(db.Model): __tablename__ = 'bwlOBJECTLOGGING' id = db.Column(db.Integer, primary_key=True) - id_object = db.Column(db.Integer(255), - db.ForeignKey('bwlOBJECT.id'), - nullable=False) + id_object = db.Column( + db.Integer().with_variant(mysql.INTEGER(255), 'mysql'), + db.ForeignKey('bwlOBJECT.id'), + nullable=False) log_type = db.Column(db.Integer, default=0, nullable=False) created = db.Column(db.DateTime, default=datetime.now) message = db.Column(db.TEXT, default="", nullable=False)