Skip to content
Snippets Groups Projects
Commit eb105d98 authored by Calum Mackervoy's avatar Calum Mackervoy Committed by Jean-Baptiste Pasquier
Browse files

feature: DjangoLDPAdmin and DjangoLDPUserAdmin

parent 96c7549a
No related branches found
No related tags found
No related merge requests found
......@@ -138,9 +138,10 @@ BASE_URL = SITE_URL
```python
from django.contrib import admin
from djangoldp.admin import DjangoLDPAdmin
from .models import Todo
admin.site.register(Todo)
admin.site.register(Todo, DjangoLDPAdmin)
```
5. You then need to have your WSGI server pointing on myldpserver/myldpserver/wsgi.py
......@@ -186,7 +187,7 @@ class Todo(Model):
See "Custom Meta options" below to see some helpful ways you can tweak the behaviour of DjangoLDP
Your model will be automatically detected and registered with an LDPViewSet and corresponding URLs, as well as being registered with the Django admin panel. If you register your model with the admin panel manually, make sure to extend the GuardedModelAdmin so that the model is registered with [Django-Guardian object permissions](https://django-guardian.readthedocs.io/en/stable/userguide/admin-integration.html)
Your model will be automatically detected and registered with an LDPViewSet and corresponding URLs, as well as being registered with the Django admin panel. If you register your model with the admin panel manually, make sure to extend djangoldp.DjangoLDPAdmin so that the model is registered with [Django-Guardian object permissions](https://django-guardian.readthedocs.io/en/stable/userguide/admin-integration.html). An alternative version which extends Django's `UserAdmin` is available as djangoldp.DjangoLDPUserAdmin
### Model Federation
......
......@@ -5,3 +5,4 @@ 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')
default_app_config = 'djangoldp.apps.DjangoldpConfig'
from importlib import import_module
from django.conf import settings
from django.contrib import admin
from guardian.admin import GuardedModelAdmin
from .models import LDPSource, Model
# automatically import selected DjangoLDP packages from settings
for package in settings.DJANGOLDP_PACKAGES:
try:
import_module('{}.admin'.format(package))
except ModuleNotFoundError:
pass
for package in settings.DJANGOLDP_PACKAGES:
try:
import_module('{}.models'.format(package))
except ModuleNotFoundError:
pass
model_classes = {cls.__name__: cls for cls in Model.__subclasses__()}
# automatically register models with the admin panel (which have not been added manually)
# NOTE: by default the models are registered with Django Guardian activated
for class_name in model_classes:
model_class = model_classes[class_name]
if not admin.site.is_registered(model_class):
admin.site.register(model_class, GuardedModelAdmin)
# admin.site.register(LDPSource)
from django.contrib.auth.admin import UserAdmin
class DjangoLDPAdmin(GuardedModelAdmin):
'''
An admin model representing a federated object. Inherits from GuardedModelAdmin to provide Django-Guardian
object-level permissions
'''
pass
class DjangoLDPUserAdmin(UserAdmin, GuardedModelAdmin):
'''An extension of UserAdmin providing the functionality of DjangoLDPAdmin'''
def get_fieldsets(self, request, obj=None):
fieldsets = super().get_fieldsets(request, obj)
federated_fields = ['urlid', 'allow_create_backlink']
if self.exclude is not None:
federated_fields = list(set(federated_fields) - set(self.exclude))
for fieldset in fieldsets:
federated_fields = list(set(federated_fields) - set(fieldset[1]['fields']))
if len(federated_fields) == 0:
return fieldsets
fieldsets = [('Federation', {'fields': federated_fields})] + list(fieldsets)
return fieldsets
from django.apps import AppConfig
from django.db import OperationalError, connection
class DjangoldpConfig(AppConfig):
name = 'djangoldp'
def ready(self):
self.create_local_source()
self.auto_register_model_admin()
def create_local_source(self):
from djangoldp.models import LDPSource, Model
def auto_register_model_admin(self):
'''
Automatically registers Model subclasses in the admin panel (which have not already been added manually)
'''
from importlib import import_module
model_classes = {}
db_tables = []
from django.conf import settings
from django.contrib import admin
from djangoldp.admin import DjangoLDPAdmin
from djangoldp.models import Model
for cls in Model.__subclasses__():
model_classes[cls.__name__] = cls
db_tables.append(LDPSource.get_meta(cls, "db_table"))
# Check that all model's table already exists
existing_tables = connection.introspection.table_names()
if not all(db_table in existing_tables for db_table in db_tables):
return
for package in settings.DJANGOLDP_PACKAGES:
try:
import_module('{}.admin'.format(package))
except ModuleNotFoundError:
pass
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("/")
for package in settings.DJANGOLDP_PACKAGES:
try:
LDPSource.objects.get(federation=path)
except LDPSource.DoesNotExist:
LDPSource.objects.create(federation=path, urlid=Model.absolute_url(model_class))
except OperationalError:
import_module('{}.models'.format(package))
except ModuleNotFoundError:
pass
model_classes = {cls.__name__: cls for cls in Model.__subclasses__()}
for class_name in model_classes:
model_class = model_classes[class_name]
if not admin.site.is_registered(model_class):
admin.site.register(model_class, DjangoLDPAdmin)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment