コード例 #1
0
    def __call__(self):
        db_session = DBSession()

        wg = get_wg(db_session, self.request)
        self.user_is_wgmember = self.user in wg.members
        self.user_is_wgleader = self.user in wg.leaders

        # we view shifts per-month here
        # use parameters to determine month, default is current month
        self.month = int(self.request.matchdict['month'])
        self.year = int(self.request.matchdict['year'])
        schedule_date = datetime.date(self.year, self.month, 1)
        self.month_info = month_info(schedule_date)
        q = """SELECT descr FROM shift_days_descriptions ORDER BY id;"""
        day_literals = [i[0] for i in list(db_session.execute(q))]
        self.days = day_literals + list(
            range(1, self.month_info.days_in_month + 1))
        shifts = db_session.query(Shift).filter(Shift.wg_id == wg.id)\
                                     .filter(Shift.month == self.month)\
                                     .filter(Shift.year == self.year)\
                                     .order_by(Shift.day, Shift.task, Shift.mem_id)\
                                     .all()

        # show msg
        if 'msg' in self.request.params:
            msg = self.request.params['msg']
        else:
            msg = ''

        return dict(shifts=shifts, wg=wg, msg=msg)
コード例 #2
0
ファイル: workgroup.py プロジェクト: vokomokum/vkmkm-erp
    def __call__(self):
        if 'msg' in self.request.params:
            msg = self.request.params['msg']
        else:
            msg = ''

        session = DBSession()
        wg = get_wg(session, self.request)
        self.user_is_wgleader = self.user in wg.leaders

        return dict(wg=wg, msg=msg, now=datetime.now())
コード例 #3
0
ファイル: workgroup.py プロジェクト: vokomokum/vkmkm-erp
    def __call__(self):
        session = DBSession()
        wg = get_wg(session, self.request)
        req = self.request

        self.possible_members = get_possible_members(session, wg)
        if 'action' in req.params:
            action = req.params['action']
            if action == "save":
                old_name = wg.name
                uwgs = [w.name for w in self.user.workgroups]
                wg = fill_wg_from_request(wg, req, session)
                if wg.exists and not wg.name == old_name\
                   and not 'Systems' in uwgs:
                    raise Exception('Only members of Systems can change the'\
                                    ' name of an existing workgroup, because'\
                                    ' it changes the email addresses of the'\
                                    ' group.')
                wg.validate()
                if not wg.exists:
                    session.add(wg)
                    session.flush()  # flushing manually so the wg gets an ID
                    return self.redirect('/workgroup/%d?msg='\
                                         'Workgroup was created.' % wg.id)
                return dict(wg=wg, msg='Workgrup has been saved.')

            elif action == 'toggle-active':
                wg = get_wg(session, self.request)
                self.confirm_toggle_active = True
                return dict(wg=wg)
            elif action == 'toggle-active-confirmed':
                wg.active = not wg.active
                msg='Workgroup {} is now {}active.'.\
                    format(wg.name, {False:'in', True:''}[wg.active])
                return dict(wg=wg, msg=msg)

        return dict(wg=wg, msg='')
