Ejemplo n.º 1
0
 def post(self):
     data = request.get_json()
     session = DBBackend().get_session()
     response = dict()
     response['status'] = 'failure'
     if not data.get('name'):
         response['data'] = "Product name is required."
         return make_response(jsonify(response)), 422
     response['data'] = 'Some error occurred. Please try again.'
     try:
         register_utility = DBUtility(session)
         product = register_utility.register_product(data)
         response['status'] = 'success'
         response['data'] = {"product_id": product.id}
         response_code = 201
         session.commit()
     except KeyError:
         response['data'] = "name missing"
         response_code = 422
     except AssertionError as ae:
         response['data'] = str(ae)
         response_code = 422
     except Exception as ex:
         response['data'] = "Something went wrong"
         response_code = 500
         response['status'] = 'failure'
         session.rollback()
     session.close()
     return make_response(jsonify(response)), response_code
Ejemplo n.º 2
0
    def post(self):
        response = dict()
        data = request.get_json()
        session = DBBackend().get_session()
        response['data'] = dict()
        response['status'] = 'failure'

        try:
            if not data and not data.get('comment', None):
                response['data'] = {"error": "Comment Data missing"}
                return make_response(jsonify(response)), 422
            comment = data.get('comment')
            db_utility = DBUtility(session)
            product_id = comment.get('product_id', None)
            user_id = comment.get('created_by', None)
            if not product_id:
                response['data'] = {'error': 'Product Id needed'}
            try:
                product_id = int(product_id)
                if user_id:
                    user_id = int(user_id)
                comment = db_utility.add_comment(comment, product_id)
                response['data'] = {'comment': comment.serialize}
            except Exception as e:
                response['data'] = {'error': e.message}
            response['status'] = 'success'
            response_code = 201
            session.commit()
        except KeyError:
            response['data'] = {"error": "Key data is missing"}
            response_code = 422
        except AssertionError as ae:
            response['data'] = str(ae)
            response_code = 422
        except Exception as ex:
            response['data'] = {"error": "Error occured" + str(ex.message)}
            response_code = 500
            response['status'] = 'failure'
            session.rollback()
        session.close()
        return make_response(jsonify(response)), response_code
Ejemplo n.º 3
0
 def post(self):
     data = request.get_json()
     session = DBBackend().get_session()
     response = dict()
     response['status'] = 'failure'
     response['data'] = 'Some error occurred. Please try again.'
     response_code = 500
     try:
         register_utility = DBUtility(session)
         user = register_utility.register_user(data)
         response['status'] = 'success'
         response['data'] = 'Successfully registered.'
         response_code = 201
         session.commit()
     except KeyError:
         response['data'] = "email or password is missing"
         response_code = 422
     except AssertionError as ae:
         response['data'] = str(ae)
         response_code = 422
     except IntegrityError, ie:
         response['data'] = "User is already registered."
         session.rollback()