forked from celestekilgore/friender-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
62 lines (39 loc) · 1.41 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import boto3
import os
import uuid
from pyzipcode import ZipCodeDatabase
from dotenv import load_dotenv
load_dotenv()
BUCKET_NAME = os.environ['BUCKET_NAME']
s3 = boto3.client(
's3',
aws_access_key_id=os.environ['ACCESS_KEY'],
aws_secret_access_key=os.environ['SECRET_ACCESS_KEY']
)
zcdb = ZipCodeDatabase()
def add_image(image):
"""Takes FileStorage image, uploads to S3 bucket, and returns public url."""
file_type, *_ = image.content_type.split("/")
if file_type != "image":
return {"errors": ["Invalid image"]}
filename = str(uuid.uuid4())
s3.upload_fileobj(
image,
BUCKET_NAME,
filename,
ExtraArgs={'ACL': 'public-read', 'ContentType': image.content_type})
return f"https://{BUCKET_NAME}.s3.amazonaws.com/{filename}"
def get_zip_codes_around_radius(zip_code, radius):
"""Takes zip code and radius and returns list of zip codes within radius."""
zip_codes = zcdb.get_zipcodes_around_radius(zip_code, radius)
return [z.zip for z in zip_codes]
def is_valid_zip_code(zip_code):
"""Takes zip code and returns boolean."""
return bool(zcdb.get(zip_code))
def form_errors_to_list(form_errors):
"""Takes WTForm dictionary errors and returns list of errors."""
errors = []
for field, field_errors in form_errors.items():
for error in field_errors:
errors.append(f"{field}: {error}")
return errors