def post(self):
     print("Processing")
     notification_category_dict = request.get_json()
     if not notification_category_dict:
         response = {'message': 'No input data provided'}
         return response, HttpStatus.bad_request_400.value
     errors = notification_category_schema.validate(
         notification_category_dict)
     if errors:
         return errors, HttpStatus.bad_request_400.value
     notification_category_name = notification_category_dict['name']
     if not NotificationCategory.is_name_unique(
             id=0, name=notification_category_name):
         response = {
             'error':
             'A notification category with the name {} already exists'.
             format(notification_category_name)
         }
         return response, HttpStatus.bad_request_400.value
     try:
         notification_category = NotificationCategory(
             notification_category_name)
         notification_category.add(notification_category)
         query = NotificationCategory.query.get(notification_category.id)
         dump_result = notification_category_schema.dump(query).data
         return dump_result, HttpStatus.created_201.value
     except SQLAlchemyError as e:
         print("Error")
         print(e)
         orm.session.rollback()
         response = {"error": str(e)}
         return response, HttpStatus.bad_request_400.value
 def post(self):
     notification_category_dict = request.get_json()
     if not notification_category_dict:
         response = {'message': 'No input data provided'}
         return response, HttpStatus.bad_request_400.value
     errors = notification_schema.validate(notification_category_dict)
     if errors:
         return errors, HttpStatus.bad_request_400.value
     try:
         notification_category_name = notification_category_dict[
             'notification_category']['name']
         notification_category = NotificationCategory.query.filter_by(
             name=notification_category_name).first()
         if notification_category is None:
             # Create a new NotificationCategory
             notification_category = NotificationCategory(
                 name=notification_category_name)
             orm.session.add(notification_category)
         # Now that we are sure we have a notification category,
         # we can create a new Notification
         notification = Notification(
             message=notification_category_dict['message'],
             ttl=notification_category_dict['ttl'],
             notification_category=notification_category)
         notification.add(notification)
         query = Notification.query.get(notification.id)
         dump_result = notification_schema.dump(query).data
         return dump_result, HttpStatus.created_201.value
     except SQLAlchemyError as e:
         orm.session.rollback()
         response = {"error": str(e)}
         return response, HttpStatus.bad_request_400.value
    def patch(self, id):
        notification_category = NotificationCategory.query.get_or_404(id)
        notification_category_dict = request.get_json()

        if not notification_category_dict:
            response = {'message': 'No input data provided'}
            return response, HttpStatus.bad_request_400.value

        errors = notification_category_schema.validate(
            notification_category_dict)
        if errors:
            return errors, HttpStatus.bad_request_400.value

        try:
            if 'name' in notification_category_dict and \
                    notification_category_dict['name'] is not None:
                notification_category_name = notification_category_dict['name']
                if NotificationCategory.is_name_unique(
                        id=id, name=notification_category_name):
                    notification_category.name = notification_category_name
                else:
                    response = {
                        'error':
                        'A category with the name <{0}> already'
                        ' exists'.format(notification_category_name)
                    }
                    return HttpStatus.bad_request_400.value
            notification_category.update()
            return self.get(id)
        except SQLAlchemyError as err:
            db.session.rollback()
            response = {'error': str(err)}
            return response, HttpStatus.bad_request_400.value
    def post(self):
        notification_category_dict = request.get_json()

        if not notification_category_dict:
            response = {"message": "No input data provided"}
            return response, HttpStatus.bad_request_400.value

        errors = notification_schema.validate(notification_category_dict)
        if errors:
            return errors, HttpStatus.bad_request_400.value

        notification_message = notification_category_dict['message']
        if not Notification.is_message_unique(id=0,
                                              message=notification_message):
            response = {
                'error':
                'A notification with the message <{0}>'
                ' already exists'.format(notification_message)
            }
            return response, HttpStatus.bad_request_400.value

        try:
            notification_category_name = notification_category_dict\
                    ['notification_category']['name']
            notification_category = NotificationCategory.query\
                    .filter_by(name=notification_category_name).first()

            if notification_category is None:
                # Create a new NotificationCategory
                notification_category = NotificationCategory(
                    name=notification_category_name)
                db.session.add(notification_category)

            # Now we can create a new Notification
            notification = Notification(
                message=notification_message,
                ttl=notification_category_dict['ttl'],
                notification_category=notification_category)
            notification.add(notification)

            query = Notification.query.get(notification.id)
            dump_result = notification_schema.dump(query).data
            return dump_result, HttpStatus.created_201.value
        except SQLAlchemyError as err:
            db.session.rollback()
            response = {"error": str(err)}
            return response, HttpStatus.bad_request_400.value
 def post(self):
     notification_category_dict = request.get_json()
     if not notification_category_dict:
         response = {'message': 'No input data provided'}
         return response, HttpStatus.bad_request_400.value
     errors = notification_category_schema.validate(
         notification_category_dict)
     if errors:
         return errors, HttpStatus.bad_request_400.value
     try:
         notification_category = NotificationCategory(
             notification_category_dict['name'])
         add(notification_category)
         query = NotificationCategory.query.get(notification_category.id)
         dump_result = notification_category_schema.dump(query).data
         return dump_result, HttpStatus.created_201.value
     except SQLAlchemyError as e:
         orm.session.rollback()
         response = {"error": str(e)}
         return response, HttpStatus.bad_request_400.value