Coder Social home page Coder Social logo

charles-vdulac / django-roa Goto Github PK

View Code? Open in Web Editor NEW
51.0 51.0 25.0 1.72 MB

Fork of original Django ROA lib. Now ROA works directly with an API like Django Rest Framework

License: BSD 3-Clause "New" or "Revised" License

Python 99.95% Shell 0.05%

django-roa's People

Contributors

charles-vdulac avatar davidbgk avatar jathanism avatar jocelyndelalande avatar nchilada avatar wyplay-portal avatar xordoquy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

django-roa's Issues

Problem creating non-roa proxy models

Hi, i'm trying to create a proxy for some ROA model that will use a normal manager insted of a ROAManager to make queries in a SQL database (i have read access only, that's why i use ROA). The problem is that i can't set the default manager correctly. This is part of my code:

class SomeROAModelProxy(SomeROAModel):
    class Meta(SomaROAModel.Meta):
        proxy = True
    objects = models.Manager()

and here is my problem:

>>> SomeROAModel._default_manager 
<django_roa.db.managers.ROAManager at 0x2470250>
>>> SomeROAModelProxy._default_manager
<django_roa.db.managers.ROAManager at 0x303d2d0>
>>> SomeROAModel._default_manager is SomeROAModelProxy._default_manager
False

The manager of SomeROAModelProxy shoud be a models.Manager instance. If I do the same with the User model for example, it works well:

class UserProxy(User):
    class Meta(User.Meta):
        proxy = True
    objects = models.Manager()

>>> User._default_manager
<django.contrib.auth.models.UserManager at 0x33ded50>
>>> UserProxy._default_manager
<django.db.models.manager.Manager at 0x3fbaa10>

How to make a relation between a local and remote Model

In your DRF example you have frontend and backend apps.

In each, you are defining the models for "Articles" "Tags" etc...

But if for example there was a model in the front end, and you wanted to create a relationship to the "backend" between the two, how would you handle this?

'NoneType' object has no attribute 'is_valid'

I have followed the examples and have hit an error I can't seem to figure out. It seems that at this line of code the serializer variable is None. The line just before it it calls the method get_serializer() on the model and I defined the serialize() method in my model. Also for context in my view I am calling Market.objects.all() which is where the following error occurs.

Error:

AttributeError at /markets/
'NoneType' object has no attribute 'is_valid'
Request Method: GET
Request URL:    http://127.0.0.1:8000/markets/
Django Version: 1.6.4
Exception Type: AttributeError
Exception Value:    
'NoneType' object has no attribute 'is_valid'
Exception Location: /Users/mdahl/Documents/Repositories/django-test/src/django-roa/django_roa/db/query.py in iterator, line 217
Python Executable:  /Users/mdahl/Documents/Repositories/django-test/bin/python
Python Version: 2.7.6
Python Path:    
['/Users/mdahl/Documents/Repositories/django-test/djangotest',
 '/Users/mdahl/Documents/Repositories/django-test/src/django-roa',
 '/Users/mdahl/Documents/Repositories/django-test/lib/python27.zip',
 '/Users/mdahl/Documents/Repositories/django-test/lib/python2.7',
 '/Users/mdahl/Documents/Repositories/django-test/lib/python2.7/plat-darwin',
 '/Users/mdahl/Documents/Repositories/django-test/lib/python2.7/plat-mac',
 '/Users/mdahl/Documents/Repositories/django-test/lib/python2.7/plat-mac/lib-scriptpackages',
 '/Users/mdahl/Documents/Repositories/django-test/lib/python2.7/lib-tk',
 '/Users/mdahl/Documents/Repositories/django-test/lib/python2.7/lib-old',
 '/Users/mdahl/Documents/Repositories/django-test/lib/python2.7/lib-dynload',
 '/usr/local/Cellar/python/2.7.6_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
 '/usr/local/Cellar/python/2.7.6_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
 '/usr/local/Cellar/python/2.7.6_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
 '/usr/local/Cellar/python/2.7.6_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
 '/usr/local/Cellar/python/2.7.6_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/Users/mdahl/Documents/Repositories/django-test/lib/python2.7/site-packages']
Server time:    Fri, 23 May 2014 23:52:23 +0000

Settings:

...

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django_roa',
    'main',
)

...

API_BASE_URL = 'http://127.0.0.1/inventory/api/v1/'

ROA_MODELS = True  # set to False if you'd like to develop/test locally
ROA_FORMAT = 'json'  # json or xml
ROA_DJANGO_ERRORS = True  # useful to ease debugging if you use test server

