Ejemplo n.º 1
0
    def _send_email(self, type, date_start, date_end, days, remarks):
        name = self.request.user.name
        email = self.request.user.email
        today = datetime.date.today()
        topic, body = self.TYPES[type]

        body = self._(
            body,
            today=today.strftime('%Y-%m-%d'),
            date_start=date_start,
            date_end=date_end,
            year=today.year,
            days=days,
            remarks=remarks,
            name=name,
        )
        with mail.EmailSender() as email_sender:
            email_sender.send(
                config['ACCOUNTANT_EMAIL'],
                topic,
                body,
                cc=email,
                sender_name=name,
                replay_to=','.join([self.request.user.email]),
            )
Ejemplo n.º 2
0
    def _hours_for_previous_months(self, date):
        current_month_start = datetime.date(date.year, date.month, 1)
        time_entries = DBSession.query('user', 'client', 'project', 'time',
                                       'description', 'ticket_id',
                                       'entry_date',
                                       'entry_status').from_statement("""
        SELECT
            u.name as "user", c.name as "client", p.name as "project",
            t.time as "time", t.description as "description",
            t.ticket_id as "ticket_id", t.date as "entry_date",
            t.deleted as "entry_status"
        FROM
            time_entry as t,
            "user" as u,
            client as c,
            project as p
        WHERE
            t.user_id = u.id AND
            t.project_id = p.id AND
            p.client_id = c.id AND
            DATE(t.modified_ts) = :date AND
            t.date < :current_month_start
        ORDER BY
            u.name, c.name, p.name
        """).params(current_month_start=current_month_start, date=date).all()

        if not time_entries:
            LOG(u"No time entries for previous months %s" % (date, ))
            return u"No time entries for previous months %s" % (date, )

        output = []
        tmp_user = ''
        for user, client, project, time, description, ticket_id, entry_date, entry_status in time_entries:
            if tmp_user != user:
                tmp_user = user
                output.append(u"")
                output.append(u"%s:" % (user, ))

            ticket_id = ticket_id and u"[%s] " % ticket_id or u""
            status = entry_status and self._(u"[Deleted]") or u""
            output.append(u"\t- [%s]%s %s / %s / %s%s %.2f h" %
                          (entry_date, status, client, project, ticket_id,
                           description, time))

        message = u'\n'.join(output)

        topic = self._(
            u"[intranet] Report with hours added for the previous months")
        with mail.EmailSender() as email_sender:
            email_sender.send(
                config['MANAGER_EMAIL'],
                topic,
                message,
            )
        LOG(u"Report with hours added for previous months - started")
        return message
Ejemplo n.º 3
0
    def action(self):
        today = datetime.date.today()
        query = DBSession.query
        uber_query = query(Client.name, Project.name, TimeEntry.ticket_id,
                           User.email, TimeEntry.description, TimeEntry.date,
                           TimeEntry.time)
        uber_query = uber_query.filter(TimeEntry.user_id==User.id)\
                               .filter(TimeEntry.project_id==Project.id)\
                               .filter(Project.tracker_id==Tracker.id)\
                               .filter(Project.client_id==Client.id)
        start, end = self._previous_month()
        uber_query = uber_query.filter(TimeEntry.date>=start)\
                               .filter(TimeEntry.date<=end)\
                               .filter(TimeEntry.deleted==False)
        uber_query = uber_query.order_by(Client.name, Project.name,
                                         TimeEntry.ticket_id, User.name)
        data = uber_query.all()

        wbk = xlwt.Workbook()
        sheet = wbk.add_sheet('%s.xls' % start.strftime('%m-%Y'))

        heading_xf = xlwt.easyxf(
            'font: bold on; align: wrap on, vert centre, horiz center')
        headings = ('Klient', 'Projekt', 'Ticket id', 'Pracownik', 'Opis',
                    'Data', 'Czas')
        headings_width = (x * 256 for x in (20, 30, 10, 40, 100, 12, 10))
        for colx, value in enumerate(headings):
            sheet.write(0, colx, value, heading_xf)
        for i, width in enumerate(headings_width):
            sheet.col(i).width = width

        sheet.set_panes_frozen(True)
        sheet.set_horz_split_pos(1)
        sheet.set_remove_splits(True)

        for j, row in enumerate(data):
            row = self._format_row(row)
            for i, cell in enumerate(row):
                sheet.write(j + 1, i, *cell)

        file_path = '/tmp/%s.xls' % start.strftime('%m-%Y')
        wbk.save(file_path)
        topic = '[intranet] Excel with projects hours'
        message = 'Excel with projects hours'
        with mail.EmailSender() as email_sender:
            email_sender.send(
                config['MANAGER_EMAIL'],
                topic,
                message,
                file_path=file_path,
            )
        return Response('ok')
