1- from flask import Blueprint , Response , jsonify , session , request
1+ from flask import Blueprint , Response , jsonify , session , request , current_app
22from functools import wraps
33from werkzeug .security import generate_password_hash
44from db .database import db
@@ -52,19 +52,58 @@ def import_students():
5252 if course :
5353 courses .append (course )
5454
55+ app = current_app ._get_current_object ()
56+
5557 def generate ():
56- total = len (student_list )
57- imported = 0
58- skipped = 0
59- errors = []
60-
61- for i , entry in enumerate (student_list ):
62- name = entry .get ('name' , '' ).strip ()
63- email = entry .get ('email' , '' ).strip ().lower ()
64-
65- if not email :
66- errors .append (f'Linha { i + 1 } : email vazio' )
67- skipped += 1
58+ with app .app_context ():
59+ total = len (student_list )
60+ imported = 0
61+ skipped = 0
62+ errors = []
63+
64+ for i , entry in enumerate (student_list ):
65+ name = entry .get ('name' , '' ).strip ()
66+ email = entry .get ('email' , '' ).strip ().lower ()
67+
68+ if not email :
69+ errors .append (f'Linha { i + 1 } : email vazio' )
70+ skipped += 1
71+ yield json .dumps ({
72+ 'progress' : {
73+ 'current' : i + 1 ,
74+ 'total' : total ,
75+ 'imported' : imported ,
76+ 'skipped' : skipped ,
77+ }
78+ }) + '\n '
79+ continue
80+
81+ if not name :
82+ name = email .split ('@' )[0 ]
83+
84+ # Check existing
85+ existing = Student .query .filter (
86+ Student .email .ilike (email )
87+ ).first ()
88+
89+ if existing :
90+ # Add courses to existing student
91+ for c in courses :
92+ if c not in existing .courses :
93+ existing .courses .append (c )
94+ db .session .commit ()
95+ skipped += 1
96+ else :
97+ # Create new student
98+ hashed = generate_password_hash (default_password )
99+ new_student = Student (email = email , password = hashed , name = name )
100+ db .session .add (new_student )
101+ db .session .flush ()
102+ for c in courses :
103+ new_student .courses .append (c )
104+ db .session .commit ()
105+ imported += 1
106+
68107 yield json .dumps ({
69108 'progress' : {
70109 'current' : i + 1 ,
@@ -73,49 +112,13 @@ def generate():
73112 'skipped' : skipped ,
74113 }
75114 }) + '\n '
76- continue
77-
78- if not name :
79- name = email .split ('@' )[0 ]
80-
81- # Check existing
82- existing = Student .query .filter (
83- Student .email .ilike (email )
84- ).first ()
85-
86- if existing :
87- # Add courses to existing student
88- for c in courses :
89- if c not in existing .courses :
90- existing .courses .append (c )
91- db .session .commit ()
92- skipped += 1
93- else :
94- # Create new student
95- hashed = generate_password_hash (default_password )
96- new_student = Student (email = email , password = hashed , name = name )
97- db .session .add (new_student )
98- db .session .flush ()
99- for c in courses :
100- new_student .courses .append (c )
101- db .session .commit ()
102- imported += 1
103115
104116 yield json .dumps ({
105- 'progress' : {
106- 'current' : i + 1 ,
107- 'total' : total ,
108- 'imported' : imported ,
109- 'skipped' : skipped ,
110- }
117+ 'done' : True ,
118+ 'imported' : imported ,
119+ 'skipped' : skipped ,
120+ 'total' : total ,
121+ 'errors' : errors ,
111122 }) + '\n '
112123
113- yield json .dumps ({
114- 'done' : True ,
115- 'imported' : imported ,
116- 'skipped' : skipped ,
117- 'total' : total ,
118- 'errors' : errors ,
119- }) + '\n '
120-
121124 return Response (generate (), mimetype = 'application/x-ndjson' )
0 commit comments