def post(self): parser = reqparse.RequestParser() parser.add_argument('name', type=unicode, required=True, help='name of the endpoint', location=('json')) args = parser.parse_args() try: end_point = models.EndPoint() end_point.name = args['name'] if 'hostnames' in request.json: for host in request.json['hostnames']: end_point.hosts.append(models.Host(host)) db.session.add(end_point) db.session.commit() except (sqlalchemy.exc.IntegrityError, sqlalchemy.orm.exc.FlushError), e: return ({'error': str(e)}, 409)
def create_multiple_users(request, geojson_polygon): with app.app_context(): end_point = models.EndPoint() end_point.name = 'myEndPoint' billing_plan = models.BillingPlan() billing_plan.name = 'free' billing_plan.end_point = end_point user1 = models.User('foo', '*****@*****.**') user1.end_point = end_point user1.billing_plan = billing_plan user1.shape = json.dumps(geojson_polygon) user2 = models.User('foodefault', '*****@*****.**') user2.end_point = models.EndPoint.get_default() user2.billing_plan = models.BillingPlan.get_default(user2.end_point) models.db.session.add(end_point) models.db.session.add(billing_plan) models.db.session.add(user1) models.db.session.add(user2) models.db.session.commit() # we end the context but need to keep some id for later (object won't survive this lost!) d = { 'user1': user1.id, 'user2': user2.id, 'end_point': end_point.id, 'billing_plan': billing_plan.id } # we can't truncate end_point and billing_plan, so we have to delete them explicitly def teardown(): with app.app_context(): end_point = models.EndPoint.query.get(d['end_point']) billing_plan = models.BillingPlan.query.get(d['billing_plan']) models.db.session.delete(end_point) models.db.session.delete(billing_plan) models.db.session.commit() request.addfinalizer(teardown) return d