Пример #1
0
    def process(self, data):
        """ denied by last_action_user_id
        send mail to user
        """
        req = Request.by_id(self.session, data['req_id'])

        # retrieve user who performed last action
        action_user = User.by_id(self.session, req.last_action_user_id)
        # send mail to user
        src = action_user.email
        dst = req.user.email
        content = """You request has been refused for the following reason: %s
Request details: %s""" % (req.reason, req.summarymail)
        try:
            self.send_mail(sender=src, target=dst, request=req,
                           content=content)

            # update request status after sending email
            req.notified = True
        except Exception as err:
            self.log.exception('Error while sending mail')
            req.flag_error(str(err))

        self.session.flush()
        transaction.commit()
Пример #2
0
    def process(self, data):
        """ submitted by user
        send mail to manager
        """
        req = Request.by_id(self.session, data['req_id'])
        # send mail to manager
        src = req.user.email
        dst = req.user.manager_mail
        if 'reminder' in data:
            content = """A request from %s is still waiting your approval
Request details: %s""" % (req.user.name, req.summarymail)
        else:
            content = """New request from %s
Request details: %s""" % (req.user.name, req.summarymail)
        try:
            self.send_mail(sender=src, target=dst, request=req,
                           content=content)
            # update request status after sending email
            req.notified = True
        except Exception as err:
            self.log.exception('Error while sending mail')
            req.flag_error(str(err), self.session)

        self.session.flush()
        transaction.commit()
Пример #3
0
 def test_summarycsv_message(self):
     from pyvac.models import Request
     req = Request.by_id(self.session, 14)
     self.assertIsInstance(req, Request)
     msg = (u"Doe,Jane,13/06/2016,13/06/2016,1.0,Exceptionnel,,"
            "I need to see Star Wars, I'm a huge fan")
     self.assertEqual(req.summarycsv, msg)
Пример #4
0
    def render(self):

        req_id = self.request.params.get('request_id')
        req = Request.by_id(self.session, req_id)
        if not req:
            return ''

        data = {'req_id': req.id}

        if self.user.is_admin:
            req.update_status('APPROVED_ADMIN')
            # save who performed this action
            req.last_action_user_id = self.user.id

            task_name = 'worker_approved'
            settings = self.request.registry.settings
            with open(settings['pyvac.celery.yaml']) as fdesc:
                Conf = yaml.load(fdesc, YAMLLoader)
            data['caldav.url'] = Conf.get('caldav').get('url')
        else:
            req.update_status('ACCEPTED_MANAGER')
            # save who performed this action
            req.last_action_user_id = self.user.id
            task_name = 'worker_accepted'

        self.session.flush()

        # call celery task directly, do not wait for polling
        from celery.registry import tasks
        from celery.task import subtask
        req_task = tasks[task_name]

        subtask(req_task).delay(data=data)

        return req.status
Пример #5
0
    def process(self, data):
        """ accepted by manager
        send mail to user
        send mail to HR
        """
        req = Request.by_id(self.session, data['req_id'])
        # send mail to user
        src = req.user.manager_mail
        dst = req.user.email
        content = """Your request has been accepted by %s. Waiting for HR validation.
Request details: %s""" % (req.user.manager_name, req.summarymail)
        try:
            self.send_mail(sender=src, target=dst, request=req,
                           content=content)

            # send mail to HR
            admin = req.user.get_admin(self.session)
            dst = self.get_admin_mail(admin)
            content = """Manager %s has accepted a new request. Waiting for your validation.
Request details: %s""" % (req.user.manager_name, req.summarymail)
            self.send_mail(sender=src, target=dst, request=req,
                           content=content)

            # update request status after sending email
            req.notified = True
        except Exception as err:
            self.log.exception('Error while sending mail')
            req.flag_error(str(err), self.session)

        self.session.flush()
        transaction.commit()
Пример #6
0
    def test_summarycsv_label(self):
        from pyvac.models import Request

        req = Request.by_id(self.session, 6)
        self.assertIsInstance(req, Request)
        msg = u"Doe,John,24/08/2011,24/08/2011,0.5,RTT,AM"
        self.assertEqual(req.summarycsv, msg)
Пример #7
0
def replay(settings):

    with open(settings['pyvac.celery.yaml']) as fdesc:
        Conf = yaml.load(fdesc, YAMLLoader)
    caldav_url = Conf.get('caldav').get('url')

    # XXX Register the database
    create_engine(settings, scoped=True)
    session = DBSession()

    calendar = get_calendar(caldav_url)
    requests = Request.find(session,
                            where=(Request.status == 'APPROVED_ADMIN',),
                            order_by=Request.user_id)
    print 'total requests', len(requests)
    print ''

    req_to_add = []

    # for each requests
    for req in requests:
        print '-' * 10
        print req.id, req.summarycal, req.date_from, req.date_to
        # check if entry in caldav exists
        results = calendar.date_search(req.date_from, req.date_to)
        if not results:
            # need to add missing entry in caldav
            print 'need to insert request'
            req_to_add.append(req.id)
        else:
            summaries = []
            for event in results:
                try:
                    parse_event(event)
                except Exception:
                    continue
                event.load()
                # XXX: if needed to delete entries
                # uid = event.instance.vevent.uid.value
                # ics = '%s/%s.ics' % (caldav_url, uid)
                # print delFromCal(caldav_url, ics)
                summary = event.instance.vevent.summary.value
                summaries.append(summary)
            if req.summarycal not in summaries:
                print 'need to insert request'
                req_to_add.append(req.id)

    for req_id in set(req_to_add):
        req = Request.by_id(session, req_id)
        print 'processing', req.id, req.summarycal, req.date_from, req.date_to
        ics_url = addToCal(caldav_url,
                           req.date_from,
                           req.date_to,
                           req.summarycal)
        # save ics url in request
        req.ics_url = ics_url
        session.add(req)

    session.flush()
    transaction.commit()
Пример #8
0
    def process(self, data):
        """ accepted by manager
        auto flag as accepted by HR
        """
        req = Request.by_id(self.session, data['req_id'])
        # after new field was added, it may not be set yet
        if not req.date_updated:
            return

        delta = datetime.now() - req.date_updated
        # after Request.date_updated + 3 days, auto accept it by HR
        if delta.days >= 3:
            # auto accept it as HR
            self.log.info('3 days passed, auto accept it by HR')

            # create history entry
            msg = 'Automatically accepted by HR after 3 days passed'
            # use error_message field, as it should not be used here
            # if it fails in ERROR it should be overwritten anyway
            # as the status will be changed from APPROVED_ADMIN to ERROR
            RequestHistory.new(self.session, req,
                               req.status, 'APPROVED_ADMIN',
                               user=None, error_message=msg)
            # update request status after sending email
            req.update_status('APPROVED_ADMIN')
            self.session.flush()
            transaction.commit()

            data['autoaccept'] = True
            async_result = subtask(WorkerApproved).delay(data=data)
            self.log.info('task scheduled %r' % async_result)
Пример #9
0
    def render(self):

        req_id = self.request.params.get('request_id')
        req = Request.by_id(self.session, req_id)
        if not req:
            return ''

        # check if request have already been consumed
        if not self.user.is_admin:
            today = datetime.now()
            if req.date_from <= today:
                log.error('User %s tried to CANCEL consumed request %d.' %
                          (self.user.login, req.id))
                return req.status

        # delete from calendar
        if req.status == 'APPROVED_ADMIN' and req.ics_url:
            settings = self.request.registry.settings
            with open(settings['pyvac.celery.yaml']) as fdesc:
                Conf = yaml.load(fdesc, YAMLLoader)
            caldav_url = Conf.get('caldav').get('url')
            delFromCal(caldav_url, req.ics_url)

        RequestHistory.new(self.session, req, req.status, 'CANCELED',
                           self.user)
        req.update_status('CANCELED')
        # save who performed this action
        req.last_action_user_id = self.user.id

        self.session.flush()
        return req.status
Пример #10
0
    def test_summarycsv(self):
        from pyvac.models import Request

        req = Request.by_id(self.session, 1)
        self.assertIsInstance(req, Request)
        msg = u"Doe,John,10/04/2015,14/04/2015,5.0,CP,"
        self.assertEqual(req.summarycsv, msg)
Пример #11
0
    def process(self, data):
        """ denied by last_action_user_id
        send mail to user
        """
        req = Request.by_id(self.session, data['req_id'])

        # retrieve user who performed last action
        action_user = User.by_id(self.session, req.last_action_user_id)
        # send mail to user
        src = action_user.email
        dst = req.user.email
        content = """Your request has been refused for the following reason: %s
Request details: %s""" % (req.reason, req.summarymail)
        try:
            self.send_mail(sender=src, target=dst, request=req,
                           content=content)

            # update request status after sending email
            req.notified = True
        except Exception as err:
            self.log.exception('Error while sending mail')
            req.flag_error(str(err), self.session)

        self.session.flush()
        transaction.commit()