コード例 #4
0
    def __call__(self):
        db_session = DBSession()
        params = self.request.params
        wg_id = self.request.matchdict['wg_id']
        wg = get_wg(db_session, self.request)
        if not wg.active:
            return self.redir_to_shiftlist(
                wg, int(params['year']), int(params['month']),
                'This workgroup is inactive, no new shifts can be added.')

        if not 'people' in params:
            people = 1
        else:
            people = int(params['people'])
        self.added_shifts = []

        def add_shift(month=None, year=None):
            ''' add a shift object to session, several times if people > 1 '''
            shift = Shift(wg_id, '', None, None, None, None)
            shift.workgroup = DBSession().query(Workgroup).get(wg_id)
            shift = fill_shift_from_request(shift, self.request)
            shift.validate()
            db_session.add(shift)
            self.added_shifts.append(shift)
            if month:
                shift.month = month
            if year:
                shift.year = year
            if people > 1:
                for _ in range(1, people):
                    s = shift.clone()
                    s.workgroup = DBSession().query(Workgroup).get(s.wg_id)
                    s.validate()
                    db_session.add(s)
                    self.added_shifts.append(s)

        day = params['day']
        if not str(day).isdigit():
            day = 1
        else:
            day = int(day)
        sdate = datetime.datetime(int(params['year']), int(params['month']),
                                  day)
        if not 'repeat' in params:
            repeat = 'once'
        else:
            repeat = params['repeat']
        if repeat == 'once':
            add_shift()
        else:
            udate = datetime.datetime(int(params['until_year']),
                                      int(params['until_month']), day)
            if repeat in ('monthly', 'bi-monthly-startnow',
                          'bi-monthly-startnext'):
                for year in range(sdate.year, udate.year + 1):
                    smonth = 1
                    if year == sdate.year:
                        smonth = sdate.month
                    umonth = 12
                    if year == udate.year:
                        umonth = udate.month
                    step = 1
                    if repeat.startswith('bi-monthly'):
                        step = 2
                    if repeat == 'bi-monthly-startnext':
                        smonth += 1
                    for month in range(smonth, umonth + 1, step):
                        add_shift(month, year)
                if len(self.added_shifts) == 0:
                    return self.redir_to_shiftlist(
                        wg, sdate.year, sdate.month,
                        "Invalid date range: {}/{} to {}/{}".format(
                            sdate.month, sdate.year, udate.month, udate.year))
            else:
                return self.redir_to_shiftlist(
                    wg, sdate.year, sdate.month,
                    'Could not create shifts. "repeat"-command unknown.')
        # inform (other) coordinators
        subject = "Workgroup {}: Shifts were created by {}"\
                    .format(ascii_save(wg.name), ascii_save(self.user.fullname))
        body = "These shifts were added:\n\n{}\n\nBest, Vokomokum"\
                .format('\n'.join([str(s) for s in self.added_shifts]))
        for c in wg.leaders:
            if c is not self.user:
                sendmail(c.mem_email, subject, body, folder='shifts')
        return self.redir_to_shiftlist(wg, sdate.year, sdate.month,
                    'Succesfully added {} shift(s) for task "{}".'\
                    .format(len(self.added_shifts),
                            ascii_save(self.request.params['task'])))
