akoumjian / django-conduit Goto Github PK
View Code? Open in Web Editor NEWEasy and Powerful REST APIs for Django
Easy and Powerful REST APIs for Django
I work with an old-school duder who only uses CentOS with SELinux. Since it still comes with Python 2.6 by default i noticed this issue here:
https://github.com/thebigspoon/django-conduit/blob/master/conduit/base.py#L1
Maybe importlib needs to be listed as a requirement on pypi? or maybe Python 2.7 listed as a requirement or maybe some nice try/except logic on import statement?
I'm sure you want to think about this more before coding a solution. So this is not a pull request, just a reminder ;-)
Here's the dynamically bound method you mentioned I'm using in one of my branches. FYI, it does break a few tests and I haven't looked at those.
I'm not entirely sure where the issue is here, so correct me if I'm off:
When cloning this project and running python setup.py install
I expect that the .egg file and source code will be copied for the build to the correct virtualvenv /lib/python<version/site-packages
directory. The .egg file is copied, but the source code is not ( see listing below ).
I ran into this problem when i was trying to figure out why after running setup.py the create_api
command was not showing up in python manage ---help
options. To resolve it i just used django-conduit as a local ( non-site-package ) django app.
Adding django-conduit 0.1.0-beta to easy-install.pth file
Saving /usr/local/src/conduit_example/avenv/lib/python2.7/site-packages/easy-install.pth
Installed /usr/local/src/conduit_example/avenv/lib/python2.7/site-packages/django_conduit-0.1.0_beta-py2.7.egg
Processing dependencies for django-conduit==0.1.0-beta
Searching for python-dateutil==2.1
Best match: python-dateutil 2.1
Adding python-dateutil 2.1 to easy-install.pth file
Using /usr/local/src/conduit_example/avenv/lib/python2.7/site-packages
Searching for six==1.3.0
Best match: six 1.3.0
Adding six 1.3.0 to easy-install.pth file
Using /usr/local/src/conduit_example/avenv/lib/python2.7/site-packages
Finished processing dependencies for django-conduit==0.1.0-beta
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named psycopg2
@thebigspoon you broke it! :-P
This issue seems to be related to #9 but was not caught.
Problem:
Assuming i have the following resource with a pk_field
override and unique IDs for that field:
class FooResource(ModelResource):
class Meta(ModelResource.Meta):
model = Foo
pk_field = 'name'
class Fields:
bar = ForeignKeyField(attribute='bar', resource_cls=BarResource, embed=False)
bazzes = ManyToManyField(attribute='bazzes', resource_cls=BazResource, embed=False)
Then I should be able to PUT new data against that detailed view, this curl however generates the following error:
curl -X PUT -H "Content-Type: application/json" -d '{"float_field": 99999.123456789, "bar": "/api/v1/bar/1/", "name": "264b8e4b", "created": "2013-06-21T01:44:57.367956+00:00", "text":"WhoooHoooo, update_objs_from_data works!", "decimal": "110.12", "birthday": "2013-06-19", "boolean": false, "bazzes": ["/api/v1/baz/1/", "/api/v1/baz/198/"], "integer": 12, "file_field": "test/test.txt"}' http://localhost:9999/api/v1/foo/264b8e4b/
Internal Server Error: /api/v1/foo/264b8e4b/
Traceback (most recent call last):
File "/usr/local/src/conduit_contrib/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 115, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/usr/local/src/conduit_contrib/conduit/base.py", line 35, in view
(request, args, kwargs,) = method(self, request, *args, **kwargs)
File "/usr/local/src/conduit_contrib/conduit/subscribe.py", line 15, in returned_wrapper
return func(self, request, *args, **kwargs)
File "/usr/local/src/conduit_contrib/conduit/api/base.py", line 639, in update_objs_from_data
bundle['obj'] = self.Meta.model.objects.get(**{self.Meta.pk_field: obj.pk})
File "/usr/local/src/conduit_contrib/venv/local/lib/python2.7/site-packages/django/db/models/manager.py", line 143, in get
return self.get_query_set().get(*args, **kwargs)
File "/usr/local/src/conduit_contrib/venv/local/lib/python2.7/site-packages/django/db/models/query.py", line 389, in get
(self.model._meta.object_name, kwargs))
The issue is the same as it was in #9 just in a different method:
https://github.com/akoumjian/django-conduit/blob/master/conduit/api/base.py#L625
Looks like a consequence of this addition:
https://github.com/akoumjian/django-conduit/blob/master/conduit/api/utils.py#L32-L36
is that now all requests work on an instance of AutoModelResource. And since our AutoModelResource has taken the place of the ModelResouce sublcass, we don't have access to attributes like 'Fields' anymore. This function returns empty explicit fields:
https://github.com/akoumjian/django-conduit/blob/master/conduit/api/base.py#L103
I'm surprised more didn't break with this change, but maybe i'm misunderstanding something.
One potential solution ( though a magic one ) is to make AutoModelResource sublcass the explicit ModelResource sublcasses. The code below works but relies on imports, naming conventions and knowing what mods the resource sublcasses are stored in:
def _get_explicit_model_resource( self, model ):
'''
with some magic naming assumptions:
1) views are always in a file called 'views.py' # bad assumption
2) all model resources are named as class in the views as '<ModelName>Resource' # bad assumption
'''
try:
module_magic_name = 'example.views'
resource_magic_name = '%sResource' % model.__name__
_temp = __import__( module_magic_name, fromlist=[ resource_magic_name, ] )
return _temp.__getattribute__( resource_magic_name )
except ImportError, e:
logger.error( "ModelResource = %s not found" % resource_magic_name )
return None
def _gen_resource(self, model):
explicit_model_resource = self._get_explicit_model_resource( model )
class AutoModelResource( explicit_model_resource ):
class Meta( explicit_model_resource.Meta ):
pass
#AutoModelResource.Meta.model = model
return AutoModelResource
I'm using virtualvenv on Ubuntu 12.10 ( Python 2.7 ) and CentOS 6.4 ( Python 2.6 ). In both cases running pip install -v django-conduit
gives a similar error about not finding it:
(venv)gcorradini@twinkie:$ pip install -v django-conduit
...
Downloading/unpacking django-conduit
Could not find any downloads that satisfy the requirement django-conduit
No distributions at all found for django-conduit
Exception information:
Traceback (most recent call last):
File "/usr/local/src/conduit_example/avenv/local/lib/python2.7/site-packages/pip-1.1-py2.7.egg/pip/basecommand.py", line 104, in main
status = self.run(options, args)
File "/usr/local/src/conduit_example/avenv/local/lib/python2.7/site-packages/pip-1.1-py2.7.egg/pip/commands/install.py", line 245, in run
requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundle=self.bundle)
File "/usr/local/src/conduit_example/avenv/local/lib/python2.7/site-packages/pip-1.1-py2.7.egg/pip/req.py", line 978, in prepare_files
url = finder.find_requirement(req_to_install, upgrade=self.upgrade)
File "/usr/local/src/conduit_example/avenv/local/lib/python2.7/site-packages/pip-1.1-py2.7.egg/pip/index.py", line 157, in find_requirement
raise DistributionNotFound('No distributions at all found for %s' % req)
DistributionNotFound: No distributions at all found for django-conduit
Storing complete log in /home/gcorradini/.pip/pip.log
Tried playing around with conduit tonight using the install instructions from https://django-conduit.readthedocs.org/en/latest/#getting-started The tagged 0.1.0 release doesn't include the management
directory when it's installed so management commands aren't available after installing with pip.
It looks like some recent commits fix this problem in setup.py
so just updating the package index ought to fix this for anyone who wants to try out conduit using:
pip install django-conduit
๐ฆ ๐ผ
Problem:
I have related resources on Foo set to use embed=False like below
class FooResource(ModelResource):
class Meta(ModelResource.Meta):
model = Foo
class Fields:
bar = ForeignKeyField(attribute='bar', resource_cls=BarResource)
bazzes = ManyToManyField(attribute='bazzes', resource_cls=BazResource)
If i POST to /api/v1/foo/
with a new object that does not have a related resource_uri specified ( below example leaves bar resource_uri blank ) I get the generic and unfriendly Django 404 html response for /api/v1/foo/
.
curl -X POST -H "Content-Type: application/json" -d '{"float_field": 99999.123456789, "bar": "", "name": "TEST_RELATED", "created": "2013-06-21T01:44:57.367956+00:00", "text": "Testing relates saves", "decimal": "110.12", "birthday": "2013-06-19", "boolean": false, "bazzes": ["/api/v1/baz/1/", "/api/v1/baz/198/"], "integer": 12, "file_field": "test/test.txt"}' http://localhost:9999/api/v1/foo/
It would be nice to get an error that is more specifc error.
The problem seems to be Resolver404 is not caught at all:
https://github.com/akoumjian/django-conduit/blob/master/conduit/api/fields.py#L105
We discussed this and decided Conduit should be as flexibile as possible regarding creating resources that have non-required fields. When making a POST
request, the payload will not require null
values to be present.
Exploratory work is started in #36
Problem:
Changing the pk_field in class Meta
for a resource still dumps default ( id ) resource_uri. In example below i made names unique on Foo objects, but resource_uri is wrong:
class FooResource(ModelResource):
class Meta(ModelResource.Meta):
model = Foo
pk_field = 'name'
class Fields:
bar = ForeignKeyField(attribute='bar', resource_cls=BarResource, embed=True)
bazzes = ManyToManyField(attribute='bazzes', resource_cls=BazResource, embed=True)
{
"float_field":100000.123456789,
"bar":{
"resource_uri":"/api/v1/bar/3/",
"id":3,
"name":""
},
"name":"d9bdd5f45",
"created":"2013-06-21T01:44:57.367956+00:00",
"text":"My foo text g oes here",
"decimal":"110.12",
"birthday":"2013-06-19",
"id":1,
"boolean":false,
"bazzes":[
{
"resource_uri":"/api/v1/baz/1/",
"id":1,
"name":"3"
},
{
"resource_uri":"/api/v1/baz/198/",
"id":198,
"name":"Complete New Baz"
}
],
"integer":12,
"file_field":"test/test.txt",
"resource_uri":"/api/v1/foo/1/"
}
Problem code:
https://github.com/akoumjian/django-conduit/blob/master/conduit/api/base.py#L94
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.