Skip to content

Commit

Permalink
Merge branch 'main' into fishchimp/feature/delete-unavailability
Browse files Browse the repository at this point in the history
  • Loading branch information
TaiHaDev authored Apr 13, 2024
2 parents 67cc08f + 7428461 commit 47447bf
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 64 deletions.
4 changes: 2 additions & 2 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ distlib = "==0.3.8"
filelock = "==3.13.3"
flask-cors = "==4.0.0"
flask-restful = "==0.3.10"
flask = "==3.0.2"
flask = "==3.0.3"
greenlet = "==3.0.3"
ics = "==0.7.2"
itsdangerous = ">=1.1.0"
Expand All @@ -29,7 +29,7 @@ markupsafe = ">=1.1.1"
minizinc = "==0.9.0"
mysqlclient = "==2.2.4"
pipenv = "*"
pycparser = "==2.21"
pycparser = "==2.22"
pyjwt = "==2.8.0"
python-dateutil = "==2.9.0.post0"
python-dotenv = "==1.0.1"
Expand Down
46 changes: 23 additions & 23 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 16 additions & 8 deletions controllers/v2/unavailability/api.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from flask_restful import reqparse, Resource, marshal_with, inputs

from .response_models import volunteer_unavailability_time
from domain import UserType
from domain import UserType, session_scope
from repository.volunteer_unavailability_v2 import EventRepository
from services.jwk import requires_auth, is_user_or_has_role
from controllers.v2.v2_blueprint import v2_api
import logging
from exception import EventNotFoundError, InvalidArgumentError

edit_parser = reqparse.RequestParser()
edit_parser.add_argument("title", type=str)
Expand All @@ -23,14 +24,21 @@ def __init__(self, event_repository: EventRepository = EventRepository()):
@requires_auth
@is_user_or_has_role(None, UserType.ROOT_ADMIN)
def put(self, user_id, event_id):
args = edit_parser.parse_args()
success = self.event_repository.edit_event(user_id, event_id, **args)
if success is True:
try:
with session_scope() as session:
args = edit_parser.parse_args()
self.event_repository.edit_event(session, user_id, event_id, **args)
return {"message": "Updated successfully"}, 200
elif success is False:
return {"message": "Event not found"}, 404
else:
return {"message": "Unexpected Error Occurred"}, 400
except InvalidArgumentError as argumentException:
logging.warning(argumentException)
return {"message": "Invalid argument from the payload"}, 400
except EventNotFoundError as notFoundError:
logging.warning(notFoundError)
return {"message": f"Event {event_id} can not be found"}, 404
except Exception as ex:
logging.error(ex)
return {"message": "Unexpected error happened within the database"}, 500


@requires_auth
@is_user_or_has_role(None, UserType.ROOT_ADMIN)
Expand Down
2 changes: 2 additions & 0 deletions exception/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .client_exception import EventNotFoundError, InvalidArgumentError

20 changes: 20 additions & 0 deletions exception/client_exception.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from .fireapp_exception import FireAppException


class EventNotFoundError(FireAppException):
def __init__(self, event_id, *args):
super().__init__(f"Event not found", *args)
self.event_id = event_id

def __str__(self):
# Optionally customize the string representation for this specific error
return f"{self.message}: Event ID {self.event_id} could not be located."


class InvalidArgumentError(FireAppException):
def __init__(self, *args):
super().__init__(f"Invalid argument(s)", *args)

def __str__(self):
# Optionally customize the string representation for this specific error
return f"{self.message}: unexpected values in the payload"
Empty file added exception/database_exception.py
Empty file.
3 changes: 3 additions & 0 deletions exception/fireapp_exception.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class FireAppException(Exception):
pass

49 changes: 24 additions & 25 deletions repository/volunteer_unavailability_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,36 @@

from flask import jsonify

from datetime import datetime

from datetime import datetime, timezone
from exception import EventNotFoundError, InvalidArgumentError
from domain import UnavailabilityTime, session_scope



class EventRepository:
def __init__(self):
pass

def edit_event(self, userId, eventId, title=None, start=None, end=None, periodicity=None):
with session_scope() as session:
try:
event = session.query(UnavailabilityTime).filter(UnavailabilityTime.eventId == eventId,
UnavailabilityTime.userId == userId).first()
if event is None:
return False
if title is not None:
event.title = title
if start is not None:
event.start = start
if end is not None:
event.end = end
if end is not None:
event.periodicity = periodicity
session.commit()
return True
except Exception as e:
session.rollback()
logging.error(e)
return None
def edit_event(self, session, userId, eventId, title=None, start=None, end=None, periodicity=None):
now = datetime.now(timezone.utc)
event = session.query(UnavailabilityTime).filter(UnavailabilityTime.eventId == eventId,
UnavailabilityTime.userId == userId).first()
if event is None:
raise EventNotFoundError(eventId)
# validate user input
if start is not None and start < now:
raise InvalidArgumentError()
if end is not None and (end < now or end < start):
raise InvalidArgumentError()
# Edit fields with new values
if title is not None:
event.title = title
if start is not None:
event.start = start
if end is not None:
event.end = end
if end is not None:
event.periodicity = periodicity
session.commit()

def get_event(self, userId):
"""
Expand Down Expand Up @@ -121,4 +120,4 @@ def check_overlapping_events(self, userId, startTime, endTime, periodicity):
# Add any other attributes you need
})

return overlapping_details
return overlapping_details
12 changes: 6 additions & 6 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ click==8.1.7; python_version >= '3.7'
distlib==0.3.8
exceptiongroup==1.2.0; python_version < '3.11'
filelock==3.13.3; python_version >= '3.8'
flask==3.0.2; python_version >= '3.8'
flask==3.0.3; python_version >= '3.8'
flask-cors==4.0.0
flask-restful==0.3.10
greenlet==3.0.3; python_version >= '3.7'
ics==0.7.2
importlib-metadata==7.0.1; python_version < '3.9'
importlib-metadata==7.1.0; python_version < '3.9'
importlib-resources==6.1.1; python_version < '3.9'
iniconfig==2.0.0; python_version >= '3.7'
itsdangerous==2.1.2; python_version >= '3.7'
Expand All @@ -30,11 +30,11 @@ markupsafe==2.1.5; python_version >= '3.7'
minizinc==0.9.0; python_version >= '3.7' and python_version < '4.0'
mysqlclient==2.2.4; python_version >= '3.8'
numpy==1.24.4; python_version >= '3.8'
packaging==23.2; python_version >= '3.7'
packaging==24.0; python_version >= '3.7'
pipenv==2023.12.1; python_version >= '3.8'
platformdirs==4.2.0; python_version >= '3.8'
pluggy==1.4.0; python_version >= '3.8'
pycparser==2.21; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'
pycparser==2.22; python_version >= '3.8'
pyjwt==2.8.0; python_version >= '3.7'
pytest==8.1.1; python_version >= '3.8'
python-dateutil==2.9.0.post0; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'
Expand All @@ -55,5 +55,5 @@ typing-extensions==4.10.0; python_version >= '3.8'
urllib3==1.26.18; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4, 3.5'
virtualenv==20.25.1; python_version >= '3.7'
virtualenv-clone==0.5.7; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'
werkzeug==3.0.1; python_version >= '3.8'
zipp==3.17.0; python_version >= '3.8'
werkzeug==3.0.2; python_version >= '3.8'
zipp==3.18.1; python_version >= '3.8'

0 comments on commit 47447bf

Please sign in to comment.