/
mixins.py
37 lines (30 loc) · 1.03 KB
/
mixins.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
"""
A django mixin providing fields to store a signature captured
with jSignature jQuery plugin
"""
import json
from django.db import models
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from .fields import JSignatureField
class JSignatureFieldsMixin(models.Model):
""" Mixin class providing fields to store a signature with jSignature """
signature = JSignatureField(
_('Signature'),
blank=True,
null=True)
signature_date = models.DateTimeField(
_('Signature date'),
blank=True,
null=True)
class Meta:
abstract = True
def save(self, *args, **kwargs):
is_new = self.pk is None
original = not is_new and self.__class__.objects.get(pk=self.pk)
if self.signature:
if is_new or json.dumps(self.signature) != original.signature:
self.signature_date = timezone.now()
else:
self.signature_date = None
super(JSignatureFieldsMixin, self).save()