def post(self): _student = Students() # Sketchy way of parsing requests, for sure there is a better way. # It iterates through JSON objects and assigns object's values with them. for key in api.payload.keys(): setattr(_student, key, api.payload.get(key)) try: db.create_all() db.session.add(_student) db.session.commit() except (SQLAlchemy.exc.SQLAlchemyError, SQLAlchemy.exc.DBAPIERROR): return {"error": "error"}, 500 return {"url": f"{url_for('students_index')}/{_student.id}"}, 201
return [o.to_dict() for o in objs] if objs else [] @staticmethod def add_to_db(raw): obj = College() obj.name = raw['name'] obj.accreditation = raw['accreditation'] obj.address = raw['address'] obj.year = raw['year'] obj.country = raw['country'] db.session.add(obj) db.session.commit() return obj.to_dict() @staticmethod def get_College(schedule=None, tax=None): obj = None if schedule and tax: obj = College.query.filter(College.schedule == schedule, College.tax == tax).all() elif schedule: obj = College.query.filter_by(schedule=schedule).all() elif tax: obj = College.query.filter_by(tax=tax).all() return obj if obj is not None else [] if __name__ == '__main__': db.create_all() db.session.commit()