From 6a9464882f74bc8a85ad86d098f8b4da04c854ca Mon Sep 17 00:00:00 2001 From: Alban Gruin Date: Sun, 22 Jan 2017 18:21:49 +0100 Subject: Génération automatique d'un token --- admin.py | 1 + models.py | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/admin.py b/admin.py index b1edf2f..95b4fd7 100644 --- a/admin.py +++ b/admin.py @@ -27,6 +27,7 @@ class GroupAdmin(admin.ModelAdmin): @admin.register(Subscription) class SubscriptionAdmin(admin.ModelAdmin): list_display = ("email", "group", "active",) + readonly_fields = ("token",) @admin.register(Room) diff --git a/models.py b/models.py index 9f9387d..7d5efd7 100644 --- a/models.py +++ b/models.py @@ -2,6 +2,9 @@ from django.db import models from django.db.models import Q from django.utils.text import slugify +import hashlib +import os + class Timetable(models.Model): name = models.CharField(max_length=64, unique=True, verbose_name="nom") @@ -75,6 +78,11 @@ class Subscription(models.Model): active = models.BooleanField(verbose_name="activé", default=False) token = models.CharField(max_length=64, unique=True, default="") + def save(self): + if self.token == "": + self.token = hashlib.sha1(os.urandom(128)).hexdigest() + super(Subscription, self).save() + class Meta: index_together = ("group", "active",) -- cgit v1.2.1