model: use methods to access a user's typed posts (#104)

This commit is contained in:
Lephe 2022-04-25 17:03:27 +01:00
parent 8098642d4b
commit db5e613f7e
Signed by: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
4 changed files with 28 additions and 16 deletions

View File

@ -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:
# <notifications> 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",

View File

@ -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()),
}

View File

@ -68,7 +68,7 @@
<th>Forum</th>
<th>Création</th>
</tr>
{% for t in member.topics %}
{% for t in member.topics() %}
<tr>
<td><a href="{{ url_for('forum_topic', f=t.forum, page=(t, 1)) }}">{{ t.title }}</a></td>
<td><a href="{{ url_for('forum_page', f=t.forum) }}">{{ t.forum.name }}</a></td>

View File

@ -11,7 +11,7 @@
<ul>
<li>{{ stats.comments }} commentaire{{ stats.comments | pluralize }}</li>
<li>{{ stats.topics }} topic{{ stats.topics | pluralize }}</li>
<li>{{ stats.programs }} topic{{ stats.programs | pluralize }}</li>
<li>{{ stats.programs }} programme{{ stats.programs | pluralize }}</li>
</ul>
<p>Les propriétés suivantes seront supprimées :</p>
<ul>