Skip to content

Commit

Permalink
Start time = now(), and solving for start by default
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas committed May 31, 2019
1 parent d469983 commit a2be1fd
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
.DS_Store
.idea/workspace.xml
.git/
logs/
venv/
Expand All @@ -9,6 +8,7 @@ app/creds.py
__pycache__/
*.env
.env
breadsheet.py

# Created by https://www.gitignore.io/api/venv,macos,pycharm
# Edit at https://www.gitignore.io/?templates=venv,macos,pycharm
Expand Down
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Thomas Berg

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
16 changes: 7 additions & 9 deletions app/main/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@
from app.main.forms import RecipeForm, StepForm, ThenWaitForm, StartFinishForm
from app.models import Recipe, Step, Difficulty
from datetime import datetime, timedelta
from flask import render_template, flash, redirect, url_for, request, send_from_directory
from flask import render_template, redirect, url_for, request, send_from_directory # , flash
from os import path
from sqlalchemy.sql import text
from sqlalchemy.sql import text, func
from global_logger import glogger
import logging

logger = glogger
logger.setLevel(logging.DEBUG)
now = datetime.now()


# map the desired URL to this function
@bp.route('/breadsheet')
Expand Down Expand Up @@ -54,6 +52,7 @@ def add_recipe():
@bp.route('/recipe', methods=['GET', 'POST'])
def recipe():
logger.info("Start of recipe(), request method: {}".format(request.method))

sform = StepForm()
recipe_id = request.args.get('id') or 1
sform.recipe_id.data = recipe_id
Expand All @@ -65,6 +64,7 @@ def recipe():
logger.debug("Replaced it with 1.")

recipe = add_recipe_ui_fields(Recipe.query.filter_by(id=recipe_id).first())

steps = set_when(Step.query.filter_by(recipe_id=recipe_id).order_by(Step.number).all(), recipe.start_time)
twforms = create_tw_forms(steps)
seform = create_start_finish_forms(recipe)
Expand Down Expand Up @@ -255,9 +255,7 @@ def add_recipe_ui_fields(recipe):
else:
recipe.difficulty_ui = difficulty_abbrev(recipe.difficulty, difficulty_values)
recipe.date_added_ui = recipe.date_added.strftime('%Y-%m-%d')
if recipe.start_time is None:
recipe.start_time = now
recipe.start_time = datetime.strptime(str(recipe.start_time), '%Y-%m-%d %H:%M:%S.%f')
recipe.start_time = datetime.strptime(str(datetime.now()), '%Y-%m-%d %H:%M:%S.%f')
recipe.start_time_ui = dt_ui(recipe.start_time)

sql = text("SELECT sum(then_wait) FROM step WHERE recipe_id = {}".format(recipe.id))
Expand All @@ -267,7 +265,7 @@ def add_recipe_ui_fields(recipe):
recipe.finish_time_ui = recipe.start_time_ui
else:
recipe.finish_time = recipe.start_time + timedelta(seconds=recipe.total_time)
recipe.finish_time_ui = dt_ui(recipe.finish_time)
recipe.finish_time_ui = recipe.finish_time
recipe.total_time_ui = hms_to_string(seconds_to_hms(recipe.total_time))

logger.debug("End of add_recipe_ui_fields(), returning: {}".format(recipe))
Expand Down Expand Up @@ -310,7 +308,7 @@ def create_start_finish_forms(recipe):
seform.start_time.data = recipe.start_time
seform.finish_date.data = recipe.finish_time
seform.finish_time.data = recipe.finish_time
seform.solve_for_start.data = str(recipe.solve_for_start)
seform.solve_for_start.data = "1"

logger.debug("End of create_start_finish_forms(), returning seform: {}".format(seform))
return seform
2 changes: 1 addition & 1 deletion app/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
{{ title }} - Breadsheet
{% endif %}
{% else %}
Breadsheet: A Recipe Scheduling Tool
Breadsheet: A Planning Tool For Long Recipes
{% endif %}
{% endblock %}

