def create_planet(): name = request.json.get('name') diameter = request.json.get('diameter') rotation_period = request.json.get('rotation_period') orbital_period = request.json.get('orbital_period') gravity = request.json.get('gravity') population = request.json.get('population') climate = request.json.get('climate') terrain = request.json.get('terrain') surface_water = request.json.get('surface_water') created = request.json.get('created') edited = request.json.get('edited') if not name: return jsonify({"Mensaje": "El nombre no puede estar vacio"}) new_planet = Planet() new_planet.name = name new_planet.diameter = diameter new_planet.rotation_period = rotation_period new_planet.orbital_period = orbital_period new_planet.gravity = gravity new_planet.population = population new_planet.climate = climate new_planet.terrain = terrain new_planet.surface_water = surface_water db.session.add(new_planet) db.session.commit() return jsonify({"Mensaje": "Planeta creado exitosamente"}), 201
def planets(id=None): if request.method == 'GET': if id is not None: planet = Planet.query.get(id) if not planet: return jsonify({"fail": "Planet not found"}), 404 return jsonify({ "success": "Planet found", "planet": planet.serialize() }), 200 else: planets = Planet.query.all() planets = list(map(lambda planet: planet.serialize(), planets)) return jsonify({ "total": len(planets), "results": planets }), 200 if request.method == 'POST': name = request.json.get('name') population = request.json.get('population') terrain = request.json.get('terrain') climate = request.json.get('climate') rotation_period = request.json.get('rotation_period') orbital_period = request.json.get('orbital_period') gravity = request.json.get('gravity') planet = Planet() planet.name = name planet.population = population planet.terrain = terrain planet.climate = climate planet.rotation_period = rotation_period planet.orbital_period = orbital_period planet.gravity = gravity planet.save() return jsonify({ "success": "planet created!", "planet": planet.serialize() }), 201 if request.method == 'PUT': pass if request.method == 'DELETE': planet = Planet.query.get(id) if not planet: return jsonify({"fail": "planet not found"}), 404 planet.delete() return jsonify({"success": "planet deleted"}), 200
def createPlanet(): planet = Planet() planet.name = request.json.get('name') planet.rotation_period = request.json.get('rotation_period') planet.orbital_period = request.json.get('orbital_period') planet.diameter = request.json.get('diameter') planet.climate = request.json.get('climate') planet.gravity = request.json.get('gravity') planet.terrain = request.json.get('terrain') planet.surface_water = request.json.get('surface_water') planet.population = request.json.get('population') planet.save() return jsonify(planet.to_dict()), 201