Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add better docs for abstract models #220

Merged
merged 3 commits into from
May 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 41 additions & 18 deletions docs/cookbook.rst
Original file line number Diff line number Diff line change
Expand Up @@ -259,24 +259,36 @@ provided in ``organizations.abstract``. Here's an example from an `accounts` app
.. code-block:: python

from django.db import models
from organizations.abstract import (AbstractOrganization,
AbstractOrganizationUser,
AbstractOrganizationOwner)
from organizations.abstract import (
AbstractOrganization,
AbstractOrganizationUser,
AbstractOrganizationOwner,
AbstractOrganizationInvitation,
)

class Account(AbstractOrganization):
"""Core organization model"""
monthly_subscription = models.IntegerField(default=1000)

class AccountUser(AbstractOrganizationUser):
"""Links a user to the organization"""
user_type = models.CharField(max_length=1, default='')

class AccountOwner(AbstractOrganizationOwner):
"""Identifies ONE user, by AccountUser, to be the owner"""
pass

class AccountInvitation(AbstractOrganizationInvitation):
"""Stores invitations for adding users to organizations"""
pass


This will create the following tables:

* `accounts_account`
* `accounts_accountuser`
* `accounts_accountowner`
* `accounts_accountinvitation`

The `accounts_account` table will include all of the necessary fields for this
and only this organization model.
Expand All @@ -300,9 +312,12 @@ Here's an example of a custom `accounts` inheriting the minimal `Base` models:
.. code-block:: python

from django.db import models
from organizations.base import (OrganizationBase,
OrganizationUserBase,
OrganizationOwnerBase)
from organizations.base import (
OrganizationBase,
OrganizationUserBase,
OrganizationOwnerBase,
OrganizationInvitationBase,
)

class Account(OrganizationBase):
monthly_subscription = models.IntegerField(default=1000)
Expand All @@ -313,6 +328,9 @@ Here's an example of a custom `accounts` inheriting the minimal `Base` models:
class AccountOwner(OrganizationOwnerBase):
pass

class AccountInvitation(OrganizationInvitationBase):
pass

Difference between abstract and base models
-------------------------------------------

Expand All @@ -338,32 +356,37 @@ code in your application, eg:

from django.contrib import admin

from .base_admin import (BaseOwnerInline,
BaseOrganizationAdmin,
BaseOrganizationUserAdmin,
BaseOrganizationOwnerAdmin)
from .models import Organization, OrganizationUser, OrganizationOwner
from organizations.base_admin import (
BaseOwnerInline,
BaseOrganizationAdmin,
BaseOrganizationUserAdmin,
BaseOrganizationOwnerAdmin,
)
from myapp.models import MyOrg, MyOrgUser, MyOrgOwner, MyOrgInvitation


class OwnerInline(BaseOwnerInline):
model = OrganizationOwner
model = MyOrgOwner


class OrganizationAdmin(BaseOrganizationAdmin):
@admin.register(MyOrg)
class OrgAdmin(BaseOrganizationAdmin):
inlines = [OwnerInline]


class OrganizationUserAdmin(BaseOrganizationUserAdmin):
@admin.register(MyOrgUser)
class OrgUserAdmin(BaseOrganizationUserAdmin):
pass


class OrganizationOwnerAdmin(BaseOrganizationOwnerAdmin):
@admin.register(MyOrgOwner)
class OrgOwnerAdmin(BaseOrganizationOwnerAdmin):
pass


admin.site.register(Organization, OrganizationAdmin)
admin.site.register(OrganizationUser, OrganizationUserAdmin)
admin.site.register(OrganizationOwner, OrganizationOwnerAdmin)
@admin.register(MyOrgInvitation)
class OrgInvitationAdmin(admin.ModelAdmin):
pass

Restricting and isolating resources
===================================
Expand Down
15 changes: 8 additions & 7 deletions src/organizations/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,32 @@

from django.contrib import admin

from organizations import models
from organizations.base_admin import BaseOrganizationAdmin
from organizations.base_admin import BaseOrganizationOwnerAdmin
from organizations.base_admin import BaseOrganizationUserAdmin
from organizations.base_admin import BaseOwnerInline
from organizations.models import Organization
from organizations.models import OrganizationOwner
from organizations.models import OrganizationUser


class OwnerInline(BaseOwnerInline):
model = OrganizationOwner
model = models.OrganizationOwner


@admin.register(models.Organization)
class OrganizationAdmin(BaseOrganizationAdmin):
inlines = [OwnerInline]


@admin.register(models.OrganizationUser)
class OrganizationUserAdmin(BaseOrganizationUserAdmin):
pass


@admin.register(models.OrganizationOwner)
class OrganizationOwnerAdmin(BaseOrganizationOwnerAdmin):
pass


admin.site.register(Organization, OrganizationAdmin)
admin.site.register(OrganizationUser, OrganizationUserAdmin)
admin.site.register(OrganizationOwner, OrganizationOwnerAdmin)
@admin.register(models.OrganizationInvitation)
class OrganizationInvitationAdmin(admin.ModelAdmin):
pass
4 changes: 1 addition & 3 deletions src/organizations/backends/modeled.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@


class ModelInvitation(InvitationBackend):
"""

"""
"""Invitation backend for model-tracked invitations"""

notification_subject = "organizations/email/notification_subject.txt"
notification_body = "organizations/email/notification_body.html"
Expand Down