Skip to content

Commit

Permalink
First draft of some documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
kennknowles committed Oct 30, 2013
1 parent 05adeeb commit 78950ac
Show file tree
Hide file tree
Showing 14 changed files with 958 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,5 @@ nosetests.xml
# Excel
~*.xlsx

/docs/_build
/django-prbac.db
6 changes: 4 additions & 2 deletions django_prbac/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ def is_string_list(self, value):

def to_python(self, value):
"""
Handles exactly two cases:
Handles exactly two cases.
1. The value is already a (unicode, not bytes) string list.
- then it is returned as-is
2. The value is a string (not super sure how Django deals w/ database fields w.r.t. unicode)
Expand Down Expand Up @@ -73,7 +74,8 @@ def is_string_set(self, value):

def to_python(self, value):
"""
Handles exactly two cases:
Handles exactly two cases.
1. The value is already a (unicode, not bytes) string set.
- then it is returned as-is
2. The value is a string (not super sure how Django deals w/ database fields w.r.t. unicode)
Expand Down
19 changes: 18 additions & 1 deletion django_prbac/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ def instantiate(self, assignment):
filtered_assignment = dict([(key, assignment[key]) for key in self.parameters & set(assignment.keys())])
return RoleInstance(self, filtered_assignment)


def has_privilege(self, privilege):
"""
Shortcut for checking privileges easily for roles with no params (aka probably users)
"""

return self.instantiate({}).has_privilege(privilege)

@property
def assignment(self):
"""
A Role stored in the database always has an empty assignment.
"""

return {}

def __repr__(self):
return 'Role(%r, parameters=%r)' % (self.name, self.parameters)

Expand Down Expand Up @@ -157,7 +173,8 @@ def has_privilege(self, privilege):


def __eq__(self, other):
return self.role.name == other.role.name and self.assignment == other.assignment
return self.name == other.name and self.assignment == other.assignment


def __repr__(self):
return 'RoleInstance(%r, parameters=%r, assignment=%r)' % (self.name, self.parameters, self.assignment)
25 changes: 21 additions & 4 deletions django_prbac/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,38 @@ def test_has_permission_immediate_no_params(self):
subrole = arbitrary.role()
superrole1 = arbitrary.role()
superrole2 = arbitrary.role()
grant = arbitrary.grant(to_role=superrole1, from_role=subrole)
arbitrary.grant(to_role=superrole1, from_role=subrole)

# A few ways of saying the same thing
self.assertTrue(subrole.instantiate({}).has_privilege(superrole1.instantiate({})))
self.assertTrue(subrole.has_privilege(superrole1.instantiate({})))
self.assertTrue(subrole.instantiate({}).has_privilege(superrole1))
self.assertTrue(subrole.has_privilege(superrole1))

self.assertFalse(subrole.instantiate({}).has_privilege(superrole2.instantiate({})))
self.assertFalse(subrole.has_privilege(superrole2.instantiate({})))
self.assertFalse(subrole.instantiate({}).has_privilege(superrole2))
self.assertFalse(subrole.has_privilege(superrole2))


def test_has_permission_transitive_no_params(self):
subrole = arbitrary.role()
midrole = arbitrary.role()
superrole1 = arbitrary.role()
superrole2 = arbitrary.role()
grant = arbitrary.grant(to_role=midrole, from_role=subrole)
grant = arbitrary.grant(to_role=superrole1, from_role=midrole)
arbitrary.grant(to_role=midrole, from_role=subrole)
arbitrary.grant(to_role=superrole1, from_role=midrole)

# A few ways of saying the same thing
self.assertTrue(subrole.instantiate({}).has_privilege(superrole1.instantiate({})))
self.assertTrue(subrole.has_privilege(superrole1.instantiate({})))
self.assertTrue(subrole.instantiate({}).has_privilege(superrole1))
self.assertTrue(subrole.has_privilege(superrole1))

self.assertFalse(subrole.instantiate({}).has_privilege(superrole2.instantiate({})))
self.assertFalse(subrole.has_privilege(superrole2.instantiate({})))
self.assertFalse(subrole.instantiate({}).has_privilege(superrole2))
self.assertFalse(subrole.has_privilege(superrole2))


def test_has_permission_far_transitive_no_params(self):
Expand All @@ -58,13 +74,14 @@ def test_has_permission_far_transitive_no_params(self):
def test_has_permission_immediate_params(self):
subrole = arbitrary.role()
superrole1 = arbitrary.role(parameters=set(['one']))
grant = arbitrary.grant(to_role=superrole1, from_role=subrole, assignment=dict(one='foo'))
arbitrary.grant(to_role=superrole1, from_role=subrole, assignment=dict(one='foo'))

print(subrole.memberships_granted.all())

self.assertTrue(subrole.instantiate({}).has_privilege(superrole1.instantiate(dict(one='foo'))))
self.assertFalse(subrole.instantiate({}).has_privilege(superrole1.instantiate(dict(one='baz'))))


class TestGrant(TestCase):

def test_instantiated_to_role_smoke_test(self):
Expand Down
153 changes: 153 additions & 0 deletions docs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
# Makefile for Sphinx documentation
#

# You can set these variables from the command line.
SPHINXOPTS =
SPHINXBUILD = sphinx-build
PAPER =
BUILDDIR = _build

# Internal variables.
PAPEROPT_a4 = -D latex_paper_size=a4
PAPEROPT_letter = -D latex_paper_size=letter
ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) .
# the i18n builder cannot share the environment and doctrees with the others
I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) .

.PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest gettext

