Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _load_zone(self):
try:
self._zone = ZoneModel.objects.get(zone_id=self.id)
try:
self._widget = ThemeManager.Widgets.get(self._zone.widget_id)
self._widget.set_data(self._zone.data)
except WidgetNotFound:
self._widget = None
self._is_loaded = True
except ZoneModel.DoesNotExist:
pass
@staticmethod
def get(id):
"""Return a specific widget"""
try:
return register.widgets[id]()
except KeyError:
raise WidgetNotFound("Widget with id '%s' does not exist" % id)
def validate(self, data):
"""Perform validation of the widget data"""
from dispatch.theme import ThemeManager
errors = {}
if data.get('widget') is not None:
try:
widget = ThemeManager.Widgets.get(data['widget'])
except WidgetNotFound as e:
errors['widget'] = str(e)
else:
for field in widget.fields:
field_data = data['data'].get(field.name)
if field_data is not None:
try:
field.validate(field_data)
except InvalidField as e:
errors[field.name] = str(e)
elif field.required:
errors[field.name] = '%s is required' % field.label
if errors:
raise ValidationError(errors)
def get_widget(self, id):
from dispatch.theme import ThemeManager
if id is None:
return None
try:
return ThemeManager.Widgets.get(id)
except WidgetNotFound:
raise WidgetNotFound('Widget with id %s does not exist' % id)
def zone(zone_id, **kwargs):
"""Renders the contents of the zone with given zone_id."""
try:
zone = ThemeManager.Zones.get(zone_id)
except ZoneNotFound:
return ''
try:
return zone.widget.render(add_context=kwargs)
except (WidgetNotFound, AttributeError):
pass
return ''
def get_widget(self, id):
from dispatch.theme import ThemeManager
if id is None:
return None
try:
return ThemeManager.Widgets.get(id)
except WidgetNotFound:
raise WidgetNotFound('Widget with id %s does not exist' % id)