def clean(self):
        cleaned_data = self.cleaned_data
        
        # note that cleaned_data may not have keys for everything if there were
        # validation errors
        if not 'coordinates_lat_input' in cleaned_data:
            cleaned_data['coordinates_lat_input'] = tuple()
        if not 'coordinates_lng_input' in cleaned_data:
            cleaned_data['coordinates_lng_input'] = tuple()
        
        # error if longitude was given, but lat wasn't.
        if bool(cleaned_data['coordinates_lat_input']) ^ bool(cleaned_data['coordinates_lng_input']):
            raise forms.ValidationError('either give both latitude and longitude, or neither')
        
        if bool(cleaned_data['coordinates_lat_input']) and bool(cleaned_data['coordinates_lng_input']):
            # act like the coordinates field wasn't hidden, then call super's 
            # clean
            self.cleaned_data['coordinates'] = "%s,%s" % (
                dms_to_dec(self.cleaned_data['coordinates_lat_input']),
                dms_to_dec(self.cleaned_data['coordinates_lng_input']),
            )
        else:
            self.cleaned_data['coordinates'] = ''

        return super(NiceLocationForm, self).clean()
 def testDmsToDec(self):
     self.assertEquals(dms_to_dec((True, 70, 30, 0)), D("-70.5"))
     self.assertEquals(
         # only test to millionth of a degree
         dms_to_dec((False, 32, 19, 24.04)).quantize(D("0.000001")),
         (D("32") + (D("19") / 60) + (D("24.04") / (60 * 60))).quantize(D("0.000001")),
     )
Beispiel #3
0
cursor = conn.cursor()

# Loop through csv object and fill database
print('Writing text to database.')
count = 0
for lst_lne in text:
    if count > lines_skip:
        # Print initial lines for QA:
        if count < 30:
            print(lst_lne)
        # Get parameters from csv string:
        station_id = int(lst_lne[0])
        # Replace quotations marks from station name.
        station_name = lst_lne[1].strip().replace('\'', ' ').replace('\"', ' ')
        country_code = lst_lne[2].strip()
        latitude = np.around(dms_to_dec(lst_lne[3]), decimals=8)
        longitude = np.around(dms_to_dec(lst_lne[4]), decimals=8)
        station_elevation = int(lst_lne[5])
        # Complete SQL query:
        sql = query.format(station_id, station_name, country_code, latitude,
                           longitude, station_elevation)
        # Execute SQL query:
        cursor.execute(sql)
    count += 1
    # Commit transaction:
    if (count % 10000) == 0:
        print('Committing transaction. Number of lines: ' + str(count))
        conn.commit()
# Final commit:
conn.commit()