Ejemplo n.º 4
0
 def action(self):
     today = datetime.date.today()
     data = self._annually_report(today.year)
     data['config'] = self.request.registry.settings
     response = render(
         'intranet3:templates/_email_reports/presence_annually_report.html',
         data, self.request)
     response = response.replace('\n', '').replace('\t', '')
     with mail.EmailSender() as email_sender:
         email_sender.send(
             config['MANAGER_EMAIL'],
             self._(u'[Intranet2] Late report'),
             html_message=response,
         )
     return Response('ok')
Ejemplo n.º 5
0
    def _remind_resolved_bugs_user(self, user):
        """ remind (via email) user to close his resolved bugs """
        LOG(u"Starting email reminder for user %s" % (user.email, ))
        my_bugs_url = self.request.url_for('/bugs/my', resolved=1)
        list_url = self.settings['FRONTEND_PREFIX'] + my_bugs_url

        bugs = Bugs(self.request, user).get_user(resolved=True)
        if not bugs:
            LOG(u"No bugs to remind user %s of" % (user.email, ))
            return self._(u'No bugs for user ${user}', user=user.email)
        output = []
        output.append(
            self.
            _(u'Hej ${user}! Masz rozwiązane niezamknięte bugi, weź je ogarnij:',
              user=user.name))
        output.append(u'')
        for i, bug in enumerate(bugs, 1):
            id = bug.id
            desc = bug.desc
            url = bug.url
            output.append(u"%(i)s. #%(id)s %(desc)s %(url)s" % locals())
        output.append(u'')
        output.append(
            self._(u'Te bugi możesz również zobaczyć pod adresem ${list_url}',
                   list_url=list_url))
        output.append(u'')
        output.append(self._(u'Pozdrawiam,\nTwój intranet'))
        message = u'\n'.join(output)

        topic = self._(u"[intranet] ${num} zgłoszeń do zamknięcia",
                       num=len(bugs))
        with mail.EmailSender() as email_sender:
            email_sender.send(
                user.email,
                topic,
                message,
            )
        LOG(u"Email reminder for user %s started" % (user.email, ))
        return message
Ejemplo n.º 6
0
 def _remind_missing_hours(self, email, name, hours, date):
     """ remind (via email) everyone about their missing hours """
     LOG(u"Starting email reminder for user %s" % (email, ))
     time_list_url = self.request.url_for('/times/list',
                                          date=date.strftime('%d.%m.%Y'))
     hours_url = self.settings['FRONTEND_PREFIX'] + time_list_url
     message = self.HOURS_EMAIL_TEMPLATE(
         name=name,
         min_hours=MIN_HOURS,
         actual='%.2f' % hours,
         url=hours_url,
         date=date.strftime('%d.%m.%Y'),
     )
     topic = self._(u"[intranet] brakujące godziny z dnia ${date}",
                    date=date.strftime('%d.%m.%Y'))
     with mail.EmailSender() as email_sender:
         email_sender.send(
             email,
             topic,
             message,
         )
     LOG(u"Email reminder for user %s started" % (email, ))
     return message
Ejemplo n.º 7
0
    def _send_report(self, coordinator_id, email, bugs):
        # Bugs filtering & ordering
        # Coordinator gets bugs from his projects, manager gets bugs from
        # all projects
        if coordinator_id is None:  # Manager
            bugs_filtered = sorted(
                bugs,
                key=lambda b: b.changeddate.replace(tzinfo=None),
            )
            title = u'Lista najstarszych niezamkniętych bugów\nwe wszystkich projektach'
        else:  # Coordinator
            bugs_filtered = [
                b for b in bugs if b.project is not None
                and b.project.coordinator_id == coordinator_id
            ]
            bugs_filtered = sorted(
                bugs_filtered,
                key=lambda b: b.changeddate.replace(tzinfo=None),
            )
            title = u'Lista najstarszych niezamkniętych bugów\nw projektach w których jesteś koordynatorem'
        if bugs_filtered:
            data = {
                'bugs': bugs_filtered[:20],
                'title': self._(title),
            }
            response = render(
                'intranet3:templates/_email_reports/old_bugs_report.html',
                data,
                request=self.request)

            with mail.EmailSender() as email_sender:
                email_sender.send(
                    email,
                    self._(u'[Intranet3] Old bugs report'),
                    html_message=response,
                )