Expand Down
4 changes: 2 additions & 2 deletions app/templates/start_finish.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<!-- <div id="start_time_text">{{ recipe.start_time_ui }}</div> -->
<div id="start_time_controls">
{{ seform.start_date }} &nbsp; {{ seform.start_time }}
<button onclick="setNow('start')" id="start_now" class="btn btn-default">Now</button>
<button onclick="setNow('start')" id="start_now" class="btn btn-default" disabled>Start Now</button>
</div>
</td>

Expand All @@ -17,7 +17,7 @@
<!-- <div id="finish_time_text">{{ recipe.finish_time_ui }}</div> -->
<div id="finish_time_controls">
{{ seform.finish_date }} &nbsp; {{ seform.finish_time }}
<button onclick="setNow('finish')" id="finish_now" class="btn btn-default" disabled>Now</button>
<button onclick="setNow('finish')" id="finish_now" class="btn btn-default">Finish Now</button>
</div>
</td>
</tr>
Expand Down
4 changes: 2 additions & 2 deletions app/templates/steps.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
<td id="step_wait{{ step.id }}">
{% for tw in twforms %}
{% if tw.step_id == step.id %}
{{ tw.then_wait_h(size=2, id="then_wait_h{}".format(step.id)) }}:{{
tw.then_wait_m(size=2, id="then_wait_m{}".format(step.id)) }}
{{ tw.then_wait_h(size=3, id="then_wait_h{}".format(step.id)) }}:{{
tw.then_wait_m(size=3, id="then_wait_m{}".format(step.id)) }}
{% endif %}
{% endfor %}
</td>
Expand Down
33 changes: 17 additions & 16 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Config(object):
logger.debug("Readme file exists? {}".format(path.isfile('README.md')))

SECRET_KEY = environ.get('SECRET_KEY') or '1mW7@LN0n32L6ntaj0d8jzsXiAW4mkPL7u5l'
BUCKET_NAME = environ.get('GCP_BUCKET_NAME') or 'local_bucketname'
BUCKET_NAME = environ.get('GCP_BUCKET_NAME') or 'local_fail_bucket_name'
db_user = environ.get('GCP_CLOUDSQL_USER')
db_pw = environ.get('GCP_CLOUDSQL_PW')
db_name = environ.get('GCP_CLOUDSQL_DBNAME')
Expand All @@ -34,24 +34,25 @@ class Config(object):
fire = firestore.Client().from_service_account_json('breadsheet-prod.json')

# this call should work now
fire_creds = fire.collection('environment_vars').document('prod').get()
logger.info("Firecreds GCP bucket: {}".format(fire_creds._data['GCP_BUCKET_NAME']))
fire_credentials = fire.collection('environment_vars').document('prod').get()
# fire_credentials = fire_credentials._data
logger.info("Fire_credentials GCP bucket: {}".format(fire_credentials._data['GCP_BUCKET_NAME']))

SECRET_KEY = fire_creds._data['SECRET_KEY'] or '2mW7@LN0n32L6ntaj0d8jzsXiAW4mkPL7u5l'
BUCKET_NAME = fire_creds._data['GCP_BUCKET_NAME'] or 'fire_fail_bucketname'
db_user = fire_creds._data['GCP_CLOUDSQL_USER']
db_pw = fire_creds._data['GCP_CLOUDSQL_PW']
db_name = fire_creds._data['GCP_CLOUDSQL_DBNAME']
db_ip = fire_creds._data['GCP_CLOUDSQL_IP']
db_port = fire_creds._data['GCP_CLOUDSQL_PORT']
db_instance = fire_creds._data['GCP_CLOUDSQL_INSTANCE']
SECRET_KEY = fire_credentials._data['SECRET_KEY'] or '2mW7@LN0n32L6ntaj0d8jzsXiAW4mkPL7u5l'
BUCKET_NAME = fire_credentials._data['GCP_BUCKET_NAME'] or 'fire_fail_bucket_name'
db_user = fire_credentials._data['GCP_CLOUDSQL_USER']
db_pw = fire_credentials._data['GCP_CLOUDSQL_PW']
db_name = fire_credentials._data['GCP_CLOUDSQL_DBNAME']
db_ip = fire_credentials._data['GCP_CLOUDSQL_IP']
db_port = fire_credentials._data['GCP_CLOUDSQL_PORT']
db_instance = fire_credentials._data['GCP_CLOUDSQL_INSTANCE']

logger.info("DB IP from fire_creds: {}".format(db_ip))
logger.info("DB instance from fire_creds: {}".format(db_instance))
logger.info("DB IP from fire_credentials: {}".format(db_ip))
logger.info("DB instance from fire_credentials: {}".format(db_instance))

# set the database URI
db_url = 'postgres+psycopg2://{db_user}:{db_pw}'.format(db_user=db_user, db_pw=db_pw)
db_url += '@/{db_name}?host=/cloudsql/{db_instance}'.format(db_name=db_name, db_instance=db_instance)
db_url = 'postgres+psycopg2://{u}:{pw}'.format(u=db_user, pw=db_pw)
db_url += '@/{db}?host=/cloudsql/{i}'.format(db=db_name, i=db_instance)
# db_url += '/.s.PGSQL.5432'

SQLALCHEMY_DATABASE_URI = db_url
Expand All @@ -62,4 +63,4 @@ class Config(object):
# silence the madness
SQLALCHEMY_TRACK_MODIFICATIONS = False

logger.debug("End of the Config() class.")
logger.info("End of the Config() class.")
18 changes: 9 additions & 9 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@
import googleclouddebugger
googleclouddebugger.enable()

logger.info("Cloud Debugger initialized! \n")
print("Cloud Debugger initialized! \n")
logger.info("Cloud Debugger initialized!")
print("Cloud Debugger initialized!")

except ImportError as error:
logger.info("Cloud Debugger import failed:\n{}".format(error))
print("Cloud Debugger import failed:\n{}".format(error))
logger.info("Cloud Debugger import failed: {}".format(error))
print("Cloud Debugger import failed: {}".format(error))

# initialize Stackdriver logging
try:
from dotenv import load_dotenv
load_dotenv(path.join(basedir, '.env'))
from env_tools import apply_env
apply_env()
cred_path = environ.get('LOCAL_GCP_CREDENTIALS_PATH')
logger.debug("cred_path: {}".format(cred_path))

Expand All @@ -34,11 +34,11 @@
client = google.cloud.logging.Client.from_service_account_json(json_file)
client.setup_logging() # attaches Stackdriver to python's standard logging module

logger.info("Logging to Stackdriver initialized! \n")
print("Logging to Stackdriver initialized! [from print()] \n")
logger.info("Logging to Stackdriver initialized!")
print("Logging to Stackdriver initialized! [from print()]")

except ImportError as error: # except OSError:
logger.info("Logging to Stackdriver failed:\n{}".format(error))
logger.info("Logging to Stackdriver failed: {}".format(error))

app = create_app()

Expand Down
6 changes: 1 addition & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
alembic==1.0.7
arrow==0.13.1
atomicwrites==1.3.0
attrs==19.1.0
Babel==2.6.0
beautifulsoup4==4.7.1
boto3==1.9.151
botocore==1.12.151
cachetools==3.1.0
Expand All @@ -16,7 +13,7 @@ docutils==0.14
dominate==2.3.5
env-tools==2.2.0
ez-setup==0.9
Flask==1.0.2
Flask==1.0.3
Flask-Bootstrap==3.3.7.1
Flask-Moment==0.7.0
Flask-SQLAlchemy==2.4.0
Expand Down Expand Up @@ -54,7 +51,6 @@ pytest==4.4.1
python-dateutil==2.8.0
python-editor==1.0.4
pytz==2018.9
regex==2019.4.14
requests==2.21.0
rsa==4.0
s3transfer==0.2.0
Expand Down

0 comments on commit a2be1fd

Please sign in to comment.