From fd2122e7aef3203515a805ec8413835c08d11abf Mon Sep 17 00:00:00 2001 From: Sylvain Fankhauser Date: Mon, 8 Oct 2012 15:21:22 +0200 Subject: [PATCH] Improve projects management Add project status in "search" command, don't show inactive projects when using "add", add start and end dates as well as project status in project detail. --- taxi/__init__.py | 2 +- taxi/app.py | 14 ++++++------- taxi/models.py | 50 +++++++++++++++++++++++++++++++++++++++++++--- taxi/projectsdb.py | 7 ++++--- taxi/remote.py | 12 +++++++++++ 5 files changed, 70 insertions(+), 15 deletions(-) diff --git a/taxi/__init__.py b/taxi/__init__.py index 93bbb90e..82eeac04 100644 --- a/taxi/__init__.py +++ b/taxi/__init__.py @@ -1 +1 @@ -__version__ = '2.3' +__version__ = '2.4-dev' diff --git a/taxi/app.py b/taxi/app.py index 1960cae2..86a596c5 100755 --- a/taxi/app.py +++ b/taxi/app.py @@ -115,7 +115,7 @@ def add(options, args): raise Exception(add.__doc__) search = args[1:] - projects = db.search(search) + projects = db.search(search, active_only=True) if len(projects) == 0: print 'No project matches your search string \'%s\'' % ' '.join(search) @@ -131,10 +131,7 @@ def add(options, args): project = projects[number] - print project - - if project.status == 0: - print 'Warning: this project is not active' + print(project) print "\nActivities:" for (key, activity) in enumerate(project.activities): @@ -171,7 +168,8 @@ def add(options, args): def search(options, args): """Usage: search search_string - Searches for a project by its name. + Searches for a project by its name. The letter in the first column indicates + the status of the project: [N]ot started, [A]ctive, [F]inished, [C]ancelled. """ db = ProjectsDb() @@ -221,8 +219,8 @@ def show(options, args): else: print project - if project.status == 1: - print "\nActivities:" + if project.is_active(): + print(u"\nActivities:") for activity in project.activities: print '%-4s %s' % (activity.id, activity.name) diff --git a/taxi/models.py b/taxi/models.py index d1a75b0a..4f73938d 100755 --- a/taxi/models.py +++ b/taxi/models.py @@ -45,6 +45,25 @@ def get_duration(self): return self.duration class Project: + STATUS_NOT_STARTED = 0; + STATUS_ACTIVE = 1; + STATUS_FINISHED = 2; + STATUS_CANCELLED = 3; + + STATUSES = { + STATUS_NOT_STARTED: 'Not started', + STATUS_ACTIVE: 'Active', + STATUS_FINISHED: 'Finished', + STATUS_CANCELLED: 'Cancelled', + } + + SHORT_STATUSES = { + STATUS_NOT_STARTED: 'N', + STATUS_ACTIVE: 'A', + STATUS_FINISHED: 'F', + STATUS_CANCELLED: 'C', + } + def __init__(self, id, name, status = None, description = None, budget = None): self.id = int(id) self.name = name @@ -54,11 +73,25 @@ def __init__(self, id, name, status = None, description = None, budget = None): self.budget = budget def __unicode__(self): - return """\nId: %s + if self.status in self.STATUSES: + status = self.STATUSES[self.status] + else: + status = 'Unknown' + + return """Id: %s Name: %s -Active: %s +Status: %s +Start date: %s +End date: %s Budget: %s -Description: %s""" % (self.id, self.name, 'yes' if self.status else 'no', self.budget, self.description) +Description: %s""" % ( + self.id, self.name, + status, + self.start_date.strftime('%d.%m.%Y'), + self.end_date.strftime('%d.%m.%Y'), + self.budget, + self.description + ) def __str__(self): return unicode(self).encode('utf-8') @@ -66,6 +99,17 @@ def __str__(self): def add_activity(self, activity): self.activities.append(activity) + def is_active(self): + return (self.status == self.STATUS_ACTIVE and + self.start_date <= datetime.datetime.now() and + self.end_date > datetime.datetime.now()) + + def get_short_status(self): + if self.status not in self.SHORT_STATUSES: + return '?' + + return self.SHORT_STATUSES[self.status] + class Activity: def __init__(self, id, name, price): self.id = int(id) diff --git a/taxi/projectsdb.py b/taxi/projectsdb.py index b65e65f5..82ce5289 100644 --- a/taxi/projectsdb.py +++ b/taxi/projectsdb.py @@ -32,7 +32,7 @@ def update(self, base_url, username, password): print 'Projects database updated successfully' - def search(self, search): + def search(self, search, active_only=False): projects = self._get_projects() found_list = [] @@ -47,7 +47,8 @@ def search(self, search): break if found: - found_list.append(project) + if not active_only or project.is_active(): + found_list.append(project) return found_list @@ -64,7 +65,7 @@ def get(self, id): class LocalProjectsDb: projects = [] - VERSION = 1 + VERSION = 2 def __init__(self, projects): self.projects = projects diff --git a/taxi/remote.py b/taxi/remote.py index d1908600..81d820aa 100644 --- a/taxi/remote.py +++ b/taxi/remote.py @@ -1,5 +1,6 @@ import urllib, urllib2, urlparse, cookielib import json +from datetime import datetime from models import Project, Activity @@ -159,6 +160,17 @@ def get_projects(self): p = Project(int(project['id']), project['name'],\ project['status'], project['description'],\ project['budget']) + + try: + p.start_date = datetime.strptime(project['startdate'], '%Y-%m-%d') + except ValueError: + p.start_date = None + + try: + p.end_date = datetime.strptime(project['enddate'], '%Y-%m-%d') + except ValueError: + p.end_date = None + i += 1 if p.status == 1: