Esempio n. 1
0
 def get(self):
     if 'terrain' in request.args:
         planets = Planet.objects(
             terrain__iexact=request.args.get('terrain'))
     elif 'climate' in request.args:
         planets = Planet.objects(
             climate__iexact=request.args.get('climate'))
     else:
         planets = Planet.objects
     # print(planets)
     response = Planet.schema(many=True).dump(planets)
     return response
Esempio n. 2
0
    def post(self):
        try:
            result = PlanetSchema().load(request.json)
            newplanet = Planet(**result)
            if Planet.objects(name__iexact=newplanet.name):
                return ApiResponseBuilder.error(
                    'this planet is already in the database', 404)
            newplanet.get_appearances()
            newplanet.save()

        except ma_exc.ValidationError as err:
            return ApiResponseBuilder.error(err.messages, 400)

        return ApiResponseBuilder.success(
            f'planet named {newplanet.name} was added successfully',
            newplanet.serialized, 201)