def post(self, request): data = request.data try: flavorslug = data['flavor'] except KeyError: raise exceptions.ValidationError( {'flavor': ['Flavor not specified in payload']}) try: flavor = AlertFlavor.objects.get(slug=flavorslug) except AlertFlavor.DoesNotExist: raise NotFound({ 'flavor': ['Flavor "{0}" does not exist.'.format(flavorslug)] }) self.check_object_permissions(request, flavor) if not flavor.enabled: raise exceptions.ValidationError( {'flavor': ['Flavor "{0}" is disabled.'.format(flavorslug)]}) # Get the links out--we'll deal with them next. link_data = data.pop('links', []) # Validate the alert data alert_ser = AlertSerializer(data=data) if not alert_ser.is_valid(): raise exceptions.ValidationError({'detail': alert_ser.errors}) # Validate links link_errors = [] for link_item in link_data: if 'name' not in link_item or 'url' not in link_item: link_errors.append( 'Missing names or urls in link data. {}'.format( repr(link_item))) if link_errors: raise exceptions.ValidationError( {'detail': { 'links': link_errors }}) # Everything is good, so let's save it all to the db. alert = alert_ser.save() for link_item in link_data: link = Link(alert=alert, name=link_item['name'], url=link_item['url']) link.save() return rest_framework.response.Response( status=201, data={'detail': { 'id': alert.id }})
def post(self, request): data = request.DATA try: flavorslug = request.DATA['flavor'] except KeyError: return self.rest_error(status=404, errors='Flavor not specified in payload.') try: flavor = AlertFlavor.objects.get(slug=flavorslug) except AlertFlavor.DoesNotExist: return self.rest_error( status=404, errors='Flavor "{}" does not exist.'.format(flavorslug)) self.check_object_permissions(request, flavor) if not flavor.enabled: return self.rest_error( status=400, errors='Flavor "{}" is disabled.'.format(flavorslug)) # Get the links out--we'll deal with them next. link_data = data.pop('links', []) # Validate the alert data alert_ser = AlertSerializer(data=data) if not alert_ser.is_valid(): return self.rest_error(status=400, errors=alert_ser.errors) # Validate links for link_item in link_data: if 'name' not in link_item or 'url' not in link_item: link_errors = 'Missing names or urls in link data. {}'.format( repr(link_data)) return self.rest_error(status=400, errors={'links': link_errors}) # Everything is good, so let's save it all to the db alert = alert_ser.object alert.save() for link_item in link_data: link = Link(alert=alert, name=link_item['name'], url=link_item['url']) link.save() return self.rest_created({'id': alert.id})
def post(self, request): data = request.data try: flavorslug = data['flavor'] except KeyError: raise exceptions.ValidationError({ 'flavor': [ 'Flavor not specified in payload' ] }) try: flavor = AlertFlavor.objects.get(slug=flavorslug) except AlertFlavor.DoesNotExist: raise NotFound({ 'flavor': [ 'Flavor "{0}" does not exist.'.format(flavorslug) ] }) self.check_object_permissions(request, flavor) if not flavor.enabled: raise exceptions.ValidationError({ 'flavor': [ 'Flavor "{0}" is disabled.'.format(flavorslug) ] }) # Get the links out--we'll deal with them next. link_data = data.pop('links', []) # Validate the alert data alert_ser = AlertSerializer(data=data) if not alert_ser.is_valid(): raise exceptions.ValidationError({'detail': alert_ser.errors}) # Validate links link_errors = [] for link_item in link_data: if 'name' not in link_item or 'url' not in link_item: link_errors.append( 'Missing names or urls in link data. {}'.format( repr(link_item))) if link_errors: raise exceptions.ValidationError( {'detail': {'links': link_errors}}) # Everything is good, so let's save it all to the db. alert = alert_ser.save() for link_item in link_data: link = Link( alert=alert, name=link_item['name'], url=link_item['url'] ) link.save() return rest_framework.response.Response( status=201, data={ 'detail': {'id': alert.id} })