353 """Dumps a pickle. |
353 """Dumps a pickle. |
354 """ |
354 """ |
355 |
355 |
356 f = open("%s.dat" % name, 'w') |
356 f = open("%s.dat" % name, 'w') |
357 cPickle.dump(target, f) |
357 cPickle.dump(target, f) |
|
358 |
|
359 |
|
360 def acceptedStudentsCSVExport(csv_filename, program_key_name): |
|
361 """Exports all accepted Students for particular program into CSV file. |
|
362 """ |
|
363 # TODO(Pawel.Solyga): Add additional Program parameter to this method |
|
364 # so we export students from different programs |
|
365 # TODO(Pawel.SOlyga): Make it universal so it works with both GHOP |
|
366 # and GSoC programs |
|
367 |
|
368 from soc.models.student_project import StudentProject |
|
369 from soc.models.student import Student |
|
370 from soc.models.organization import Organization |
|
371 |
|
372 getStudentProjects = getEntities(StudentProject) |
|
373 student_projects = getStudentProjects() |
|
374 student_projects_amount = len(student_projects) |
|
375 print "Fetched %d Student Projects." % student_projects_amount |
|
376 print "Fetching Student entities from Student Projects." |
|
377 accepted_students = {} |
|
378 student_organization = {} |
|
379 counter = 0 |
|
380 for sp_key in student_projects.keys(): |
|
381 key = student_projects[sp_key].student.key().name() |
|
382 accepted_students[key] = student_projects[sp_key].student |
|
383 org_name = student_projects[sp_key].scope.name |
|
384 student_organization[key] = org_name |
|
385 counter += 1 |
|
386 print str(counter) + '/' + str(student_projects_amount) + ' ' + key + ' (' + org_name + ')' |
|
387 print "All Student entities fetched." |
|
388 |
|
389 students_key_order = ['link_id', 'given_name', 'surname', |
|
390 'name_on_documents', 'email', 'res_street', 'res_city', 'res_state', |
|
391 'res_country', 'res_postalcode', 'phone', 'ship_street', 'ship_city', |
|
392 'ship_state', 'ship_country', 'ship_postalcode', 'birth_date', |
|
393 'tshirt_size', 'tshirt_style', 'name', 'school_name', 'school_country', |
|
394 'major', 'degree'] |
|
395 |
|
396 print "Preparing Students data for export." |
|
397 students_data = [accepted_students[i].toDict(students_key_order) for i in accepted_students.keys()] |
|
398 |
|
399 print "Adding organization name to Students data." |
|
400 for student in students_data: |
|
401 student['organization'] = student_organization[program_key_name + '/' + student['link_id']] |
|
402 |
|
403 students_key_order.append('organization') |
|
404 |
|
405 saveDataToCSV(csv_filename, students_data, students_key_order) |
|
406 print "Accepted Students exported to %s file." % csv_filename |
|
407 |
|
408 |
|
409 def saveDataToCSV(csv_filename, data, key_order): |
|
410 """Saves data in order into CSV file. |
|
411 |
|
412 This is a helper function used with acceptedStudentsCSVExport(). |
|
413 """ |
|
414 |
|
415 import csv |
|
416 import StringIO |
|
417 |
|
418 from soc.logic import dicts |
|
419 |
|
420 file_handler = StringIO.StringIO() |
|
421 |
|
422 writer = csv.DictWriter(file_handler, key_order, dialect='excel') |
|
423 writer.writerow(dicts.identity(key_order)) |
|
424 |
|
425 # encode the data to UTF-8 to ensure compatibiliy |
|
426 for row_dict in data: |
|
427 for key in row_dict.keys(): |
|
428 value = row_dict[key] |
|
429 if isinstance(value, basestring): |
|
430 row_dict[key] = value.encode("utf-8") |
|
431 else: |
|
432 row_dict[key] = str(value) |
|
433 writer.writerow(row_dict) |
|
434 |
|
435 csv_data = file_handler.getvalue() |
|
436 csv_file = open(csv_filename, 'w') |
|
437 csv_file.write(csv_data) |
|
438 csv_file.close() |
358 |
439 |
359 |
440 |
360 def main(args): |
441 def main(args): |
361 """Main routine. |
442 """Main routine. |
362 """ |
443 """ |
409 'popSaver': popSaver, |
490 'popSaver': popSaver, |
410 'rawSaver': rawSaver, |
491 'rawSaver': rawSaver, |
411 'startSpam': startSpam, |
492 'startSpam': startSpam, |
412 'reviveJobs': reviveJobs, |
493 'reviveJobs': reviveJobs, |
413 'deidleJobs': deidleJobs, |
494 'deidleJobs': deidleJobs, |
|
495 'acceptedStudentsCSVExport': acceptedStudentsCSVExport, |
414 } |
496 } |
415 |
497 |
416 interactive.remote(args, context) |
498 interactive.remote(args, context) |
417 |
499 |
418 if __name__ == '__main__': |
500 if __name__ == '__main__': |