From 399b5d70c5790a7b910e6401348580de3f61155c Mon Sep 17 00:00:00 2001 From: Jean-Baptiste <bleme@pm.me> Date: Mon, 23 Sep 2019 11:56:05 +0200 Subject: [PATCH] update: auto create local source if no source exists for this type --- README.md | 10 ++++++++++ djangoldp/__init__.py | 5 ++++- djangoldp/apps.py | 18 ++++++++++++++++++ djangoldp/models.py | 10 +++++++--- 4 files changed, 39 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6dc314ee..59e28d42 100644 --- a/README.md +++ b/README.md @@ -242,6 +242,16 @@ REST_FRAMEWORK = { } ``` +## Sources +To enable sources auto creation for all models, change `djangoldp` by `djangoldp.apps.DjangoldpConfig`, on `INSTALLED_APPS` + +```python +INSTALLED_APPS = [ + 'djangoldp.apps.DjangoldpConfig', +] +``` + + ## License Licence MIT diff --git a/djangoldp/__init__.py b/djangoldp/__init__.py index 8169cccc..cfe28de3 100644 --- a/djangoldp/__init__.py +++ b/djangoldp/__init__.py @@ -1,4 +1,7 @@ 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', + 'owner_perms') diff --git a/djangoldp/apps.py b/djangoldp/apps.py index e6a226fc..aac1db2d 100644 --- a/djangoldp/apps.py +++ b/djangoldp/apps.py @@ -3,3 +3,21 @@ from django.apps import AppConfig class DjangoldpConfig(AppConfig): name = 'djangoldp' + + def ready(self): + self.create_local_source() + + 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: + continue + path = model_class.get_container_path().strip("/") + try: + existing_source = LDPSource.objects.get(federation=path) + except LDPSource.DoesNotExist: + LDPSource.objects.create(federation=path, urlid=Model.absolute_url(model_class)) diff --git a/djangoldp/models.py b/djangoldp/models.py index 3340dc4f..76662dc2 100644 --- a/djangoldp/models.py +++ b/djangoldp/models.py @@ -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) + + @classonlymethod + 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)) else: - return self.urlid + return instance_or_model.urlid def get_container_id(self): return Model.container_id(self) -- GitLab