Skip to content

Commit

Permalink
Adding steps _actually_ works now
Browse files Browse the repository at this point in the history
  • Loading branch information
brystmar committed Mar 29, 2020
1 parent 1510e1c commit 2688e0d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
2 changes: 1 addition & 1 deletion app.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# https://yamlchecker.com/
runtime: python37
runtime: python38
includes:
- env_variables.yaml

Expand Down
17 changes: 12 additions & 5 deletions backend/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@
from backend.global_logger import logger
from datetime import datetime, timedelta
from pynamodb.attributes import ListAttribute
import shortuuid


def generate_new_id() -> str:
"""Primary key (id) is a 17-digit epoch timestamp. Ex: 1560043140.168794"""
def generate_new_id(short=False) -> str:
"""
Recipe primary key (id) is a 17-digit epoch timestamp. Ex: 1560043140.168794. Will eventually change to short uuid.
Step primary key (step_id) is a 22-digit short uuid.
"""
# Ensure all ids are the same length. Timestamps can end in 0, which the system truncates.
new_id = ""
while len(new_id) != 17:
new_id = str(datetime.utcnow().timestamp())
return new_id
if short:
return shortuuid.uuid()
else:
while len(new_id) != 17:
new_id = str(datetime.utcnow().timestamp())
return new_id


def set_when(steps: ListAttribute(), when: datetime) -> list:
Expand Down
13 changes: 7 additions & 6 deletions backend/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
from datetime import datetime, timedelta
from pynamodb.models import Model
from pynamodb.attributes import UnicodeAttribute, UTCDateTimeAttribute, NumberAttribute, \
MapAttribute, ListAttribute, BooleanAttribute, Attribute
MapAttribute, ListAttribute, BooleanAttribute
import json
import shortuuid


class Step(MapAttribute):
Expand Down Expand Up @@ -69,8 +68,8 @@ def __init__(self, step_id=step_id, number=number, text=text, then_wait=then_wai
super().__init__(**attrs)

# Null handling for step_id is a little different until the Prod db is updated
if isinstance(step_id, UnicodeAttribute):
step_id = shortuuid.uuid()
if isinstance(step_id, UnicodeAttribute) or step_id is None:
step_id = generate_new_id(short=True)

self.step_id = step_id
self.number = self._null_handler(number)
Expand Down Expand Up @@ -120,7 +119,9 @@ class Meta:
steps = ListAttribute(of=Step)

## Datetime attributes ##
# Each is stored as a UTC timestamp, no time zone
# Each is stored as a UTC timestamp
# TODO: Change the db values to epoch
# TODO: Convert epoch to/from datetime in the Recipe model
date_added = UTCDateTimeAttribute()
start_time = UTCDateTimeAttribute()

Expand Down Expand Up @@ -189,7 +190,7 @@ def __init__(self, id=id, name=name, author=author, source=source, difficulty=di
"""Update the UI fields when initialized, as necessary."""
super().__init__(**attrs)

self.id = id or generate_new_id()
self.id = id or generate_new_id(short=False)
self.name = name
self.author = self._null_handler(author)
self.source = self._null_handler(source)
Expand Down
3 changes: 2 additions & 1 deletion backend/recipe_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def post(self) -> json:
# Create a new Recipe from the provided data
try:
now = datetime.utcnow()
new_recipe = Recipe(id=generate_new_id(),
new_recipe = Recipe(id=None,
name=data['name'],
difficulty=data['difficulty'],
length=0,
Expand Down Expand Up @@ -187,6 +187,7 @@ def put(self, recipe_id) -> json:

# If there are steps, create a Step for each, then calculate the recipe length
try:
# TODO: Move this into Recipe.__init__
if data['steps']:
logger.debug(f"Parsing step array into a list of {len(data['steps'])} Steps.")
recipe.steps = []
Expand Down

0 comments on commit 2688e0d

Please sign in to comment.