Ritorna la lista ordinata dei polls. :param poll_id: :return: """ args = parser.parse_args() type = args.get('type') if type == 'P': polls_query = Sondaggio.query().order(-Sondaggio.positivi) elif type == 'A': polls_query = Sondaggio.query().order(-Sondaggio.astenuti) else: polls_query = Sondaggio.query().order(-Sondaggio.negativi) polls_list = list() for poll in polls_query.fetch(): poll_dict = { 'POLL ID': poll.encode, 'POLL TITLE': poll.title, 'POSITIVI': poll.positivi, 'NEGATIVI': poll.negativi, 'ASTENUTI': poll.astenuti, } polls_list.append(poll_dict) return {'Polls': polls_list} api.add_resource(Polls, '/api/v0.1/polls') api.add_resource(OrderPools, '/api/v0.1/orderpolls/') api.add_resource(Poll, '/api/v0.1/poll') api.add_resource(PollID, '/api/v0.1/poll/<string:poll_id>')
return {'Error:': 'Invalid inputs'}, 400 new_city.put() return {'Success': 'Rule created'} def put(self, city, zone, rule): rule_q = CityModel.query(CityModel.name == city, CityModel.zone == zone, CityModel.rule == rule).get() if not rule_q: return {'Error 404:': 'Rule does not exist.'}, 404 body = request.get_json() if not body: return {'Error:': 'Invalid inputs'}, 400 try: time = body['time'] intensity = int(body['intensity']) rule_q.time = time rule_q.intensity = intensity except KeyError: return {'Error:': 'Invalid inputs'}, 400 except BadValueError: return {'Error:': 'Invalid inputs'}, 400 rule_q.put() return {'Success': 'Rule modified'} api.add_resource(City, '/api/v0.1/city/<string:city>') api.add_resource(Zone, '/api/v0.1/city/<string:city>/zone/<string:zone>') api.add_resource( Rule, '/api/v0.1/city/<string:city>/zone/<string:zone>/rule/<string:rule>')
prenotazione.posti_prenotati = posti_richiesti else: prenotazione = Prenotazione(email=user, sala=room, orario=time, posti_prenotati=posti_richiesti) prenotazione.put() sala.put() except Error: return {'Error': 'Errore sconosciuto'}, 500 return {'Result': 'Prenotazione effettuata con successo'}, 200 class Users(Resource): def get(self, email): prenotazioni = Prenotazione.query(Prenotazione.email == email).fetch() lista_prenotazioni = list() for p in prenotazioni: lista_prenotazioni.append({ 'room': p.sala, 'time': p.orario, 'seats': p.posti_prenotati, }) return lista_prenotazioni api.add_resource(Init, "/api/v0.1/init/start") api.add_resource(Reservations, '/api/v0.1/reservations/<string:room>/<string:time>') api.add_resource(Users, '/api/v0.1/users/<string:email>')
if color: return color.to_dict() return 'Color not found.' def post(self, name): args = parser.parse_args() color = create_color(name, args.r, args.g, args.b) color.put() return color.to_dict() def put(self, name): args = parser.parse_args() color = Color.get_by_id(name) if color: color = create_color(name, args.r, args.g, args.b) color.put() return color.to_dict() else: return 'Color not found.' def delete(self, name): e = ndb.Key('Color', name).get() if e: e.key.delete() return 'Color deleted' return 'Color not found.' api.add_resource(APIColorList, '/api/v0.1/color/') api.add_resource(APIColor, '/api/v0.1/color/<string:name>')
class StateFloor(Resource): def get(self, num_floor): if int(num_floor) < 0 or int(num_floor) > 2: return {'Error': 'Invalid Input'}, 401 name_piano = DICT_ID[num_floor] floor = Floor.get_by_id(name_piano) if not floor: return {'Error': 'Floor not found'}, 404 list_rooms = list() for room in floor.rooms: list_rooms.append({ 'Name': room.name, 'State Light Bulb': room.state_light_bulb, 'Percentage': room.percentage }) return {'Floor': floor.name, 'Rooms': list_rooms} class EditRoom(Resource): def put(self, name_room): floor_list = Floor.query(Floor.rooms.name == 'Studio').fetch() for floor in floor_list: print(floor) return "" api.add_resource(StateFloor, '/api/v1.0/state_floor/<string:num_floor>') api.add_resource(EditRoom, '/api/v1.0/edit_floor/<string:name_room>')
parser.add_argument('cognome', type=str, required=True, location='json') parser.add_argument('autorizzazione', type=str, location='json') #Api che permette ad un controllore di richiedere le credenziali dell'ultimo accesso class GetUltimoApi(Resource): def get(self): accessi = Accessi.query().order(-Accessi.timestamp).fetch(1) return {'codice': accessi[0].codice, 'nome' : accessi[0].nome, 'cognome': accessi[0].cognome, 'autorizzazione': accessi[0].autorizzazione, 'timestamp' : str(accessi[0].timestamp), 'fascia_orario': accessi[0].datatemp } api.add_resource(GetUltimoApi, '/api/v0.1/ultimo') #Api utilizzata per ricevere in input i dati rilevati dal sensore e restituire l'esito class AccessoApi(Resource): def post(self): if not request.json or 'codice' not in request.json: abort(400) #prendo il numero della tessera codice = request.json['codice'] utenti = Utenti.query(Utenti.codice==codice).fetch(1) if not utenti: return {'message': 'CODICE SCONOSCIUTO'} utente = utenti[0]
from flask_restful import Resource, reqparse, inputs from app.flask_app import api parser = reqparse.RequestParser(bundle_errors=True) parser.add_argument("r", type=inputs.int_range(0, 255), required=True, location='json') parser.add_argument("g", type=inputs.int_range(0, 255), required=True, location='json') parser.add_argument("b", type=inputs.int_range(0, 255), required=True, location='json') class Color(Resource): def get(self, name): return {'name': name} def post(self, name): args = parser.parse_args() return {'post_name': name, 'r': args.r, 'g': args.g, 'b': args.b} api.add_resource(Color, '/api/v0.1/color/<string:name>')
class ArtistsPOST(Resource): def post(self): body = request.get_json() if ArtistModel.query(ArtistModel.name == body['artist']).get(): return { 'Error 409': 'An artist with the same name is already stored in the database.' }, 409 id_artist = id = str(uuid.uuid1()) name_artist = body['artist'] num_search_artist = body['s_num'] artist = ArtistModel(id=id_artist, name=name_artist, num_search=num_search_artist) artist.put() return {'id': id_artist} class ArtistsGET(Resource): def get(self, id): artist = ArtistModel.query(ArtistModel.id == id).get() if not artist: return {'Error 404': 'The artist does not exist'}, 404 return {'artist': artist.name, 's_num': artist.num_search} api.add_resource(Artist, '/api/1/artist') api.add_resource(ArtistsPOST, '/api/1/artists') api.add_resource(ArtistsGET, '/api/1/artists/<string:id>')
def post(self, value): """ Aggiunge una lista di errori noti per la parola indicata. In particolare, se la parola e' gia' presente incrementa il contatore. Successivamente aggiunge le parole errate associate :param word: :return: """ word = Word.get_by_id(value.title()) if not word: return {'Error': 'Parola non trovata'}, 404 args = parser.parse_args() errors = args.get('errors') for error in errors: error = error.title() if len(error.split()) < 2 and error not in word.errors: word.errors.append(error.title()) word.counter = word.counter + 1 word.put() word_dict = { 'Parola Corretta': word.correct, 'Numero occorrenze': word.counter, 'Parole errate associate': word.errors } return word_dict api.add_resource(Last, '/api/corrections/last') api.add_resource(Frequents, '/api/corrections/frequents') api.add_resource(Words, '/api/correct/<string:value>')
user = User.query_single_by_username(username_old) if not user: print("UPDATE_ENTITY: User not found") abort(400) user.username = username_new user.age = age_new user.birthday = birthday_new user.put() return user.to_json() def delete(self): username = request.headers["username"] if not username: print('No data in this post. aborting') abort(400) print username user = User.query_single_by_username(username) if not user: print "User does not exist" abort(400) user.key.delete() return {}, 204 api.add_resource(UserApi, '/v1/user')
parking_free.append(parking.parking_id) return {'Slots': parking_free} class Slot(Resource): def get(self, slot_id): if slot_id not in range(101, 111) and slot_id not in range(201, 211) and slot_id not in range(301, 311): return {'Error': 'Invalid Input Parameters'}, 400 parking = Parking.get_by_id(slot_id) if not parking: return {'Error': 'Parking not found'}, 404 else: return {'Slot': parking.parking_id, 'State': parking.state} def put(self, slot_id): if slot_id not in range(101, 111) and slot_id not in range(201, 211) and slot_id not in range(301, 311): return {'Error': 'Invalid Input Parameters'}, 400 args = parser.parse_args() state = args.get('state') parking = Parking.get_by_id(slot_id) if not parking: return {'Error': 'Parking not found'}, 404 parking.state = state parking.put() return {'Slot': parking.parking_id, 'State': parking.state} api.add_resource(Init, '/api/v1.0/init') api.add_resource(Available, '/api/v1.0/availables') api.add_resource(Slot, '/api/v1.0/slot/<int:slot_id>')