def new_person(self, request, response, pathmatch): d=dict() for p in ['firstname', 'lastname', 'email']: if p not in request.params or not request.params[p]: response.send(code=400, body="<p>Nicht alle notwendigen Felder ausgefüllt. {} fehlt.</p>".format(p)) return d[p]=request.params[p] for p in ['hobby', 'team']: if p in request.params and request.params[p]: if isinstance(request.params[p], list): d[p]=request.params[p] else: d[p]=[request.params[p]] else: d[p]=[] person = Person(firstname=d['firstname'], lastname=d['lastname'], email=d['email'], hobbies=d['hobby']) person.store() for tid in d['team']: try: team = Team(id=tid) team.add_person(person) team.store() except Exception: pass response.send(code=200, body="")
def add_hobby(self, request, response, pathmatch): person = Person(id=pathmatch.group('id')) hobby = unquote(pathmatch.group('hobby')) # path parts aren't decoded by server framework if hobby not in person.get_hobbies(): person.hobbies.append(hobby) person.store() response.send(code=200, body="")
def delete_hobby(self, request, response, pathmatch): person = Person(id=pathmatch.group('id')) hobbies = person.get_hobbies() try: hobby = unquote(pathmatch.group('hobby')) # path parts aren't decoded by server framework hobbies.remove(hobby) person.hobbies = hobbies person.store() response.send(code=200, body="") except ValueError: response.send(code=404, body="<p>Fehler: Hobby '{}' nicht gefunden.</p>".format(hobby))