Skip to content

Commit

Permalink
[QCDP24-32] implemented state filter
Browse files Browse the repository at this point in the history
  • Loading branch information
awset committed Sep 10, 2024
1 parent 67816a4 commit 47c9b38
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
7 changes: 5 additions & 2 deletions ckanext/datarequests/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
except ImportError:
from cgi import escape

from ckan import authz
from ckan import authz, model
from ckan.lib import mailer
from ckan.lib.redis import connect_to_redis
from ckan.plugins import toolkit as tk
Expand Down Expand Up @@ -537,6 +537,9 @@ def list_datarequests(context, data_dict):
# Filter by status
status = data_dict.get('status', None)

# Filter by state
state = data_dict.get('state', None)

# Free text filter
q = data_dict.get('q', None)

Expand All @@ -550,7 +553,7 @@ def list_datarequests(context, data_dict):
# Call the function
db_datarequests = db.DataRequest.get_ordered_by_date(requesting_organisation=requesting_organisation,
user_id=user_id, status=status,
q=q, desc=desc)
q=q, desc=desc, state=state)

# Dictize the results
datarequests = []
Expand Down
10 changes: 8 additions & 2 deletions ckanext/datarequests/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,24 @@ class DataRequest(model.core.StatefulObjectMixin, model.DomainObject):
def get(cls, **kw):
'''Finds all the instances required.'''
query = model.Session.query(cls).autoflush(False)
query = query.filter(or_(cls.state == model.core.State.ACTIVE, cls.state == None))
return query.filter_by(**kw).all()

@classmethod
def datarequest_exists(cls, title):
'''Returns true if there is a Data Request with the same title (case insensitive)'''
query = model.Session.query(cls).autoflush(False)
query = query.filter(or_(cls.state == model.core.State.ACTIVE, cls.state == None))
return query.filter(func.lower(cls.title) == func.lower(title)).first() is not None

@classmethod
def get_ordered_by_date(cls, requesting_organisation=None, user_id=None, closed=None, q=None, desc=False, status=None):
def get_ordered_by_date(cls, requesting_organisation=None, user_id=None, closed=None, q=None, desc=False, status=None, state=None):
'''Personalized query'''
query = model.Session.query(cls).autoflush(False)
if state is None:
query = query.filter(or_(cls.state == model.core.State.ACTIVE, cls.state == None))
else:
query = query.filter_by(state=state)

params = {}

Expand Down Expand Up @@ -121,7 +127,7 @@ def get_ordered_by_date(cls, requesting_organisation=None, user_id=None, closed=
@classmethod
def get_open_datarequests_number(cls):
'''Returns the number of data requests that are open'''
return model.Session.query(func.count(cls.id)).filter_by(closed=False).scalar()
return model.Session.query(func.count(cls.id)).filter_by(closed=False).filter(or_(cls.state == model.core.State.ACTIVE, cls.state == None)).scalar()


class Comment(model.DomainObject):
Expand Down

0 comments on commit 47c9b38

Please sign in to comment.