Skip to content

Commit 963494a

Browse files
authored
Merge pull request #77 from tejasai2007/praveshan
feat: add mentor_mentee map function and delete users from data function
2 parents b5e006d + 9e590eb commit 963494a

File tree

2 files changed

+77
-7
lines changed

2 files changed

+77
-7
lines changed

app/db/crud.py

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ def get_submissions_for_user(db: Session, email: str, track_id: Optional[int] =
155155

156156
def 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

162162
def 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()

app/main.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from fastapi import FastAPI
2-
from app.db.crud import sync_users_from_sheet
2+
from app.db.crud import sync_users_from_sheet,mentor_mentee_map,delete_users_from_data
33
from app.db.db import Base, engine
44
from app.routes import auth, progress, tracks, leaderboard, mentors , submissions
55
from fastapi.middleware.cors import CORSMiddleware
@@ -19,6 +19,8 @@ async def start_background_sync():
1919
async def loop():
2020
while True:
2121
sync_users_from_sheet()
22+
mentor_mentee_map()
23+
delete_users_from_data()
2224
await asyncio.sleep(60)
2325
asyncio.create_task(loop())
2426

0 commit comments

Comments
 (0)