Commit 399b5d70 authored by Jean-Baptiste's avatar Jean-Baptiste

update: auto create local source if no source exists for this type

parent 3df810c3
Pipeline #5512 passed with stage
in 1 minute and 25 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`
```python
INSTALLED_APPS = [
'djangoldp.apps.DjangoldpConfig',
]
```
## 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',
'owner_perms')
......@@ -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))
......@@ -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)
......
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