Skip to content

Commit 7725404

Browse files
Merge pull request #230 from cuappdev/fix-image-upload
fix image upload
2 parents 4caf1f5 + 6b0fa9e commit 7725404

1 file changed

Lines changed: 21 additions & 22 deletions

File tree

src/schema.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import graphene
2+
import base64
23
import os
34
from flask_jwt_extended import create_access_token, create_refresh_token, get_jwt_identity, get_jwt, jwt_required
45
from functools import wraps
@@ -68,6 +69,7 @@ def to_local_time(dt):
6869
# Convert to local timezone (server-local)
6970
return dt_utc.astimezone()
7071

72+
7173
def goal_at(goal_history, window_start_date):
7274
"""
7375
Determine the workout goal for a given window start date from the goal history.
@@ -83,6 +85,7 @@ def goal_at(goal_history, window_start_date):
8385

8486
return goal_history[-1][0]
8587

88+
8689
# MARK: - Gym
8790

8891

@@ -248,6 +251,7 @@ class Meta:
248251
def resolve_effective_at(self, info):
249252
return to_local_time(self.effective_at)
250253

254+
251255
# MARK: - User
252256

253257

@@ -258,8 +262,7 @@ class Meta:
258262
friendships = graphene.List(lambda: Friendship)
259263
friends = graphene.List(lambda: User)
260264
total_gym_days = graphene.Int(
261-
required=True,
262-
description="Get the total number of gym days (unique workout days) for user."
265+
required=True, description="Get the total number of gym days (unique workout days) for user."
263266
)
264267
streak_start = graphene.Date(
265268
description="The start date of the most recent active streak, up until the current date."
@@ -269,7 +272,9 @@ def resolve_total_gym_days(self, info):
269272
return (
270273
Workout.get_query(info)
271274
.filter(WorkoutModel.user_id == self.id)
272-
.with_entities(func.count(func.distinct(cast(WorkoutModel.workout_time, Date)))) # We cast the datetiem object as a Date object to get the unique days
275+
.with_entities(
276+
func.count(func.distinct(cast(WorkoutModel.workout_time, Date)))
277+
) # We cast the datetiem object as a Date object to get the unique days
273278
.scalar()
274279
)
275280

@@ -290,7 +295,7 @@ def resolve_active_streak(self, info):
290295
if not workout_date_rows:
291296
return 0
292297

293-
workout_dates = [row[0] for row in workout_date_rows]
298+
workout_dates = [row[0] for row in workout_date_rows]
294299

295300
goal_hist = (
296301
db_session.query(UserWorkoutGoalHistoryModel.workout_goal, UserWorkoutGoalHistoryModel.effective_at)
@@ -316,7 +321,7 @@ def resolve_active_streak(self, info):
316321

317322
day_iterator = day_pointer
318323
count_in_window = 0
319-
324+
320325
while day_iterator < total_workout_days and workout_dates[day_iterator] >= window_start:
321326
count_in_window += 1
322327
day_iterator += 1
@@ -357,10 +362,7 @@ def resolve_streak_start(self, info):
357362
return None
358363

359364
goal_hist = (
360-
db_session.query(
361-
UserWorkoutGoalHistoryModel.workout_goal,
362-
UserWorkoutGoalHistoryModel.effective_at,
363-
)
365+
db_session.query(UserWorkoutGoalHistoryModel.workout_goal, UserWorkoutGoalHistoryModel.effective_at)
364366
.filter(UserWorkoutGoalHistoryModel.user_id == user.id)
365367
.order_by(UserWorkoutGoalHistoryModel.effective_at.desc())
366368
.all()
@@ -450,7 +452,7 @@ def resolve_max_streak(self, info):
450452
if not workout_date_rows:
451453
return 0
452454

453-
workout_dates = [row[0] for row in workout_date_rows]
455+
workout_dates = [row[0] for row in workout_date_rows]
454456

455457
goal_hist = (
456458
db_session.query(UserWorkoutGoalHistoryModel.workout_goal, UserWorkoutGoalHistoryModel.effective_at)
@@ -484,7 +486,7 @@ def resolve_max_streak(self, info):
484486
count_in_window += 1
485487
day_iterator += 1
486488

487-
goal_days = goal_at(goal_hist, window_start)
489+
goal_days = goal_at(goal_hist, window_start)
488490

489491
if count_in_window == 0:
490492
max_met_goal = max(max_met_goal, run_met_goal)
@@ -554,6 +556,7 @@ def resolve_friend(self, info):
554556
def resolve_accepted_at(self, info):
555557
return to_local_time(self.accepted_at)
556558

559+
557560
# MARK: - Giveaway
558561

559562

@@ -703,7 +706,7 @@ def resolve_get_weekly_workout_days(self, info, id):
703706

704707
def resolve_get_all_reports(self, info):
705708
query = ReportModel.query.all()
706-
return query
709+
return query
707710

708711
def resolve_get_hourly_average_capacities_by_facility_id(self, info, facility_id):
709712
valid_facility_ids = [14492437, 8500985, 7169406, 10055021, 2323580, 16099753, 15446768, 12572681]
@@ -829,10 +832,11 @@ def mutate(self, info, name, net_id, email, encoded_image=None):
829832

830833
if encoded_image:
831834
upload_url = os.getenv("DIGITAL_OCEAN_URL")
832-
payload = {"bucket": os.getenv("BUCKET_NAME"), "image": encoded_image} # Base64-encoded image string
833-
headers = {"Content-Type": "application/json"}
835+
image_bytes = base64.b64decode(encoded_image)
836+
files = {"image": ("profile.png", image_bytes, "image/png")}
837+
data = {"bucket": os.getenv("BUCKET_NAME")}
834838
try:
835-
response = requests.post(upload_url, json=payload, headers=headers)
839+
response = requests.post(upload_url, files=files, data=data)
836840
response.raise_for_status()
837841
json_response = response.json()
838842
final_photo_url = json_response.get("data")
@@ -999,8 +1003,7 @@ class SetWorkoutGoals(graphene.Mutation):
9991003
class Arguments:
10001004
user_id = graphene.Int(required=True, description="The ID of the user.")
10011005
workout_goal = graphene.Int(
1002-
required=True,
1003-
description="The new workout goal for the user in terms of number of days per week.",
1006+
required=True, description="The new workout goal for the user in terms of number of days per week."
10041007
)
10051008

10061009
Output = User
@@ -1043,11 +1046,7 @@ def mutate(self, info, user_id, workout_goal):
10431046
user.workout_goal = workout_goal
10441047

10451048
db_session.add(
1046-
UserWorkoutGoalHistoryModel(
1047-
user_id=user.id,
1048-
workout_goal=workout_goal,
1049-
effective_at=effective_at,
1050-
)
1049+
UserWorkoutGoalHistoryModel(user_id=user.id, workout_goal=workout_goal, effective_at=effective_at)
10511050
)
10521051

10531052
db_session.commit()

0 commit comments

Comments
 (0)