Skip to content

Commit eceaed1

Browse files
committed
logs
1 parent c265893 commit eceaed1

1 file changed

Lines changed: 70 additions & 37 deletions

File tree

src/schema.py

Lines changed: 70 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
from sqlalchemy import func, cast, Date
3737
import boto3
3838
from botocore.config import Config
39-
from botocore.exceptions import ClientError
4039

4140
local_tz = ZoneInfo("America/New_York")
4241

@@ -844,37 +843,53 @@ def mutate(self, info, name, net_id, email, encoded_image=None):
844843
final_photo_url = None
845844

846845
if encoded_image:
847-
848-
s3 = boto3.client(
849-
"s3",
850-
endpoint_url=os.getenv("DIGITAL_OCEAN_URL"),
851-
aws_access_key_id=os.getenv("DIGITAL_OCEAN_ACCESS"),
852-
aws_secret_access_key=os.getenv("DIGITAL_OCEAN_SECRET_ACCESS"),
853-
config=Config(s3={"addressing_style": "path"}),
846+
bucket = "appdev-upload"
847+
path = f"uplift-dev/user-profile/{net_id}-profile.png"
848+
region = "nyc3"
849+
850+
logging.info(f"DIGITAL_OCEAN_URL: {os.getenv('DIGITAL_OCEAN_URL')}")
851+
logging.info(
852+
"CreateUser profile picture upload: net_id=%s, bucket=%s, key=%s",
853+
net_id,
854+
bucket,
855+
path,
854856
)
855-
857+
856858
try:
857859
image_data = base64.b64decode(encoded_image, validate=True)
858860
except (binascii.Error, ValueError) as err:
859-
raise GraphQLError("Invalid profile image encoding.")
861+
logging.warning(
862+
"Invalid profile image encoding: %s: %s",
863+
type(err).__name__,
864+
err,
865+
)
866+
raise GraphQLError("Invalid profile image encoding.")
860867

861868
try:
862-
bucket = "appdev-upload"
863-
path = f"uplift-dev/user-profile/{net_id}-profile.png"
864-
region = "nyc3"
865-
869+
logging.info("Attempting S3 put_object for new user profile picture...")
870+
s3 = boto3.client(
871+
"s3",
872+
endpoint_url=os.getenv("DIGITAL_OCEAN_URL"),
873+
aws_access_key_id=os.getenv("DIGITAL_OCEAN_ACCESS"),
874+
aws_secret_access_key=os.getenv("DIGITAL_OCEAN_SECRET_ACCESS"),
875+
config=Config(s3={"addressing_style": "path"}),
876+
)
866877
s3.put_object(
867878
Bucket=bucket,
868-
Key=path,
879+
Key=path,
869880
Body=image_data,
870881
ContentType="image/png",
871-
ACL="public-read"
882+
ACL="public-read",
872883
)
873-
884+
logging.info("S3 put_object succeeded for new user profile picture")
874885
final_photo_url = f"https://{bucket}.{region}.digitaloceanspaces.com/{path}"
875-
except ClientError as e:
876-
print("Upload error:", e)
877-
raise GraphQLError("Error uploading user profile picture.")
886+
except Exception as e:
887+
logging.error(
888+
"S3 upload failed (create user): %s: %s",
889+
type(e).__name__,
890+
e,
891+
)
892+
raise GraphQLError(f"S3 error: {type(e).__name__}: {e}")
878893

879894
new_user = UserModel(name=name, net_id=net_id, email=email, encoded_image=final_photo_url)
880895
db_session.add(new_user)
@@ -906,37 +921,55 @@ def mutate(self, info, user_id, name=None, email=None, encoded_image=None):
906921
existing_user.email = email
907922
if encoded_image is not None:
908923
final_photo_url = None
909-
s3 = boto3.client(
910-
"s3",
911-
endpoint_url=os.getenv("DIGITAL_OCEAN_URL"),
912-
aws_access_key_id=os.getenv("DIGITAL_OCEAN_ACCESS"),
913-
aws_secret_access_key=os.getenv("DIGITAL_OCEAN_SECRET_ACCESS"),
914-
config=Config(s3={"addressing_style": "path"}),
924+
bucket = "appdev-upload"
925+
path = f"uplift-dev/user-profile/{existing_user.net_id}-profile.png"
926+
region = "nyc3"
927+
928+
logging.info(f"DIGITAL_OCEAN_URL: {os.getenv('DIGITAL_OCEAN_URL')}")
929+
logging.info(
930+
"EditUser profile picture upload: user_id=%s, net_id=%s, bucket=%s, key=%s",
931+
user_id,
932+
existing_user.net_id,
933+
bucket,
934+
path,
915935
)
916-
936+
917937
try:
918938
image_data = base64.b64decode(encoded_image, validate=True)
919939
except (binascii.Error, ValueError) as err:
940+
logging.warning(
941+
"Invalid profile image encoding: %s: %s",
942+
type(err).__name__,
943+
err,
944+
)
920945
raise GraphQLError("Invalid profile image encoding.")
921946

922947
try:
923-
bucket = "appdev-upload"
924-
path = f"uplift-dev/user-profile/{existing_user.net_id}-profile.png"
925-
region = "nyc3"
926-
948+
logging.info("Attempting S3 put_object for edited user profile picture...")
949+
s3 = boto3.client(
950+
"s3",
951+
endpoint_url=os.getenv("DIGITAL_OCEAN_URL"),
952+
aws_access_key_id=os.getenv("DIGITAL_OCEAN_ACCESS"),
953+
aws_secret_access_key=os.getenv("DIGITAL_OCEAN_SECRET_ACCESS"),
954+
config=Config(s3={"addressing_style": "path"}),
955+
)
927956
s3.put_object(
928957
Bucket=bucket,
929-
Key=path,
958+
Key=path,
930959
Body=image_data,
931960
ContentType="image/png",
932-
ACL="public-read"
961+
ACL="public-read",
933962
)
934-
963+
logging.info("S3 put_object succeeded for edited user profile picture")
935964
final_photo_url = f"https://{bucket}.{region}.digitaloceanspaces.com/{path}"
936965
existing_user.encoded_image = final_photo_url
937-
except ClientError as e:
938-
print("Upload error:", e)
939-
raise GraphQLError("Error adding new user profile picture.")
966+
except Exception as e:
967+
logging.error(
968+
"S3 upload failed (edit user): %s: %s",
969+
type(e).__name__,
970+
e,
971+
)
972+
raise GraphQLError(f"S3 error: {type(e).__name__}: {e}")
940973

941974
db_session.commit()
942975
return existing_user

0 commit comments

Comments
 (0)