Skip to content

Commit

Permalink
fixes some problems with shortnames.
Browse files Browse the repository at this point in the history
MapSet POST now works
  • Loading branch information
deleted committed Jul 26, 2012
1 parent 29fc8d0 commit 00366c2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
2 changes: 1 addition & 1 deletion geocamMapSet/api_urls.py
Expand Up @@ -18,6 +18,6 @@
url(r'^layer/(?P<id>\d+)$', layer_handler),
url(r'^mapsets/$', mapset_handler),
url(r'^mapset/(?P<username>[^/]+)/?$', mapset_handler),
url(r'^mapset/(?P<username>[^/]+)/(?P<shortname>[^/]+)$', mapset_handler),
url(r'^mapset/(?P<username>[^/]+)/(?P<shortname>[^/]+)$', mapset_handler, name='mapset_resource'),
#url(r'^mapset/(?P<id>\d+)$', mapset_handler),
)
17 changes: 6 additions & 11 deletions geocamMapSet/handlers.py
Expand Up @@ -21,14 +21,16 @@ def _fetch(self, username, shortname, *args, **kwargs):
except ObjectDoesNotExist:
return rc.NOT_FOUND
except MultipleObjectsReturned:
return rc.BAD_REQUEST
return rc.DUPLICATE_ENTRY
else:
return self.model.objects.filter(*args, **kwargs)

def read(self, request, username=None, shortname=None):
if username and not shortname:
return rc.BAD_REQUEST
result = self._fetch(username, shortname)
if isinstance(result, HttpResponse):
return result
if isinstance(result, QuerySet):
# it's a bit lame that we have to deserialize and reserialize again...
return json.dumps( list(json.loads(inst.json) for inst in result) )
Expand All @@ -38,16 +40,9 @@ def read(self, request, username=None, shortname=None):
def create(self, request, username='alice', *args, **kwargs):
# return super(MapSetHandler, self).create(request, *args, **kwargs)
attrs = self.flatten_dict(request.data)

try:
inst = self.queryset(request).get(**attrs)
return rc.DUPLICATE_ENTRY
except self.model.DoesNotExist:
inst = self.model.fromJSON(username, None, attrs)
inst.save()
return inst
except self.model.MultipleObjectsReturned:
return rc.DUPLICATE_ENTRY
inst = self.model.fromJSON(username, None, attrs)
inst.save()
return inst.json


def update(self, request, username=None, shortname=None):
Expand Down
19 changes: 10 additions & 9 deletions geocamMapSet/models.py
Expand Up @@ -157,15 +157,12 @@ def getViewUrl(self):

@classmethod
def shortNameFromName(cls, name):
# switch to lowercase
name = name.lower()
# replaces spaces with hyphens
name = re.sub(' +', '-', name)
# remove special characters
name = re.sub('[^a-zA-Z0-9_-]', '', name)

# check for existing similar shortnames
similar_names = set( ms.name for ms in cls.objects.filter(shortName__startswith=name).only('shortName') )
similar_names = set( ms.shortName for ms in cls.objects.filter(shortName__startswith=name).only('shortName') )
if similar_names:
root = name
i = 0
Expand All @@ -189,8 +186,8 @@ def fromJSON(cls, userName, shortName=None, obj={}):
if val is not None:
vals[field] = val
vals['author'] = User.objects.get(username=userName)
if shortName:
vals['shortName'] = shortName
requested_shortname = shortName or vals['name']
vals['shortName'] = cls.shortNameFromName(requested_shortname)
return MapSet(**vals)

class Meta:
Expand All @@ -201,10 +198,14 @@ def mapset_pre_save(sender, instance, raw, *args, **kwargs):
'''
Pre-save signal handler.
Ensure that a shortName is assigned before save.
Add any boilerplate to the stored JSON representation.
'''
if not raw:
if not instance.shortName:
instance.shortName = sender.shortNameFromName(instance.name)
if not instance.shortName:
instance.shortName = sender.shortNameFromName(instance.name)

json_rep = json.loads(instance.json)
json_rep['url'] = getattr(settings, 'BASE_URL', '') + reverse( 'mapset_resource', kwargs={'username':instance.author.username, 'shortname':instance.shortName} )
instance.json = json.dumps(json_rep)

class MapSetLayer(models.Model):
name = models.CharField(primary_key=True, max_length=255)
Expand Down

0 comments on commit 00366c2

Please sign in to comment.