@@ -155,8 +155,8 @@ def get_submissions_for_user(db: Session, email: str, track_id: Optional[int] =
155155
156156def get_sheet_data ():
157157 client = _gspread_client ()
158- worksheet = client .open_by_key (os .getenv ("GOOGLE_SHEET_ID" )).worksheet ("P1-Mapping" )
159- expected_headers = ["Full name" , "Email address" ]
158+ worksheet = client .open_by_key (os .getenv ("GOOGLE_SHEET_ID" )).worksheet ("Copy of P1-Mapping" )
159+ expected_headers = ["Full name" , "Email address" , "Faction name" , "Status" ]
160160 return worksheet .get_all_records (expected_headers = expected_headers )
161161
162162def sync_users_from_sheet ():
@@ -167,16 +167,84 @@ def sync_users_from_sheet():
167167 for row in rows :
168168 email = row .get ("Email address" , "" ).strip ()
169169 name = row .get ("Full name" , "" ).strip ()
170- if not email or not name :
170+ faction = row .get ("Faction name" , "" ).strip ()
171+ status = row .get ("Status" , "" ).strip ().lower ()
172+ if not email or not name or not faction or status == "kicked" :
171173 continue
172174 if get_user_by_email (db , email ):
173175 continue
174- user = models .User (name = name , email = email , role = "mentee" )
176+ user = models .User (name = name , email = email , role = "mentee" , group_name = faction )
175177 db .add (user )
176178 inserted_count += 1
177- db .commit ()
179+ db .commit ()
178180 print (f"Inserted { inserted_count } new users." )
179181 except Exception as e :
180182 print (f"Error syncing users: { e } " )
181183 finally :
182- db .close ()
184+ db .close ()
185+
186+ def mentor_mentee_map ():
187+ db : Session = SessionLocal ()
188+ try :
189+ mentors = db .query (models .User ).filter (models .User .role == "mentor" ).all ()
190+ mentees = db .query (models .User ).filter (models .User .role == "mentee" ).all ()
191+ existing = {
192+ (m .mentor_id , m .mentee_id )
193+ for m in db .query (models .MentorMenteeMap ).all ()
194+ }
195+
196+ new_mappings = []
197+ for mentor in mentors :
198+ for mentee in mentees :
199+ if (mentor .id , mentee .id ) not in existing :
200+ new_mappings .append (
201+ models .MentorMenteeMap (
202+ mentor_id = mentor .id ,
203+ mentee_id = mentee .id ,
204+ )
205+ )
206+ if new_mappings :
207+ db .add_all (new_mappings )
208+ db .commit ()
209+ print (f"Added { len (new_mappings )} new mentor-mentee mappings" , flush = True )
210+ else :
211+ print ("No new mappings needed" , flush = True )
212+
213+ except Exception as e :
214+ db .rollback ()
215+ print (f"Error in mentor_mentee_map: { e } " , flush = True )
216+ finally :
217+ db .close ()
218+
219+ def delete_users_from_data ():
220+ db : Session = SessionLocal ()
221+ try :
222+ rows = get_sheet_data ()
223+ deleted_count = 0
224+ for row in rows :
225+ name = row .get ("Full name" , "" ).strip ()
226+ email = row .get ("Email address" , "" ).strip ().lower ()
227+ status = row .get ("Status" , "" ).strip ().lower ()
228+ if not name or status != "kicked" :
229+ continue
230+ query = db .query (models .User )
231+ if email :
232+ user = query .filter (models .User .email == email ).first ()
233+ else :
234+ user = query .filter (models .User .name == name ).first ()
235+ if not user :
236+ continue
237+ db .query (models .MentorMenteeMap ).filter (
238+ (models .MentorMenteeMap .mentee_id == user .id ) |
239+ (models .MentorMenteeMap .mentor_id == user .id )
240+ ).delete (synchronize_session = False )
241+ db .delete (user )
242+ deleted_count += 1
243+ print (f"Deleted user { user .name } ({ user .email } ) and mappings." )
244+ db .commit ()
245+ print (f"Deleted { deleted_count } kicked users and their mappings." )
246+ except Exception as e :
247+ db .rollback ()
248+ print (f"Error deleting users: { e } " )
249+ finally :
250+ db .close ()
0 commit comments