Ejemplo n.º 8
0
    def _today_hours_without_ticket(self, date, projects, omit_users):
        if not omit_users:
            # because u.id NOT IN (,) returns error
            omit_users = (987654321, )
        time_entries = DBSession.query('user', 'description', 'time',
                                       'project', 'client').from_statement("""
        SELECT
            u.name as "user", t.description as "description",
            t.time as "time", p.name as "project", c.name as "client"
        FROM
            time_entry as t, project as p, client as c, "user" as u
        WHERE
            t.deleted = False AND
            t.date = :date AND
            t.ticket_id IS NULL AND
            u.id = t.user_id AND
            p.id = t.project_id AND
            c.id = p.client_id AND
            p.id IN :projects AND
            u.id NOT IN :users
        ORDER BY u.name, c.name, p.name
        """).params(date=date,
                    projects=tuple(projects),
                    users=tuple(omit_users)).all()

        if not time_entries:
            LOG(u"No time entries for report with hours without ticket added on %s"
                % (date, ))
            return u"No time entries for report with hours without ticket added on %s" % (
                date, )

        output = []
        total_sum = 0
        user_sum = defaultdict(lambda: 0.0)
        user_entries = defaultdict(lambda: [])

        for user, description, time, project, client in time_entries:
            total_sum += time
            user_sum[user] += time
            user_entries[user].append((description, time, project, client))

        output.append(
            self._(u"Daily hours report without bugs (${total_sum} h)",
                   total_sum=u'%.2f' % total_sum))

        for user, entries in user_entries.iteritems():
            output.append(u"")
            output.append(u"\t%s (%.2f h):" % (user, user_sum[user]))

            for description, time, project, client in entries:
                output.append(u"\t\t- %s / %s / %s %.2f h" %
                              (client, project, description, time))

        message = u'\n'.join(output)

        topic = self._(u"[intranet] Daily hours report without bugs")
        with mail.EmailSender() as email_sender:
            email_sender.send(
                config['MANAGER_EMAIL'],
                topic,
                message,
            )
        LOG(u"Report with hours without ticket on %s - started" % (date, ))
        return message
Ejemplo n.º 9
0
    def _today_hours(self, date, projects, omit_users):
        time_entries = DBSession.query('uid', 'user', 'description', 'time',
                                       'project', 'client', 'ticket_id',
                                       'tracker_id',
                                       'total_time').from_statement("""
                SELECT
                    u.id as "uid", u.name as "user", t.description as "description",
                    t.time as "time", p.name as "project", c.name as "client",
                    t.ticket_id as "ticket_id", p.tracker_id as "tracker_id",
                    (
                        SELECT SUM(te.time)
                        FROM time_entry te
                        WHERE te.ticket_id=t.ticket_id
                    ) as "total_time"
                FROM
                    time_entry as t, project as p, client as c, "user" as u
                WHERE
                    t.deleted = False AND
                    t.date = :date AND
                    u.id = t.user_id AND
                    p.id = t.project_id AND
                    c.id = p.client_id AND
                    p.id IN :projects AND
                    u.id NOT IN :users
                ORDER BY u.name, c.name, p.name
            """).params(date=date,
                        projects=tuple(projects),
                        users=tuple(omit_users)).all()
        if not time_entries:
            s = u"No time entries for report with hours added on %s" % date
            LOG(s)
            return s

        output = []
        total_sum = 0
        user_sum = defaultdict(lambda: 0.0)
        user_entries = defaultdict(lambda: [])
        trackers = {}
        for (uid, user, description, time, project, client, ticket_id,
             tracker_id, total_time) in time_entries:
            # Lazy dict filling
            if not tracker_id in trackers:
                trackers[tracker_id] = Tracker.query.get(tracker_id)
            tracker = trackers[tracker_id]
            ticket_url = tracker.get_bug_url(ticket_id)

            total_sum += time
            user_sum[uid] += time
            user_entries[(uid, user)].append(
                (description, time, project, client, ticket_id, ticket_url,
                 total_time))
        output.append(
            self._(u"Daily hours report (${total_sum} h)",
                   total_sum='%.2f' % total_sum))

        user_entries = sorted(
            user_entries.iteritems(),
            key=lambda u: user_sum[u[0][0]],
            reverse=True,
        )
        base_url = self.request.registry.settings['FRONTEND_PREFIX']
        for (user_id, user_name), entries in user_entries:
            entries = sorted(
                entries,
                key=itemgetter(1),
                reverse=True,
            )
            output.append(u"")
            time_link = base_url + self.request.url_for(
                '/times/list_user',
                user_id=user_id,
                date=date.strftime("%d.%m.%Y"),
            )
            output.append(u"\t<a href=\"%s\">%s</a> (%.2f h):" %
                          (time_link, user_name, user_sum[user_id]))

            for (description, time, project, client, ticket_id, bug_url,
                 total_time) in entries:
                if ticket_id:
                    ticket_id = "[<a href=\"%s\">%s</a>] " % (bug_url,
                                                              ticket_id)
                else:
                    ticket_id = ""
                total_time = " (%.2fh)" % total_time if ticket_id else ""
                output.append(u"\t\t- %s / %s / %s%s - %.2fh%s" %
                              (client, project, ticket_id, description, time,
                               total_time))

        message = u'<br />\n'.join(output).replace('\t', '&emsp;&emsp;')

        topic = self._(u"[intranet] Daily hours report")
        with mail.EmailSender() as email_sender:
            email_sender.send(
                config['MANAGER_EMAIL'],
                topic,
                html_message=message,
            )
        LOG(u"Report with hours on %s - started" % (date, ))
        return message