@@ -54,6 +54,7 @@ def manifest_data_to_db(course_name, manifest_path):
5454 chapters = Table ("chapters" , meta , autoload = True , autoload_with = engine )
5555 subchapters = Table ("sub_chapters" , meta , autoload = True , autoload_with = engine )
5656 questions = Table ("questions" , meta , autoload = True , autoload_with = engine )
57+ source_code = Table ("source_code" , meta , autoload = True , autoload_with = engine )
5758 course_attributes = Table (
5859 "course_attributes" , meta , autoload = True , autoload_with = engine
5960 )
@@ -155,12 +156,15 @@ def manifest_data_to_db(course_name, manifest_path):
155156 logger .debug ("looking for data-component" )
156157 # pdb.set_trace()
157158 el = question .find (".//*[@data-component]" )
159+ old_ww_id = None
158160 # Unbelievably if find finds something it evals to False!!
159161 if el is not None :
160162 if "id" in el .attrib :
161163 idchild = el .attrib ["id" ]
162164 else :
163165 idchild = "fix_me"
166+ if "the-id-on-the-webwork" in el .attrib :
167+ old_ww_id = el .attrib ["the-id-on-the-webwork" ]
164168 else :
165169 el = question .find ("./div" )
166170 if el is None :
@@ -194,15 +198,19 @@ def manifest_data_to_db(course_name, manifest_path):
194198 chapter = chapter .find ("./id" ).text ,
195199 qnumber = qlabel ,
196200 )
201+ if old_ww_id :
202+ namekey = old_ww_id
203+ else :
204+ namekey = idchild
197205 res = sess .execute (
198- f"""select * from questions where name='{ idchild } ' and base_course='{ course_name } '"""
206+ f"""select * from questions where name='{ namekey } ' and base_course='{ course_name } '"""
199207 ).first ()
200208 if res :
201209 ins = (
202210 questions .update ()
203211 .where (
204212 and_ (
205- questions .c .name == idchild ,
213+ questions .c .name == namekey ,
206214 questions .c .base_course == course_name ,
207215 )
208216 )
@@ -211,6 +219,44 @@ def manifest_data_to_db(course_name, manifest_path):
211219 else :
212220 ins = questions .insert ().values (** valudict )
213221 sess .execute (ins )
222+ if qtype == "datafile" :
223+ d = el .find ("./*pre" )
224+ if d is not None :
225+ file_contents = ET .tostring (d ).decode ("utf8" )
226+ else :
227+ d = el .find ("./*textarea" )
228+ if d is not None :
229+ file_contents = ET .tostring (d ).decode ("utf8" )
230+ else :
231+ d = el .find ("./*img" )
232+ if d is not None :
233+ file_contents = d .attrib ["src" ]
234+
235+ if "data-filename" in el .attrib ["data-filename" ]:
236+ filename = el .attrib ["data-filename" ]
237+ else :
238+ filename = el .attrib ["id" ]
239+
240+ # write datafile contents to the source_code table
241+ res = res = sess .execute (
242+ f"""select * from source_code where acid='{ filename } ' and course_id='{ course_name } '"""
243+ ).first ()
244+ vdict = dict (
245+ acid = filename , course_id = course_name , main_code = file_contents
246+ )
247+ if res :
248+ upd = (
249+ source_code .update ()
250+ .where (
251+ and_ (
252+ source_code .c .acid == filename ,
253+ questions .c .course_id == course_name ,
254+ )
255+ )
256+ .values (** vdict )
257+ )
258+ else :
259+ ins = source_code .insert ().values (** vdict )
214260
215261 latex = root .find ("./latex-macros" )
216262 logger .info ("Setting attributes for this base course" )
0 commit comments