def add(request): if request.method == "POST": form = request.POST # print(form) # for testing school = School() school.name = form['name'] school.code = form['code'] school.address = form['address'] school.no_of_students = form['no_of_students'] school.save() return redirect('schools:school_detail', school.pk) context = {} return render(request, 'schools/add.html', context)
def handle(self, *args, **options): filename, self.image_location = args file = open(filename, 'r') self.data = csv.DictReader(file) dev_user = User.objects.get(email='*****@*****.**') for d in self.data: # ,Unnamed: 0_x,S No_x,Date,Block,Cluster,School Name_spotways, # KLP ID,Address,PIN Code,PlayGround?,Fence?,Landmark #1, # Landmark #2,Route to School,Bus Details, # LAT_spotways,LONG_spotways,Unnamed: 0_y,S No_y,klp_district, # klp_block,klp_cluster,dise_code,School Name_ems,category, # management,LAT_ems,LONG_ems,institution_gender,hierarchy,moi klpid = d['KLP ID'] address = d['Address'] landmark = d['Landmark #1'] bus = d['Bus Details'] pincode = d['PIN Code'] lat = d['LAT_spotways'] lon = d['LONG_spotways'] coordinates = 'POINT ('+lon+' '+lat+')' connection, cursor = self.connectKlpCoord() if klpid.startswith('5'): continue print '='*20 try: school = School.objects.get(id=klpid) except School.DoesNotExist: # School doesn't exist print 'Creating new school with id: %s' % klpid print d school = School( id=klpid, name=d.get('School Name_ems'), cat=d.get('category'), sex=d.get('institution_gender', 'co-ed'), moi=d.get('moi', 'kannada') or 'kannada', mgmt=d.get('management', 'ed'), status=2 ) school.admin3 = self.get_or_create_admin3(d) if address: if school.address and (not school.address == address): school_address = school.address school_address.address = address else: print 'Creating new address for klpid: %s' % klpid school_address = Address.objects.create( address=address ) school.address = school_address # Update address details. if landmark: school_address.landmark = landmark if bus: school_address.bus = bus if pincode: school_address.pincode = pincode school_address.save() school.save() # Story story = { 'date': d['Date'], 'user': dev_user, 'email': '*****@*****.**', 'name': 'Team KLP', 'school': school } self.createStory(story, klpid, d) if lat.strip() and lon.strip(): self.updateCoord(cursor, klpid, coordinates) cursor.close() connection.commit() connection.close()
def handle(self, *args, **options): filename, self.image_location = args file = open(filename, 'r') self.data = csv.DictReader(file) dev_user = User.objects.get(email='*****@*****.**') for d in self.data: # ,Unnamed: 0_x,S No_x,Date,Block,Cluster,School Name_spotways, # KLP ID,Address,PIN Code,PlayGround?,Fence?,Landmark #1, # Landmark #2,Route to School,Bus Details, # LAT_spotways,LONG_spotways,Unnamed: 0_y,S No_y,klp_district, # klp_block,klp_cluster,dise_code,School Name_ems,category, # management,LAT_ems,LONG_ems,institution_gender,hierarchy,moi klpid = d['KLP ID'] address = d['Address'] landmark = d['Landmark #1'] bus = d['Bus Details'] pincode = d['PIN Code'] lat = d['LAT_spotways'] lon = d['LONG_spotways'] coordinates = 'POINT (' + lon + ' ' + lat + ')' connection, cursor = self.connectKlpCoord() if klpid.startswith('5'): continue print '=' * 20 try: school = School.objects.get(id=klpid) except School.DoesNotExist: # School doesn't exist print 'Creating new school with id: %s' % klpid print d school = School(id=klpid, name=d.get('School Name_ems'), cat=d.get('category'), sex=d.get('institution_gender', 'co-ed'), moi=d.get('moi', 'kannada') or 'kannada', mgmt=d.get('management', 'ed'), status=2) school.admin3 = self.get_or_create_admin3(d) if address: if school.address and (not school.address == address): school_address = school.address school_address.address = address else: print 'Creating new address for klpid: %s' % klpid school_address = Address.objects.create(address=address) school.address = school_address # Update address details. if landmark: school_address.landmark = landmark if bus: school_address.bus = bus if pincode: school_address.pincode = pincode school_address.save() school.save() # Story story = { 'date': d['Date'], 'user': dev_user, 'email': '*****@*****.**', 'name': 'Team KLP', 'school': school } self.createStory(story, klpid, d) if lat.strip() and lon.strip(): self.updateCoord(cursor, klpid, coordinates) cursor.close() connection.commit() connection.close()
def import_secondary_schools(): period = datetime.datetime(day=31, month=12, year=2007) filename = "%s/documentation/data/2007/secondary.csv" % settings.BASE_DIR n = 1 with open(filename, "rb") as ifile: reader = csv.reader(ifile) for row in reader: if n > 1: school = School() school.code = row[0].strip() school.name = row[1].strip() school.address = row[2].strip() school.level = School.SECONDARY school.ownership = get_ownership(row[3].strip()) school.sponsor = get_sponsor(row[4].strip()) school.student_gender = get_student_gender(row[5].strip()) school.school_type = get_school_type(row[6].strip()) school.student_needs = get_student_needs(row[6].strip()) # location province, created = Province.objects.get_or_create(name=row[23].strip().upper()) school.province = province county, created = County.objects.get_or_create(name=row[24].strip().upper()) school.county = county if row[30]: constituency = Constituency.objects.filter(name=row[30].strip().upper()).first() if not constituency: constituency, created = Constituency.objects.get_or_create(name=row[30].strip().upper(), county=county) school.constituency = constituency if row[25]: district = District.objects.filter(name=row[25].strip().upper()).first() if not district: district, created = District.objects.get_or_create(name=row[25].strip().upper(), province=province) school.district = district if row[26]: division, created = Division.objects.get_or_create(name=row[26].strip().upper(), district=district) school.division = division if row[27]: location, created = Location.objects.get_or_create(name=row[27].strip().upper(), division=division) school.location = location if row[28]: sub_location, created = SubLocation.objects.get_or_create(name=row[28].strip().upper(), location=location) school.sub_location = sub_location if row[29]: school_zone, created = SchoolZone.objects.get_or_create(name=row[29].strip().upper(), county=county) school.school_zone = school_zone if row[31]: coord = row[31].split(",") x = float(coord[0][1:]) y = float(coord[1][1:-2]) school.coordinates = Point(y, x) school.save() # staff if row[12]: staff1, created = Staff.objects.get_or_create(period=period, school=school, staff_type=Staff.TSC_MALE, number=row[12].strip(), is_teacher=True) if row[13]: staff2, created = Staff.objects.get_or_create(period=period, school=school, staff_type=Staff.TSC_FEMALE, number=row[13].strip(), is_teacher=True) if row[14]: staff3, created = Staff.objects.get_or_create(period=period, school=school, staff_type=Staff.LOCAL_MALE, number=row[14].strip(), is_teacher=True) if row[15]: staff4, created = Staff.objects.get_or_create(period=period, school=school, staff_type=Staff.LOCAL_FEMALE, number=row[15].strip(), is_teacher=True) if row[16]: staff5, created = Staff.objects.get_or_create(period=period, school=school, staff_type=Staff.PTA_MALE, number=row[16].strip(), is_teacher=True) if row[17]: staff6, created = Staff.objects.get_or_create(period=period, school=school, staff_type=Staff.PTA_FEMALE, number=row[17].strip(), is_teacher=True) if row[18]: staff7, created = Staff.objects.get_or_create(period=period, school=school, staff_type=Staff.OTHER_MALE, number=row[18].strip(), is_teacher=True) if row[19]: staff8, created = Staff.objects.get_or_create(period=period, school=school, staff_type=Staff.OTHER_FEMALE, number=row[19].strip(), is_teacher=True) if row[20]: staff9, created = Staff.objects.get_or_create(period=period, school=school, staff_type=Staff.NON_TEACHING_MALE, number=row[20].strip(), is_teacher=False) if row[21]: staff10, created = Staff.objects.get_or_create(period=period, school=school, staff_type=Staff.NON_TEACHING_FEMALE, number=row[21].strip(), is_teacher=False) # facts if row[8]: fact1, created = Fact.objects.get_or_create(name="Total Enrolment", period=period, school=school, value=row[8].strip()) if row[9]: fact2, created = Fact.objects.get_or_create(name="Pupil Teacher Ratio", period=period, school=school, value=row[9].strip()) if row[10]: fact3, created = Fact.objects.get_or_create(name="Total Teaching staff", period=period, school=school, value=row[10].strip()) if row[11]: fact4, created = Fact.objects.get_or_create(name="Acreage per enrolment", period=period, school=school, value=row[11].strip()) if row[22]: fact5, created = Fact.objects.get_or_create(name="Acreage", period=period, school=school, value=row[22].strip()) n += 1