Commit cd9a45ee authored by Jean-Baptiste's avatar Jean-Baptiste

Merge branch '73-reload-source' into 'master'

Resolve "Need to restart server to access created ldp source"

Closes #73

See merge request !82
parents 8d60dbe4 a67f2ed6
Pipeline #4785 passed with stage
in 1 minute and 21 seconds
from django.conf import settings from django.contrib.auth.models import User
from django.contrib.auth.models import AnonymousUser, User
from django.db import models from django.db import models
from django.db.models.base import ModelBase from django.db.models.base import ModelBase
from django.urls import get_resolver from django.urls import get_resolver
from django.utils.decorators import classonlymethod from django.utils.decorators import classonlymethod
from guardian.shortcuts import get_perms
from djangoldp.permissions import LDPPermissions from djangoldp.permissions import LDPPermissions
User._meta.rdf_type = "foaf:user" User._meta.rdf_type = "foaf:user"
......
import django
import sys import sys
import django
from django.conf import settings from django.conf import settings
settings.configure(DEBUG=False, settings.configure(DEBUG=False,
ALLOWED_HOSTS = ["*"], ALLOWED_HOSTS=["*"],
DATABASES={ DATABASES={
'default': { 'default': {
'ENGINE': 'django.db.backends.sqlite3', 'ENGINE': 'django.db.backends.sqlite3',
} }
}, },
LDP_RDF_CONTEXT = { LDP_RDF_CONTEXT={
"@context": { "@context": {
"@vocab": "http://happy-dev.fr/owl/#", "@vocab": "http://happy-dev.fr/owl/#",
"foaf": "http://xmlns.com/foaf/0.1/", "foaf": "http://xmlns.com/foaf/0.1/",
...@@ -35,7 +36,8 @@ settings.configure(DEBUG=False, ...@@ -35,7 +36,8 @@ settings.configure(DEBUG=False,
"control": "acl:Control" "control": "acl:Control"
} }
}, },
AUTHENTICATION_BACKENDS = ('django.contrib.auth.backends.ModelBackend', 'guardian.backends.ObjectPermissionBackend'), AUTHENTICATION_BACKENDS=(
'django.contrib.auth.backends.ModelBackend', 'guardian.backends.ObjectPermissionBackend'),
ROOT_URLCONF='djangoldp.urls', ROOT_URLCONF='djangoldp.urls',
DJANGOLDP_PACKAGES=['djangoldp.tests'], DJANGOLDP_PACKAGES=['djangoldp.tests'],
INSTALLED_APPS=('django.contrib.auth', INSTALLED_APPS=('django.contrib.auth',
...@@ -46,10 +48,9 @@ settings.configure(DEBUG=False, ...@@ -46,10 +48,9 @@ settings.configure(DEBUG=False,
'djangoldp', 'djangoldp',
'djangoldp.tests', 'djangoldp.tests',
), ),
SITE_URL = 'http://happy-dev.fr', SITE_URL='http://happy-dev.fr',
) )
django.setup() django.setup()
from django.test.runner import DiscoverRunner from django.test.runner import DiscoverRunner
...@@ -64,9 +65,9 @@ failures = test_runner.run_tests([ ...@@ -64,9 +65,9 @@ failures = test_runner.run_tests([
'djangoldp.tests.tests_auto_author', 'djangoldp.tests.tests_auto_author',
'djangoldp.tests.tests_get', 'djangoldp.tests.tests_get',
'djangoldp.tests.tests_delete', 'djangoldp.tests.tests_delete',
'djangoldp.tests.tests_sources',
# 'djangoldp.tests.tests_temp' # 'djangoldp.tests.tests_temp'
]) ])
if failures: if failures:
sys.exit(failures) sys.exit(failures)
from rest_framework.test import APIRequestFactory, APIClient, APITestCase
from djangoldp.models import LDPSource
class TestSource(APITestCase):
def setUp(self):
self.factory = APIRequestFactory()
self.client = APIClient()
def tearDown(self):
pass
def test_get_resource(self):
source = LDPSource.objects.create(federation="source_name", container="http://bar.foo/")
response = self.client.get('/sources/{}/'.format(source.federation), content_type='application/ld+json')
self.assertEqual(response.status_code, 200)
import sys
from importlib import reload
from django.apps import apps from django.apps import apps
from django.conf import settings from django.conf import settings
from django.conf.urls import url, include from django.conf.urls import url, include
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.core.exceptions import FieldDoesNotExist from django.core.exceptions import FieldDoesNotExist
from django.core.urlresolvers import get_resolver from django.core.urlresolvers import get_resolver
from django.db.models.signals import post_save, post_delete
from django.db.utils import OperationalError, ProgrammingError from django.db.utils import OperationalError, ProgrammingError
from django.dispatch import receiver
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from django.urls import clear_url_caches
from django.utils.decorators import classonlymethod from django.utils.decorators import classonlymethod
from guardian.shortcuts import get_objects_for_user
from pyld import jsonld from pyld import jsonld
from rest_framework import status from rest_framework import status
from rest_framework.authentication import SessionAuthentication from rest_framework.authentication import SessionAuthentication
from rest_framework.parsers import JSONParser from rest_framework.parsers import JSONParser
from rest_framework.renderers import JSONRenderer from rest_framework.renderers import JSONRenderer
from rest_framework.viewsets import ModelViewSet
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from djangoldp.models import LDPSource, Model from djangoldp.models import LDPSource, Model
from djangoldp.permissions import LDPPermissions from djangoldp.permissions import LDPPermissions
...@@ -133,7 +137,8 @@ class LDPViewSet(LDPViewSetGenerator): ...@@ -133,7 +137,8 @@ class LDPViewSet(LDPViewSetGenerator):
meta_args['exclude'] = self.exclude or () meta_args['exclude'] = self.exclude or ()
meta_class = type('Meta', (), meta_args) meta_class = type('Meta', (), meta_args)
from djangoldp.serializers import LDPSerializer from djangoldp.serializers import LDPSerializer
return type(LDPSerializer)(self.model._meta.object_name.lower() + name_prefix + 'Serializer', (LDPSerializer,), {'Meta': meta_class}) return type(LDPSerializer)(self.model._meta.object_name.lower() + name_prefix + 'Serializer', (LDPSerializer,),
{'Meta': meta_class})
def create(self, request, *args, **kwargs): def create(self, request, *args, **kwargs):
serializer = self.get_write_serializer(data=request.data) serializer = self.get_write_serializer(data=request.data)
...@@ -255,7 +260,7 @@ class LDPNestedViewSet(LDPViewSet): ...@@ -255,7 +260,7 @@ class LDPNestedViewSet(LDPViewSet):
nested_related_name = related_field.remote_field.name nested_related_name = related_field.remote_field.name
return cls.urls( return cls.urls(
lookup_field= Model.get_meta(related_field.related_model, 'lookup_field', 'pk'), lookup_field=Model.get_meta(related_field.related_model, 'lookup_field', 'pk'),
model=related_field.related_model, model=related_field.related_model,
exclude=(nested_related_name,) if related_field.one_to_many else (), exclude=(nested_related_name,) if related_field.one_to_many else (),
parent_model=cls.get_model(**kwargs), parent_model=cls.get_model(**kwargs),
...@@ -283,3 +288,14 @@ class LDPSourceViewSet(LDPViewSet): ...@@ -283,3 +288,14 @@ class LDPSourceViewSet(LDPViewSet):
def get_queryset(self, *args, **kwargs): def get_queryset(self, *args, **kwargs):
return super().get_queryset(*args, **kwargs).filter(federation=self.federation) return super().get_queryset(*args, **kwargs).filter(federation=self.federation)
@receiver([post_save, post_delete], sender=LDPSource)
def reload_sources_module(sender, instance, **kwargs):
urlconf = settings.ROOT_URLCONF
clear_url_caches()
if 'djangoldp.urls' in sys.modules:
reload(sys.modules['djangoldp.urls'])
if urlconf in sys.modules:
reload(sys.modules[urlconf])
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment