view getan/project.py @ 352:2e7885dc6669

Add lazy loading of Project Entries Only load the Entries of a Project when they are required. This will allow to load also only specific entries from a project.
author Björn Ricks <bjoern.ricks@intevation.de>
date Mon, 03 Mar 2014 14:26:14 +0100
parents 349b4571e120
children 4f5094f3f615
line wrap: on
line source
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# (c) 2008, 2009, 2010 by
#   Sascha L. Teichmann <sascha.teichmann@intevation.de>
#   Ingo Weinzierl <ingo.weinzierl@intevation.de>
#
# This is Free Software licensed unter the terms of GPLv3 or later.
# For details see LICENSE coming with the source of 'getan'.

import locale

from datetime import datetime


class Project(object):

    def __init__(self, backend, id, key, desc, total):
        self.backend = backend
        self.id = id
        self.key = key
        self.desc = desc
        self._entries = None
        self.total = total
        self.start = None
        self.stop = None

    def year(self):
        total = 0
        now = datetime.now()
        for entry in self.entries:
            start = entry.start
            if start.year == now.year:
                total += (entry.end - start).seconds
        return total

    def month(self):
        total = 0
        now = datetime.now()
        for entry in self.entries:
            start = entry.start
            if start.month == now.month and start.year == now.year:
                total += (entry.end - start).seconds
        return total

    def week(self):
        total = 0
        now = datetime.now()
        tweek = now.strftime('%W')
        for entry in self.entries:
            start = entry.start
            if start.strftime('%W') == tweek and start.year == now.year:
                total += (entry.end - start).seconds
        return total

    def day(self):
        total = 0
        now = datetime.now()
        for entry in self.entries:
            start = entry.start
            if start.month == now.month and start.year == now.year \
                    and start.day == now.day:
                total += (entry.end - start).seconds
        return total

    def load_entries(self, year=None, week=None):
        self._entries = self.backend.load_entries(self.id, year, week)

    @property
    def entries(self):
        if self._entries is None:
            self.load_entries()
        return self._entries


class Entry(object):

    def __init__(self, id, project_id, start, end, desc):
        self.id = id
        self.project_id = project_id
        self.start = start
        self.end = end
        self.desc = desc

        # carefully handle non unicode string
        # urwid seems to have some issue with plain str
        if self.desc and not isinstance(self.desc, unicode):
            self.desc = unicode(self.desc, locale.getpreferredencoding())

    def duration(self):
        return (self.end - self.start)

    def __str__(self):
        return ("[%s | %s | %s | %s | %s]" %
               (self.id, self.project_id, self.start, self.end, self.desc))

# vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)