def upload_delegate_positions(request, conference): errors = set() if request.method == 'POST': form = UploadFileForm(request.POST, request.FILES) if(form.is_valid()): uploaded_file = request.FILES['file'] committees = Committee.objects.filter(conference=conference) committees_dict = dictify_queryset(committees, 'name') countries = Country.objects.filter(conference=conference) countries_dict = dictify_queryset(countries, 'name') all_positions = DelegatePosition.objects.filter(country__in=countries, committee__in=committees) positions_dict = dict() for pos in all_positions: positions_dict.setdefault((pos.country,pos.committee),[]).append(pos) positions_by_country = sort_queryset(all_positions, 'country') try: positions_reader = UnicodeCSVDictReader(uploaded_file) for row in positions_reader: try: country_name = row["Country"].strip() except KeyError: errors.add("Not a valid CSV file: No Country column.") break else: try: country = countries_dict[country_name] except KeyError: errors.add("Could not find country " + unicode(country_name, errors='replace') + " (skipping)") else: for committee_name,value in row.items(): committee_name = committee_name.strip() value = value.strip() if value == "": value = "0" if committee_name != "Country": try: committee = committees_dict[committee_name] except KeyError: errors.add("Could not find committee " + unicode(committee_name, errors='replace') + " (skipping)") else: try: new_count = int(value) except exceptions.ValueError: errors.add("Invalid value for " + country.name + "/" + committee.name + ": " + unicode(value, errors='replace') + " (skipping)") else: set_delegate_position_count(committee, country, positions_dict.get((country,committee),[]), get_school_from_position_set(positions_by_country.get(country, [])), new_count) except csv.Error: errors.add("Not a valid CSV file.") if len(errors) > 0: return simplejson.dumps({'errors':list(errors),'table':get_delegate_positions_table(committees, countries)}) return simplejson.dumps({'table':get_delegate_positions_table(committees, countries)})
def upload_school_country_assignments(request, conference): errors = set() if request.method == 'POST': form = UploadFileForm(request.POST, request.FILES) if(form.is_valid()): uploaded_file = request.FILES['file'] countries = Country.objects.filter(conference=conference) countries_dict = dictify_queryset(countries, 'name') schools = School.objects.filter(conferences__id__exact=conference.id) schools_dict = dictify_queryset(schools, 'name') all_positions = DelegatePosition.objects.filter(country__in=countries) positions_dict = sort_queryset(all_positions, 'country') try: assignments_reader = UnicodeCSVDictReader(uploaded_file) for row in assignments_reader: try: country_name = row["Country"].strip() except KeyError: errors.add("Not a valid CSV file: No Country column.") break else: try: country = countries_dict[country_name] except KeyError: errors.add("Could not find country " + unicode(country_name, errors='replace') + " (skipping)") else: try: school_name = row["School"].strip() except KeyError: errors.add("Not a valid CSV file: No School column.") break else: school = None try: if len(school_name) > 1: school = schools_dict[school_name] try: set_country_school_assignment(positions_dict[country], school) except KeyError: errors.add("No positions for " + unicode(country_name, errors='replace') + " - cannot assign to " + unicode(school_name, errors='replace')) except KeyError: errors.add("Could not find school " + unicode(school_name, errors='replace') + " for " + unicode(country_name, errors='replace') + " (skipping)") except csv.Error: errors.add("Not a valid CSV file.") if len(errors) > 0: return simplejson.dumps({'errors':list(errors),'table':get_country_school_assignment_table(countries)}) return simplejson.dumps({'table':get_country_school_assignment_table(countries)})