diff --git a/docs/hours.John.json b/docs/hours.John.json index 2525ead..81fa447 100644 --- a/docs/hours.John.json +++ b/docs/hours.John.json @@ -112,5 +112,6 @@ {"date": "12/24/2015", "hours": 0.3, "description": "Remove Marshmallow - deprecate HARD"}, {"date": "12/27/2015", "hours": 3.2, "description": "Continue timer v1 work including lots of work on formatters, preliminary post working."}, {"date": "12/28/2015", "hours": 2.8, "description": "Timer v1 API - CRUD methods implemented, addtional testing."}, -{"date": "01/01/2016", "hours": 2.6, "description": "V1 Timer work - refactor Timer into new RestfulResource class which can serve as the basis of our work going forward"} +{"date": "01/01/2016", "hours": 2.6, "description": "V1 Timer work - refactor Timer into new RestfulResource class which can serve as the basis of our work going forward"}, +{"date": "01/02/2016", "hours": 1.4, "description": "Fix mongo; work on auth issues for v1 timer fixes #10"} ]} diff --git a/goalboost/blueprints/api/timer_resource.py b/goalboost/blueprints/api/timer_resource.py index f0ac2ca..1539cc4 100644 --- a/goalboost/blueprints/api/timer_resource.py +++ b/goalboost/blueprints/api/timer_resource.py @@ -1,16 +1,16 @@ from flask import jsonify, request, Response import http.client -from flask import abort + +from flask.ext.login import current_user + from goalboost.blueprints.api.restful_resource import RestfulResource from goalboost.blueprints.auth.token_auth import httpBasicAuth from goalboost.model.timer_models import TimerDAO, TimerFormatter, TimerEntity - class TimerResource(RestfulResource): @httpBasicAuth.login_required def get_one(self, id): - # def timer_get_one(timer_id): dao = TimerDAO() timer = dao.get(id) as_dict = TimerFormatter().model_to_dict(timer) diff --git a/goalboost/blueprints/auth/token_auth.py b/goalboost/blueprints/auth/token_auth.py index 8843512..bda007b 100644 --- a/goalboost/blueprints/auth/token_auth.py +++ b/goalboost/blueprints/auth/token_auth.py @@ -12,13 +12,9 @@ def verify_password(username, password): user = User.verify_auth_token(password) if not user: return False - - # Adding these lines will (correctly -- was tested at one point) mean "current_user" can be used from - # contexts that need it, but leaving them off for now as it is not a good design for a restful API, - # which verify_password is designed to support - # - # user.authenticated = True - # login_user(user, remember=True) - - return True - + # The next two lines set the current user correctly for the token case, on a + # per-request basis, Tthe user still needs to re-authenticate with each + # request, so the RESTful statelessness is implemented correctly. + user.authenticated = True + login_user(user, remember=True) + return True \ No newline at end of file diff --git a/goalboost/model/timer_models.py b/goalboost/model/timer_models.py index 067306b..793cedc 100644 --- a/goalboost/model/timer_models.py +++ b/goalboost/model/timer_models.py @@ -126,14 +126,49 @@ class LocalTicker(): TimeDog is a better name. ''' class RemoteTicker(object): + startTime = None + seconds = 0 + is_running = False + + def __init__(self, startTime=None): + if startTime is None: + self.startTime = datetime.utcnow() + else: + self.startTime = startTime + def start(self): - pass + self.startTime = datetime.utcnow() + self.is_running = True + def stop(self): - pass + self.seconds = self.seconds + self.current_elapsed() + self.is_running = False + + + def current_elapsed(self): + # current_elapsed is only diff between lastRestart and now if we're running. + # If we're stopped then the total should haved been added to the seconds field + # and current_elapsed is 0 + if not self.is_running: + return 0 + now = datetime.utcnow() + then = self.startTime + return int((now - then).total_seconds()) + + def total_elapsed(self): + return self.current_elapsed() + self.seconds def counter(self): - pass + return self.fmt_seconds(self.total_elapsed()) + + def fmt_seconds(self, seconds): + hrs = int(seconds/3600) + mins_secs = seconds % 3600 + mins = int(mins_secs / 60) + secs = mins_secs % 60 + return "{:0>2}:{:0>2}:{:0>2}".format(hrs, mins, secs) + class TimerFormatter(ModelFormatter): pass \ No newline at end of file diff --git a/manage.py b/manage.py index 3288b52..ad5f3f7 100644 --- a/manage.py +++ b/manage.py @@ -3,7 +3,7 @@ from goalboost.model import db from goalboost import app -from goalboost.model.timer_models import TimerEntity +from goalboost.model.timer_models import TimerEntity, RemoteTicker from goalboost.model.legacy_timer_models import LegacyTimer manager = Manager(app) @@ -14,7 +14,7 @@ def runserver_debug(): @manager.shell def make_shell_context(): - return dict(app=app, db=db, Timer=LegacyTimer, TimerEntity=TimerEntity) + return dict(app=app, db=db, Timer=LegacyTimer, TimerEntity=TimerEntity, Timer2=RemoteTicker) manager.add_command("shell", Shell(make_context=make_shell_context))