Skip to content

Commit

Permalink
Add exists() query
Browse files Browse the repository at this point in the history
  • Loading branch information
AliRn76 committed Jul 26, 2024
1 parent bafa6b0 commit afd2a45
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion panther/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def print_info(config: Config):
if config.HAS_WS:
try:
import gunicorn

Check notice on line 144 in panther/cli/utils.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

Unsatisfied package requirements

Package containing module 'gunicorn' is not listed in the project requirements
gunicorn_msg = f'{h} * You have WS so make sure to run gunicorn with --preload{h}\n'
gunicorn_msg = f'{h} * You have WS, so make sure to run gunicorn with --preload{h}\n'
except ImportError:
pass

Expand Down
20 changes: 20 additions & 0 deletions panther/db/queries/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,26 @@ async def find_one_or_raise(cls, _filter: dict | None = None, /, **kwargs) -> Se

raise NotFoundAPIError(detail=f'{cls.__name__} Does Not Exist')

@classmethod
async def exists(cls, _filter: dict | None = None, /, **kwargs) -> bool:
"""
Check if document exists in collection or not
Example:
-------
>>> from app.models import User
>>> await User.exists(age=18, name='Ali')
or
>>> await User.exists({'age': 18, 'name': 'Ali'})
or
>>> await User.exists({'age': 18}, name='Ali')
"""
if await cls.count(_filter, **kwargs) > 0:
return True
else:
return False

async def save(self) -> None:
"""
Save the document
Expand Down
14 changes: 14 additions & 0 deletions panther/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ def generate_secret_key() -> str:


def round_datetime(dt: datetime, delta: timedelta):
"""
Example:
>>> round_datetime(datetime(2024, 7, 15, 13, 22, 11, 562159), timedelta(days=2))
datetime.datetime(2024, 7, 16, 0, 0)
>>> round_datetime(datetime(2024, 7, 16, 13, 22, 11, 562159), timedelta(days=2))
datetime.datetime(2024, 7, 16, 0, 0)
>>> round_datetime(datetime(2024, 7, 17, 13, 22, 11, 562159), timedelta(days=2))
datetime.datetime(2024, 7, 18, 0, 0)
>>> round_datetime(datetime(2024, 7, 18, 13, 22, 11, 562159), timedelta(days=2))
datetime.datetime(2024, 7, 18, 0, 0)
"""
return datetime.min + round((dt - datetime.min) / delta) * delta


Expand Down
6 changes: 6 additions & 0 deletions panther/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ async def send(self, data: any = None):
"""
return await super().send(data=data)

async def close(self, code: int = status.WS_1000_NORMAL_CLOSURE, reason: str = ''):
"""
Called whenever server or client, wants to close the connection
"""
return await super().close(code=code, reason=reason)


async def send_message_to_websocket(connection_id: str, data: any):
await config.WEBSOCKET_CONNECTIONS.publish(connection_id=connection_id, action='send', data=data)
Expand Down

0 comments on commit afd2a45

Please sign in to comment.