gestioCOF/utils/models.py
Aurélien Delobelle 05eeb6a25c core -- Install django-allauth-ens
Refer to allauth doc for an accurate features list:
  http://django-allauth.readthedocs.io/en/latest/

Users can now change their password, ask for a password reset, or set
one if they don't have one.

In particular, it allows users whose account has been created via a
clipper authentication to configure a password before losing their
clipper. Even if they have already lost it, they are able to get one
using the "Reset password" functionality.

Allauth multiple emails management is deactivated. Requests to the
related url redirect to the home page.

All the login and logout views are replaced by the allauth' ones. It
also concerns the Django and Wagtail admin sites.

Note that users are no longer logged out of the clipper CAS server when
they authenticated via this server. Instead a message suggests the user
to disconnect.

Clipper connections and `login_clipper`
---------------------------------------

- Non-empty `login_clipper` are now unique among `CofProfile` instances.
- They are created once for users with a non-empty 'login_clipper' (with
the data migration 0014_create_clipper_connections).
- The `login_clipper` of CofProfile instances are sync with their
clipper connections:
    * `CofProfile.sync_clipper_connections` method updates the
connections based on `login_clipper`.
    * Signals receivers `sync_clipper…` update `login_clipper` based on
connections creations/updates/deletions.

Misc
----

- Add NullCharField (model field) which allows to use `unique=True` on
CharField (even with empty strings).
- Parts of kfet mixins for TestCase are now in shared.tests.testcase,
  as they are used elsewhere than in the kfet app.
2018-10-21 17:09:12 +02:00

43 lines
1.5 KiB
Python

from django.core.exceptions import ImproperlyConfigured
from django.db import models
class NullCharField(models.CharField):
"""
CharField that stores '' as NULL and returns NULL as ''.
Use this for unique CharField.
As long as this field is referenced in a migration, this definition must be
kept.
When upgrading to Django >= 1.11, it is possible to drop the methods:
`from_db_value`, `to_python` and `get_prep_value`.
Source:
https://github.com/django-oscar/django-oscar/blob/1a2e8279161a396e70e44339d7206f25355b33a3/src/oscar/models/fields/__init__.py#L94
"""
description = "CharField that stores '' as NULL and returns NULL as ''"
def __init__(self, *args, **kwargs):
if not kwargs.get("null", True) or not kwargs.get("blank", True):
raise ImproperlyConfigured("NullCharField implies 'null == blank == True'.")
kwargs["null"] = kwargs["blank"] = True
super().__init__(*args, **kwargs)
def from_db_value(self, value, *args, **kwargs):
return self.to_python(value)
def to_python(self, *args, **kwargs):
val = super().to_python(*args, **kwargs)
return val if val is not None else ""
def get_prep_value(self, *args, **kwargs):
prepped = super().get_prep_value(*args, **kwargs)
return prepped if prepped != "" else None
def deconstruct(self):
name, path, args, kwargs = super().deconstruct()
del kwargs["null"]
del kwargs["blank"]
return name, path, args, kwargs