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

View file

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