Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MakeTemplate(Resource):
- def __init__(self):
- self.parser = reqparse.RequestParser()
- def _insert_db(self, args):
- model_json = models.Template(
- company=args["companyName"],
- json_template=self.json_data,
- product_name=args["productName"],
- image_location=args["imageLocation"],
- update_date=None)
- db.session.add(model_json)
- db.session.commit()
- def _parse_json(self, args):
- self.json_data = json.loads(args['data'])
- return jsonify({"error": "Expected json in 'data' arg"})
- def post(self):
- self.parser.add_argument('companyName', type=str)
- self.parser.add_argument('productName', type=str, required=True)
- self.parser.add_argument('data', required=True)
- self.parser.add_argument('imageLocation', type=str)
- args = self.parser.parse_args()
- self._parse_json(args)
- self._insert_db(args)
- return jsonify({"error": "None", "status": "Success"})
- def get(self):
- result = db.session.query(models.Template).all()
- model_data = []
- for row in result:
- model_data.append({
- "created_date": row.created_date,
- "productName": row.product_name,
- "annote": row.json_template,
- "companyName": row.company,
- "image_location": row.image_location
- })
- return jsonify({"data": model_data})
- def put(self):
- self.parser = reqparse.RequestParser()
- self.parser.add_argument('editProducte', type=str, required=True)
- self.parser.add_argument('editData', required=True)
- args = parser.parse_args()
- editProducte = args["editProduct"]
- new_json = args["editData"]
- model_json = models.Template.query.filter_by(
- product_name=editProducte).first()
- model_json.json_template = "{'new' :" test" }"
- model_json.update_date = datetime.datetime.utcnow()
- db.session.commit()
- return jsonify({"data": new_json})
- def put(self):
- parser = reqparse.RequestParser()
- parser.add_argument('editInvoice', type=str, required=True)
- parser.add_argument('editData', required=True)
- args = parser.parse_args()
- editInvoice = args["editInvoice"]
- new_json = args["editData"]
- rows_changed = models.Template.query.filter_by(
- invoice_name=editInvoice).update(dict(json_template={}))
- db.session.commit()
- class Template(app.db.Model):
- id = app.db.Column(app.db.Integer, primary_key=True)
- created_date = app.db.Column(DATETIME, default=datetime.datetime.utcnow)
- update_date = app.db.Column(DATETIME)
- company = app.db.Column(VARCHAR)
- invoice_name = app.db.Column(VARCHAR, nullable=False, unique=True)
- image_location = app.db.Column(VARCHAR, nullable=True)
- json_template = app.db.Column(JSON, nullable=False)
- def __repr__(self):
- return '<User %r>' % self.company
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement