jazzband / django-fsm-log

Automatic logging for Django FSM
https://django-fsm-log.readthedocs.io/en/latest/
MIT License
241 stars 78 forks source link

`fsm_log_by` decorator does not persist `by` #69

Closed jlost closed 6 years ago

jlost commented 6 years ago

model:

@fsm_log_by
@transition(field=status, source=Status.PENDING_CODE_REVIEW.name, target=Status.CODE_REJECTED.name)
def reject_code(self, by=None):
        print('REJECTING...')
        print(by.id)
        pass

(Successfully prints user's id but doesn't persist by to StateLog)

views.py:

def post(self, request, job_id):
     job = get_object_or_404(Job, pk=job_id)
     if not can_proceed(job.reject_code):
          raise PermissionDenied
     job.reject_code(self.request.user)
     job.save()

DB:

postgres=# select * from django_fsm_log_statelog;
 id |           timestamp           |     state     |  transition  | object_id | by_id | content_type_id
----+-------------------------------+---------------+--------------+-----------+-------+-----------------
  1 | 2018-02-06 14:40:30.159429+00 | QUEUED        | approve_code |         2 |       |               1
  2 | 2018-02-06 14:58:45.591487+00 | DELETED       | delete       |         1 |       |               1
  3 | 2018-02-06 15:28:10.691508+00 | QUEUED        | approve_code |         3 |       |               1
  4 | 2018-02-07 14:08:30.8747+00   | CODE_REJECTED | reject_code  |         4 |       |               1
  5 | 2018-02-07 14:09:42.174577+00 | CODE_REJECTED | reject_code  |         5 |       |               1
  6 | 2018-02-07 14:32:12.451424+00 | CODE_REJECTED | reject_code  |         6 |       |               1
(6 rows)

REPL:

>>> StateLog.objects.get(pk=6).by
>>> StateLog.objects.get(pk=6).by_id

This is with the latest pip release, 1.5.0

jlost commented 6 years ago

Found my obvious rookie mistake -- passing in the user as an arg rather than kwarg.

Fix: job.reject_code(self.request.user) -> job.reject_code(by=self.request.user)