Пример #12
0
    def render(self):

        req_id = self.request.params.get('request_id')
        req = Request.by_id(self.session, req_id)
        if not req:
            return ''
        reason = self.request.params.get('reason')

        req.reason = reason
        RequestHistory.new(self.session,
                           req,
                           req.status,
                           'DENIED',
                           self.user,
                           reason=reason)
        req.update_status('DENIED')
        # save who performed this action
        req.last_action_user_id = self.user.id

        self.session.flush()

        # call celery task directly, do not wait for polling
        from celery.registry import tasks
        from celery.task import subtask
        req_task = tasks['worker_denied']
        data = {'req_id': req.id}
        subtask(req_task).apply_async(kwargs={'data': data}, countdown=5)

        log.info('scheduling task worker_denied for %s' % data)

        return req.status
Пример #13
0
 def test_summarycsv_message(self):
     from pyvac.models import Request
     req = Request.by_id(self.session, 14)
     self.assertIsInstance(req, Request)
     msg = (u",Doe,Jane,13/06/2016,13/06/2016,1.0,Exceptionnel,,"
            "I need to see Star Wars, I'm a huge fan")
     self.assertEqual(req.summarycsv, msg)
Пример #14
0
    def process(self, data):
        """ accepted by manager
        send mail to user
        send mail to HR
        """
        req = Request.by_id(self.session, data['req_id'])
        # send mail to user
        src = req.user.manager_mail
        dst = req.user.email
        content = """Your request has been accepted by %s. Waiting for HR validation.
Request details: %s""" % (req.user.manager_name, req.summarymail)
        try:
            self.send_mail(sender=src, target=dst, request=req,
                           content=content)

            # send mail to HR
            admin = req.user.get_admin(self.session)
            dst = self.get_admin_mail(admin)
            content = """Manager %s has accepted a new request. Waiting for your validation.
Request details: %s""" % (req.user.manager_name, req.summarymail)
            self.send_mail(sender=src, target=dst, request=req,
                           content=content)

            # update request status after sending email
            req.notified = True
        except Exception as err:
            self.log.exception('Error while sending mail')
            req.flag_error(str(err))

        self.session.flush()
        transaction.commit()
Пример #15
0
 def test_in_conflict_manager(self):
     from pyvac.models import Request
     req = Request.by_id(self.session, 1)
     self.assertIsInstance(req, Request)
     nb_conflicts = Request.in_conflict_manager(self.session, req,
                                                count=True)
     self.assertEqual(nb_conflicts, 1)
Пример #16
0
    def process(self, data):
        """ submitted by user
        send mail to manager
        """
        req = Request.by_id(self.session, data['req_id'])
        # send mail to manager
        src = req.user.email
        dst = req.user.manager_mail
        if 'reminder' in data:
            content = """A request from %s is still waiting your approval
Request details: %s""" % (req.user.name, req.summarymail)
        else:
            content = """New request from %s
Request details: %s""" % (req.user.name, req.summarymail)
        try:
            self.send_mail(sender=src, target=dst, request=req,
                           content=content)
            # update request status after sending email
            req.notified = True
        except Exception as err:
            self.log.exception('Error while sending mail')
            req.flag_error(str(err))

        self.session.flush()
        transaction.commit()
Пример #17
0
 def test_in_conflict_manager(self):
     from pyvac.models import Request
     req = Request.by_id(self.session, 1)
     self.assertIsInstance(req, Request)
     nb_conflicts = Request.in_conflict_manager(self.session, req,
                                                count=True)
     self.assertEqual(nb_conflicts, 1)
Пример #18
0
    def render(self):

        req_id = self.request.params.get('request_id')
        req = Request.by_id(self.session, req_id)
        if not req:
            return ''

        # check if request have already been consumed
        if not self.user.is_admin:
            today = datetime.now()
            if req.date_from <= today:
                log.error('User %s tried to CANCEL consumed request %d.' %
                          (self.user.login, req.id))
                return req.status

        # delete from calendar
        if req.status == 'APPROVED_ADMIN' and req.ics_url:
            settings = self.request.registry.settings
            with open(settings['pyvac.celery.yaml']) as fdesc:
                Conf = yaml.load(fdesc, YAMLLoader)
            caldav_url = Conf.get('caldav').get('url')
            delFromCal(caldav_url, req.ics_url)

        req.update_status('CANCELED')
        # save who performed this action
        req.last_action_user_id = self.user.id

        self.session.flush()
        return req.status
