diff --git a/wcs/admin/forms.py b/wcs/admin/forms.py
index 4e5831d9e..3d3ca0c58 100644
--- a/wcs/admin/forms.py
+++ b/wcs/admin/forms.py
@@ -1875,7 +1875,7 @@ class FormsDirectory(AccessControlled, Directory):
def new(self):
get_response().breadcrumb.append(('new', _('New')))
- if get_publisher().role_class.count() == 0:
+ if not (get_publisher().role_class.exists()):
return template.error_page(self.section, _('You first have to define roles.'))
formdefui = self.formdef_ui_class(None)
form = formdefui.new_form_ui()
diff --git a/wcs/admin/users.py b/wcs/admin/users.py
index 222fe5877..59f4d06db 100644
--- a/wcs/admin/users.py
+++ b/wcs/admin/users.py
@@ -498,7 +498,7 @@ class UsersDirectory(Directory):
# XXX: user must be logged in to get here
user = get_publisher().user_class()
user_ui = UserUI(user)
- first_user = get_publisher().user_class.count() == 0
+ first_user = not (get_publisher().user_class.exists())
if first_user:
user.is_admin = first_user
form = user_ui.form()
diff --git a/wcs/api.py b/wcs/api.py
index eb83650a9..317a680b6 100644
--- a/wcs/api.py
+++ b/wcs/api.py
@@ -428,7 +428,7 @@ class ApiFormsDirectory(Directory):
if get_request().form.get('full') == 'on':
raise RequestError('no such parameter "full"')
- if FormDef.count() == 0:
+ if not (FormDef.exists()):
# early return, this avoids running a query against a missing SQL view.
get_response().set_content_type('application/json')
return json.dumps({'data': []}, cls=misc.JSONEncoder)
@@ -908,7 +908,7 @@ class ApiUserDirectory(Directory):
return json.dumps(user_info, cls=misc.JSONEncoder)
def get_user_forms(self, user):
- if FormDef.count() == 0:
+ if not (FormDef.exists()):
# early return, this avoids running a query against a missing SQL view.
return []
diff --git a/wcs/backoffice/data_management.py b/wcs/backoffice/data_management.py
index d43fa8227..77cd86223 100644
--- a/wcs/backoffice/data_management.py
+++ b/wcs/backoffice/data_management.py
@@ -92,7 +92,7 @@ class DataManagementDirectory(ManagementDirectory):
def _q_index(self):
html_top('data_management', _('Cards'))
- if CardDef.count() == 0:
+ if not (CardDef.exists()):
return self.empty_site_message(_('Cards'))
return template.QommonTemplateResponse(
templates=['wcs/backoffice/data-management.html'], context={'view': self}
diff --git a/wcs/backoffice/management.py b/wcs/backoffice/management.py
index 7d017da16..9037b8b60 100644
--- a/wcs/backoffice/management.py
+++ b/wcs/backoffice/management.py
@@ -409,7 +409,7 @@ class ManagementDirectory(Directory):
html_top('management', _('Global statistics'))
get_response().breadcrumb.append(('statistics', _('Global statistics')))
- if FormDef.count() == 0:
+ if not (FormDef.exists()):
r = TemplateIO(html=True)
r += htmltext('
')
r += htmltext('
%s
') % _('Global statistics')
@@ -605,7 +605,7 @@ class ManagementDirectory(Directory):
html_top('management', _('Management'))
- if FormDef.count() == 0:
+ if not (FormDef.exists()):
return self.empty_site_message(_('Global View'))
limit = misc.get_int_or_400(
@@ -733,7 +733,7 @@ class ManagementDirectory(Directory):
def count(self):
if not get_publisher().is_using_postgresql():
raise errors.TraversalError()
- if FormDef.count() == 0:
+ if not (FormDef.exists()):
return misc.json_response({'count': 0})
from wcs import sql
@@ -853,7 +853,7 @@ class FormPage(Directory):
def check_access(self, api_name=None):
session = get_session()
user = get_request().user
- if user is None and get_publisher().user_class.count() == 0:
+ if user is None and not (get_publisher().user_class.exists()):
user = get_publisher().user_class()
user.is_admin = True
if not user:
diff --git a/wcs/backoffice/root.py b/wcs/backoffice/root.py
index 4bdeb2faa..062b2efed 100644
--- a/wcs/backoffice/root.py
+++ b/wcs/backoffice/root.py
@@ -92,7 +92,7 @@ class RootDirectory(BackofficeRootDirectory):
return True
if not get_request().user:
- if get_publisher().user_class.count() == 0:
+ if not (get_publisher().user_class.exists()):
# setting up the site, access is granted to settings and users
# sections
return subdirectory in ('settings', 'users')
@@ -146,7 +146,7 @@ class RootDirectory(BackofficeRootDirectory):
get_response().filter['admin_for_all'] = True
return
- if get_publisher().user_class.count() > 0:
+ if get_publisher().user_class.exists():
user = req.user
if not user:
raise errors.AccessUnauthorizedError(
diff --git a/wcs/formdef.py b/wcs/formdef.py
index 04245d8a2..ca08b6e7b 100644
--- a/wcs/formdef.py
+++ b/wcs/formdef.py
@@ -466,7 +466,7 @@ class FormDef(StorableObject):
# or if there are not yet any submitted forms (or if site
# is using the SQL storage as internal identifier is not used
# in that mode.
- if self.id is None or get_publisher().is_using_postgresql() or self.data_class().count() == 0:
+ if self.id is None or get_publisher().is_using_postgresql() or not (self.data_class().exists()):
self.internal_identifier = new_internal_identifier
object_only = kwargs.pop('object_only', False)
diff --git a/wcs/qommon/ident/idp.py b/wcs/qommon/ident/idp.py
index 3ef47e12e..95db5e18f 100644
--- a/wcs/qommon/ident/idp.py
+++ b/wcs/qommon/ident/idp.py
@@ -200,7 +200,7 @@ class MethodDirectory(Directory):
user.set_attributes_from_formdata(data)
user.form_data = data
- if get_publisher().user_class.count() == 0:
+ if not (get_publisher().user_class.exists()):
user.is_admin = True
session = get_session()
diff --git a/wcs/qommon/ident/password.py b/wcs/qommon/ident/password.py
index bbb06221d..150229ae5 100644
--- a/wcs/qommon/ident/password.py
+++ b/wcs/qommon/ident/password.py
@@ -592,7 +592,7 @@ class MethodDirectory(Directory):
if identities_cfg.get('email-as-username', False):
user.email = username
- if get_publisher().user_class.count() == 0:
+ if not (get_publisher().user_class.exists()):
user.is_admin = True
user.store()
diff --git a/wcs/qommon/storage.py b/wcs/qommon/storage.py
index 4adba4000..1797d65c7 100644
--- a/wcs/qommon/storage.py
+++ b/wcs/qommon/storage.py
@@ -456,6 +456,10 @@ class StorableObject:
return len(cls.select(clause))
return len(cls.keys())
+ @classmethod
+ def exists(cls, clause=None):
+ return bool(cls.count(clause))
+
@classmethod
def sort_results(cls, objects, order_by):
if not order_by:
diff --git a/wcs/sql.py b/wcs/sql.py
index 6e0b94d14..5ce4c6aa4 100644
--- a/wcs/sql.py
+++ b/wcs/sql.py
@@ -1878,6 +1878,25 @@ class SqlMixin:
cur.close()
return count
+ @classmethod
+ @guard_postgres
+ def exists(cls, clause=None):
+ where_clauses, parameters, func_clause = parse_clause(clause)
+ if func_clause:
+ # fallback to counting the result of a select()
+ return len(cls.select(clause))
+ sql_statement = 'SELECT 1 FROM %s' % cls._table_name
+ if where_clauses:
+ sql_statement += ' WHERE ' + ' AND '.join(where_clauses)
+ sql_statement += ' LIMIT 1'
+ conn, cur = get_connection_and_cursor()
+ cur.execute(sql_statement, parameters)
+ check = cur.fetchone()
+ result = check is not None
+ conn.commit()
+ cur.close()
+ return result
+
@classmethod
@guard_postgres
def get_with_indexed_value(