Пример #1
0
 def add_planet(payload):
     error = False
     try:
         name = request.get_json()['name']
         moons_number = request.get_json()['moonsNumber']
         add = Planets(name=name, moons_number=moons_number)
         add.insert()
     except:
         error = True
     finally:
         if error:
             abort(422)
         else:
             return jsonify({
                 'success': True,
                 'name': name,
                 'moons_number': moons_number
             })
Пример #2
0
def create_planets():
    name = request.json['name']
    population = request.json['population']
    sizeKm = request.json['sizeKm']

    new_planet = Planets(name, population, sizeKm)
    db.session.add(new_planet)
    db.session.commit()

    return 'Planets created succesfully'
Пример #3
0
def add_planet():

    request_body = request.get_json()

    planet = Planets(id=request_body["id"], name=request_body["name"], diameter=request_body["diameter"], rotation_period=request_body["rotation_period"], orbital_period=request_body["orbital_period"], climate=request_body["climate"], population=request_body["population"], terrain=request_body["terrain"])

    db.session.add(planet)
    db.session.commit()

    return jsonify("Planet added!"), 200
Пример #4
0
def add_planet():
    lista=[]
    for i in range(1,11):
        people = requests.get(f"https://www.swapi.tech/api/planets/{i}").json()["result"]["properties"]
        lista.append(people)
    
    for request_body in lista:
        planeta = Planets(name=request_body["name"], population=request_body["population"], terrain=request_body["terrain"], diameter=request_body["diameter"], rotation_period=request_body["rotation_period"], orbital_period=request_body["orbital_period"], gravity=request_body["gravity"], url=request_body["url"], climate=request_body["climate"], surface_water=request_body["surface_water"], created=request_body["created"], edited=request_body["edited"])
        db.session.add(planeta)
        db.session.commit()

    return jsonify("Planets were added"), 200
Пример #5
0
def addinfo():
    data = request.get_json()
    for i in data:
        planeta = Planets(name=i["name"],
                          diameter=i["diameter"],
                          rotation_period=i["rotation_period"],
                          orbital_period=i["orbital_period"],
                          surface_water=i["surface_water"],
                          gravity=i["gravity"],
                          terrain=i["terrain"],
                          population=i["population"],
                          climate=i["climate"],
                          image=i["image"])
        db.session.add(planeta)
        db.session.commit()
    return jsonify("Message : Se adiciono la data!"), 200
Пример #6
0
def planet_post():
    data = request.get_json()
    planets = Planets(name=data["name"],
                      population=data["population"],
                      terrain=data["terrain"],
                      diameter=data["diameter"],
                      rotation_period=data["rotation_period"],
                      orbital_period=data["orbital_period"],
                      gravity=data["gravity"],
                      climate=data["climate"],
                      surface_water=data["surface_water"],
                      created=data["created"],
                      edited=data["edited"],
                      url=data["url"])
    db.session.add(planets)
    db.session.commit()
    return jsonify("Message : Se adiciono el planeta!"), 200
    return jsonify(request_body), 200
Пример #7
0
def new_plan():

    request_body = request.get_json()

    new_planet = Planets(climate=request_body["climate"],
                         diameter=request_body["diameter"],
                         edited=request_body["edited"],
                         gravity=request_body["gravity"],
                         name=request_body["name"],
                         orbital_period=request_body["orbital_period"],
                         population=request_body["population"],
                         rotation_period=request_body["rotation_period"],
                         surface_water=request_body["surface_water"],
                         terrain=request_body["terrain"],
                         url=request_body["url"])

    db.session.add(new_planet)
    db.session.commit()

    return jsonify("Planet added correctly"), 200
Пример #8
0
def get_planets():
    if request.method == 'GET':
        planetas = Planets.query.all()
        serializadoenlista = list(
            map(lambda x: x.serialize_planets(), planetas))
        return jsonify(serializadoenlista), 200
    else:
        url = "https://www.swapi.tech/api/planets/"
        r = requests.get(url)  #obtengo la info de la api
        data = r.json()  #se convierte a json

        for x in data['results']:
            r2 = requests.get(x['url'])
            data2 = r2.json()

            propiedades = data2['result']['properties']
            climate = propiedades['climate']

            gravity = propiedades['gravity']
            name = propiedades['name']
            terrain = propiedades['terrain']
            diameter = propiedades['diameter']
            surface = propiedades['surface_water']
            rotation = int(propiedades['rotation_period'])
            orbital = int(propiedades['orbital_period'])
            population = propiedades['population']
            #se ingresan la info en la db
            new_post = Planets(climate=climate,
                               gravity=gravity,
                               name=name,
                               terrain=terrain,
                               diameter=diameter,
                               rotation_period=rotation,
                               orbital_period=orbital,
                               population=population,
                               surface_water=surface)
            print(new_post)
            db.session.add(new_post)
            db.session.commit()

        return jsonify("Hecho")
Пример #9
0
def handle_planets():  
    
    return jsonify(Planets.getAllPlanets()), 200
Пример #10
0
def get_planets():
    data = jsonify(Planets.get_planets())
    return data
Пример #11
0
def load_data():
    for planet in listplanets:
        new_planet = Planets()
        new_planet.name = planet["name"]
        new_planet.rotation_period = str(planet["rotation_period"])
        new_planet.orbital_period = planet["orbital_period"]
        new_planet.diameter = planet["diameter"]
        new_planet.climate = planet["climate"]
        new_planet.gravity = planet["gravity"]
        new_planet.terrain = planet["terrain"]
        new_planet.surface_water = planet["surface_water"]
        new_planet.population = planet["population"]
        new_planet.url = planet["url"]
        db.session.add(new_planet)
        db.session.commit()

    for person in listpeople:
        new_person = People()
        new_person.name = person["name"]
        new_person.height = person["height"]
        new_person.hair_color = person["hair_color"]
        new_person.skin_color = person["skin_color"]
        new_person.eye_color = person["eye_color"]
        new_person.birth_year = person["birth_year"]
        new_person.gender = person["gender"]
        new_person.homeworld = person["homeworld"]
        new_person.url = person["url"]
        db.session.add(new_person)
        db.session.commit()

    response_body = {"msg": "loading... initial data to database...  "}
    return jsonify(response_body), 200
Пример #12
0
def add_new_planet():
    request_body = request.data
    new_planet = Planets()
    new_planet.name = request.json.get('name')
    new_planet.diameter = request.json.get('diameter')
    new_planet.rotation_period = request.json.get('rotation_period')
    new_planet.orbital_period = request.json.get('orbital_period')
    new_planet.gravity = request.json.get('gravity')
    new_planet.population = request.json.get('population')
    new_planet.climate = request.json.get('climate')
    new_planet.terrain = request.json.get('terrain')
    new_planet.surface_water = request.json.get('surface_water')

    new_planet.save()
    return jsonify(new_planet.serialize())