Commit 286f1109 authored by Jean-Baptiste's avatar Jean-Baptiste

Merge branch '179-automatic-local-source' into 'master'

Resolve "Automatic local source"

Closes #179

See merge request !105
parents 3df810c3 399b5d70
Pipeline #5513 passed with stage
in 1 minute and 19 seconds
......@@ -242,6 +242,16 @@ REST_FRAMEWORK = {
## Sources
To enable sources auto creation for all models, change `djangoldp` by `djangoldp.apps.DjangoldpConfig`, on `INSTALLED_APPS`
## License
Licence MIT
from django.db.models import options
__version__ = '0.0.0'
options.DEFAULT_NAMES += ('lookup_field', 'rdf_type', 'rdf_context', 'auto_author', 'owner_field', 'view_set', 'container_path', 'permission_classes', 'serializer_fields', 'nested_fields', 'depth', 'anonymous_perms', 'authenticated_perms', 'owner_perms')
options.DEFAULT_NAMES += (
'lookup_field', 'rdf_type', 'rdf_context', 'auto_author', 'owner_field', 'view_set', 'container_path',
'permission_classes', 'serializer_fields', 'nested_fields', 'depth', 'anonymous_perms', 'authenticated_perms',
......@@ -3,3 +3,21 @@ from django.apps import AppConfig
class DjangoldpConfig(AppConfig):
name = 'djangoldp'
def ready(self):
def create_local_source(self):
from djangoldp.models import LDPSource, Model
model_classes = {cls.__name__: cls for cls in Model.__subclasses__()}
for class_name in model_classes:
model_class = model_classes[class_name]
if model_class is LDPSource:
path = model_class.get_container_path().strip("/")
existing_source = LDPSource.objects.get(federation=path)
except LDPSource.DoesNotExist:
LDPSource.objects.create(federation=path, urlid=Model.absolute_url(model_class))
......@@ -34,10 +34,14 @@ class Model(models.Model):
return cls.__clean_path(path)
def get_absolute_url(self):
if self.urlid is None or self.urlid != '':
return '{}{}'.format(settings.BASE_URL, Model.resource_id(self))
return Model.absolute_url(self)
def absolute_url(cls, instance_or_model):
if isinstance(instance_or_model, ModelBase) or instance_or_model.urlid is None or instance_or_model.urlid == '':
return '{}{}'.format(settings.BASE_URL, Model.resource(instance_or_model))
return self.urlid
return instance_or_model.urlid
def get_container_id(self):
return Model.container_id(self)
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