Пример #19
0
    def process(self, data):
        """ approved by HR
        send mail to user
        send mail to manager
        """
        req = Request.by_id(self.session, data['req_id'])

        admin = req.user.get_admin(self.session)
        # send mail to user
        src = self.get_admin_mail(admin)
        dst = req.user.email
        if 'autoaccept' in data:
            content = """Your request was automatically approved, it has been added to calendar.
Request details: %s""" % req.summarymail
        else:
            content = """HR has accepted your request, it has been added to calendar.
Request details: %s""" % req.summarymail
        try:
            self.send_mail(sender=src, target=dst, request=req, content=content)

            # send mail to manager
            src = self.get_admin_mail(admin)
            dst = req.user.manager_mail
            if 'autoaccept' in data:
                content = """A request you accepted was automatically approved, it has been added to calendar.
Request details: %s""" % req.summarymail
            else:
                content = """HR has approved a request you accepted, it has been added to calendar.
Request details: %s""" % req.summarymail
            self.send_mail(sender=src, target=dst, request=req, content=content)

            # update request status after sending email
            req.notified = True
        except Exception as err:
            log.exception('Error while sending mail')
            req.flag_error(str(err))

        try:
            if 'caldav.url' in data:
                caldav_url = data['caldav.url']
            else:
                conf_file = sys.argv[1]
                with open(conf_file) as fdesc:
                    Conf = yaml.load(fdesc, YAMLLoader)
                caldav_url = Conf.get('caldav').get('url')

            # add new entry in caldav
            addToCal(caldav_url,
                     req.date_from,
                     req.date_to,
                     req.summarycal)
        except Exception as err:
            log.exception('Error while adding to calendar')
            req.flag_error(str(err))

        self.session.flush()
        transaction.commit()
Пример #20
0
    def render(self):

        req_id = self.request.params.get('request_id')
        req = Request.by_id(self.session, req_id)
        if not req:
            return ''

        req.update_status('CANCELED')
        self.session.flush()
        return req.status
Пример #21
0
def replay(settings):

    with open(settings["pyvac.celery.yaml"]) as fdesc:
        Conf = yaml.load(fdesc, YAMLLoader)
    caldav_url = Conf.get("caldav").get("url")

    # XXX Register the database
    create_engine(settings, scoped=True)
    session = DBSession()

    calendar = get_calendar(caldav_url)
    requests = Request.find(session, where=(Request.status == "APPROVED_ADMIN",), order_by=Request.user_id)
    print "total requests", len(requests)
    print ""

    req_to_add = []

    # for each requests
    for req in requests:
        print "-" * 10
        print req.id, req.summarycal, req.date_from, req.date_to
        # check if entry in caldav exists
        results = calendar.date_search(req.date_from, req.date_to)
        if not results:
            # need to add missing entry in caldav
            print "need to insert request"
            req_to_add.append(req.id)
        else:
            summaries = []
            for event in results:
                try:
                    parse_event(event)
                except Exception:
                    continue
                event.load()
                # XXX: if needed to delete entries
                # uid = event.instance.vevent.uid.value
                # ics = '%s/%s.ics' % (caldav_url, uid)
                # print delFromCal(caldav_url, ics)
                summary = event.instance.vevent.summary.value
                summaries.append(summary)
            if req.summarycal not in summaries:
                print "need to insert request"
                req_to_add.append(req.id)

    for req_id in set(req_to_add):
        req = Request.by_id(session, req_id)
        print "processing", req.id, req.summarycal, req.date_from, req.date_to
        ics_url = addToCal(caldav_url, req.date_from, req.date_to, req.summarycal)
        # save ics url in request
        req.ics_url = ics_url
        session.add(req)

    session.flush()
    transaction.commit()
Пример #22
0
    def test_by_user_outdated(self):
        from pyvac.models import User, Request

        user1 = User.by_login(self.session, u"jdoe")
        requests = Request.by_user(self.session, user1)
        self.assertEqual(len(requests), 5)

        outdated = Request.by_id(self.session, 7)
        self.assertIsInstance(outdated, Request)
        self.assertEqual(outdated.user, user1)
        self.assertFalse(outdated in requests)
Пример #23
0
    def render(self):

        req_id = self.request.params.get('request_id')
        req = Request.by_id(self.session, req_id)
        if not req:
            return ''

        data = {'req_id': req.id}

        only_manager = False
        # we should handle the case where the admin is also a user manager
        if (self.user.ldap_user and (req.user.manager_dn == self.user.dn)
                and (req.status == 'PENDING')):
            only_manager = True

        if self.user.is_admin and not only_manager:
            # create history entry
            RequestHistory.new(self.session, req,
                               req.status, 'APPROVED_ADMIN',
                               self.user)
            req.update_status('APPROVED_ADMIN')
            # save who performed this action
            req.last_action_user_id = self.user.id

            task_name = 'worker_approved'
            settings = self.request.registry.settings
            with open(settings['pyvac.celery.yaml']) as fdesc:
                Conf = yaml.load(fdesc, YAMLLoader)
            data['caldav.url'] = Conf.get('caldav').get('url')
        else:
            # create history entry
            RequestHistory.new(self.session, req,
                               req.status, 'ACCEPTED_MANAGER',
                               self.user)
            req.update_status('ACCEPTED_MANAGER')
            # save who performed this action
            req.last_action_user_id = self.user.id

            task_name = 'worker_accepted'

        self.session.flush()

        # call celery task directly, do not wait for polling
        from celery.registry import tasks
        from celery.task import subtask
        req_task = tasks[task_name]

        subtask(req_task).apply_async(kwargs={'data': data}, countdown=5)

        log.info('scheduling task %s for req_id: %d' % (task_name,
                                                        data['req_id']))
        return req.status
Пример #24
0
 def test_set_status_cancel_ko_consumed_during(self):
     self.config.testing_securitypolicy(userid=u'manager3',
                                        permissive=True)
     from pyvac.models import Request
     from pyvac.views.request import Cancel
     req_id = 5
     req = Request.by_id(self.session, req_id)
     self.assertEqual(req.status, u'APPROVED_ADMIN')
     with freeze_time('2015-04-25',
                      ignore=['celery', 'psycopg2', 'sqlalchemy',
                              'icalendar']):
         status = Cancel(self.create_request({'request_id': req_id}))()
         self.assertEqual(status, u'APPROVED_ADMIN')
Пример #25
0
 def test_set_status_cancel_ko_consumed_during(self):
     self.config.testing_securitypolicy(userid=u'manager3',
                                        permissive=True)
     from pyvac.models import Request
     from pyvac.views.request import Cancel
     req_id = 5
     req = Request.by_id(self.session, req_id)
     self.assertEqual(req.status, u'APPROVED_ADMIN')
     with freeze_time('2015-04-25',
                      ignore=['celery', 'psycopg2', 'sqlalchemy',
                              'icalendar']):
         status = Cancel(self.create_request({'request_id': req_id}))()
         self.assertEqual(status, u'APPROVED_ADMIN')
Пример #26
0
    def test_by_user_outdated(self):
        from pyvac.models import User, Request
        user1 = User.by_login(self.session, u'jdoe')
        with freeze_time('2015-08-01',
                         ignore=['celery', 'psycopg2', 'sqlalchemy',
                                 'icalendar']):
            requests = Request.by_user(self.session, user1)
        self.assertEqual(len(requests), 8)

        outdated = Request.by_id(self.session, 7)
        self.assertIsInstance(outdated, Request)
        self.assertEqual(outdated.user, user1)
        self.assertFalse(outdated in requests)
Пример #27
0
    def test_by_user_outdated(self):
        from pyvac.models import User, Request
        user1 = User.by_login(self.session, u'jdoe')
        with freeze_time('2015-08-01',
                         ignore=['celery', 'psycopg2', 'sqlalchemy',
                                 'icalendar']):
            requests = Request.by_user(self.session, user1)
        self.assertEqual(len(requests), 8)

        outdated = Request.by_id(self.session, 7)
        self.assertIsInstance(outdated, Request)
        self.assertEqual(outdated.user, user1)
        self.assertFalse(outdated in requests)
Пример #28
0
 def test_set_status_refuse_manager_ok(self):
     self.config.testing_securitypolicy(userid=u'manager1',
                                        permissive=True)
     from pyvac.models import Request
     from pyvac.views.request import Refuse
     req_id = 1
     req = Request.by_id(self.session, req_id)
     orig_status = req.status
     status = Refuse(self.create_request({'request_id': req_id}))()
     self.assertEqual(status, u'DENIED')
     self.session.commit()
     self.assertEqual(req.status, u'DENIED')
     self.assertEqual(req.notified, False)
     req.update_status(orig_status)
Пример #29
0
 def test_set_status_accept_admin_ok(self):
     self.config.testing_securitypolicy(userid=u'admin',
                                        permissive=True)
     from pyvac.models import Request
     from pyvac.views.request import Accept
     req_id = 1
     req = Request.by_id(self.session, req_id)
     orig_status = req.status
     status = Accept(self.create_request({'request_id': req_id}))()
     self.assertEqual(status, u'APPROVED_ADMIN')
     self.session.commit()
     self.assertEqual(req.status, u'APPROVED_ADMIN')
     self.assertEqual(req.notified, False)
     req.update_status(orig_status)
Пример #30
0
 def test_set_status_refuse_manager_ok(self):
     self.config.testing_securitypolicy(userid=u'manager1',
                                        permissive=True)
     from pyvac.models import Request
     from pyvac.views.request import Refuse
     req_id = 1
     req = Request.by_id(self.session, req_id)
     orig_status = req.status
     status = Refuse(self.create_request({'request_id': req_id}))()
     self.assertEqual(status, u'DENIED')
     self.session.commit()
     self.assertEqual(req.status, u'DENIED')
     self.assertEqual(req.notified, False)
     req.update_status(orig_status)
Пример #31
0
 def test_set_status_accept_admin_ok(self):
     self.config.testing_securitypolicy(userid=u'admin',
                                        permissive=True)
     from pyvac.models import Request
     from pyvac.views.request import Accept
     req_id = 1
     req = Request.by_id(self.session, req_id)
     orig_status = req.status
     status = Accept(self.create_request({'request_id': req_id}))()
     self.assertEqual(status, u'APPROVED_ADMIN')
     self.session.commit()
     self.assertEqual(req.status, u'APPROVED_ADMIN')
     self.assertEqual(req.notified, False)
     req.update_status(orig_status)
Пример #32
0
 def test_set_status_cancel_ok(self):
     self.config.testing_securitypolicy(userid=u'janedoe',
                                        permissive=True)
     from pyvac.models import Request
     from pyvac.views.request import Cancel
     req_id = 1
     req = Request.by_id(self.session, req_id)
     orig_status = req.status
     status = Cancel(self.create_request({'request_id': req_id}))()
     self.assertEqual(status, u'CANCELED')
     self.session.commit()
     self.assertEqual(req.status, u'CANCELED')
     self.assertEqual(req.notified, False)
     req.update_status(orig_status)
Пример #33
0
 def test_set_status_accept_manager_ok(self):
     self.config.testing_securitypolicy(userid='manager1',
                                        permissive=True)
     from pyvac.models import Request
     from pyvac.views.request import Accept
     req_id = 1
     req = Request.by_id(self.session, req_id)
     orig_status = req.status
     status = Accept(self.create_request({'request_id': req_id}))()
     self.assertEqual(status, 'ACCEPTED_MANAGER')
     self.session.commit()
     self.assertEqual(req.status, 'ACCEPTED_MANAGER')
     self.assertEqual(req.notified, False)
     req.update_status(orig_status)
Пример #34
0
 def test_set_status_refuse_admin_reason_ok(self):
     self.config.testing_securitypolicy(userid='admin',
                                        permissive=True)
     from pyvac.models import Request
     from pyvac.views.request import Refuse
     req_id = 1
     req = Request.by_id(self.session, req_id)
     orig_status = req.status
     status = Refuse(self.create_request({'request_id': req_id,
                                          'reason': 'we need you'}))()
     self.assertEqual(status, 'DENIED')
     self.session.commit()
     self.assertEqual(req.status, 'DENIED')
     self.assertEqual(req.notified, False)
     self.assertEqual(req.reason, 'we need you')
     req.update_status(orig_status)
Пример #35
0
 def test_set_status_cancel_ok(self):
     self.config.testing_securitypolicy(userid=u'janedoe',
                                        permissive=True)
     from pyvac.models import Request
     from pyvac.views.request import Cancel
     req_id = 1
     req = Request.by_id(self.session, req_id)
     orig_status = req.status
     with freeze_time('2015-03-01',
                      ignore=['celery', 'psycopg2', 'sqlalchemy',
                              'icalendar']):
         status = Cancel(self.create_request({'request_id': req_id}))()
         self.assertEqual(status, u'CANCELED')
         self.session.commit()
         self.assertEqual(req.status, u'CANCELED')
         self.assertEqual(req.notified, False)
         req.update_status(orig_status)
Пример #36
0
 def test_set_status_cancel_ok(self):
     self.config.testing_securitypolicy(userid=u'janedoe',
                                        permissive=True)
     from pyvac.models import Request
     from pyvac.views.request import Cancel
     req_id = 1
     req = Request.by_id(self.session, req_id)
     orig_status = req.status
     with freeze_time('2015-03-01',
                      ignore=['celery', 'psycopg2', 'sqlalchemy',
                              'icalendar']):
         status = Cancel(self.create_request({'request_id': req_id}))()
         self.assertEqual(status, u'CANCELED')
         self.session.commit()
         self.assertEqual(req.status, u'CANCELED')
         self.assertEqual(req.notified, False)
         req.update_status(orig_status)
Пример #37
0
    def process(self, data):
        """ accepted by manager
        auto flag as accepted by HR
        """
        req = Request.by_id(self.session, data['req_id'])
        delta = (req.created_at + relativedelta(days=3)) - datetime.now()
        # after Request.created_at + 3 days, auto accept it by HR
        if delta.days > 3:
            # auto accept it as HR
            self.log.info('3 days passed, auto accept it by HR')

            # update request status after sending email
            req.update_status('APPROVED_ADMIN')
            self.session.flush()
            transaction.commit()

            data['autoaccept'] = True
            async_result = subtask(WorkerApproved).delay(data=data)
            self.log.info('task scheduled %r' % async_result)
Пример #38
0
    def render(self):

        req_id = self.request.params.get('request_id')
        req = Request.by_id(self.session, req_id)
        if not req:
            return ''

        data = {'req_id': req.id}

        only_manager = False
        # we should handle the case where the admin is also a user manager
        if (self.user.ldap_user and (req.user.manager_dn == self.user.dn)
                and (req.status == 'PENDING')):
            only_manager = True

        if self.user.is_admin and not only_manager:
            req.update_status('APPROVED_ADMIN')
            # save who performed this action
            req.last_action_user_id = self.user.id

            task_name = 'worker_approved'
            settings = self.request.registry.settings
            with open(settings['pyvac.celery.yaml']) as fdesc:
                Conf = yaml.load(fdesc, YAMLLoader)
            data['caldav.url'] = Conf.get('caldav').get('url')
        else:
            req.update_status('ACCEPTED_MANAGER')
            # save who performed this action
            req.last_action_user_id = self.user.id
            task_name = 'worker_accepted'

        self.session.flush()

        # call celery task directly, do not wait for polling
        from celery.registry import tasks
        from celery.task import subtask
        req_task = tasks[task_name]

        subtask(req_task).apply_async(kwargs={'data': data}, countdown=5)

        log.info('scheduling task %s for %s' % (task_name, data))
        return req.status
Пример #39
0
    def process(self, data):
        """ submitted by user

        re-send mail to manager if close to requested date_from
        """
        req = Request.by_id(self.session, data['req_id'])
        # after new field was added, it may not be set yet
        if not req.date_updated:
            return

        delta_deadline = req.date_from - req.date_updated
        if delta_deadline.days <= 2:
            if datetime.now().date() != req.date_updated.date():
                # resend the mail
                self.log.info('2 days left before requested date, '
                              'remind the manager')

                data['reminder'] = True
                async_result = subtask(WorkerPending).delay(data=data)
                self.log.info('task scheduled %r' % async_result)
Пример #40
0
    def process(self, data):
        """ submitted by user

        re-send mail to manager if close to requested date_from
        """
        req = Request.by_id(self.session, data['req_id'])
        # after new field was added, it may not be set yet
        if not req.date_updated:
            return

        delta_deadline = req.date_from - req.date_updated
        if delta_deadline.days <= 2:
            if datetime.now().date() != req.date_updated.date():
                # resend the mail
                self.log.info('2 days left before requested date, '
                              'remind the manager')

                data['reminder'] = True
                async_result = subtask(WorkerPending).delay(data=data)
                self.log.info('task scheduled %r' % async_result)
Пример #41
0
    def render(self):
        request = Request.by_id(self.session,
                                int(self.request.matchdict['req_id']))

        if self.user.has_no_role:
            # can only see own requests
            if request.user.id != self.user.id:
                return HTTPFound(
                    location=route_url('list_request', self.request))

        if self.user.is_manager:
            # can only see own requests and managed user requests
            if ((request.user.id != self.user.id)
                    and (request.user.manager_id != self.user.id)):
                return HTTPFound(
                    location=route_url('list_request', self.request))

        if request:
            return {u'history': request.history, 'req': request}

        return {}
Пример #42
0
    def render(self):

        req_id = self.request.params.get('request_id')
        req = Request.by_id(self.session, req_id)
        if not req:
            return ''
        reason = self.request.params.get('reason')

        req.reason = reason
        req.update_status('DENIED')
        # save who performed this action
        req.last_action_user_id = self.user.id
        self.session.flush()

        # call celery task directly, do not wait for polling
        from celery.registry import tasks
        from celery.task import subtask
        req_task = tasks['worker_denied']
        data = {'req_id': req.id}
        subtask(req_task).delay(data=data)

        return req.status
Пример #43
0
    def process(self, data):
        """ accepted by manager
        auto flag as accepted by HR
        """
        req = Request.by_id(self.session, data['req_id'])
        # after new field was added, it may not be set yet
        if not req.date_updated:
            return

        delta = datetime.now() - req.date_updated
        # after Request.date_updated + 3 days, auto accept it by HR
        if delta.days >= 3:
            # auto accept it as HR
            self.log.info('3 days passed, auto accept it by HR')

            # update request status after sending email
            req.update_status('APPROVED_ADMIN')
            self.session.flush()
            transaction.commit()

            data['autoaccept'] = True
            async_result = subtask(WorkerApproved).delay(data=data)
            self.log.info('task scheduled %r' % async_result)
