本文整理汇总了Python中models.City.save方法的典型用法代码示例。如果您正苦于以下问题:Python City.save方法的具体用法?Python City.save怎么用?Python City.save使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.City
的用法示例。
在下文中一共展示了City.save方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run_city
# 需要导入模块: from models import City [as 别名]
# 或者: from models.City import save [as 别名]
def run_city(verbose=True, step=10, begin=0, end=0):
import csv
filename = os.path.join(settings.BASE_PATH, 'apps', 'world', 'data', 'cities.csv')
reader = csv.reader(open(filename, 'rb'), delimiter=',', quotechar="'")
for row in reader:
c = City()
c.name = row[4]
c.capital = bool(int(row[9]))
c.world_city = bool(int(row[12]))
c.mega_city = bool(int(row[13]))
c.point = Point(float(row[22]), float(row[21]))
#don't save the city if a country can't be found for it
try:
c.country = CountryBorder.objects.get(iso3=row[17])
except :
continue
try:
c.state = StateBorder.objects.get(name=row[18])
except :
try:
c.state = StateBorder.objects.get(mpoly__contains=c.point)
except :
pass
c.sqkm = int(row[40])
topleft = Point(float(row[48]), float(row[55]))
topright = Point(float(row[51]), float(row[55]))
bottomleft = Point(float(row[48]), float(row[52]))
bottomright = Point(float(row[51]), float(row[52]))
c.mpoly = MultiPolygon(Polygon(LinearRing(topleft, topright, bottomright, bottomleft, topleft)))
c.save()
print c.name
示例2: test01_setup
# 需要导入模块: from models import City [as 别名]
# 或者: from models.City import save [as 别名]
def test01_setup(self):
"Setting up for related model tests."
for name, state, lon, lat in cities:
loc = Location(point=Point(lon, lat))
loc.save()
c = City(name=name, state=state, location=loc)
c.save()
示例3: test_parse_city
# 需要导入模块: from models import City [as 别名]
# 或者: from models.City import save [as 别名]
def test_parse_city(self):
response = '''
{"response":[
{"cid":1,"title":"Москва","region":"Regione Abruzzo область"},
{"cid":1074996,"title":"Москва","area":"Порховский район","region":"Псковская область"},
{"cid":1102561,"title":"Москва","area":"Пеновский район","region":"Тверская область"},
{"cid":1130701,"title":"Москва","area":"Верхошижемский район","region":"Кировская область"}
]}
'''
country = CountryFactory.create(remote_id=1)
instance = City(country=country)
instance.parse(json.loads(response)['response'][0])
instance.save()
self.assertEqual(instance.remote_id, 1)
self.assertEqual(instance.name, u'Москва')
instance = City(country=country)
instance.parse(json.loads(response)['response'][1])
instance.save()
self.assertEqual(instance.remote_id, 1074996)
self.assertEqual(instance.name, u'Москва')
self.assertEqual(instance.area, u"Порховский район")
self.assertEqual(instance.region, u"Псковская область")
示例4: test11_lookup_insert_transform
# 需要导入模块: from models import City [as 别名]
# 或者: from models.City import save [as 别名]
def test11_lookup_insert_transform(self):
"Testing automatic transform for lookups and inserts."
if DISABLE:
return
# San Antonio in 'WGS84' (SRID 4326)
sa_4326 = "POINT (-98.493183 29.424170)"
wgs_pnt = fromstr(sa_4326, srid=4326) # Our reference point in WGS84
# Oracle doesn't have SRID 3084, using 41157.
if SpatialBackend.oracle:
# San Antonio in 'Texas 4205, Southern Zone (1983, meters)' (SRID 41157)
# Used the following Oracle SQL to get this value:
# SELECT SDO_UTIL.TO_WKTGEOMETRY(SDO_CS.TRANSFORM(SDO_GEOMETRY('POINT (-98.493183 29.424170)', 4326), 41157)) FROM DUAL;
nad_wkt = "POINT (300662.034646583 5416427.45974934)"
nad_srid = 41157
else:
# San Antonio in 'NAD83(HARN) / Texas Centric Lambert Conformal' (SRID 3084)
nad_wkt = (
"POINT (1645978.362408288754523 6276356.025927528738976)"
) # Used ogr.py in gdal 1.4.1 for this transform
nad_srid = 3084
# Constructing & querying with a point from a different SRID. Oracle
# `SDO_OVERLAPBDYINTERSECT` operates differently from
# `ST_Intersects`, so contains is used instead.
nad_pnt = fromstr(nad_wkt, srid=nad_srid)
if SpatialBackend.oracle:
tx = Country.objects.get(mpoly__contains=nad_pnt)
else:
tx = Country.objects.get(mpoly__intersects=nad_pnt)
self.assertEqual("Texas", tx.name)
# Creating San Antonio. Remember the Alamo.
sa = City(name="San Antonio", point=nad_pnt)
sa.save()
# Now verifying that San Antonio was transformed correctly
sa = City.objects.get(name="San Antonio")
self.assertAlmostEqual(wgs_pnt.x, sa.point.x, 6)
self.assertAlmostEqual(wgs_pnt.y, sa.point.y, 6)
# If the GeometryField SRID is -1, then we shouldn't perform any
# transformation if the SRID of the input geometry is different.
# SpatiaLite does not support missing SRID values.
if not SpatialBackend.spatialite:
m1 = MinusOneSRID(geom=Point(17, 23, srid=4326))
m1.save()
self.assertEqual(-1, m1.geom.srid)
示例5: create_or_getCity
# 需要导入模块: from models import City [as 别名]
# 或者: from models.City import save [as 别名]
def create_or_getCity(cityName, state, lat,lon):
try:
c = City.objects.get(name=cityName)
except City.DoesNotExist:
r = requests.get("http://photon.komoot.de/api/?q={},{}&limit=1&lat={}&lon={}".format(cityName, state.name, lat,lon)).json()
lastCity = City.objects.latest('created_on')
c = City()
c.id = lastCity.id +1
c.name=cityName
c.stateID=state.id
c.lat= r['features'][0]['geometry']['coordinates'][1]
c.lon= r['features'][0]['geometry']['coordinates'][0]
c.live="true"
c.save()
return c
示例6: load2mysql
# 需要导入模块: from models import City [as 别名]
# 或者: from models.City import save [as 别名]
def load2mysql(self):
with open(self.path) as f:
provinces = json.load(f)
for province in provinces:
# print province["name"]
p = Province(name=province["name"])
p.save()
for city in province["city"]:
# print " %s" % city["name"]
c = City(name=city["name"], province=p)
c.save()
for area in city["area"]:
# print " %s" % area
cou = County(name=area, city=c)
cou.save()
示例7: test11_lookup_insert_transform
# 需要导入模块: from models import City [as 别名]
# 或者: from models.City import save [as 别名]
def test11_lookup_insert_transform(self):
"Testing automatic transform for lookups and inserts."
if DISABLE: return
# San Antonio in 'WGS84' (SRID 4326)
sa_4326 = 'POINT (-98.493183 29.424170)'
wgs_pnt = fromstr(sa_4326, srid=4326) # Our reference point in WGS84
# Oracle doesn't have SRID 3084, using 41157.
if oracle:
# San Antonio in 'Texas 4205, Southern Zone (1983, meters)' (SRID 41157)
# Used the following Oracle SQL to get this value:
# SELECT SDO_UTIL.TO_WKTGEOMETRY(SDO_CS.TRANSFORM(SDO_GEOMETRY('POINT (-98.493183 29.424170)', 4326), 41157)) FROM DUAL;
nad_wkt = 'POINT (300662.034646583 5416427.45974934)'
nad_srid = 41157
else:
# San Antonio in 'NAD83(HARN) / Texas Centric Lambert Conformal' (SRID 3084)
nad_wkt = 'POINT (1645978.362408288754523 6276356.025927528738976)' # Used ogr.py in gdal 1.4.1 for this transform
nad_srid = 3084
# Constructing & querying with a point from a different SRID. Oracle
# `SDO_OVERLAPBDYINTERSECT` operates differently from
# `ST_Intersects`, so contains is used instead.
nad_pnt = fromstr(nad_wkt, srid=nad_srid)
if oracle:
tx = Country.objects.get(mpoly__contains=nad_pnt)
else:
tx = Country.objects.get(mpoly__intersects=nad_pnt)
self.assertEqual('Texas', tx.name)
# Creating San Antonio. Remember the Alamo.
sa = City(name='San Antonio', point=nad_pnt)
sa.save()
# Now verifying that San Antonio was transformed correctly
sa = City.objects.get(name='San Antonio')
self.assertAlmostEqual(wgs_pnt.x, sa.point.x, 6)
self.assertAlmostEqual(wgs_pnt.y, sa.point.y, 6)
示例8: test02_proxy
# 需要导入模块: from models import City [as 别名]
# 或者: from models.City import save [as 别名]
def test02_proxy(self):
"Testing Lazy-Geometry support (using the GeometryProxy)."
#### Testing on a Point
pnt = Point(0, 0)
nullcity = City(name='NullCity', point=pnt)
nullcity.save()
# Making sure TypeError is thrown when trying to set with an
# incompatible type.
for bad in [5, 2.0, LineString((0, 0), (1, 1))]:
try:
nullcity.point = bad
except TypeError:
pass
else:
self.fail('Should throw a TypeError')
# Now setting with a compatible GEOS Geometry, saving, and ensuring
# the save took, notice no SRID is explicitly set.
new = Point(5, 23)
nullcity.point = new
# Ensuring that the SRID is automatically set to that of the
# field after assignment, but before saving.
self.assertEqual(4326, nullcity.point.srid)
nullcity.save()
# Ensuring the point was saved correctly after saving
self.assertEqual(new, City.objects.get(name='NullCity').point)
# Setting the X and Y of the Point
nullcity.point.x = 23
nullcity.point.y = 5
# Checking assignments pre & post-save.
self.assertNotEqual(Point(23, 5), City.objects.get(name='NullCity').point)
nullcity.save()
self.assertEqual(Point(23, 5), City.objects.get(name='NullCity').point)
nullcity.delete()
#### Testing on a Polygon
shell = LinearRing((0, 0), (0, 100), (100, 100), (100, 0), (0, 0))
inner = LinearRing((40, 40), (40, 60), (60, 60), (60, 40), (40, 40))
# Creating a State object using a built Polygon
ply = Polygon(shell, inner)
nullstate = State(name='NullState', poly=ply)
self.assertEqual(4326, nullstate.poly.srid) # SRID auto-set from None
nullstate.save()
ns = State.objects.get(name='NullState')
self.assertEqual(ply, ns.poly)
# Testing the `ogr` and `srs` lazy-geometry properties.
if gdal.HAS_GDAL:
self.assertEqual(True, isinstance(ns.poly.ogr, gdal.OGRGeometry))
self.assertEqual(ns.poly.wkb, ns.poly.ogr.wkb)
self.assertEqual(True, isinstance(ns.poly.srs, gdal.SpatialReference))
self.assertEqual('WGS 84', ns.poly.srs.name)
# Changing the interior ring on the poly attribute.
new_inner = LinearRing((30, 30), (30, 70), (70, 70), (70, 30), (30, 30))
ns.poly[1] = new_inner
ply[1] = new_inner
self.assertEqual(4326, ns.poly.srid)
ns.save()
self.assertEqual(ply, State.objects.get(name='NullState').poly)
ns.delete()
示例9: upload
# 需要导入模块: from models import City [as 别名]
# 或者: from models.City import save [as 别名]
def upload(request):
"""
Uploads the receipt
:url: /shoebox/upload/
:param POST['email']: email identifying user
:param POST['business_name']: i.e. McDonalds (blank)
:param POST['address']: business address (blank)
:param POST['location']: i.e. JFK International (blank)
:param POST['phone']: phone number (blank)
:param POST['city']: city (blank)
:param POST['state']: state (blank)
:param POST['purchase_date']: purchase date in NeatReceipts format
:param POST['tax']: tax (blank)
:param POST['tip']: tip (blank)
:param POST['amount']: total amount
:param POST['payment_method']: Visa, Master, Cash etc
:param POST['category']: NeatReceipts category
:param FILES['img']: Receipts image
:rtype: JSON
::
#: if success in posting returns id of created or update object in string format
{'result': 'id'}
#: if failed
{'result': '-1'}
#: if request was not POST
{'result': '0'}
"""
if request.method == 'POST':
form = ReceiptUploadForm(request.POST, request.FILES)
if form.is_valid():
instance = form.save(commit=False)
# assign to the current user uploading data
instance.user, created = OTNUserTemp.objects.get_or_create(email=request.POST['email'])
instance.save()
receipt = DetailReceipt(basic=instance)
if 'business_name' in request.POST:
b = Business(name=request.POST['business_name'])
if 'location' in request.POST:
b.location = request.POST['location']
if 'phone' in request.POST:
b.phone = request.POST['phone']
if 'address' in request.POST:
b.address = request.POST['address']
if 'city' in request.POST:
c = City(name=request.POST['city'], state=request.POST['state'])
c.save()
b.city = c
b.save()
receipt.business = b
if 'category' in request.POST:
cat, created = Category.objects.get_or_create(name=request.POST['category'])
receipt.category = cat
if 'tax' in request.POST:
receipt.tax = request.POST['tax']
if 'tip' in request.POST:
receipt.tip = request.POST['tip']
if 'payment_method' in request.POST:
pmethod = request.POST['payment_method'].lower()
if pmethod.find('cash') != -1:
receipt.payment_method = receipt.CASH
elif pmethod.find('amex') != -1:
receipt.payment_method = receipt.AMEX
elif pmethod.find('visa') != -1:
receipt.payment_method = receipt.VISA
elif pmethod.find('master') != -1:
receipt.payment_method = receipt.MASTER
elif pmethod.find('discover') != -1:
receipt.payment_method = receipt.DISCOVER
else:
receipt.payment_method = receipt.CASH
receipt.save()
return JSONHttpResponse({'result':str(receipt.id)})
else:
return JSONHttpResponse({'result':'-1', 'form_errors':str(form.errors)})
else:
return JSONHttpResponse({'result':'0'})