Skip to content

Commit

Permalink
better TemplateSyntaxError exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bashu committed Oct 10, 2016
1 parent feb8a31 commit b4d2dfa
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions easy_maps/templatetags/easy_maps_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

register = template.Library()

CENTER = settings.EASY_MAPS_CENTER


class EasyMapTag(InclusionTag):
"""
Expand All @@ -31,14 +29,14 @@ class EasyMapTag(InclusionTag):
Argument('address', resolve=True, required=True),
IntegerArgument('width', required=False, default=None),
IntegerArgument('height', required=False, default=None),
IntegerArgument('zoom', required=False, default=16),
IntegerArgument('zoom', required=False, default=None),
'using',
Argument('template_name', default=None, required=False),
)

def render_tag(self, context, **kwargs):
params = dict((k, v) for k, v in kwargs.items() if v is not None)
if len(params.keys()) == 3 or len(params.keys()) > 5:
params = dict((k, v) for k, v in kwargs.items() if v and k not in ['template_name'])
if 'address' in params and (len(params) == 2 or len(params) > 4):
raise template.TemplateSyntaxError(
"easy_map tag has the following syntax: "
"{% easy_map <address> [<width> <height>] [zoom] [using <template_name>] %}"
Expand All @@ -53,14 +51,19 @@ def parse_address(self, address=None):
return address

if not address:
return Address(latitude=CENTER[0], longitude=CENTER[1])
return Address(
latitude=settings.EASY_MAPS_CENTER[0],
longitude=settings.EASY_MAPS_CENTER[1],
)
else:
return Address.objects.get_or_create(address=address)[0]

raise NotImplementedError

def get_context(self, context, **kwargs):
kwargs.update({'map': self.parse_address(kwargs.pop('address'))})
if not kwargs.get('zoom', None):
kwargs['zoom'] = 16 # default value
return kwargs

register.tag(EasyMapTag)

0 comments on commit b4d2dfa

Please sign in to comment.