Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.82 KB | None | 0 0
  1. def carga_secciones(request):
  2. log = "[Error] %s En la linea: %s \n"
  3. log_secciones = open("log_secciones.txt","w")
  4. PROA = request.FILES['proa']
  5. fs_PROA = FileSystemStorage()
  6. filename_PROA = fs_PROA.save(PROA.name.lower(), PROA)
  7. PROA_url = fs_PROA.url(filename_PROA)
  8. PROA_url = PROA_url[1:].capitalize()
  9.  
  10. wb = load_workbook(PROA_url)
  11. sheets_PROA = []
  12.  
  13. for sheet in wb.get_sheet_names():
  14. sheet = sheet.replace(u'\xa0', u'')
  15. sheet = str(sheet)
  16. sheets_PROA.append(wb[sheet])
  17.  
  18. for sheet in sheets_PROA:
  19. #carreer = obtenerCarrera(sheet.title)[0]
  20. carreer = get_object_or_404(Carreer, diurno="UNAB11500")
  21. i = 2
  22. while sheet["A"+str(i)].value != None: #Este recorre el excel
  23. log_secciones.write("[I]"+str(i)+"\n")
  24. period = int(sheet["D"+str(i)].value)
  25. try:
  26. try:
  27. nrc = int(sheet["I"+str(i)].value) #Empiezo obteniendo nrc
  28. class_type = sheet["S"+str(i)].value #con esto reviso que tipo de ramo es, o teoria o complementario (tutoring)
  29. if sheet.title == "UNAB11500" or sheet.title == "UNAB21500" or sheet.title == "UNAB21503" or sheet.title == "UNAB21501": # Como informatica funciona distinto que otras carreras aca distingo una de otra
  30. flag = class_type == "TEORICO - PRACTICO" and int(str(sheet["R"+str(i)].value).split("/")[0]) > 2
  31. log_secciones.write("[CLASS TYPE] %s \n" % (unidecode(class_type)))
  32. if unidecode(class_type) == "TEORIA" or unidecode(class_type) == "PRÁCTICA" or flag:
  33. log_secciones.write("[NRC] %d Catedra %d\n" % (nrc,period))
  34. lecture = get_object_or_404(Course, nrc=nrc,period=period)
  35. else:
  36. log_secciones.write("[NRC] %d Tutoria %s\n" % (nrc,period))
  37. tutoring = get_object_or_404(Tutoring, nrc=nrc,period=period)
  38. else: #Para industria, falta ver para las otras carreras
  39. if unidecode(class_type) == "LABORATORIO" or unidecode(class_type) == "AYUDANTIA":
  40. tutoring = get_object_or_404(Tutoring, nrc=nrc,period=period)
  41. else:
  42. lecture = get_object_or_404(Course, nrc=nrc,period=period)
  43.  
  44. except Exception,e:
  45. exc_type, exc_obj, exc_tb = sys.exc_info()
  46.  
  47. log_secciones.write(log % (e,exc_tb.tb_lineno ))
  48. print "Error 1: ",e," En la linea: ", exc_tb.tb_lineno
  49. if sheet.title == "UNAB21503" or sheet.title == "UNAB21501":
  50. nom_shift = "Advance"
  51. else:
  52. nom_shift = sheet["H"+str(i)].value.capitalize() #Se genera el ciclo en la base de datos si no existe
  53. try:
  54. shift = get_object_or_404(Shift, name=nom_shift)
  55. except Exception,e:
  56. print e
  57. shift = Shift.objects.create(name=nom_shift)
  58. shift.save()
  59. code = sheet["G"+str(i)].value
  60. vacants = int(sheet["V"+str(i)].value)
  61. rut_profesor = sheet["O"+str(i)].value #En la casilla de rut no siempre hay solo 1, en los casos que hay mas solo se toma el primero.
  62. if "/" in str(rut_profesor):
  63. rut_profesor = str(rut_profesor).split("/")[0].strip(" ")
  64.  
  65. dv_profesor = str(rut_profesor)[-1]
  66. rut_profesor = str(rut_profesor)[:-1]
  67.  
  68. nom_profesor = sheet["P"+str(i)].value
  69. try: #Si el profe no existe se crea
  70. log_secciones.write("[RUT DEL PROFESOR]"+str(rut_profesor)+"\n")
  71. try:
  72. teacher = get_object_or_404(UserProfile, rut=int(rut_profesor))
  73. except:
  74. teachers = get_list_or_404(UserProfile, rut=int(rut_profesor))
  75. for teacher in teachers:
  76. if teacher.user_type== "DO":
  77. teacher=teacher
  78.  
  79.  
  80.  
  81. except Exception,e :
  82. exc_type, exc_obj, exc_tb = sys.exc_info()
  83. log_secciones.write(log % (e,exc_tb.tb_lineno ))
  84. print "Error 2: ",e," En la linea: ", exc_tb.tb_lineno
  85. nom_profesor = nom_profesor.split("/")[0].split(" ")
  86. for nom in nom_profesor:
  87. nom = unidecode(nom)
  88. if nom == "":
  89. nom_profesor.pop(nom_profesor.index(nom))
  90. ii=2
  91. username = nom_profesor[0][0:i].lower() + "." + nom_profesor[-2].lower() + "." + nom_profesor[-1].lower()
  92. while User.objects.filter(username=username).exists():
  93. ii+=1
  94. username = nom_profesor[0][0:i].lower() + "." + nom_profesor[-2].lower() + "." + nom_profesor[-1].lower()
  95. user = User.objects.create_user(username=username,password="UNAB2020")
  96. user.save()
  97. user.first_name = nom_profesor[0].capitalize()
  98. user.last_name = nom_profesor[-2].capitalize()+" "+nom_profesor[-1].capitalize()
  99. user.save()
  100. teacher = UserProfile.objects.create(user=user,
  101. rut=rut_profesor,
  102. dv=dv_profesor,
  103. user_type="DO")
  104. teacher.save()
  105.  
  106. code_asignatura = sheet["E"+str(i)].value+str(sheet["F"+str(i)].value)
  107. try:#si la asignatura no existe se crea
  108. subject = get_object_or_404(Subject, code=str(code_asignatura))
  109. if carreer not in subject.carreer.all(): #Agrega las carreras que tienen este ramo
  110. subject.carreer.add(get_object_or_404(Carreer, diurno="UNAB11500"))
  111.  
  112. subject.save()
  113.  
  114.  
  115. except Exception,e:
  116. exc_type, exc_obj, exc_tb = sys.exc_info()
  117. log_secciones.write(log % (e,exc_tb.tb_lineno ))
  118. subject_name = sheet["Q"+str(i)].value
  119. subject_name = unidecode(subject_name)
  120. subject = Subject.objects.create(name=subject_name,
  121. code=code_asignatura,)
  122. subject.save()
  123. subject.carreer.add(get_object_or_404(Carreer, diurno="UNAB11500"))
  124. subject.save()
  125. log_secciones.write("Subject creado %s %s\n" %(subject_name,code_asignatura))
  126.  
  127. nrc = sheet["I"+str(i)].value
  128. class_type = sheet["S"+str(i)].value
  129. schedules = sheet["U"+str(i)].value
  130.  
  131. carreers = sheet["AA"+str(i)].value
  132. carreers = carreers.split("/")
  133. carreers_imparted = [] #Estas son las carreras que pueden inscribirse en esta seccion (diferente a las carreras que pueden tomar el ramo)
  134. for carreer in carreers:
  135. carreers_imparted.append(get_object_or_404(Carreer, diurno="UNAB11500"))
  136.  
  137. if schedules != None:
  138. #GENERA LOS HORARIOS CON SU SALA
  139. edificios = sheet["X"+str(i)].value
  140. salas = sheet["Y"+str(i)].value
  141. if edificios != None:
  142. edificios = edificios.split("/")
  143. if salas != None:
  144. salas = salas.split("/")
  145. schedules = unidecode(schedules)
  146. schedules = schedules.split("/")
  147. schedules = map(str,schedules)
  148. schedules_final = []
  149. for horario in schedules:
  150. n_horario = str(horario.replace(" ","").replace(".",""))
  151. schedules_final.append(str(n_horario.split("-")[0]))
  152. horarios = ""
  153. a=0
  154. for horario in schedules_final:
  155. dia = horario[:2]
  156. hora = horario[2:]
  157. if hora in module_dict:
  158. hora = module_dict[hora]
  159. dia = day_dict[dia]
  160. if edificios == None and salas == None:
  161. horario_final = dia+hora+";"+str(edificios)+"-"+str(salas)
  162. elif edificios == None:
  163. horario_final = dia+hora+";"+str(edificios)+"-"+salas[a].replace(" ","")
  164. elif salas == None:
  165. horario_final = dia+hora+";"+edificios[a].replace(" ","")+"-"+str(salas)
  166. else:
  167. horario_final = dia+hora+";"+edificios[a].replace(" ","")+"-"+salas[a].replace(" ","")
  168. if horarios == "":
  169. horarios += horario_final
  170. else:
  171. horarios += "/"+horario_final
  172. else:
  173. pass
  174. a+=1
  175. else:
  176. horarios = ""
  177.  
  178. n_section = Section.objects.create(shift=shift, code=code)
  179. n_section.save()
  180.  
  181. if sheet.title == "UNAB11500" or sheet.title == "UNAB21500" or sheet.title == "UNAB21503" or sheet.title == "UNAB21503": #ACA SE CREA EL CURSO CON TODOS LOS DATOS QUE SE RECOLECTARON ANTES (PARA INFORMATICA PRIMERO)
  182. log_secciones.write("PROCESO DE CREACION\n")
  183. flag = class_type == "TEORICO - PRACTICO" and int(str(sheet["R"+str(i)].value).split("/")[0]) > 2
  184. if class_type == "TEORIA" or class_type == "PRÁCTICA" or flag:
  185. log_secciones.write("INTENTANDO DE CREAR UNA CATEDRA\n")
  186. n_course = Course.objects.create(section=n_section,
  187. class_schedule=horarios,
  188. teacher=teacher,
  189. subject=subject,
  190. period=period,
  191. nrc=int(nrc),
  192. vacants=vacants)
  193. n_course.save()
  194. log_secciones.write("[NRC] %d Catedra %d Creado\n" % (nrc,period))
  195. for carreer in carreers_imparted:
  196. n_course.carreers_imparted.add(carreer)
  197. n_course.save()
  198.  
  199.  
  200.  
  201. else:
  202. log_secciones.write("INTENTADO DE CREAR UNA TUTORIA\n")
  203. related = sheet["K"+str(i)].value
  204. if related != None:
  205. related = related.strip("/")
  206. else:
  207. pass
  208.  
  209. n_course = Tutoring.objects.create(section=n_section,
  210. class_schedule=horarios,
  211. period=period,
  212. related=related,
  213. teacher=teacher,
  214. subject=subject,
  215. nrc=int(nrc),
  216. vacants=vacants)
  217. n_course.save()
  218. log_secciones.write("[NRC] %d Tutoria %d Creado\n" % (nrc,period))
  219. for carreer in carreers_imparted:
  220. n_course.carreers_imparted.add(carreer)
  221. n_course.save()
  222.  
  223. else: #Industria
  224. if class_type == "LABORATORIO" or class_type == "AYUDANTIA":
  225. related = sheet["K"+str(i)].value
  226. if related != None:
  227. related = related.strip("/")
  228. else:
  229. pass
  230.  
  231. n_course = Tutoring.objects.create(section=n_section,
  232. period=period,
  233. class_schedule=horarios,
  234. related=related,
  235. teacher=teacher,
  236. subject=subject,
  237. nrc=int(nrc),
  238. vacants=vacants)
  239. n_course.save()
  240. for carreer in carreers_imparted:
  241. n_course.carreers_imparted.add(carreer)
  242. n_course.save()
  243.  
  244. else:
  245. n_course = Course.objects.create(section=n_section,
  246. class_schedule=horarios,
  247. teacher=teacher,
  248. subject=subject,
  249. period=period,
  250. nrc=int(nrc),
  251. vacants=vacants)
  252. n_course.save()
  253. for carreer in carreers_imparted:
  254. n_course.carreers_imparted.add(carreer)
  255. n_course.save()
  256.  
  257. i+=1
  258.  
  259. except Exception,e :
  260. exc_type, exc_obj, exc_tb = sys.exc_info()
  261. log_secciones.write(log % (e,exc_tb.tb_lineno ))
  262. print "Error 4: ",e," En la linea: ", exc_tb.tb_lineno
  263. i+=1
  264.  
  265. log_secciones.close()
  266. os.remove(PROA_url)
  267. # template_name = "upload_data.html"
  268. # data = {}
  269. # if Course.objects.all().exists() and Tutoring.objects.all().exists():
  270. # data["Course_uploaded"] = True
  271. # else:
  272. # data["Course_uploaded"] = False
  273.  
  274. # if UserProfile.objects.filter(user_type="ES").exists():
  275. # data["Students_uploaded"] = True
  276. # else:
  277. # data["Students_uploaded"] = False
  278. # return render(request,template_name,data
  279. return "exito"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement