franceconnect_data: use a persistent cache for tokens (#55156)
This commit is contained in:
parent
54ad1170f4
commit
6f8374289f
|
@ -42,8 +42,8 @@ class Migration(migrations.Migration):
|
|||
(
|
||||
'text_template',
|
||||
models.TextField(
|
||||
default="{{ given_name }} {{ family_name }} {% if gender == 'male' %}né{% else %}née{% endif %} le {{ birthdate }} à {{ birthplace }}",
|
||||
verbose_name='FranceConnect text template',
|
||||
default="{{ fc.given_name }} {{ fc.family_name }} {% if fc.gender == 'male' %}né{% else %}née{% endif %} le {{ fc.birthdate }}",
|
||||
verbose_name='Text template',
|
||||
),
|
||||
),
|
||||
(
|
||||
|
@ -76,8 +76,6 @@ class Migration(migrations.Migration):
|
|||
),
|
||||
),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'FranceConnect',
|
||||
},
|
||||
options={'verbose_name': 'Data sources through FranceConnect'},
|
||||
),
|
||||
]
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
# Generated by Django 2.2.19 on 2021-06-24 13:22
|
||||
|
||||
import uuid
|
||||
|
||||
import django.contrib.postgres.fields.jsonb
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('franceconnect_data', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Token',
|
||||
fields=[
|
||||
(
|
||||
'guid',
|
||||
models.UUIDField(
|
||||
default=uuid.uuid4, primary_key=True, serialize=False, verbose_name='UUID'
|
||||
),
|
||||
),
|
||||
('timestamp', models.DateTimeField(auto_now_add=True, verbose_name='Timestamp')),
|
||||
('content', django.contrib.postgres.fields.jsonb.JSONField(verbose_name='Content')),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'FranceConnect data token',
|
||||
'verbose_name_plural': 'FranceConnect data tokens',
|
||||
},
|
||||
),
|
||||
]
|
|
@ -18,7 +18,7 @@ import datetime
|
|||
import json
|
||||
import uuid
|
||||
|
||||
from django.core.cache import cache
|
||||
from django.contrib.postgres.fields import JSONField
|
||||
from django.core.exceptions import PermissionDenied
|
||||
from django.db import models
|
||||
from django.http import HttpResponseBadRequest, HttpResponseRedirect
|
||||
|
@ -256,9 +256,21 @@ class Resource(BaseResource):
|
|||
}
|
||||
|
||||
def store(self, data):
|
||||
ref = str(uuid.uuid4().hex)
|
||||
cache.set(ref, data)
|
||||
return ref
|
||||
# automatic cleaning of the cache
|
||||
Token.objects.filter(timestamp__lt=now() - datetime.timedelta(days=60)).delete()
|
||||
token = Token.objects.create(content=data)
|
||||
return token.guid.hex
|
||||
|
||||
def retrieve(self, ref):
|
||||
return cache.get(ref)
|
||||
token = Token.objects.filter(guid=ref).first()
|
||||
return token and token.content
|
||||
|
||||
|
||||
class Token(models.Model):
|
||||
guid = models.UUIDField(verbose_name=_('UUID'), primary_key=True, default=uuid.uuid4)
|
||||
timestamp = models.DateTimeField(verbose_name=_('Timestamp'), auto_now_add=True)
|
||||
content = JSONField(verbose_name=_('Content'))
|
||||
|
||||
class Meta:
|
||||
verbose_name = _('FranceConnect data token')
|
||||
verbose_name_plural = _('FranceConnect data tokens')
|
||||
|
|
Loading…
Reference in New Issue