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
|
||||
Family.find(self.surname.downcase)
|
||||
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
|
||||
|
||||
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.helpers import json_loads_from_request
|
||||
|
||||
__families__ = Families()
|
||||
|
||||
@ -8,9 +8,7 @@ __families__ = Families()
|
||||
if GET:
|
||||
response.body = __families__.getall()
|
||||
elif POST:
|
||||
family = json.loads(
|
||||
request.body.s_iter.read(int(request.headers['Content-Length']))
|
||||
)['family']
|
||||
family = json_loads_from_request(request)['family']
|
||||
family_id = __families__.add(family)
|
||||
response.headers['Location'] = '/families/{}.json'.format(family_id)
|
||||
family['id'] = family_id
|
||||
|
@ -1,6 +1,5 @@
|
||||
import json
|
||||
from server.people import People
|
||||
from aspen import Response
|
||||
from server.helpers import json_loads_from_request
|
||||
|
||||
__people__ = People()
|
||||
|
||||
@ -9,12 +8,11 @@ __people__ = People()
|
||||
if GET:
|
||||
response.body = __people__.getall()
|
||||
elif POST:
|
||||
person = json.loads(
|
||||
request.body.s_iter.read(int(request.headers['Content-Length']))
|
||||
)['person']
|
||||
person = json_loads_from_request(request)['person']
|
||||
person_id = __people__.add(person)
|
||||
response.headers['Location'] = '/people/{}.json'.format(person_id)
|
||||
response.body = {}
|
||||
raise Response(201)
|
||||
person['id'] = person_id
|
||||
response.body = person
|
||||
response.code = 201
|
||||
|
||||
# vim:filetype=python
|
||||
|
Loading…
Reference in New Issue
Block a user