diff --git a/server/bin/server.py b/server/bin/server.py index fa80adc..0daf750 100755 --- a/server/bin/server.py +++ b/server/bin/server.py @@ -93,20 +93,21 @@ def main(): scheduler.make() app_args = { - 'scheduler' : scheduler, 'postgres_url': postgres_url, 'logger': logger, } + app_args_with_scheduler = app_args.copy() + app_args_with_scheduler.update({'scheduler' : scheduler}) + application = tornado.web.Application([ - (r'/hf/?', handler.HuntflowWebhookHandler, app_args), - (r'/token', handler.TokenObtainPairHandler, {'postgres_url': postgres_url}), - (r'/token/refresh', handler.TokenRefreshHandler), - (r'/manage/list', handler.ListCandidatesHandler, {'postgres_url': postgres_url}), - (r'/manage/delete', handler.DeleteInterviewHandler, {'scheduler' : scheduler, - 'postgres_url': postgres_url}), - (r'/manage/fwd_list', handler.ListCandidatesWithFwdHandler, {'postgres_url': postgres_url}), - (r'/manage/fwd', handler.ShowFwdHandler, {'postgres_url': postgres_url}) + (r'/hf/?', handler.HuntflowWebhookHandler, app_args_with_scheduler), + (r'/token', handler.TokenObtainPairHandler, app_args), + (r'/token/refresh', handler.TokenRefreshHandler, {'logger': logger}), + (r'/manage/list', handler.ListCandidatesHandler, app_args), + (r'/manage/delete', handler.DeleteInterviewHandler, app_args_with_scheduler), + (r'/manage/fwd_list', handler.ListCandidatesWithFwdHandler, app_args), + (r'/manage/fwd', handler.ShowFwdHandler, app_args) ]) application.listen(options.port) diff --git a/server/huntflow_reloaded/handler.py b/server/huntflow_reloaded/handler.py index 0cbf81a..bc513be 100644 --- a/server/huntflow_reloaded/handler.py +++ b/server/huntflow_reloaded/handler.py @@ -183,8 +183,10 @@ async def status_type_handler(self): self._form_valid_basic_attrs() if self.event.get('calendar_event'): + self._logger.info('Handling interview request') await self.handle_calendar_event() elif self.event.get('employment_date'): + self._logger.info('Handling first working day request') await self.handle_employment_date() else: raise IncompleteRequest @@ -207,9 +209,14 @@ async def handle_calendar_event(self): # pylint: disable=too-many-locals message_type = 'interview' _id = self.basic_attrs['_id'] - candidate = await models.Candidate.query.where(models.Candidate.id == _id).gino.all() + candidate = await models.Candidate.query.where(models.Candidate.id == _id).gino.first() if not candidate: + self._logger.info('Setting interview for {} {}'.format( + self.basic_attrs['first_name'], + self.basic_attrs['last_name'] + )) + await models.Candidate.create( id=self.basic_attrs['_id'], first_name=self.basic_attrs['first_name'], @@ -222,6 +229,9 @@ async def handle_calendar_event(self): # pylint: disable=too-many-locals .gino.first() if interview: + self._logger.info('Rescheduling interview for {} {}'.format( + candidate.last_name, candidate.last_name)) + message_type = 'rescheduled-interview' if interview.jobs: @@ -321,10 +331,14 @@ def __init__(self, application, request, **kwargs): self.user = None self.valid = False - def initialize(self, postgres_url): # pylint: disable=arguments-differ + + def initialize(self, postgres_url, logger): # pylint: disable=arguments-differ self._postgres_url = postgres_url + self._logger = logger async def post(self): # pylint: disable=arguments-differ + self._logger.info('Generating token pair') + body = self.request.body.decode('utf8') try: @@ -340,6 +354,8 @@ async def post(self): # pylint: disable=arguments-differ email = user['email'] password = user['password'] except KeyError: + self._logger.debug(body) + self.write('Incomplete request') self.set_status(500) return @@ -358,6 +374,8 @@ async def post(self): # pylint: disable=arguments-differ } self.set_status(400) + self._logger.debug(body) + self.write(data) async def validate(self, email, password): @@ -376,7 +394,12 @@ async def validate(self, email, password): class TokenRefreshHandler(RequestHandler): # pylint: disable=abstract-method """Class implementing handler for refreshing tokens pair request.""" + def initialize(self, logger): # pylint: disable=arguments-differ + self._logger = logger + async def post(self): # pylint: disable=arguments-differ + self._logger.info('Refreshing token pair') + try: refresh = RefreshToken(self.get_argument('refresh')) data = {'access': str(refresh.access_token())} @@ -396,6 +419,10 @@ async def post(self): # pylint: disable=arguments-differ class ManageHandler(RequestHandler): # pylint: disable=abstract-method, """Class implementing common methods for handling /manage endpoint. """ + def initialize(self, postgres_url, logger): # pylint: disable=arguments-differ + self._postgres_url = postgres_url + self._logger = logger + async def _connect_to_database(self): """Connecting to ORM. """ @@ -436,15 +463,20 @@ def __init__(self, application, request, **kwargs): self.token = None self._decoded_body = {} - def initialize(self, postgres_url, scheduler): # pylint: disable=arguments-differ - self._postgres_url = postgres_url + def initialize(self, postgres_url, scheduler, logger): # pylint: disable=arguments-differ + super(DeleteInterviewHandler, self).initialize(postgres_url, logger) self._scheduler = scheduler async def post(self): # pylint: disable=arguments-differ + self._logger.info('Handling "delete interview" request') + if not self.current_user: + self._logger.info('Authorization failed') return body = self.request.body.decode('utf8') + self._logger.info(body) + try: _decoded_body = json_decode(body) except json.decoder.JSONDecodeError: @@ -503,11 +535,9 @@ class ListCandidatesHandler(ManageHandler): # pylint: disable=abstract-method which have non-expired interview. """ - def initialize(self, postgres_url): # pylint: disable=arguments-differ - self._postgres_url = postgres_url - - async def get(self): # pylint: disable=arguments-differ + self._logger.info('Handling "list candidate with interview" request') + if not self.current_user: return @@ -540,10 +570,9 @@ class ListCandidatesWithFwdHandler(ManageHandler): # pylint: disable=abstract-m which have first working day attribute. """ - def initialize(self, postgres_url): # pylint: disable=arguments-differ - self._postgres_url = postgres_url - async def get(self): # pylint: disable=arguments-differ + self._logger.info('Handling "list candidates with fwd" request') + if not self.current_user: return @@ -575,10 +604,9 @@ class ShowFwdHandler(ManageHandler): # pylint: disable=abstract-method first working day for the specified candidate. """ - def initialize(self, postgres_url): # pylint: disable=arguments-differ - self._postgres_url = postgres_url - async def get(self): # pylint: disable=arguments-differ + self._logger.info('Handling "get fwd for specified candidate" request') + if not self.current_user: return diff --git a/server/test/runtests.py b/server/test/runtests.py index 03528f4..d4397ce 100755 --- a/server/test/runtests.py +++ b/server/test/runtests.py @@ -292,21 +292,18 @@ def get_handlers(self): app_args = { 'postgres_url': POSTGRES_URL, - 'scheduler': self.test_scheduler, 'logger': LOGGER } - db_args = {'postgres_url': POSTGRES_URL} + app_args_with_scheduler = app_args.copy() + app_args_with_scheduler.update({'scheduler': self.test_scheduler}) return [ - ('/hf', handler.HuntflowWebhookHandler, app_args), - (r'/token', handler.TokenObtainPairHandler, db_args), - (r'/token/refresh', handler.TokenRefreshHandler), - (r'/manage/list/', handler.ListCandidatesHandler, db_args), - (r'/manage/delete/', handler.DeleteInterviewHandler, { - 'scheduler' : self.test_scheduler, - 'postgres_url': POSTGRES_URL - }), + ('/hf', handler.HuntflowWebhookHandler, app_args_with_scheduler), + (r'/token', handler.TokenObtainPairHandler, app_args), + (r'/token/refresh', handler.TokenRefreshHandler, {'logger': LOGGER}), + (r'/manage/list/', handler.ListCandidatesHandler, app_args), + (r'/manage/delete/', handler.DeleteInterviewHandler, app_args_with_scheduler), ] def get_tokens(self): @@ -529,18 +526,18 @@ def get_handlers(self): app_args = { 'postgres_url': POSTGRES_URL, - 'scheduler': self.test_scheduler, 'logger': LOGGER } - db_args = {'postgres_url': POSTGRES_URL} + app_args_with_scheduler = app_args.copy() + app_args_with_scheduler.update({'scheduler': self.test_scheduler}) return [ - ('/hf', handler.HuntflowWebhookHandler, app_args), - (r'/token', handler.TokenObtainPairHandler, db_args), - (r'/token/refresh', handler.TokenRefreshHandler), - (r'/manage/fwd_list/', handler.ListCandidatesWithFwdHandler, db_args), - (r'/manage/fwd/', handler.ShowFwdHandler, db_args), + ('/hf', handler.HuntflowWebhookHandler, app_args_with_scheduler), + (r'/token', handler.TokenObtainPairHandler, app_args), + (r'/token/refresh', handler.TokenRefreshHandler, {'logger': LOGGER}), + (r'/manage/fwd_list/', handler.ListCandidatesWithFwdHandler, app_args), + (r'/manage/fwd/', handler.ShowFwdHandler, app_args), ] def get_tokens(self):