1+ import base64
2+
13import graphene
24import os
35from flask_jwt_extended import create_access_token , create_refresh_token , get_jwt_identity , get_jwt , jwt_required
@@ -68,6 +70,7 @@ def to_local_time(dt):
6870 # Convert to local timezone (server-local)
6971 return dt_utc .astimezone ()
7072
73+
7174def goal_at (goal_history , window_start_date ):
7275 """
7376 Determine the workout goal for a given window start date from the goal history.
@@ -83,6 +86,7 @@ def goal_at(goal_history, window_start_date):
8386
8487 return goal_history [- 1 ][0 ]
8588
89+
8690# MARK: - Gym
8791
8892
@@ -248,6 +252,7 @@ class Meta:
248252 def resolve_effective_at (self , info ):
249253 return to_local_time (self .effective_at )
250254
255+
251256# MARK: - User
252257
253258
@@ -258,8 +263,7 @@ class Meta:
258263 friendships = graphene .List (lambda : Friendship )
259264 friends = graphene .List (lambda : User )
260265 total_gym_days = graphene .Int (
261- required = True ,
262- description = "Get the total number of gym days (unique workout days) for user."
266+ required = True , description = "Get the total number of gym days (unique workout days) for user."
263267 )
264268 streak_start = graphene .Date (
265269 description = "The start date of the most recent active streak, up until the current date."
@@ -269,7 +273,9 @@ def resolve_total_gym_days(self, info):
269273 return (
270274 Workout .get_query (info )
271275 .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
276+ .with_entities (
277+ func .count (func .distinct (cast (WorkoutModel .workout_time , Date )))
278+ ) # We cast the datetiem object as a Date object to get the unique days
273279 .scalar ()
274280 )
275281
@@ -290,7 +296,7 @@ def resolve_active_streak(self, info):
290296 if not workout_date_rows :
291297 return 0
292298
293- workout_dates = [row [0 ] for row in workout_date_rows ]
299+ workout_dates = [row [0 ] for row in workout_date_rows ]
294300
295301 goal_hist = (
296302 db_session .query (UserWorkoutGoalHistoryModel .workout_goal , UserWorkoutGoalHistoryModel .effective_at )
@@ -316,7 +322,7 @@ def resolve_active_streak(self, info):
316322
317323 day_iterator = day_pointer
318324 count_in_window = 0
319-
325+
320326 while day_iterator < total_workout_days and workout_dates [day_iterator ] >= window_start :
321327 count_in_window += 1
322328 day_iterator += 1
@@ -357,10 +363,7 @@ def resolve_streak_start(self, info):
357363 return None
358364
359365 goal_hist = (
360- db_session .query (
361- UserWorkoutGoalHistoryModel .workout_goal ,
362- UserWorkoutGoalHistoryModel .effective_at ,
363- )
366+ db_session .query (UserWorkoutGoalHistoryModel .workout_goal , UserWorkoutGoalHistoryModel .effective_at )
364367 .filter (UserWorkoutGoalHistoryModel .user_id == user .id )
365368 .order_by (UserWorkoutGoalHistoryModel .effective_at .desc ())
366369 .all ()
@@ -450,7 +453,7 @@ def resolve_max_streak(self, info):
450453 if not workout_date_rows :
451454 return 0
452455
453- workout_dates = [row [0 ] for row in workout_date_rows ]
456+ workout_dates = [row [0 ] for row in workout_date_rows ]
454457
455458 goal_hist = (
456459 db_session .query (UserWorkoutGoalHistoryModel .workout_goal , UserWorkoutGoalHistoryModel .effective_at )
@@ -484,7 +487,7 @@ def resolve_max_streak(self, info):
484487 count_in_window += 1
485488 day_iterator += 1
486489
487- goal_days = goal_at (goal_hist , window_start )
490+ goal_days = goal_at (goal_hist , window_start )
488491
489492 if count_in_window == 0 :
490493 max_met_goal = max (max_met_goal , run_met_goal )
@@ -554,6 +557,7 @@ def resolve_friend(self, info):
554557 def resolve_accepted_at (self , info ):
555558 return to_local_time (self .accepted_at )
556559
560+
557561# MARK: - Giveaway
558562
559563
@@ -703,7 +707,7 @@ def resolve_get_weekly_workout_days(self, info, id):
703707
704708 def resolve_get_all_reports (self , info ):
705709 query = ReportModel .query .all ()
706- return query
710+ return query
707711
708712 def resolve_get_hourly_average_capacities_by_facility_id (self , info , facility_id ):
709713 valid_facility_ids = [14492437 , 8500985 , 7169406 , 10055021 , 2323580 , 16099753 , 15446768 , 12572681 ]
@@ -829,10 +833,11 @@ def mutate(self, info, name, net_id, email, encoded_image=None):
829833
830834 if encoded_image :
831835 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" }
836+ image_bytes = base64 .b64decode (encoded_image )
837+ files = {"image" : ("profile.png" , image_bytes , "image/png" )}
838+ data = {"bucket" : os .getenv ("BUCKET_NAME" )}
834839 try :
835- response = requests .post (upload_url , json = payload , headers = headers )
840+ response = requests .post (upload_url , files = files , data = data )
836841 response .raise_for_status ()
837842 json_response = response .json ()
838843 final_photo_url = json_response .get ("data" )
@@ -999,8 +1004,7 @@ class SetWorkoutGoals(graphene.Mutation):
9991004 class Arguments :
10001005 user_id = graphene .Int (required = True , description = "The ID of the user." )
10011006 workout_goal = graphene .Int (
1002- required = True ,
1003- description = "The new workout goal for the user in terms of number of days per week." ,
1007+ required = True , description = "The new workout goal for the user in terms of number of days per week."
10041008 )
10051009
10061010 Output = User
@@ -1043,11 +1047,7 @@ def mutate(self, info, user_id, workout_goal):
10431047 user .workout_goal = workout_goal
10441048
10451049 db_session .add (
1046- UserWorkoutGoalHistoryModel (
1047- user_id = user .id ,
1048- workout_goal = workout_goal ,
1049- effective_at = effective_at ,
1050- )
1050+ UserWorkoutGoalHistoryModel (user_id = user .id , workout_goal = workout_goal , effective_at = effective_at )
10511051 )
10521052
10531053 db_session .commit ()
0 commit comments