コード例 #5
0
    def __call__(self):
        db_session = DBSession()
        wg = get_wg(db_session, self.request)
        if not wg:
            raise Exception(
                "Don't know which workgroup this is supposed to be.")

        shift = get_shift(db_session, self.request)
        if not shift:
            raise Exception("No shift with id %d" %
                            self.request.matchdict['s_id'])

        def redir(msg):
            return self.redir_to_shiftlist(wg, shift.year, shift.month, msg)

        if not wg.active:
            return redir(
                'Workgroup is inactive, editing shifts therefore not possible.'
            )

        action = self.request.matchdict['action']
        if action == "":
            raise Exception('No action given.')

        if action == "setmember":
            if not 'mem_id' in self.request.params:
                return redir('No member selected.')
            if not self.user in wg.members and not self.user.mem_admin:
                return redir(
                    'You are not allowed to assign shifts in this workgroup.')
            if self.request.params['mem_id'] == '--':
                member = None
            else:
                member = get_member(db_session, self.request)

            # prepare some things for mailing
            schedule_url = '{}/workgroup/{}/shifts/{}/{}'.format(
                self.request.application_url, shift.workgroup.id, shift.year,
                shift.month)
            q = 'This email was automatically generated, so please do not '\
                'directly reply to it. You may direct any questions regarding '\
                'the workgroup to your coordinator(s). Only technical questions '\
                'go to [email protected].'\
                '\n\nBest,\nVokomokum'
            old_member = shift.member

            def mail_old_assignee():
                # send old assignee an email
                if old_member and not self.user == old_member:
                    subject = 'You have been signed out of a shift.'
                    body = 'Hi,\n\n{} has signed you off a shift that '\
                           'you were previously assigned to.\nThe shift is '\
                           'now:\n\n{}\n\nYou can view the shift '\
                           'schedule at {}.\n{}'.format(
                            ascii_save(self.user.fullname), shift,
                            schedule_url, q)
                    sendmail(old_member.mem_email,
                             subject,
                             body,
                             folder='shifts')

            if member:
                shift.member = member
                shift.state = 'assigned'
                shift.validate()
                if not self.user == member:
                    # send new assignee an email
                    subject = 'You have been assigned to a shift.'
                    body = 'Hi,\n\n{} has assigned you to a shift: '\
                           '\n\n{}\n\nYou can view the shift schedule at {}'\
                           '\n\n{}'.format(ascii_save(self.user.fullname),
                            str(shift), schedule_url, q)
                    sendmail(member.mem_email, subject, body, folder='shifts')
                # let coordinator(s) know, as well
                subject = "Workgroup {}: The shift for {} on day '{}' in "\
                          "{}/{} is now assigned to {}".format(shift.workgroup,
                           shift.task, shift.day, shift.month, shift.year,
                           ascii_save(shift.member.fullname))
                body = "The assignment was done by member {}."\
                            .format(ascii_save(self.user.fullname))
                if old_member:
                    body += " The previous assignee was: {}."\
                            .format(ascii_save(old_member.fullname))
                else:
                    body += " No one was assigned to this shift before."
                body += "\n\n{}".format(q)
                for c in wg.leaders:
                    if c is not self.user:
                        sendmail(c.mem_email, subject, body, folder='shifts')
                # and inform previous assignee
                mail_old_assignee()
                name = ascii_save(shift.member.fullname)
                return redir(u'{} has been signed up for the shift.'\
                             .format(name))
            else:
                if shift.is_locked and not self.user in wg.leaders\
                                   and not self.user.mem_admin:
                    return redir(
                        'Shift is already locked. Ask your workgroup admin for help.'
                    )
                shift.member = None
                shift.state = 'open'
                shift.validate()
                mail_old_assignee()
                # let coordinator(s) know, as well
                subject = "Workgroup {}: Member {} was unassigned from the "\
                          "shift for {} on day '{}' in {}/{}"\
                           .format(shift.workgroup,
                           ascii_save(old_member.fullname),
                           shift.task, shift.day, shift.month, shift.year)
                body = "The un-assignment was done by member {}."\
                            .format(ascii_save(self.user.fullname))
                body += "\n\n{}".format(q)
                for c in wg.leaders:
                    if c is not self.user:
                        sendmail(c.mem_email, subject, body, folder='shifts')
                return redir('Shift is now open.')
            return redir('You are not allowed to do this.')

        elif action == "settask":
            if self.user in wg.leaders or self.user.mem_admin:
                if not 'task' in self.request.params:
                    return dict(msg='No task given.')
                shift.task = self.request.params['task']
                shift.validate()
                return redir('Changed task of shift.')
            return redir('You are not allowed to edit the task.')

        elif action == "setday":
            if self.user in wg.leaders or self.user.mem_admin:
                if not 'day' in self.request.params:
                    return dict(msg='No day given.')
                shift.day = self.request.params['day']
                shift.validate()
                return redir('Changed day of shift to {}.'.format(shift.day))
            return redir('You are not allowed to set the day.')

        elif action == "setstate":
            if self.user in wg.leaders or self.user.mem_admin:
                if not 'state' in self.request.params:
                    return redir('No state given.')
                shift.state = self.request.params['state']
                shift.validate()
                return redir('Changed shift state to {}.'.format(shift.state))
            return redir('You are not allowed to set the state.')

        elif action == 'delete':
            if self.user in wg.leaders or self.user.mem_admin:
                db_session.delete(shift)
                return redir('Deleted shift.')
            return redir('You are not allowed to delete a shift.')