help:
@echo "Please use \`make <target>' where <target> is one of"
@echo " html to make standalone HTML files"
@echo " dirhtml to make HTML files named index.html in directories"
@echo " singlehtml to make a single large HTML file"
@echo " pickle to make pickle files"
@echo " json to make JSON files"
@echo " htmlhelp to make HTML files and a HTML help project"
@echo " qthelp to make HTML files and a qthelp project"
@echo " devhelp to make HTML files and a Devhelp project"
@echo " epub to make an epub"
@echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter"
@echo " latexpdf to make LaTeX files and run them through pdflatex"
@echo " text to make text files"
@echo " man to make manual pages"
@echo " texinfo to make Texinfo files"
@echo " info to make Texinfo files and run them through makeinfo"
@echo " gettext to make PO message catalogs"
@echo " changes to make an overview of all changed/added/deprecated items"
@echo " linkcheck to check all external links for integrity"
@echo " doctest to run all doctests embedded in the documentation (if enabled)"

clean:
-rm -rf $(BUILDDIR)/*

html:
$(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html
@echo
@echo "Build finished. The HTML pages are in $(BUILDDIR)/html."

dirhtml:
$(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml
@echo
@echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml."

singlehtml:
$(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml
@echo
@echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml."

pickle:
$(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle
@echo
@echo "Build finished; now you can process the pickle files."

json:
$(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json
@echo
@echo "Build finished; now you can process the JSON files."

htmlhelp:
$(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp
@echo
@echo "Build finished; now you can run HTML Help Workshop with the" \
".hhp project file in $(BUILDDIR)/htmlhelp."

qthelp:
$(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp
@echo
@echo "Build finished; now you can run "qcollectiongenerator" with the" \
".qhcp project file in $(BUILDDIR)/qthelp, like this:"
@echo "# qcollectiongenerator $(BUILDDIR)/qthelp/django-prbac.qhcp"
@echo "To view the help file:"
@echo "# assistant -collectionFile $(BUILDDIR)/qthelp/django-prbac.qhc"

devhelp:
$(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp
@echo
@echo "Build finished."
@echo "To view the help file:"
@echo "# mkdir -p $$HOME/.local/share/devhelp/django-prbac"
@echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/django-prbac"
@echo "# devhelp"

epub:
$(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub
@echo
@echo "Build finished. The epub file is in $(BUILDDIR)/epub."

latex:
$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
@echo
@echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex."
@echo "Run \`make' in that directory to run these through (pdf)latex" \
"(use \`make latexpdf' here to do that automatically)."

latexpdf:
$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
@echo "Running LaTeX files through pdflatex..."
$(MAKE) -C $(BUILDDIR)/latex all-pdf
@echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex."

text:
$(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text
@echo
@echo "Build finished. The text files are in $(BUILDDIR)/text."

man:
$(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man
@echo
@echo "Build finished. The manual pages are in $(BUILDDIR)/man."

texinfo:
$(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo
@echo
@echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo."
@echo "Run \`make' in that directory to run these through makeinfo" \
"(use \`make info' here to do that automatically)."

info:
$(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo
@echo "Running Texinfo files through makeinfo..."
make -C $(BUILDDIR)/texinfo info
@echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo."

gettext:
$(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale
@echo
@echo "Build finished. The message catalogs are in $(BUILDDIR)/locale."

changes:
$(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes
@echo
@echo "The overview file is in $(BUILDDIR)/changes."

linkcheck:
$(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck
@echo
@echo "Link check complete; look for any errors in the above output " \
"or in $(BUILDDIR)/linkcheck/output.txt."

doctest:
$(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest
@echo "Testing of doctests in the sources finished, look at the " \
"results in $(BUILDDIR)/doctest/output.txt."
11 changes: 11 additions & 0 deletions docs/apidoc/django_prbac.migrations.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
migrations Package
==================

:mod:`0001_initial` Module
--------------------------

.. automodule:: django_prbac.migrations.0001_initial
:members:
:undoc-members:
:show-inheritance:

83 changes: 83 additions & 0 deletions docs/apidoc/django_prbac.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
django_prbac Package
====================

:mod:`django_prbac` Package
---------------------------

.. automodule:: django_prbac.__init__
:members:
:undoc-members:
:show-inheritance:

:mod:`admin` Module
-------------------

.. automodule:: django_prbac.admin
:members:
:undoc-members:
:show-inheritance:

:mod:`arbitrary` Module
-----------------------

.. automodule:: django_prbac.arbitrary
:members:
:undoc-members:
:show-inheritance:

:mod:`decorators` Module
------------------------

.. automodule:: django_prbac.decorators
:members:
:undoc-members:
:show-inheritance:

:mod:`exceptions` Module
------------------------

.. automodule:: django_prbac.exceptions
:members:
:undoc-members:
:show-inheritance:

:mod:`fields` Module
--------------------

.. automodule:: django_prbac.fields
:members:
:undoc-members:
:show-inheritance:

:mod:`mock_settings` Module
---------------------------

.. automodule:: django_prbac.mock_settings
:members:
:undoc-members:
:show-inheritance:

:mod:`models` Module
--------------------

.. automodule:: django_prbac.models
:members:
:undoc-members:
:show-inheritance:

:mod:`urls` Module
------------------

.. automodule:: django_prbac.urls
:members:
:undoc-members:
:show-inheritance:

Subpackages
-----------

.. toctree::

django_prbac.migrations
django_prbac.tests

Loading

0 comments on commit 78950ac

Please sign in to comment.