diff --git a/README.md b/README.md index 559eecac8004bc7e5b7b1d63efe0d9fe222ea980..cd9b4226dfdeb7796a7f36adafa3dc7a3a609fb1 100644 --- a/README.md +++ b/README.md @@ -14,19 +14,19 @@ It aims at enabling people with little development skills to serve their own dat ## Installation -### 1- Install this module and all its dependencies +1. Install this module and all its dependencies ``` pip install djangoldp ``` -### 2- Create a django project +2. Create a django project ``` django-admin startproject myldpserver ``` -### 3- Create your django model inside a file myldpserver/myldpserver/models.py +3. Create your django model inside a file myldpserver/myldpserver/models.py ``` from django.db import models @@ -37,7 +37,7 @@ class Todo(models.Model): ``` -#### 3.1 Configure field visibility (optional) +3.1. Configure field visibility (optional) Note that at this stage you can limit access to certain fields of models using ``` @@ -56,7 +56,7 @@ User._meta.serializer_fields = ('username','first_name','last_name') Note that this will be overridden if you explicitly set the fields= parameter as an argument to LDPViewSet.urls(), and filtered if you set the excludes= parameter. -### 4- Add a url in your urls.py: +4. Add a url in your urls.py: ``` from django.conf.urls import url @@ -72,14 +72,14 @@ urlpatterns = [ This creates 2 routes, one for the list, and one with an ID listing the detail of an object. -### 5- In the settings.py file, add your application name at the beginning of the application list, and add the following lines +5. In the settings.py file, add your application name at the beginning of the application list, and add the following lines ``` STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static') LDP_RDF_CONTEXT = 'https://cdn.happy-dev.fr/owl/hdcontext.jsonld' ``` -### 6- You can also register your model for the django administration site +6. You can also register your model for the django administration site ``` from django.contrib import admin @@ -88,14 +88,14 @@ from .models import Todo admin.site.register(Todo) ``` -### 7- You then need to have your WSGI server pointing on myldpserver/myldpserver/wsgi.py +7. You then need to have your WSGI server pointing on myldpserver/myldpserver/wsgi.py -### 8- You will probably need to create a super user +8. You will probably need to create a super user ``` ./manage.py createsuperuser ``` -### 9- If you have no CSS on the admin screens : +9. If you have no CSS on the admin screens : ``` ./manage.py collectstatic ``` @@ -122,10 +122,25 @@ In the following example, besides the urls `/members/` and `/members/<pk>/`, two url(r'^members/', LDPViewSet.urls(model=Member, nested_fields=("skills",))), ``` +From the 0.5 we added permissions check by default on every route, so you may encounter 400 errors code on your POST requests. You can disable those checks by specifying the permission_classes as an empty array in our URLs files. + + +``` +url(r'^posts/', LDPViewSet.urls(model=Post, permission_classes=(), filter_backends = ())), +``` + ## Custom Meta options on models ### rdf_type ### auto_author +This property allows to associate a model with the logged in user. + +```python +class MyModel(models.Model): + author_user = models.ForeignKey(settings.AUTH_USER_MODEL) + class Meta: + auto_author = 'author_user' +``` ## License diff --git a/djangoldp/views.py b/djangoldp/views.py index b4ae860297dbd663be7a4fe595f3ccacbb9c582a..5c52c3ae1e0c3d597304be55f80a7cc111f0d281 100644 --- a/djangoldp/views.py +++ b/djangoldp/views.py @@ -37,13 +37,17 @@ class NoCSRFAuthentication(SessionAuthentication): class WACPermissions(DjangoObjectPermissions): perms_map = { 'GET': ['%(app_label)s.view_%(model_name)s'], - 'OPTIONS': ['%(app_label)s.view_%(model_name)s'], + 'OPTIONS': [], 'HEAD': ['%(app_label)s.view_%(model_name)s'], 'POST': ['%(app_label)s.add_%(model_name)s'], 'PUT': ['%(app_label)s.change_%(model_name)s'], 'PATCH': ['%(app_label)s.change_%(model_name)s'], 'DELETE': ['%(app_label)s.delete_%(model_name)s'], } + def has_permission(self, request, view): + if request.method == 'OPTIONS': + return True + return super().has_permission(request, view) class AnnonReadOnly(WACPermissions): authenticated_users_only = False @@ -81,7 +85,7 @@ class LDPViewSetGenerator(ModelViewSet): def get_detail_expr(cls, lookup_field=None, **kwargs): '''builds the detail url based on the lookup_field''' lookup_field = lookup_field or cls.get_lookup_arg(**kwargs) - lookup_group = r'\d' if lookup_field == 'pk' else r'[\w-]' + lookup_group = r'\d' if lookup_field == 'pk' else r'[\w\-\.]' return r'(?P<{}>{}+)/'.format(lookup_field, lookup_group) @classonlymethod diff --git a/setup.py b/setup.py index e577b78b7dcd84cb01a80b490efb67d97e167d2f..6f73137be113dae8bad8b37aa7980ec3fb68c400 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import setup setup( name='djangoldp', - version='0.5a9', + version='0.5a10', url='https://git.happy-dev.fr/happy-dev/djangoldp/', author="Startin'blox", author_email='sylvain@happy-dev.fr',