gadventures / django-fsm-admin Goto Github PK
View Code? Open in Web Editor NEWMixin and template tags to integrate django-fsm transitions into the django admin.
License: Other
Mixin and template tags to integrate django-fsm transitions into the django admin.
License: Other
It would be great if there was a mixin to allow for transitions to multiple objects. I wrote up simple extension to the mixin that allows for this, but it's not very generic for generation of buttons. This is useful for creating ListViews where a user can transition multiple objects.
Is there a possibility to decorate a method as transition, which when a state transition is allowed, does not show up in the admin page?
I'm asking this, because some of my potential transitions shall only be triggered by external events (through the API), and it thus shall not be possible to trigger them using the buttons added automatically by django-fsm-admin.
I am getting this error as soon as I try to use FSMTransitionMixin
.
ImportError: cannot import name 'ugettext' from 'django.utils.translation'
The Model._deferred attribute is removed as dynamic model classes when using QuerySet.defer() and only() is removed.
https://docs.djangoproject.com/en/1.10/releases/1.10/
The fix is:
diff --git a/fsm_admin/templatetags/fsm_admin.py b/fsm_admin/templatetags/fsm_admin.py
index 3b7e1fb..086c989 100644
--- a/fsm_admin/templatetags/fsm_admin.py
+++ b/fsm_admin/templatetags/fsm_admin.py
@@ -3,6 +3,7 @@ import logging
from django import template
from django.contrib.admin.templatetags.admin_modify import submit_row
from django.conf import settings
+from django.db import models
register = template.Library()
@@ -47,7 +48,7 @@ def fsm_submit_row(context):
original = context.get('original', None)
model_name = ''
if original is not None:
- if original._deferred:
+ if original is models.DEFERRED:
model_name = type(original).__base__._meta.verbose_name
else:
model_name = original.__class__._meta.verbose_name
https://github.com/gadventures/django-fsm-admin#try-the-example
$ git clone [email protected]:gadventures/django-fsm-admin.git
$ cd django-fsm-admin
$ mkvirtualenv fsm_admin
$ pip install -r requirements.txt
$ python fsm_admin/setup.py develop
$ cd example
$ ./manage.py syncdb
$ ./manage.py runserver
Here's what I did:
gableroux@zbookarch ~/repos/test
git clone [email protected]:gadventures/django-fsm-admin.git
Cloning into 'django-fsm-admin'...
remote: Counting objects: 401, done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 401 (delta 0), reused 0 (delta 0), pack-reused 393
Receiving objects: 100% (401/401), 74.72 KiB | 0 bytes/s, done.
Resolving deltas: 100% (188/188), done.
Checking connectivity... done.
gableroux@zbookarch ~/repos/test
cd django-fsm-admin
gableroux@zbookarch ~/repos/test/django-fsm-admin master
mkvirtualenv fsm_admin
Using base prefix '/usr'
New python executable in /home/gableroux/.virtualenvs/fsm_admin/bin/python
Installing setuptools, pip, wheel...done.
virtualenvwrapper.user_scripts creating /home/gableroux/.virtualenvs/fsm_admin/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/gableroux/.virtualenvs/fsm_admin/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/gableroux/.virtualenvs/fsm_admin/bin/preactivate
virtualenvwrapper.user_scripts creating /home/gableroux/.virtualenvs/fsm_admin/bin/postactivate
virtualenvwrapper.user_scripts creating /home/gableroux/.virtualenvs/fsm_admin/bin/get_env_details
(fsm_admin) gableroux@zbookarch ~/repos/test/django-fsm-admin master
pip install -r requirements.txt
Collecting Django<1.7,>=1.6 (from -r requirements.txt (line 1))
Using cached Django-1.6.11-py2.py3-none-any.whl
Collecting django-fsm==2.0.1 (from -r requirements.txt (line 2))
Installing collected packages: Django, django-fsm
Successfully installed Django-1.6.11 django-fsm-2.0.1
(fsm_admin) gableroux@zbookarch ~/repos/test/django-fsm-admin master
python fsm_admin/setup.py develop
python: can't open file 'fsm_admin/setup.py': [Errno 2] No such file or directory
✘ (fsm_admin) gableroux@zbookarch ~/repos/test/django-fsm-admin master
python setup.py develop
running develop
running egg_info
creating django_fsm_admin.egg-info
writing dependency_links to django_fsm_admin.egg-info/dependency_links.txt
writing requirements to django_fsm_admin.egg-info/requires.txt
writing django_fsm_admin.egg-info/PKG-INFO
writing top-level names to django_fsm_admin.egg-info/top_level.txt
writing manifest file 'django_fsm_admin.egg-info/SOURCES.txt'
reading manifest file 'django_fsm_admin.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
warning: no files found matching 'README.md'
writing manifest file 'django_fsm_admin.egg-info/SOURCES.txt'
running build_ext
Creating /home/gableroux/.virtualenvs/fsm_admin/lib/python3.5/site-packages/django-fsm-admin.egg-link (link to .)
Adding django-fsm-admin 1.2.3 to easy-install.pth file
Installed /home/gableroux/repos/test/django-fsm-admin
Processing dependencies for django-fsm-admin==1.2.3
Searching for django-fsm>=2.1.0
Reading https://pypi.python.org/simple/django-fsm/
Best match: django-fsm 2.3.0
Downloading https://pypi.python.org/packages/source/d/django-fsm/django-fsm-2.3.0.tar.gz#md5=fa5d93ba1ce93cb27543acbcfa24e267
Processing django-fsm-2.3.0.tar.gz
Writing /tmp/easy_install-yvhrawb3/django-fsm-2.3.0/setup.cfg
Running django-fsm-2.3.0/setup.py -q bdist_egg --dist-dir /tmp/easy_install-yvhrawb3/django-fsm-2.3.0/egg-dist-tmp-fu5u6224
creating /home/gableroux/.virtualenvs/fsm_admin/lib/python3.5/site-packages/django_fsm-2.3.0-py3.5.egg
Extracting django_fsm-2.3.0-py3.5.egg to /home/gableroux/.virtualenvs/fsm_admin/lib/python3.5/site-packages
Adding django-fsm 2.3.0 to easy-install.pth file
Installed /home/gableroux/.virtualenvs/fsm_admin/lib/python3.5/site-packages/django_fsm-2.3.0-py3.5.egg
Searching for Django==1.6.11
Best match: Django 1.6.11
Adding Django 1.6.11 to easy-install.pth file
Using /home/gableroux/.virtualenvs/fsm_admin/lib/python3.5/site-packages
Finished processing dependencies for django-fsm-admin==1.2.3
(fsm_admin) gableroux@zbookarch ~/repos/test/django-fsm-admin master
cd example
(fsm_admin) gableroux@zbookarch ~/repos/test/django-fsm-admin/example master
python manage.py syncdb
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/gableroux/.virtualenvs/fsm_admin/lib/python3.5/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/home/gableroux/.virtualenvs/fsm_admin/lib/python3.5/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/gableroux/.virtualenvs/fsm_admin/lib/python3.5/site-packages/django/core/management/__init__.py", line 261, in fetch_command
commands = get_commands()
File "/home/gableroux/.virtualenvs/fsm_admin/lib/python3.5/site-packages/django/core/management/__init__.py", line 107, in get_commands
apps = settings.INSTALLED_APPS
File "/home/gableroux/.virtualenvs/fsm_admin/lib/python3.5/site-packages/django/conf/__init__.py", line 54, in __getattr__
self._setup(name)
File "/home/gableroux/.virtualenvs/fsm_admin/lib/python3.5/site-packages/django/conf/__init__.py", line 50, in _setup
self._configure_logging()
File "/home/gableroux/.virtualenvs/fsm_admin/lib/python3.5/site-packages/django/conf/__init__.py", line 72, in _configure_logging
from django.utils.log import DEFAULT_LOGGING
File "/home/gableroux/.virtualenvs/fsm_admin/lib/python3.5/site-packages/django/utils/log.py", line 7, in <module>
from django.views.debug import ExceptionReporter, get_exception_reporter_filter
File "/home/gableroux/.virtualenvs/fsm_admin/lib/python3.5/site-packages/django/views/debug.py", line 12, in <module>
from django.template import Template, Context, TemplateDoesNotExist
File "/home/gableroux/.virtualenvs/fsm_admin/lib/python3.5/site-packages/django/template/__init__.py", line 53, in <module>
from django.template.base import (ALLOWED_VARIABLE_CHARS, BLOCK_TAG_END,
File "/home/gableroux/.virtualenvs/fsm_admin/lib/python3.5/site-packages/django/template/base.py", line 19, in <module>
from django.utils.html import escape
File "/home/gableroux/.virtualenvs/fsm_admin/lib/python3.5/site-packages/django/utils/html.py", line 14, in <module>
from .html_parser import HTMLParser, HTMLParseError
File "/home/gableroux/.virtualenvs/fsm_admin/lib/python3.5/site-packages/django/utils/html_parser.py", line 12, in <module>
HTMLParseError = _html_parser.HTMLParseError
AttributeError: module 'html.parser' has no attribute 'HTMLParseError'
✘ (fsm_admin) gableroux@zbookarch ~/repos/test/django-fsm-admin/example master
Tried with Python 3.5.1
Note that it works with Python 2.7.11
;)
mkvirtualenv -p $(which python2.7)
As you can see, path to setup.py
is wrong in the listed commands ;)
It would be useful to have an admin action with changelists to perform transition changes (e.g. soft-deletion) on a list of selected objects (e.g. locks), similar to the submit buttons on the detail view.
This could be handled via ModelAdmin's actions.
The list would contain any possible target transition, or just the valid ones according to the existing/listed objects (from the queryset).
(It might be even possible to only provide valid choices in the dropdown, by using JS and looking at the selected objects, but that's not that trivial then anymore)
I see that this might result in trying to apply invalid transitions, but in this case these could just get skipped (with a message/notification).
When using Django FSM Admin with Django FSM Log on Python 3.4, the call to trans_func(request=request, by=request.user)
fails with an AttributeError
on the the kwarg request
and the try/except is only looking for TypeError
. The error reads as follows where my_transition
is the name of the transition method:
my_transition() got an unexpected keyword argument 'request'
It appears that if we remove the @fsm_log_by decorator then everything works. This is strange because we have several other transitions that have logging that works.
From https://github.com/gadventures/django-fsm-admin/blob/master/fsm_admin/mixins.py#L162-L172
try:
# Attempt to pass in the request and by argument if using django-fsm-log
trans_func(request=request, by=request.user)
except TypeError:
try:
# Attempt to pass in the by argument if using django-fsm-log
trans_func(by=request.user)
except TypeError:
# If the function does not have a by attribute, just call with no arguments
trans_func()
new_state = self.display_fsm_field(obj, fsm_field_name)
Hi,
With lots of pleasure I'm using fsm-admin, great stuff! I notice that there is a minor warning when moving to 1.10. Any plans to adjust the code?
fsm_admin/mixins.py:78: RemovedInDjango110Warning: 'get_field_by_name is an unofficial API that has been deprecated. You may be able to replace it with 'get_field()' with: return self.model._meta.get_field_by_name(fsm_field_name)[0]
Is it possible to get a version bump for the Django 1.10 fixes please?
Please add to the README that it is required to have "django.core.context_processors.request" in TEMPLATE_CONTEXT_PROCESSORS in Django settings.
When run pip install it geting outdated files.
Hi!
I've been using this lib, and it has been extremely helpful for state management within the admin.
I wanted to name the admin buttons with custom names, and could not find any doc / example here.
When I found the right property in the source code, I thought I would add a quicker example in the repo, for the next person who looks for the same feature
This should be added to the example project's settings.py
, otherwise it doesn't work:
from django.conf import global_settings
TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
'django.core.context_processors.request',
)
I need a way to access the transition's permission param to show/hide the button in template, any clue on how to do?
I believe that instead of displaying all buttons and treat permissions in model or view, it would be more elegant to hide those who do not have the necessary permissions on the template
Django==1.10.2
django-grappelli==2.8.2
django-fsm==2.4.0
django-fsm-admin==1.2.3
django-fsm-log==1.3.0
It appears that the fsm_submit_line_grappelli template is not receiving any context, and therefore fails to build the correct "delete" URL.
I've mentioned this over at sehmaschine/django-grappelli#783 - not sure where the problem lies yet.
QuickCast demo is offline. Could you please upload the demo video somewhere else?
Many thanks,
in tempaltetags/fsm-admin.py:
This will make it possible to use custom=dict('verbose') in @transition
By example:
58 line code
if 'verbose' in transition.custom::
return '{0} {1}'.format(transition.custom['verbose'], model_name).title()
else:
return '{0} {1}'.format(transition.name.replace('_', ' '), model_name).title()
Which tends to be one of the transition buttons. "Save" should be the default!
pip install django-fsm-admin
does not work. The actuall package on PyPi does not contain templates nor templatetags. Actually it only contains mixins.py ...
pip install -e git://github.com/gadventures/django-fsm-admin.git#egg=django-fsm-admin
doesn´t work either as it does not install the templates dir.
Hi there, could you guys think about providing a new release to make the support for Django v4 available on pypi?
Hi,
I defined the states as a choice list to be able to translate the labels.
However this does not reflect in the admin panel, the state is still displayed as "new" (the key) where I was expecting "New" (the value)
Am I missing something?
#States are listed as choices to be translatable
NEW = 'new'
ACCEPTED = 'accepted'
AWAITING_REVIEW = 'awaiting_review'
REVIEWED = 'reviewed'
COMPLETED = 'completed'
CANCELLED = 'cancelled'
REJECTED = 'rejected'
STATES = (
(NEW, _('New')),
(ACCEPTED, _('Accepted')),
(AWAITING_REVIEW, _('Awaiting Review')),
(REVIEWED, _('Reviewed')),
(COMPLETED, _('Completed')),
(CANCELLED, _('Cancelled')),
(REJECTED, _('Rejected')),
)
state = FSMField(_('state'), default=NEW, protected=True)
In the FSM Mixin for save_model, the transition is executed before the saving of the model, which causes the inlines to be saved after the transition has occurred.
Is this on purpose? Is there any reason this order was chosen?
If there are post signals attached to the transition which involve the related models, they won't take into account the modifications through the inlines in the current setup.
I'm using django-fsm-admin with Python 3.4.0. So far, I've found two cases where I've had to change iteritems() to items(). I'm guessing that there will be other changes that need to be made as well.
I have a branch at https://github.com/greg-barnett/django-fsm-admin/tree/python3
This branch hasn't been tested with python 2, which is why I'm opening an issue rather than a pull request. I have no experience with making code compatible with both python 2 & 3.
Hi,
This repository seems to lack maintenance, I propose myself as a new maintainer.
Could you give me the right to push in your repository or just transfer it on my account in github?
Thank you!
It seems the current version is not compatible with other django apps, such as Django-guardian. Only the first mixin will show up in the admin page. If I put the django-fsm-admin in front of django-guardian mixin, then only django-fsm-admin buttons will show. How can I use both packages on the admin page?
For example, this code will show django-fsm-admin buttons only.
class PIAFormAdmin(FSMTransitionMixin, GuardedModelAdmin)
This will show django-guardian buttons only
class PIAFormAdmin(GuardedModelAdmin, FSMTransitionMixin)
So the order matters. But I want to show buttons from both packages on the admin page.
I suggest the documentation is adjusted so that it clarifies that in order to show the status as read only, you also need to add the field in the ModelAdmin's readonly_fields. This took me a while to work out (I assumed it would do it automatically).
Thanks for the package though!
On django 1.11.5
:
The following error is shown with runserver:
AttributeError: module 'django.conf.global_settings' has no attribute 'TEMPLATE_CONTEXT_PROCESSORS'
So I left it out as it was apparently deprecated in django 1.8 and removed in 1.10
Now I get a Direct state modification is not allowed
error, when using the buttons provided on admin.
For my project, it is important that it is possible to add button buttons to get_list_display.
In your example, I saw how this can be done in the form of editing.
I looked at the source code templatetags
and did not see anything that I need.
Tell me, do you have a ready-made version of how to do this, or do I need to implement it from scratch?
mixins.py: fsmfield = obj._meta.get_field_by_name(field)[0]
It's about bringing django 1.10 support to the table, right?..
It could be very useful to customize the input buttons in the submit_line to customize colors or to attach js callbacks.
Specifically I'm hoping for 1.5.5 support. Is there some reason why you want to pin django-fsm-admin to one particular version of Django, and would you be amenable to a pull request that supports 1.5?
I'm also curious about requiring django-fsm 1.5.1 versus 2.0, but that's the version I'm using, so I'm less concerned in the short term :)
In order to allow certain transitions only to a set of staff users, I'd like to access the User object inside my customized condition functions.
This unfortunately is not possible, because that function never "sees" the request
object. By patching the templatetag function fsm_transition_hints
and the method FSMTransitionMixin.get_transition_hints()
is would easily by possible to pass to request
object to my condition function.
If you have concerns about backward compatibility with existing condition functions, I could add a boolean named, say pass_request
to the custom
dict. Then the request
object is passed in only when this is True
.
What do you think about this and shall I create a pull request for this feature?
Is it possible to add intermediate pages triggered by the state-changes?
Example case: changing state sends email, but allows you to add email content before sending in a new form.
so that I can install from PyPI rather than GitHub.
Thanks
Hello,
I'm trying to use Django FSM and Django FSM Admin but I get the error:
'Employee' object has no attribute 'get_available_user_state_transitions'
In template C:\Python27\lib\site-packages\fsm_admin\templates\fsm_admin\change_form.html, error at line 4: {% fsm_submit_row %}
The last traceback is:
transitions = getattr(obj, transitions_func)(user) if obj else []
with transitions_func = u'get_available_user_state_transitions'
(in Python27\lib\site-packages\fsm_admin\mixins.py in _fsm_get_transitions)
I don't understand...
in models.py:
class Employee(models.Model):
step = FSMField(default=STEP_1, choices=CHOICES, protected=True)
...
@transition(field=step, source=STEP_1, target=STEP_2)
def step1to2(self):
pass
in admin.py:
@admin.register(Employee)
class EmployeeAdmin(FSMTransitionMixin, reversion.VersionAdmin, admin.ModelAdmin):
...
in settings.py:
INSTALLED_APPS = (
'suit',
'django.contrib.admin',
'django_fsm',
'fsm_admin',
...
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.