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

Lephe 5 months ago
parent 8098642d4b
commit db5e613f7e
Signed by: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
  1. 34
  2. 6
  3. 2
  4. 2

@ -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, \
from app.models.trophy import Trophy, TrophyMember, Title
from app.models.notification import Notification
from 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,
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(
def topics(self):
return db.session.query(Topic).filter(
def programs(self):
return db.session.query(Program).filter(
# 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:
# 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(): = other
for p in self.programs:
for p in self.programs(): = other
for c in self.comments:
for c in self.comments(): = other
def delete_posts(self):
"""Deletes the user's posts."""
for t in self.topics:
for t in self.topics():
for p in self.programs:
for p in self.programs():
for c in self.comments:
for c in self.comments():
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",

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

@ -68,7 +68,7 @@
{% for t in member.topics %}
{% for t in member.topics() %}
<td><a href="{{ url_for('forum_topic',, page=(t, 1)) }}">{{ t.title }}</a></td>
<td><a href="{{ url_for('forum_page', }}">{{ }}</a></td>

@ -11,7 +11,7 @@
<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>
<p>Les propriétés suivantes seront supprimées :</p>