Model:

from django.conf import settings
from django.db import models
from django_roa import Model as ROAModel


class ApiModel(ROAModel):
    @classmethod
    def get_resource_url_list(cls):
        return u'{base_url}{base_name}/'.format(
            base_name=cls.api_base_name,
            base_url=settings.API_BASE_URL,
        )

    def get_resource_url_count(self):
        return self.get_resource_url_list()

    class Meta:
        abstract = True


class Market(ApiModel):
    api_base_name = 'market'

    id = models.IntegerField(primary_key=True)
    display_name = models.CharField(max_length=64)
    notes = models.TextField(default='')

    @classmethod
    def serializer(cls):
        from serializers import MarketSerializer
        return MarketSerializer

Serializer:

from rest_framework import serializers
from models import Market


class MarketSerializer(serializers.ModelSerializer):
    class Meta:
        model = Market

        fields = (
            'id',
            'display_name',
            'notes',
        )

Changing ROA_MODELS setting in tests

The ROA_MODELS setting seems to only be set on application load, preventing it from being toggled in a test case like in the following example. It seems it would be useful if it were designed so this setting could be toggled in this manner for non-integration tests.

class TestUser(unittest.TestCase):
    def setUp(self):
        # Use local database for models as these tests are not integration tests
        settings.ROA_MODELS = False

    def tearDown(self):
        # Revert setting back to use API for database
        settings.ROA_MODELS = True

    def test_create(self):
        user = User(name='John Doe')
        try:
            user.save()
            self.assertTrue(True)
        except Exception as e:
            self.fail('Failed to create user with exception: %s' % e)

Looking at how the ROA_MODELS setting is currently implemented I am not sure how to achieve testing locally without actually changing the setting in settings.py, which would then break the functionality of running the dev server against an actual API server. It would be rather tedious to constantly change this setting back and forth depending on if I am running the dev server versus tests.

Example server made with Django Rest Framework

Can you pls put to the repo an example server project made with Django Rest Framework. I didn't manage to make your version of django-roa work properly with my own simple server on Django Rest Framework. Maybe it requires some kind of special configuration of server side which I'm not aware of.
Thanks.

How to make a get/post/delete/put

Hello,

I was wondering if you could provide more detail on how to make the following calls. I searched on the github and wiki page and was unable to understand how to make the calls.

Thank you

Python 3 Support

Just putting it up here, a quick win would be adding 3.3 and 3.4 to the travis.ci test suites, haven't dug in deeper yet.

Trying to run examples

I tried to run django_roa_server from examples following the instructions on https://bitbucket.org/david/django-roa/wiki/GettingStarted but I get this error:
Traceback (most recent call last):
File "manage.py", line 4, in
from django.core.management import execute_manager
ImportError: cannot import name execute_manager

I already tried using a new manage.py file but it didn't run neither. Could you help me to run it?

Support for writable ManyToManyField's

Hii, I'm using ROA as a client of a Django Rest Framework API. I defined in both sides a basic many to many field. In DRF i can read and write this via json as a list ok primary keys.
In ROA i can read this field correctly but for writing I can't: The clear() method doesn't work and add() throws thid error:
Cannot add "<Model: XXXX>": instance is on database "None", value is on database "default"
It seems that this happens because it reads the model on the local database and not via the API.
Some useful information:

>>> model = Model.objects.first()
>>> model.m2m_field
<django.db.models.fields.related.ManyRelatedManager at 0x7f2323f6a810>
>>> model.m2m_field.is_roa_manager
True

How can I solve this?

Incorrect behaviour when using queryset based on another queryset

Pasting IPython output
In [92]: # Normal queryset
In [93]: qs = User.objects.all()

In [94]: another_qs = qs.filter(pk=1)

In [95]: qs is another_qs
Out[95]: False

In [96]: another_qs # Only the filtered ones
Out[96]: [User: user1]

In [97]: qs # Should display all users
Out[97]: [User: user1, User: user2, User: user3, User: user4]

In [98]: # Queryset using ROA
In [99]: qs = SomeRoaModel.objects.all()

In [100]: another_qs = qs.filter(pk=1)

In [101]: qs is another_qs
Out[101]: False

In [102]: another_qs # Only the filtered ones
Out[102]: [SomeRoaModel: AAA]

In [103]: qs # Should display all objects (AAA, BBB, CCC and DDD)
Out[103]: [SomeRoaModel: AAA] <-- It shows only the another_qs' objects instead of the qs ones

This problem is breaking the validation of a ROAModel in my admin site, how can it be solved?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.