scripts/stats.py
changeset 3016 63625e7e0cac
parent 3015 ad05c8063e37
equal deleted inserted replaced
3015:ad05c8063e37 3016:63625e7e0cac
   729   
   729   
   730   export_fields = ['google_account', 'google_code_project_name', 'role']
   730   export_fields = ['google_account', 'google_code_project_name', 'role']
   731   print 'Exporting the data to CSV'
   731   print 'Exporting the data to CSV'
   732   saveDataToCSV(csv_filename, data, export_fields)
   732   saveDataToCSV(csv_filename, data, export_fields)
   733   print "Exported Roles for Google Code to %s file." % csv_filename
   733   print "Exported Roles for Google Code to %s file." % csv_filename
       
   734 
       
   735 
       
   736 def entityToDict(entity, field_names=None):
       
   737   """Returns a dict with all specified values of this entity.
       
   738 
       
   739   Args:
       
   740     entity: entity that will be converted to dictionary
       
   741     field_names: the fields that should be included, defaults to
       
   742       all fields that are of a type that is in DICT_TYPES.
       
   743   """
       
   744   from google.appengine.ext import db
       
   745 
       
   746   DICT_TYPES = (db.StringProperty, db.IntegerProperty)
       
   747   result = {}
       
   748 
       
   749   if not field_names:
       
   750     props = entity.properties().iteritems()
       
   751     field_names = [k for k, v in props if isinstance(v, DICT_TYPES)]
       
   752 
       
   753   for key in field_names:
       
   754     # Skip everything that is not valid
       
   755     if not hasattr(entity, key):
       
   756       continue
       
   757 
       
   758     result[key] = getattr(entity, key)
       
   759 
       
   760   if hasattr(entity, 'name'):
       
   761     name_prop = getattr(entity, 'name')
       
   762     if callable(name_prop):
       
   763       result['name'] = name_prop()
       
   764 
       
   765   return result
       
   766 
       
   767 
       
   768 def surveyRecordCSVExport(csv_filename, survey_record_model, 
       
   769                           survey_model, survey_key):
       
   770   """CSV export for Survey Records for selected survey type and given survey key.
       
   771   
       
   772   Args:
       
   773     csv_filename: the name of the csv file to save
       
   774     survey_record_model: model of surver record that will be exported
       
   775     survey_model: model of the survey that wil be exported
       
   776     survey_key: key of the survey that records will be exported
       
   777   """
       
   778   from soc.models.project_survey import ProjectSurvey
       
   779   from soc.models.grading_project_survey import GradingProjectSurvey
       
   780 
       
   781   # fetch survey
       
   782   survey = survey_model.get(survey_key)
       
   783 
       
   784   if not survey:
       
   785     print "Survey of given model and key doesn't exist."
       
   786     return
       
   787 
       
   788   schema = eval(survey.survey_content.schema)
       
   789   ordered_properties = survey.survey_content.orderedProperties()
       
   790 
       
   791   getSurveyRecords = getEntities(survey_record_model)
       
   792   survey_records = getSurveyRecords()
       
   793   survey_records_amount = len(survey_records)
       
   794   print "Fetched %d Survey Records." % survey_records_amount
       
   795 
       
   796   count = 0
       
   797 
       
   798   print "Preparing SurveyRecord data for export."
       
   799 
       
   800   sr_key = {}
       
   801   comments_properties = []
       
   802   for property_name in ordered_properties:
       
   803     sr_key[property_name] = schema[property_name]['question']
       
   804     if schema[property_name]['has_comment']:
       
   805       sr_key['comment_for_' + property_name] = 'None'
       
   806       comments_properties.append('comment_for_' + property_name)
       
   807 
       
   808   for comment in comments_properties:
       
   809     ordered_properties.append(comment)
       
   810 
       
   811   survey_record_data = []
       
   812   for i in survey_records.keys():
       
   813     if str(survey_records[i].survey.key()) != survey_key:
       
   814         continue
       
   815     data = entityToDict(survey_records[i], ordered_properties)
       
   816 
       
   817     if (survey_model == GradingProjectSurvey) or \
       
   818         (survey_model == ProjectSurvey):
       
   819       data['organization'] = survey_records[i].org.name
       
   820       data['project_title'] = survey_records[i].project.title
       
   821       data['user_link_id'] = survey_records[i].user.link_id
       
   822 
       
   823       if survey_model == GradingProjectSurvey:
       
   824         data['project_grade'] = survey_records[i].grade
       
   825 
       
   826     survey_record_data.append(data)
       
   827 
       
   828     count += 1
       
   829     print str(count) + '/' + str(survey_records_amount) + ' ' + str(i)
       
   830 
       
   831   if (survey_model == GradingProjectSurvey) or (survey_model == ProjectSurvey):
       
   832     ordered_properties.append('organization')
       
   833     ordered_properties.append('project_title')
       
   834     ordered_properties.append('user_link_id')
       
   835     sr_key['organization'] = 'None'
       
   836     sr_key['project_title'] = 'None'
       
   837     sr_key['user_link_id'] = 'None'
       
   838 
       
   839     if survey_model == GradingProjectSurvey:
       
   840       ordered_properties.append('project_grade')
       
   841       sr_key['project_grade'] = 'None'
       
   842 
       
   843   survey_record_data.insert(0, sr_key)
       
   844 
       
   845   saveDataToCSV(csv_filename, survey_record_data, ordered_properties)
       
   846   print "Survey Records exported to %s file." % csv_filename
   734 
   847 
   735 
   848 
   736 def main(args):
   849 def main(args):
   737   """Main routine.
   850   """Main routine.
   738   """
   851   """
   791       'exportStudentsWithProjects': exportStudentsWithProjects,
   904       'exportStudentsWithProjects': exportStudentsWithProjects,
   792       'exportUniqueOrgAdminsAndMentors': exportUniqueOrgAdminsAndMentors,
   905       'exportUniqueOrgAdminsAndMentors': exportUniqueOrgAdminsAndMentors,
   793       'exportOrgsForGoogleCode': exportOrgsForGoogleCode,
   906       'exportOrgsForGoogleCode': exportOrgsForGoogleCode,
   794       'exportRolesForGoogleCode': exportRolesForGoogleCode,
   907       'exportRolesForGoogleCode': exportRolesForGoogleCode,
   795       'startUniqueUserIdConversion': startUniqueUserIdConversion,
   908       'startUniqueUserIdConversion': startUniqueUserIdConversion,
       
   909       'surveyRecordCSVExport': surveyRecordCSVExport,
   796   }
   910   }
   797 
   911 
   798   interactive.remote(args, context)
   912   interactive.remote(args, context)
   799 
   913 
   800 if __name__ == '__main__':
   914 if __name__ == '__main__':