Skip to content

Commit

Permalink
Fix timed emails (#16)
Browse files Browse the repository at this point in the history
* Fix keyerror

* Add stage environment variable

* Shrink timeout

* Remove event from connect()

* Fix for loop

* Properly fix for loop

* Remove event
  • Loading branch information
thebeanogamer authored Jan 8, 2020
1 parent 2605d6a commit 1f633f4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
10 changes: 5 additions & 5 deletions linkshortener/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from linkshortener.shortener import connect


def generate(event):
db = connect(event)
def generate():
db = connect()
page = (
jinja2.Environment(
loader=jinja2.FileSystemLoader(searchpath="./linkshortener/templates/"),
Expand Down Expand Up @@ -37,10 +37,10 @@ def view(event, context):


def summary(event, context):
if event["httpMethod"] is None:
if event.get("httpMethod") is None:
db = connect()
new = False
for i in db.scan():
for i in db.scan()["Items"]:
if i["uses"]["recent"] != 0:
new = True
if not new:
Expand All @@ -50,7 +50,7 @@ def summary(event, context):
Destination={"ToAddresses": [environ.get("ADMIN_CONTACT")]},
Message={
"Body": {
"Html": {"Charset": "UTF-8", "Data": generate(event)},
"Html": {"Charset": "UTF-8", "Data": generate()},
"Text": {
"Charset": "UTF-8",
"Data": "This email must be viewed with HTML",
Expand Down
14 changes: 7 additions & 7 deletions linkshortener/shortener.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
from botocore.config import Config


def connect(event):
def connect():
return boto3.resource(
"dynamodb",
endpoint_url="http://localhost:8000"
if event["requestContext"]["stage"] == "dev"
if os.environ.get("STAGE") == "dev"
else None,
region_name="eu-west-2",
config=Config(connect_timeout=5, retries={"max_attempts": 3}),
Expand Down Expand Up @@ -40,7 +40,7 @@ def redirect(url):


def shortener(event, context):
db = connect(event)
db = connect()
try:
url = db.get_item(Key={"code": event["pathParameters"]["id"]})["Item"]["url"]
db.update_item(
Expand All @@ -57,7 +57,7 @@ def shortener(event, context):


def create(event, context):
db = connect(event)
db = connect()
try:
db.put_item(
Item={
Expand All @@ -74,7 +74,7 @@ def create(event, context):


def view(event, context):
db = connect(event)
db = connect()
if event["queryStringParameters"] is not None and event[
"queryStringParameters"
].get("code") not in (None, ""):
Expand All @@ -90,7 +90,7 @@ def view(event, context):


def delete(event, context):
db = connect(event)
db = connect()
try:
db.delete_item(
Key={"code": sanitize(json.loads(event["body"])["code"])},
Expand All @@ -103,7 +103,7 @@ def delete(event, context):


def fallback(event, context):
return redirect(os.environ["FALLBACK_URL"])
return redirect(os.environ.get("FALLBACK_URL"))


def robots(event, context):
Expand Down
3 changes: 2 additions & 1 deletion serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ provider:
runtime: python3.8
region: eu-west-2
memorySize: 128
timeout: 20
timeout: 6
apiKeys:
- ${opt:stage}-adminkey
environment:
Expand All @@ -16,6 +16,7 @@ provider:
DOMAIN: ${env:DOMAIN}
SES_REGION: ${env:SES_REGION}
FALLBACK_URL: ${env:FALLBACK_URL}
STAGE: ${opt:stage, self:provider.stage}
iamRoleStatements:
- Effect: Allow
Action:
Expand Down

0 comments on commit 1f633f4

Please sign in to comment.