Playing with ActiveResource just a bit more
before trashing all of it. What I really ought to be doing is playing with Sequel or getting more cozy with ActiveRecord.
This commit is contained in:
parent
32aaca146b
commit
2599a100da
@ -8,6 +8,14 @@ class Person < ActiveResource::Base
|
|||||||
def family
|
def family
|
||||||
Family.find(self.surname.downcase)
|
Family.find(self.surname.downcase)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def family=(family_surname)
|
||||||
|
self.surname = family_surname
|
||||||
|
Family.find(family_surname)
|
||||||
|
rescue StandardError => e
|
||||||
|
STDERR.puts("#{e.class.name}:#{e.message}\n#{e.backtrace.join("\n")}")
|
||||||
|
Family.create(surname: family_surname.downcase)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Family < ActiveResource::Base
|
class Family < ActiveResource::Base
|
||||||
|
5
ares/server/helpers.py
Normal file
5
ares/server/helpers.py
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
import json
|
||||||
|
|
||||||
|
def json_loads_from_request(request):
|
||||||
|
raw = request.body.s_iter.read(int(request.headers['Content-Length']))
|
||||||
|
return json.loads(raw)
|
@ -1,5 +1,5 @@
|
|||||||
import json
|
|
||||||
from server.families import Families
|
from server.families import Families
|
||||||
|
from server.helpers import json_loads_from_request
|
||||||
|
|
||||||
__families__ = Families()
|
__families__ = Families()
|
||||||
|
|
||||||
@ -8,9 +8,7 @@ __families__ = Families()
|
|||||||
if GET:
|
if GET:
|
||||||
response.body = __families__.getall()
|
response.body = __families__.getall()
|
||||||
elif POST:
|
elif POST:
|
||||||
family = json.loads(
|
family = json_loads_from_request(request)['family']
|
||||||
request.body.s_iter.read(int(request.headers['Content-Length']))
|
|
||||||
)['family']
|
|
||||||
family_id = __families__.add(family)
|
family_id = __families__.add(family)
|
||||||
response.headers['Location'] = '/families/{}.json'.format(family_id)
|
response.headers['Location'] = '/families/{}.json'.format(family_id)
|
||||||
family['id'] = family_id
|
family['id'] = family_id
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
import json
|
|
||||||
from server.people import People
|
from server.people import People
|
||||||
from aspen import Response
|
from server.helpers import json_loads_from_request
|
||||||
|
|
||||||
__people__ = People()
|
__people__ = People()
|
||||||
|
|
||||||
@ -9,12 +8,11 @@ __people__ = People()
|
|||||||
if GET:
|
if GET:
|
||||||
response.body = __people__.getall()
|
response.body = __people__.getall()
|
||||||
elif POST:
|
elif POST:
|
||||||
person = json.loads(
|
person = json_loads_from_request(request)['person']
|
||||||
request.body.s_iter.read(int(request.headers['Content-Length']))
|
|
||||||
)['person']
|
|
||||||
person_id = __people__.add(person)
|
person_id = __people__.add(person)
|
||||||
response.headers['Location'] = '/people/{}.json'.format(person_id)
|
response.headers['Location'] = '/people/{}.json'.format(person_id)
|
||||||
response.body = {}
|
person['id'] = person_id
|
||||||
raise Response(201)
|
response.body = person
|
||||||
|
response.code = 201
|
||||||
|
|
||||||
# vim:filetype=python
|
# vim:filetype=python
|
||||||
|
Loading…
Reference in New Issue
Block a user