This commit is contained in:
j 2011-03-05 15:31:56 +01:00
parent 6c3fbb6fa3
commit 6ae8b6c57d
2 changed files with 18 additions and 12 deletions

View file

@ -18,7 +18,8 @@ class Place(models.Model):
modified = models.DateTimeField(auto_now=True)
user = models.ForeignKey(User, null=True, related_name='places')
name = fields.TupleField(default=[])
name = models.CharField(max_length=1024)
aliases = fields.TupleField(default=[])
name_sort = models.CharField(max_length=200)
name_find = models.TextField(default='', editable=False)
@ -62,7 +63,7 @@ class Place(models.Model):
'user': self.user.username,
}
for key in ('created', 'modified',
'name', 'geoname', 'countryCode',
'name', 'aliases', 'geoname', 'countryCode',
'south', 'west', 'north', 'east',
'lat', 'lng', 'size', 'matches', 'type'):
j[key] = getattr(self, key)
@ -70,10 +71,9 @@ class Place(models.Model):
def save(self, *args, **kwargs):
if not self.name_sort:
self.name_sort = ', '.join(self.name)
self.name_sort = self.name #', '.join(self.name)
self.geoname_sort = ', '.join(reversed(self.geoname.split(', ')))
self.name_find = '|%s|'%'|'.join(self.name)
self.name_find = '|%s|'%'|'.join([self.name]+list(self.aliases))
#update center
#self.lat = ox.location.center(self.south, self.north)

View file

@ -20,6 +20,7 @@ def addPlace(request):
'''
param data {
name: "",
aliases: [],
geoname: "",
countryCode: '',
south: float,
@ -34,17 +35,19 @@ def addPlace(request):
data = json.loads(request.POST['data'])
exists = False
names = data.pop('name')
if isinstance(names, basestring):
names = [names]
for name in names:
for name in [names] + data.get('aliases', []):
if models.Place.objects.filter(name_find__icontains=u'|%s|'%name).count() != 0:
exists = True
if not exists:
place = models.Place()
place.user = request.user
place.name = tuple(names)
place.name = names
place.aliases = tuple(data.pop('aliases', []))
for key in data:
setattr(place, key, data[key])
value = data[key]
if isinstance(value, list):
value = tuple(value)
setattr(place, key, value)
place.save()
response = json_response(place.json())
else:
@ -70,7 +73,7 @@ def editPlace(request):
names = [names]
if place.editable(request.user):
conflict = False
for name in names:
for name in names + data.get('aliases', []):
if models.Place.objects.filter(name_find__icontains=u'|%s|'%name).exclude(id=place.id).count() != 0:
conflict = True
if 'geoname' in data:
@ -79,7 +82,10 @@ def editPlace(request):
if not conflict:
for key in data:
if key != 'id':
setattr(place, key, data[key])
value = data[key]
if isinstance(value, list):
value = tuple(value)
setattr(place, key, value)
place.save()
response = json_response(place.json())
else: