"""Import the reviewed, local source snapshot once; never overwrite live data.""" import json from datetime import date from pathlib import Path from werkzeug.security import generate_password_hash DEFAULT_PASSWORD = 'Password@123' def next_coordinator(db): roster=db.execute('SELECT * FROM users WHERE active=1 AND archived=0 AND rotation_order IS NOT NULL ORDER BY rotation_order,id').fetchall() if not roster: raise ValueError('Add at least one active member to the coordinator rotation.') current=db.execute('SELECT coordinator_id FROM meetings ORDER BY meeting_date DESC LIMIT 1').fetchone() ids=[u['id'] for u in roster] index=(ids.index(current['coordinator_id'])+1)%len(ids) if current and current['coordinator_id'] in ids else 0 return roster[index] def create_meeting(db, meeting_date, title, location, presenter='', minutes_by='', coordinator_id=None): if coordinator_id is None: coordinator_id=next_coordinator(db)['id'] coordinator=db.execute('SELECT name FROM users WHERE id=?',(coordinator_id,)).fetchone() minutes_by=minutes_by or coordinator['name'] db.execute("UPDATE meetings SET status='closed' WHERE status='active'") meeting_id = db.execute( 'INSERT INTO meetings(meeting_date,title,location,presenter,minutes_by,status,coordinator_id) VALUES(?,?,?,?,?,?,?)', (meeting_date, title, location, presenter, minutes_by, 'active',coordinator_id)).lastrowid db.execute('INSERT INTO reviews(meeting_id,action_id) SELECT ?,a.id FROM actions a JOIN users u ON u.id=a.user_id WHERE a.completed=0 AND a.archived=0 AND u.archived=0', (meeting_id,)) return meeting_id def seed_database(db, source_path): if db.execute("SELECT 1 FROM metadata WHERE key='seeded'").fetchone(): return source = json.loads(Path(source_path).read_text(encoding='utf-8')) ids = {} for order,user in enumerate(source['users'],1): ids[user['name']] = db.execute( 'INSERT INTO users(name,username,password_hash,is_admin,rotation_order) VALUES(?,?,?,?,?)', (user['name'], user['username'], generate_password_hash(DEFAULT_PASSWORD), user['username']=='kishan',order if order<=7 else None)).lastrowid for index, report in enumerate(source['reports']): mid = db.execute('INSERT INTO meetings(meeting_date,title,location,presenter,minutes_by,status,source_file,coordinator_id) VALUES(?,?,?,?,?,?,?,?)', (report['date'],report['title'],report['location'],report['presenter'],'Kishan H M','closed',report['file'],ids['Kishan H M'])).lastrowid contributors = set() for update in report['updates']: uid = ids[update['owner']] contributors.add(uid) db.execute('INSERT INTO updates(meeting_id,user_id,body) VALUES(?,?,?)',(mid,uid,update['body'])) for action in report['actions']: uid = ids[action['owner']] contributors.add(uid) db.execute('INSERT INTO actions(meeting_id,user_id,body,due_date,due_note,archived) VALUES(?,?,?,?,?,?)', (mid,uid,action['body'],action['due_date'],action['due_note'],index != len(source['reports'])-1)) for uid in contributors: db.execute('INSERT INTO submissions(meeting_id,user_id,reviewed,submitted_at) VALUES(?,?,1,?)',(mid,uid,report['date']+' 00:00:00')) latest = source['reports'][-1] from datetime import timedelta earliest=max(date.today(),date.fromisoformat(latest['date'])+timedelta(days=7)) first_date = (earliest+timedelta(days=(-earliest.weekday())%7)).isoformat() create_meeting(db, first_date, latest['title'], latest['location']) db.execute("INSERT INTO metadata(key,value) VALUES('seeded','1')") db.commit() def migrate_weekly(db): """One-time migration for the initial local preview, preserving entered data.""" if 'archived' not in [r['name'] for r in db.execute('PRAGMA table_info(users)')]: db.execute('ALTER TABLE users ADD COLUMN archived INTEGER NOT NULL DEFAULT 0') db.commit() if 'report_order' not in [r['name'] for r in db.execute('PRAGMA table_info(users)')]: db.execute('ALTER TABLE users ADD COLUMN report_order INTEGER') db.commit() if db.execute('SELECT 1 FROM users LIMIT 1').fetchone() and not db.execute("SELECT 1 FROM metadata WHERE key='report_order_v1'").fetchone(): from exporter import report_person_key users=db.execute('SELECT id,name FROM users').fetchall() for position,user in enumerate(sorted(users,key=lambda user:report_person_key(user['name'])),1): db.execute('UPDATE users SET report_order=? WHERE id=?',(position,user['id'])) db.execute("INSERT INTO metadata(key,value) VALUES('report_order_v1','1')") db.commit() if 'prepared_on' not in [r['name'] for r in db.execute('PRAGMA table_info(meetings)')]: db.execute('ALTER TABLE meetings ADD COLUMN prepared_on TEXT') db.commit() if 'rotation_order' not in [r['name'] for r in db.execute('PRAGMA table_info(users)')]: db.execute('ALTER TABLE users ADD COLUMN rotation_order INTEGER') db.execute('UPDATE users SET rotation_order=id') if 'coordinator_id' not in [r['name'] for r in db.execute('PRAGMA table_info(meetings)')]: db.execute('ALTER TABLE meetings ADD COLUMN coordinator_id INTEGER REFERENCES users(id)') if db.execute('SELECT 1 FROM users LIMIT 1').fetchone() and not db.execute("SELECT 1 FROM metadata WHERE key='seven_member_rotation'").fetchone(): rotation=['kishan','harshitha','gokul','manish','madhan','honey','madhav'] db.execute('UPDATE users SET rotation_order=NULL') for position,username in enumerate(rotation,1): db.execute('UPDATE users SET rotation_order=? WHERE username=?',(position,username)) db.execute("INSERT INTO metadata(key,value) VALUES('seven_member_rotation','1')") db.commit() if db.execute("SELECT 1 FROM metadata WHERE key='weekly_rotation_v1'").fetchone(): return kishan=db.execute("SELECT id FROM users WHERE username='kishan'").fetchone() if kishan: db.execute("UPDATE meetings SET coordinator_id=?,minutes_by='Kishan H M' WHERE source_file IS NOT NULL",(kishan['id'],)) db.execute("UPDATE meetings SET meeting_date='2026-09-21' WHERE source_file='MOM_Technical Report on 15-09-2026.xlsx'") current=db.execute("SELECT * FROM meetings WHERE status='active'").fetchone() if current and not current['coordinator_id']: following=db.execute("SELECT id,name FROM users WHERE username='harshitha'").fetchone() db.execute('UPDATE meetings SET coordinator_id=?,minutes_by=? WHERE id=?',(following['id'],following['name'],current['id'])) count=db.execute('SELECT (SELECT COUNT(*) FROM updates WHERE meeting_id=?)+(SELECT COUNT(*) FROM actions WHERE meeting_id=?)',(current['id'],current['id'])).fetchone()[0] if not count and current['meeting_date']=='2026-09-23': db.execute("UPDATE meetings SET meeting_date='2026-09-28' WHERE id=?",(current['id'],)) db.execute("INSERT INTO metadata(key,value) VALUES('weekly_rotation_v1','1')") db.commit()