scripts/stats.py
changeset 3015 ad05c8063e37
parent 2770 71a5a56cf29e
child 3016 63625e7e0cac
equal deleted inserted replaced
3014:75c2410d3f85 3015:ad05c8063e37
   576 
   576 
   577   csv_data = file_handler.getvalue()
   577   csv_data = file_handler.getvalue()
   578   csv_file = open(csv_filename, 'w')
   578   csv_file = open(csv_filename, 'w')
   579   csv_file.write(csv_data)
   579   csv_file.write(csv_data)
   580   csv_file.close()
   580   csv_file.close()
       
   581 
       
   582 
       
   583 def exportOrgsForGoogleCode(csv_filename, gc_project_prefix='', 
       
   584                             scope_path_start=''):
       
   585   """Export all Organizations from given program as CSV.
       
   586       
       
   587   CSV file will contain 3 columns: organization name, organization google 
       
   588   code project name, organization description.
       
   589   
       
   590   Args:
       
   591     csv_filename: the name of the csv file to save
       
   592     gc_project_prefix: Google Code project prefix for example
       
   593       could be google-summer-of-code-2009- for GSoC 2009
       
   594     scope_path_start: the start of the scope path of the roles to get could be
       
   595       google/gsoc2009 if you want to export all GSoC 2009 Organizations.
       
   596   """
       
   597   from soc.models.organization import Organization
       
   598   
       
   599   print 'Retrieving all Organizations'
       
   600   orgs = getEntities(Organization)()
       
   601   orgs_export = []
       
   602   
       
   603   print 'Preparing data for CSV export'
       
   604   for key in orgs.keys():
       
   605     if not orgs[key].scope_path.startswith(scope_path_start):
       
   606       continue
       
   607     
       
   608     org_for_export = {} 
       
   609     org_short_name = orgs[key].short_name
       
   610     org_short_name = org_short_name.replace(' ','-').replace('.', '')
       
   611     org_short_name = org_short_name.replace('/','-').replace('!','').lower()
       
   612     org_for_export['org_description'] = orgs[key].description
       
   613     org_for_export['org_name'] = orgs[key].name
       
   614     org_for_export['google_code_project_name'] = gc_project_prefix + \
       
   615         org_short_name
       
   616     orgs_export.append(org_for_export)
       
   617 
       
   618   export_fields = ['org_name', 'google_code_project_name', 'org_description']
       
   619   print 'Exporting the data to CSV'
       
   620   saveDataToCSV(csv_filename, orgs_export, export_fields)
       
   621   print "Exported Organizations for Google Code to %s file." % csv_filename
       
   622 
       
   623 
       
   624 def exportRolesForGoogleCode(csv_filename, gc_project_prefix='', 
       
   625                              scope_path_start=''):
       
   626   """Export all Students/Mentors/OrgAdmins Roles from given program as CSV.
       
   627       
       
   628   CSV file will contain 3 columns: google_account, organization google 
       
   629   code project name and role (project member or project owner).
       
   630   
       
   631   Args:
       
   632     csv_filename: the name of the csv file to save
       
   633     gc_project_prefix: Google Code project prefix for example
       
   634       could be google-summer-of-code-2009- for GSoC 2009
       
   635     scope_path_start: the start of the scope path of the roles to get could be
       
   636       google/gsoc2009 if you want to export all GSoC 2009 Organizations.
       
   637   """
       
   638   from soc.models.org_admin import OrgAdmin
       
   639   from soc.models.student_project import StudentProject
       
   640   
       
   641   # get all projects
       
   642   getStudentProjects = getEntities(StudentProject)
       
   643   student_projects = getStudentProjects()
       
   644   org_admins = getEntities(OrgAdmin)()
       
   645   all_org_admins = org_admins.values()
       
   646   
       
   647   org_admins_by_orgs = {}
       
   648   students_by_orgs = {}
       
   649   mentors_by_orgs = {}
       
   650   
       
   651   for org_admin in all_org_admins:
       
   652       
       
   653     if not org_admin.scope_path.startswith(scope_path_start):
       
   654       continue
       
   655       
       
   656     org_short_name = org_admin.scope.short_name
       
   657     org_short_name = org_short_name.replace(' ','-').replace('.', '')
       
   658     org_short_name = org_short_name.replace('/','-').replace('!','').lower()
       
   659     if gc_project_prefix + org_short_name not in org_admins_by_orgs.keys():
       
   660       org_admins_by_orgs[gc_project_prefix + org_short_name] = []
       
   661     
       
   662     org_admins_by_orgs[gc_project_prefix + \
       
   663         org_short_name].append(str(org_admin.user.account))
       
   664     print 'OrgAdmin ' + str(org_admin.user.account) + \
       
   665         ' for ' + gc_project_prefix + org_short_name
       
   666   
       
   667   for student_project in student_projects.values():
       
   668 
       
   669     if student_project.status != 'accepted' or not \
       
   670       student_project.scope_path.startswith(scope_path_start):
       
   671       # no need to export this project
       
   672       continue
       
   673     
       
   674     student_entity = student_project.student
       
   675     mentor_entity = student_project.mentor
       
   676     org_short_name = student_project.scope.short_name
       
   677     org_short_name = org_short_name.replace(' ','-').replace('.', '')
       
   678     org_short_name = org_short_name.replace('/','-').replace('!','').lower()
       
   679     if gc_project_prefix + org_short_name not in students_by_orgs.keys():
       
   680       students_by_orgs[gc_project_prefix + org_short_name] = []
       
   681     students_by_orgs[gc_project_prefix + \
       
   682         org_short_name].append(str(student_entity.user.account))
       
   683     print 'Student ' + str(student_entity.user.account) + \
       
   684         ' for ' + gc_project_prefix + org_short_name
       
   685     if gc_project_prefix + org_short_name not in mentors_by_orgs.keys():
       
   686       mentors_by_orgs[gc_project_prefix + org_short_name] = []
       
   687     mentors_by_orgs[gc_project_prefix + \
       
   688         org_short_name].append(str(mentor_entity.user.account))
       
   689     print 'Mentor ' + str(mentor_entity.user.account) + \
       
   690         ' for ' + gc_project_prefix + org_short_name
       
   691   
       
   692   roles_data = {}
       
   693   
       
   694   # prepare org admins data
       
   695   for org_key in org_admins_by_orgs.keys():
       
   696     for org_admin in org_admins_by_orgs[org_key]:
       
   697       roles_data[org_admin + '|' + org_key] = \
       
   698           {'role': 'project_owner', 
       
   699            'google_code_project_name': org_key, 
       
   700            'google_account': org_admin}
       
   701 
       
   702   # prepare mentors data
       
   703   for org_key in mentors_by_orgs.keys():
       
   704     for mentor in mentors_by_orgs[org_key]:
       
   705       if mentor + '|' + org_key not in roles_data.keys():
       
   706         roles_data[mentor + '|' + org_key] = \
       
   707             {'role': 'project_member', 
       
   708              'google_code_project_name': org_key, 
       
   709              'google_account': mentor}
       
   710 
       
   711   # prepare students data
       
   712   for org_key in students_by_orgs.keys():
       
   713     for student in students_by_orgs[org_key]:
       
   714       roles_data[student + '|' + org_key] = \
       
   715           {'role': 'project_member', 
       
   716            'google_code_project_name': org_key, 
       
   717            'google_account': student}
       
   718    
       
   719   data = []
       
   720   
       
   721   # add @gmail.com to all accounts that don't have @ in the account string
       
   722   # gmail.com is authorized domain for Google AppEngine that's why it's
       
   723   # missing
       
   724   for roles_key in roles_data.keys():
       
   725     if roles_data[roles_key]['google_account'].find('@') == -1:
       
   726       roles_data[roles_key]['google_account'] = \
       
   727         roles_data[roles_key]['google_account'] + '@gmail.com'
       
   728     data.append(roles_data[roles_key])
       
   729   
       
   730   export_fields = ['google_account', 'google_code_project_name', 'role']
       
   731   print 'Exporting the data to CSV'
       
   732   saveDataToCSV(csv_filename, data, export_fields)
       
   733   print "Exported Roles for Google Code to %s file." % csv_filename
   581 
   734 
   582 
   735 
   583 def main(args):
   736 def main(args):
   584   """Main routine.
   737   """Main routine.
   585   """
   738   """
   635       'startSpam': startSpam,
   788       'startSpam': startSpam,
   636       'reviveJobs': reviveJobs,
   789       'reviveJobs': reviveJobs,
   637       'deidleJobs': deidleJobs,
   790       'deidleJobs': deidleJobs,
   638       'exportStudentsWithProjects': exportStudentsWithProjects,
   791       'exportStudentsWithProjects': exportStudentsWithProjects,
   639       'exportUniqueOrgAdminsAndMentors': exportUniqueOrgAdminsAndMentors,
   792       'exportUniqueOrgAdminsAndMentors': exportUniqueOrgAdminsAndMentors,
       
   793       'exportOrgsForGoogleCode': exportOrgsForGoogleCode,
       
   794       'exportRolesForGoogleCode': exportRolesForGoogleCode,
   640       'startUniqueUserIdConversion': startUniqueUserIdConversion,
   795       'startUniqueUserIdConversion': startUniqueUserIdConversion,
   641   }
   796   }
   642 
   797 
   643   interactive.remote(args, context)
   798   interactive.remote(args, context)
   644 
   799