[Python-modules-commits] [drf-fsm-transitions] 01/02: Imported Upstream version 0.2.0

Michael Fladischer fladi at moszumanska.debian.org
Thu Sep 3 15:23:30 UTC 2015


This is an automated email from the git hooks/post-receive script.

fladi pushed a commit to branch master
in repository drf-fsm-transitions.

commit 5ad9e63352150f8e09d68e17c319878a9e4312a6
Author: Michael Fladischer <FladischerMichael at fladi.at>
Date:   Wed Jul 1 10:26:56 2015 +0200

    Imported Upstream version 0.2.0
---
 .gitignore                            | 54 +++++++++++++++++++++++++++++++++++
 LICENSE                               | 22 ++++++++++++++
 README.md                             | 47 ++++++++++++++++++++++++++++++
 drf_fsm_transitions/__init__.py       |  0
 drf_fsm_transitions/viewset_mixins.py | 45 +++++++++++++++++++++++++++++
 setup.py                              | 14 +++++++++
 6 files changed, 182 insertions(+)

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..db4561e
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,54 @@
+# Byte-compiled / optimized / DLL files
+__pycache__/
+*.py[cod]
+
+# C extensions
+*.so
+
+# Distribution / packaging
+.Python
+env/
+build/
+develop-eggs/
+dist/
+downloads/
+eggs/
+lib/
+lib64/
+parts/
+sdist/
+var/
+*.egg-info/
+.installed.cfg
+*.egg
+
+# PyInstaller
+#  Usually these files are written by a python script from a template
+#  before PyInstaller builds the exe, so as to inject date/other infos into it.
+*.manifest
+*.spec
+
+# Installer logs
+pip-log.txt
+pip-delete-this-directory.txt
+
+# Unit test / coverage reports
+htmlcov/
+.tox/
+.coverage
+.cache
+nosetests.xml
+coverage.xml
+
+# Translations
+*.mo
+*.pot
+
+# Django stuff:
+*.log
+
+# Sphinx documentation
+docs/_build/
+
+# PyBuilder
+target/
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..7e0ebca
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,22 @@
+The MIT License (MIT)
+
+Copyright (c) 2014 Jacob Haslehurst
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..3d99ada
--- /dev/null
+++ b/README.md
@@ -0,0 +1,47 @@
+drf-fsm-transitions
+===================
+
+Automatically hook your Django-FSM transitions up to Django REST Framework
+
+## Installation
+
+```bash
+pip install drf-fsm-transitions
+```
+
+
+## Usage
+
+When declaring your viewset, simply mix in the result of `get_viewset_transition_action_mixin`
+
+```python
+from rest_framework import viewsets
+from drf_transition_methods.viewset_mixins import get_viewset_transition_action_mixin
+
+from .models import Article
+
+
+class ArticleViewSet(
+    get_viewset_transition_action_mixin(Article),
+    viewsets.ModelViewSet
+):
+    queryset = Article.objects.all()
+```
+
+if `Article` had 2 transitions, `delete` and `publish`, the following API calls would be set up
+
+- `POST /api/article/1234/delete/`
+- `POST /api/article/1234/publish/`
+
+### Saving
+
+By default, the model instance will be saved after the transition has been successfully called. This can be disabled with the `save_after_transition` attribute
+
+```python
+class ArticleViewSet(
+    get_viewset_transition_action_mixin(Article),
+    viewsets.ModelViewSet
+):
+    queryset = Article.objects.all()
+    save_after_transition = False
+```
diff --git a/drf_fsm_transitions/__init__.py b/drf_fsm_transitions/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/drf_fsm_transitions/viewset_mixins.py b/drf_fsm_transitions/viewset_mixins.py
new file mode 100644
index 0000000..7329d03
--- /dev/null
+++ b/drf_fsm_transitions/viewset_mixins.py
@@ -0,0 +1,45 @@
+from rest_framework.decorators import detail_route
+from rest_framework.response import Response
+
+
+def get_transition_viewset_method(transition_name):
+    '''
+    Create a viewset method for the provided `transition_name`
+    '''
+    @detail_route(methods=['post'])
+    def inner_func(self, request, pk=None):
+        object = self.get_object()
+        transition_method = getattr(object, transition_name)
+
+        transition_method(by=self.request.user)
+
+        if self.save_after_transition:
+            object.save()
+
+        serializer = self.get_serializer(object)
+        return Response(serializer.data)
+
+    return inner_func
+
+
+def get_viewset_transition_action_mixin(model):
+    '''
+    Find all transitions defined on `model`, then create a corresponding
+    viewset action method for each and apply it to `Mixin`. Finally, return
+    `Mixin`
+    '''
+    instance = model()
+
+    class Mixin(object):
+        save_after_transition = True
+
+    transitions = instance.get_all_status_transitions()
+    transition_names = set(x.name for x in transitions)
+    for transition_name in transition_names:
+        setattr(
+            Mixin,
+            transition_name,
+            get_transition_viewset_method(transition_name)
+        )
+
+    return Mixin
diff --git a/setup.py b/setup.py
new file mode 100644
index 0000000..a13f554
--- /dev/null
+++ b/setup.py
@@ -0,0 +1,14 @@
+#!/usr/bin/env python
+
+from setuptools import setup, find_packages
+
+setup(
+    name='drf-fsm-transitions',
+    version='0.2.0',
+    description='Automatically hook your Django-FSM transitions up to Django REST Framework',
+    author='Jacob Haslehurst',
+    author_email='jacob at haslehurst.net',
+    url='https://github.com/hzy/drf-fsm-transitions',
+    packages=find_packages(),
+    install_requires=['django', 'django_fsm', 'djangorestframework']
+)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/drf-fsm-transitions.git



More information about the Python-modules-commits mailing list