diff --git a/invenio_accounts/api.py b/invenio_accounts/api.py index e1c98a75..b462c811 100644 --- a/invenio_accounts/api.py +++ b/invenio_accounts/api.py @@ -16,8 +16,10 @@ def __init__(self): """Constructor.""" self.updated_users = set() self.updated_roles = set() + self.updated_domains = set() self.deleted_users = set() self.deleted_roles = set() + self.deleted_domains = set() class DBUsersChangeHistory: @@ -43,6 +45,12 @@ def add_updated_role(self, session_id, role_id): session = self._get_session(session_id) session.updated_roles.add(role_id) + def add_updated_domain(self, session_id, domain_id): + """Adds a user to the updated domains list.""" + assert domain_id is not None + session = self._get_session(session_id) + session.updated_domains.add(domain_id) + def add_deleted_user(self, session_id, user_id): """Adds a user to the deleted users list.""" assert user_id is not None @@ -55,6 +63,12 @@ def add_deleted_role(self, session_id, role_id): session = self._get_session(session_id) session.deleted_roles.add(role_id) + def add_deleted_domain(self, session_id, domain_id): + """Adds a role to the deleted domain list.""" + assert domain_id is not None + session = self._get_session(session_id) + session.deleted_domains.add(domain_id) + def clear_dirty_sets(self, session): """Removes session object.""" sid = id(session) diff --git a/invenio_accounts/datastore.py b/invenio_accounts/datastore.py index 5bad177d..4d9f44a0 100644 --- a/invenio_accounts/datastore.py +++ b/invenio_accounts/datastore.py @@ -12,6 +12,7 @@ from flask import current_app from flask_security import SQLAlchemyUserDatastore, user_confirmed +from sqlalchemy.orm import joinedload from .models import Domain, Role, User from .proxies import current_db_change_history @@ -113,7 +114,11 @@ def find_role_by_id(self, role_id): def find_domain(self, domain): """Find a domain.""" - return Domain.query.filter_by(domain=domain).one_or_none() + return ( + Domain.query.filter_by(domain=domain) + .options(joinedload(Domain.category_name)) + .one_or_none() + ) def create_domain(self, domain, **kwargs): """Create a new domain.""" diff --git a/invenio_accounts/domains.py b/invenio_accounts/domains.py index 640e98cc..4652afef 100644 --- a/invenio_accounts/domains.py +++ b/invenio_accounts/domains.py @@ -20,6 +20,7 @@ def on_user_confirmed(app, user): domain = datastore.find_domain(user.domain) if domain is None: domain = datastore.create_domain(user.domain) + datastore.mark_changed(id(datastore.db.session), model=domain) # Verify user if domain is verified. if domain.status == DomainStatus.verified: diff --git a/invenio_accounts/models.py b/invenio_accounts/models.py index 9955268c..1fed7ad5 100644 --- a/invenio_accounts/models.py +++ b/invenio_accounts/models.py @@ -498,9 +498,6 @@ class DomainOrg(db.Model): parent = db.relationship("DomainOrg", remote_side=[id]) """Relationship to parent.""" - domains = db.relationship("Domain", back_populates="org") - """Relationship to domains for this organisation.""" - @classmethod def create(cls, pid, name, json=None, parent=None): """Create a domain organisation.""" @@ -564,12 +561,15 @@ class Domain(db.Model, Timestamp): org_id = db.Column(db.Integer(), db.ForeignKey(DomainOrg.id), nullable=True) """Organisation associated with domain.""" - org = db.relationship("DomainOrg", back_populates="domains") + org = db.relationship("DomainOrg", backref="domains") # spammer, mail-provider, organisation, company category = db.Column(db.Integer(), db.ForeignKey(DomainCategory.id), nullable=True) """Category of domain.""" + category_name = db.relationship("DomainCategory", backref="domains") + """Relationship to category""" + num_users = db.Column(db.Integer(), default=0, nullable=False) """Computed property to store number of users in domain.""" diff --git a/invenio_accounts/tasks.py b/invenio_accounts/tasks.py index 9fd92b54..6d9c65b0 100644 --- a/invenio_accounts/tasks.py +++ b/invenio_accounts/tasks.py @@ -128,6 +128,7 @@ def update_domain_status(): # Update the database i = 0 + now = datetime.utcnow() for row in domain_updates: domain, users, active, inactive, confirmed, verified, blocked = row db.session.query(Domain).filter(Domain.domain == domain).update( @@ -138,6 +139,7 @@ def update_domain_status(): "num_confirmed": confirmed, "num_verified": verified, "num_blocked": blocked, + "updated": now, } ) i += 1