diff --git a/app/models/user.py b/app/models/user.py index 6e31c62..325040d 100644 --- a/app/models/user.py +++ b/app/models/user.py @@ -3,11 +3,17 @@ from flask_login import UserMixin from sqlalchemy import func as SQLfunc from os.path import isfile from PIL import Image + from app import app, db from app.models.priv import SpecialPrivilege, Group, GroupMember, \ GroupPrivilege from app.models.trophy import Trophy, TrophyMember, Title from app.models.notification import Notification +from app.models.post import Post +from app.models.comment import Comment +from app.models.topic import Topic +from app.models.program import Program + import app.utils.unicode_names as unicode_names import app.utils.ldap as ldap from app.utils.unicode_names import normalize @@ -113,9 +119,15 @@ class Member(User): # Relations trophies = db.relationship('Trophy', secondary=TrophyMember, back_populates='owners') - topics = db.relationship('Topic') - programs = db.relationship('Program') - comments = db.relationship('Comment') + + # Access to polymorphic posts + # TODO: Check that the query uses the double index on Post.{author_id,type} + def comments(self): + return db.session.query(Comment).filter(Post.author_id==self.id).all() + def topics(self): + return db.session.query(Topic).filter(Post.author_id==self.id).all() + def programs(self): + return db.session.query(Program).filter(Post.author_id==self.id).all() # Other fields populated automatically through relations: # List of unseen notifications (of type Notification) @@ -129,7 +141,7 @@ class Member(User): self.email_confirmed = not V5Config.ENABLE_EMAIL_CONFIRMATION if not V5Config.USE_LDAP: self.set_password(password) - # Workflow with LDAP enabled is User → Postgresql → LDAP → set password + # Workflow with LDAP enabled is User → PostgreSQL → LDAP → set password self.xp = 0 self.theme = 'default_theme' @@ -149,23 +161,23 @@ class Member(User): Transfers all the posts to another user. This is generally used to transfer ownership to a newly-created Guest before deleting an account. """ - for t in self.topics: + for t in self.topics(): t.author = other db.session.add(t) - for p in self.programs: + for p in self.programs(): p.author = other db.session.add(p) - for c in self.comments: + for c in self.comments(): c.author = other db.session.add(c) def delete_posts(self): """Deletes the user's posts.""" - for t in self.topics: + for t in self.topics(): t.delete() - for p in self.programs: + for p in self.programs(): p.delete() - for c in self.comments: + for c in self.comments(): c.delete() def delete(self): @@ -450,7 +462,7 @@ class Member(User): progress(levels, post_count) if context in ["new-program", None]: - program_count = len(self.programs) + program_count = len(self.programs()) levels = { 5: "Programmeur du dimanche", diff --git a/app/routes/admin/account.py b/app/routes/admin/account.py index 3cd065f..3cf49d5 100644 --- a/app/routes/admin/account.py +++ b/app/routes/admin/account.py @@ -128,9 +128,9 @@ def adm_delete_account(user_id): # TODO: Number of comments by *other* members which will be deleted stats = { - 'comments': len(user.comments), - 'topics': len(user.topics), - 'programs': len(user.programs), + 'comments': len(user.comments()), + 'topics': len(user.topics()), + 'programs': len(user.programs()), 'groups': len(user.groups), 'privs': len(user.special_privileges()), } diff --git a/app/templates/account/user.html b/app/templates/account/user.html index 1df1a06..4db27fc 100644 --- a/app/templates/account/user.html +++ b/app/templates/account/user.html @@ -68,7 +68,7 @@ Forum Création - {% for t in member.topics %} + {% for t in member.topics() %} {{ t.title }} {{ t.forum.name }} diff --git a/app/templates/admin/delete_account.html b/app/templates/admin/delete_account.html index a22b3be..4bd6fa1 100644 --- a/app/templates/admin/delete_account.html +++ b/app/templates/admin/delete_account.html @@ -11,7 +11,7 @@

Les propriétés suivantes seront supprimées :