Пример #44
0
    def render(self):

        req_id = self.request.params.get('request_id')
        req = Request.by_id(self.session, req_id)
        if not req:
            return ''
        reason = self.request.params.get('reason')

        req.reason = reason
        req.update_status('DENIED')
        # save who performed this action
        req.last_action_user_id = self.user.id
        self.session.flush()

        # call celery task directly, do not wait for polling
        from celery.registry import tasks
        from celery.task import subtask
        req_task = tasks['worker_denied']
        data = {'req_id': req.id}
        subtask(req_task).apply_async(kwargs={'data': data}, countdown=5)

        log.info('scheduling task worker_denied for %s' % data)

        return req.status
Пример #45
0
 def test_summarycsv(self):
     from pyvac.models import Request
     req = Request.by_id(self.session, 1)
     self.assertIsInstance(req, Request)
     self.assertEqual(req.summarycsv, u'John Doe,10/04/2014,14/04/2014,5.0,CP')
Пример #46
0
 def test_summary(self):
     from pyvac.models import Request
     req = Request.by_id(self.session, 1)
     self.assertIsInstance(req, Request)
     self.assertEqual(req.summary, u'John Doe: 10/04/2015 - 14/04/2015')
Пример #47
0
 def test_summarycal(self):
     from pyvac.models import Request
     req = Request.by_id(self.session, 1)
     self.assertIsInstance(req, Request)
     self.assertEqual(req.summarycal, u'John Doe - 5.0 CP')
Пример #48
0
 def test_summarycsv(self):
     from pyvac.models import Request
     req = Request.by_id(self.session, 1)
     self.assertIsInstance(req, Request)
     msg = u'1337,Doe,John,10/04/2015,14/04/2015,5.0,CP,,'
     self.assertEqual(req.summarycsv, msg)
Пример #49
0
 def test_summarycsv_label(self):
     from pyvac.models import Request
     req = Request.by_id(self.session, 6)
     self.assertIsInstance(req, Request)
     msg = u'1337,Doe,John,24/08/2011,24/08/2011,0.5,RTT,AM,'
     self.assertEqual(req.summarycsv, msg)
Пример #50
0
 def test_summarycal(self):
     from pyvac.models import Request
     req = Request.by_id(self.session, 1)
     self.assertIsInstance(req, Request)
     self.assertEqual(req.summarycal, u'John Doe - 5.0 CP')
Пример #51
0
 def test_summary(self):
     from pyvac.models import Request
     req = Request.by_id(self.session, 1)
     self.assertIsInstance(req, Request)
     self.assertEqual(req.summary, u'John Doe: 10/04/2015 - 14/04/2015')
Пример #52
0
    def process(self, data):
        """ approved by HR
        send mail to user
        send mail to manager
        """
        req = Request.by_id(self.session, data['req_id'])

        admin = req.user.get_admin(self.session)
        # send mail to user
        src = self.get_admin_mail(admin)
        dst = req.user.email
        if 'autoaccept' in data:
            content = """Your request was automatically approved, it has been added to calendar.
Request details: %s""" % req.summarymail
        else:
            content = """HR has accepted your request, it has been added to calendar.
Request details: %s

You can find the corresponding .ics file as attachment.""" % req.summarymail
        try:
            self.send_mail_ics(sender=src, target=dst, request=req,
                               content=content)

            # send mail to manager
            src = self.get_admin_mail(admin)
            dst = req.user.manager_mail
            if 'autoaccept' in data:
                content = """A request you accepted was automatically approved, it has been added to calendar.
Request details: %s""" % req.summarymail
            else:
                content = """HR has approved a request you accepted, it has been added to calendar.
Request details: %s""" % req.summarymail
            self.send_mail(sender=src, target=dst, request=req,
                           content=content)

            # update request status after sending email
            req.notified = True
        except Exception as err:
            self.log.exception('Error while sending mail')
            req.flag_error(str(err), self.session)

        try:
            if 'caldav.url' in data:
                caldav_url = data['caldav.url']
            else:
                conf_file = sys.argv[1]
                with open(conf_file) as fdesc:
                    Conf = yaml.load(fdesc, YAMLLoader)
                caldav_url = Conf.get('caldav').get('url')

            # add new entry in caldav
            ics_url = addToCal(caldav_url,
                               req.date_from,
                               req.date_to,
                               req.summarycal)
            # save ics url in request
            req.ics_url = ics_url
            self.log.info('Request %d added to cal: %s ' % (req.id, ics_url))
        except Exception as err:
            self.log.exception('Error while adding to calendar')
            req.flag_error(str(err), self.session)

        self.session.flush()
        transaction.commit()