Skip to content

Commit

Permalink
Merge pull request #269 from barseghyanartur/feature/147-implement-cl…
Browse files Browse the repository at this point in the history
…ass-based-views

Feature/147 implement class based views
  • Loading branch information
barseghyanartur committed Jul 11, 2022
2 parents 1ef8617 + 8b11548 commit ff44238
Show file tree
Hide file tree
Showing 24 changed files with 3,325 additions and 209 deletions.
55 changes: 55 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,61 @@ are used for versioning (schema follows below):
0.3.4 to 0.4).
- All backwards incompatible changes are mentioned in this document.

0.19
----
2022-07-11

- Introduce class based views. Function based views are still supported
and will be supported until at least 0.23.

Migration to class based views is simple. Only your project's ``urls.py``
would change:

.. code-block:: python
urlpatterns = [
# ...
url(r'^fobi/', include('fobi.urls.class_based.view')),
url(r'^fobi/', include('fobi.urls.class_based.edit')),
# ...
]
To use function based views, simply replace the previous line with:

.. code-block:: python
urlpatterns = [
# ...
url(r'^fobi/', include('fobi.urls.view')),
url(r'^fobi/', include('fobi.urls.edit')),
# ...
]
- Class-based permissions (work only in combination with class-based views).

Example:

.. code-block:: python
from fobi.permissions.definitions import edit_form_entry_permissions
from fobi.permissions.generic import BasePermission
from fobi.permissions.helpers import (
any_permission_required_func, login_required,
)
class EditFormEntryPermission(BasePermission):
"""Permission to edit form entries."""
def has_permission(self, request, view) -> bool:
return login_required(request) and any_permission_required_func(
edit_form_entry_permissions
)(request.user)
def has_object_permission(self, request, view, obj) -> bool:
return login_required(request) and any_permission_required_func(
edit_form_entry_permissions
)(request.user) and obj.user == request.user
0.18
----
2022-06-23
Expand Down
107 changes: 107 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
.PHONY: help clean

define BROWSER_PYSCRIPT
import os, webbrowser, sys

from urllib.request import pathname2url

webbrowser.open("file://" + pathname2url(os.path.abspath(sys.argv[1])))
endef
export BROWSER_PYSCRIPT

BROWSER := python -c "$$BROWSER_PYSCRIPT"

help:
@echo "clean | Remove all build, test, coverage and Python artifacts"
@echo "clean-build | Remove build artifacts"
@echo "clean-pyc | Remove Python file artifacts"
@echo "clean-test | Remove test and coverage artifacts"
@echo "run | Run the project in Docker"

clean: clean-build clean-pyc clean-test

