Skip to content

Commit

Permalink
fixed PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
khoroshevskyi committed Aug 24, 2023
1 parent 7601cdb commit 21f39f0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 28 deletions.
1 change: 0 additions & 1 deletion docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [0.6.0] -- 2023-08-24
- Added date filter to project annotation
- Updated peppy without attmap

## [0.5.5] -- 2023-07-19
- Updated requirements
Expand Down
3 changes: 3 additions & 0 deletions pepdbagent/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@
POSTGRES_DIALECT = "postgresql"

DEFAULT_LIMIT_INFO = 5

SUBMISSION_DATE_KEY = "submission_date"
LAST_UPDATE_DATE_KEY = "last_update_date"
59 changes: 36 additions & 23 deletions pepdbagent/modules/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@
from sqlalchemy.exc import IntegrityError
from sqlalchemy.sql.selectable import Select

from pepdbagent.const import DEFAULT_LIMIT, DEFAULT_OFFSET, DEFAULT_TAG, PKG_NAME
from pepdbagent.const import (
DEFAULT_LIMIT,
DEFAULT_OFFSET,
DEFAULT_TAG,
PKG_NAME,
SUBMISSION_DATE_KEY,
LAST_UPDATE_DATE_KEY,
)
from pepdbagent.db_utils import BaseEngine, Projects
from pepdbagent.exceptions import FilterError, ProjectNotFoundError, RegistryPathError
from pepdbagent.models import AnnotationList, AnnotationModel
Expand Down Expand Up @@ -243,11 +250,9 @@ def _count_projects(
search_str=search_str,
admin_list=admin,
)
if filter_by:
statement = self._add_date_filter(
statement, filter_by, filter_start_date, filter_end_date
)
ff = str(statement)
statement = self._add_date_filter_if_provided(
statement, filter_by, filter_start_date, filter_end_date
)
result = self._pep_db_engine.session_execute(statement).first()

try:
Expand Down Expand Up @@ -310,10 +315,9 @@ def _get_projects(
search_str=search_str,
admin_list=admin,
)
if filter_by:
statement = self._add_date_filter(
statement, filter_by, filter_start_date, filter_end_date
)
statement = self._add_date_filter_if_provided(
statement, filter_by, filter_start_date, filter_end_date
)
statement = self._add_order_by_keyword(statement, by=order_by, desc=order_desc)
statement = statement.limit(limit).offset(offset)

Expand Down Expand Up @@ -355,7 +359,7 @@ def _add_order_by_keyword(
order_by_obj = Projects.last_update_date
elif by == "name":
order_by_obj = Projects.name
elif by == "submission_date":
elif by == SUBMISSION_DATE_KEY:
order_by_obj = Projects.submission_date
else:
_LOGGER.warning(
Expand Down Expand Up @@ -406,7 +410,7 @@ def _add_condition(
return statement

@staticmethod
def _add_date_filter(
def _add_date_filter_if_provided(
statement: Select,
filter_by: Optional[Literal["submission_date", "last_update_date"]],
filter_start_date: Optional[str],
Expand All @@ -422,18 +426,27 @@ def _add_date_filter(
:param filter_end_date: Filter end date. Format: "YYYY:MM:DD". if None: present date will be used
:return: sqlalchemy representation of a SELECT statement with where clause with added filter
"""
start_date = convert_date_string_to_date(filter_start_date)
if filter_end_date:
end_date = convert_date_string_to_date(filter_end_date)
else:
end_date = datetime.now()
if filter_by == "submission_date":
statement = statement.filter(Projects.submission_date.between(start_date, end_date))
elif filter_by == "last_update_date":
statement = statement.filter(Projects.last_update_date.between(start_date, end_date))
if filter_by and filter_start_date:
start_date = convert_date_string_to_date(filter_start_date)
if filter_end_date:
end_date = convert_date_string_to_date(filter_end_date)
else:
end_date = datetime.now()
if filter_by == SUBMISSION_DATE_KEY:
statement = statement.filter(
Projects.submission_date.between(start_date, end_date)
)
elif filter_by == LAST_UPDATE_DATE_KEY:
statement = statement.filter(
Projects.last_update_date.between(start_date, end_date)
)
else:
raise FilterError("Invalid filter_by was provided!")
return statement
else:
raise FilterError("Invalid filter_by was provided!")
return statement
if filter_by:
_LOGGER.warning(f"filter_start_date was not provided, skipping filter...")
return statement

def get_project_number_in_namespace(
self,
Expand Down
5 changes: 1 addition & 4 deletions pepdbagent/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,4 @@ def convert_date_string_to_date(date_string: str) -> datetime.datetime:
:param date_str: date string in format [YYYY/MM/DD]. e.g. 2022/02/22
:return: datetime format
"""
try:
return datetime.datetime.strptime(date_string, "%Y/%m/%d") + datetime.timedelta(days=1)
except Exception:
raise IncorrectDateFormat
return datetime.datetime.strptime(date_string, "%Y/%m/%d") + datetime.timedelta(days=1)

0 comments on commit 21f39f0

Please sign in to comment.