forked from lrvick/django-barcode-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
44 lines (35 loc) · 1.44 KB
/
models.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
38
39
40
41
42
43
44
from django.db import models
from django.conf import settings
from django.core.files.base import ContentFile
from django.contrib.auth.models import User
from barauth.utils import gen_passhash
from barauth.utils import print_card
from PyQRNative import QRCode, QRErrorCorrectLevel
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
class UserBarcode(models.Model):
user = models.ForeignKey(User, unique=True)
barcode = models.ImageField(
upload_to='%s/img/barcodes' % settings.MEDIA_ROOT
)
def __unicode__(self):
return self.user.email
def user_create_barcode(sender, instance, created, **kwargs):
instance = User.objects.get(email=instance.email)
password_hash = gen_passhash(instance)
qr = QRCode(8, QRErrorCorrectLevel.Q)
qr.addData("####%s|%s" % (str(instance.pk), str(password_hash)))
qr.make()
im = qr.makeImage()
temp_file = StringIO()
# We'll take the username if we have to, but prefer first+last
im.save(temp_file, format='png')
barcode_contents = ContentFile(temp_file.getvalue())
user_barcode = UserBarcode.objects.get_or_create(user=instance)[0]
user_barcode.barcode.save('%s.png' % str(instance.pk), barcode_contents)
if settings.PRINT_CARDS:
print_card(instance, user_barcode.barcode.name)
pass
models.signals.post_save.connect(user_create_barcode, sender=User, dispatch_uid="apps.barauth.models")