clean-build:
rm -rf build/
rm -rf dist/
rm -rf **/*.egg-info
rm -rf static/CACHE

clean-pyc:
find . -name '*.pyc' -exec rm -f {} +
find . -name '*.pyo' -exec rm -f {} +
find . -name '*~' -exec rm -f {} +
find . -name '__pycache__' -exec rm -fr {} +

clean-test:
rm -rf .pytest_cache; \
rm -rf .ipython/profile_default; \
rm -rf htmlcov; \
rm -rf build; \
rm -f .coverage; \
rm -f coverage.xml; \
rm -f junit.xml; \
rm -rf .hypothesis; \
find . -name '*.py,cover' -exec rm -f {} +

fix-file-permissions:
sudo chown $$USER:$$USER src/fobi/migrations/ -R || true
sudo chown $$USER:$$USER src/fobi/contrib/apps/djangocms_integration/migrations/ -R || true
sudo chown $$USER:$$USER src/fobi/contrib/apps/wagtail_integration/migrations/ -R || true
sudo chown $$USER:$$USER src/fobi/contrib/form_handlers/db_store/migrations/ -R || true
sudo chown $$USER:$$USER examples/simple/page/migrations/ -R || true
sudo chown $$USER:$$USER tmp/ -R || true

run: prepare-required-files
docker-compose -f docker-compose.yml up --remove-orphans;

build: prepare-required-files
docker-compose -f docker-compose.yml build;

build-%: prepare-required-files
docker-compose -f docker-compose.yml build $*;

stop:
docker-compose -f docker-compose.yml stop;

make-migrations:
docker-compose -f docker-compose.yml exec backend ./manage.py makemigrations $(APP);

migrate:
docker-compose -f docker-compose.yml exec backend ./manage.py migrate $(APP);

test:
docker-compose -f docker-compose.yml exec backend pytest /backend/src/$(TEST_PATH);

show-migrations:
docker-compose -f docker-compose.yml exec backend ./manage.py showmigrations

show-urls:
docker-compose -f docker-compose.yml exec backend ./manage.py show_urls

shell:
docker-compose -f docker-compose.yml exec backend python examples/simple/manage.py shell

create-superuser:
docker-compose -f docker-compose.yml exec backend python examples/simple/manage.py createsuperuser

fobi-sync-plugins:
docker-compose -f docker-compose.yml exec backend ./manage.py fobi_sync_plugins

pip-install:
docker-compose -f docker-compose.yml exec backend pip install -r requirements/local.txt

pip-list:
docker-compose -f docker-compose.yml exec backend pip list

black:
docker-compose -f docker-compose.yml exec backend black .

isort:
docker-compose -f docker-compose.yml exec backend isort . --overwrite-in-place

bash:
docker-compose -f docker-compose.yml run backend /bin/bash

prepare-required-files:
mkdir -p examples/logs examples/db examples/media examples/media/static examples/media/fobi_plugins/content_image
mkdir -p examples/media/fobi_plugins/file
59 changes: 58 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ Main features and highlights
- Developer-friendly API, which allows to edit existing or build new form
fields and handlers without touching the core.
- Support for custom user model.
- Class based views (and class-based permissions). Forms have an
owner (``auth.User``). Default permissions are set for the owner, but
class-based views provide a lot of freedom and can be easily customized.
- `Theming`_. There are 4 ready to use `Bundled themes`_: "Bootstrap 3",
"Foundation 5", "Simple" (with editing interface in style of Django admin)
and "DjangoCMS admin style" theme (which is another simple theme with editing
Expand Down Expand Up @@ -127,7 +130,6 @@ Roadmap
Some of the upcoming/in-development features/improvements are:

- Implement disabling forms based on dates.
- Class based views.
- Cloning of forms.
- JSON schema support.
- Webpack integration.
Expand Down Expand Up @@ -1179,6 +1181,61 @@ Add the callback module to ``INSTALLED_APPS``.
# ...
)
Class-based views
=================
Views
-----
Migration to class based views is simple. Only your project's ``urls.py``
would change:

.. code-block:: python
urlpatterns = [
# ...
url(r'^fobi/', include('fobi.urls.class_based.view')),
url(r'^fobi/', include('fobi.urls.class_based.edit')),
# ...
]
To use function based views, simply replace the previous line with:

.. code-block:: python
urlpatterns = [
# ...
url(r'^fobi/', include('fobi.urls.view')),
url(r'^fobi/', include('fobi.urls.edit')),
# ...
]
Permissions
-----------

Class-based permissions work only in combination with class-based views.

Example:

.. code-block:: python
from fobi.permissions.definitions import edit_form_entry_permissions
from fobi.permissions.generic import BasePermission
from fobi.permissions.helpers import (
any_permission_required_func, login_required,
)
class EditFormEntryPermission(BasePermission):
"""Permission to edit form entries."""
def has_permission(self, request, view) -> bool:
return login_required(request) and any_permission_required_func(
edit_form_entry_permissions
)(request.user)
def has_object_permission(self, request, view, obj) -> bool:
return login_required(request) and any_permission_required_func(
edit_form_entry_permissions
)(request.user) and obj.user == request.user
Suggestions
===========
Custom action for the form
Expand Down
65 changes: 65 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,71 @@ are used for versioning (schema follows below):
0.3.4 to 0.4).
- All backwards incompatible changes are mentioned in this document.

0.19
----
2022-07-11

- Introduce class based views. Function based views are still supported
and will be supported until at least 0.23.

Migration to class based views is simple. Only your project's ``urls.py``
would change:

.. code-block:: python
urlpatterns = [
# ...
url(r'^fobi/', include('fobi.urls.class_based.view')),
url(r'^fobi/', include('fobi.urls.class_based.edit')),
# ...
]
To use function based views, simply replace the previous line with:

.. code-block:: python
urlpatterns = [
# ...
url(r'^fobi/', include('fobi.urls.view')),
url(r'^fobi/', include('fobi.urls.edit')),
# ...
]
- Class-based permissions (work only in combination with class-based views).

Example:

.. code-block:: python
from fobi.permissions.definitions import edit_form_entry_permissions
from fobi.permissions.generic import BasePermission
from fobi.permissions.helpers import (
any_permission_required_func, login_required,
)
class EditFormEntryPermission(BasePermission):
"""Permission to edit form entries."""
def has_permission(self, request, view) -> bool:
return login_required(request) and any_permission_required_func(
edit_form_entry_permissions
)(request.user)
def has_object_permission(self, request, view, obj) -> bool:
return login_required(request) and any_permission_required_func(
edit_form_entry_permissions
)(request.user) and obj.user == request.user
0.18
----
2022-06-23

- Tested against Python 3.9, Django 3.2 and 4.0.

.. note::

Release dedicated to my dear son, Tigran, who turned 10 recently.

0.17.1
------
2021-01-25
Expand Down
Loading

0 comments on commit ff44238

Please sign in to comment.