La version 5 de Planète Casio. Regroupe le forum, les programmes, les tutoriel, les sprites et tous les autres outils développés par nos soins.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

88 lines
3.0 KiB

# Planète Casio v5
# models.privs: Database models for groups and privilege management
from app import db
from config import V5Config
# Privileges are represented by strings (slugs), for instance "post-news" or
# "delete-own-posts". Belonging to a group automatically grants a user the
# privileges of that group; additionally, administrators (or any people with
# the "grant-special-privileges" privilege) can grant privileges on a per-user
# basis.
# SpecialPrivilege: Privilege manually granted to a user
class SpecialPrivilege(db.Model):
__tablename__ = 'special_privilege'
id = db.Column(db.Integer, primary_key=True)
# Member that is granted the privilege
mid = db.Column(db.Integer, db.ForeignKey('member.id'), index=True)
# Privilege name
priv = db.Column(db.String(V5Config.PRIVS_MAXLEN))
def __init__(self, member, priv):
self.mid = member.id
self.priv = priv
def __repr__(self):
return f'<Privilege "{self.priv}" of member #{mid}>'
# Group: User group, corresponds to a community role and a set of privileges
class Group(db.Model):
__tablename__ = 'group'
# Unique group ID
id = db.Column(db.Integer, primary_key=True)
# Full name, such as "Administrateur" or "Membre d'honneur".
name = db.Column(db.Unicode(50), unique=True)
# The CSS code should not assume any specific layout and typically applies
# to a text node. Use attributes like color, font-style, font-weight, etc.
css = db.Column(db.UnicodeText)
# Textual description
description = db.Column(db.UnicodeText)
# List of members (lambda delays evaluation)
members = db.relationship('Member', secondary=lambda:GroupMember,
back_populates='groups')
def __init__(self, name, css, descr):
self.name = name
self.css = css
self.description = descr
self.members = []
def delete(self):
"""
Deletes the group and the associated information:
* Group privileges
"""
for gp in GroupPrivilege.query.filter_by(gid=self.id).all():
db.session.delete(gp)
db.session.commit()
db.session.delete(self)
db.session.commit()
def privs(self):
gps = GroupPrivilege.query.filter_by(gid=self.id).all()
return sorted(gp.priv for gp in gps)
def __repr__(self):
return f'<Group "{self.name}">'
# Many-to-many relation for users belonging to groups
GroupMember = db.Table('group_member', db.Model.metadata,
db.Column('gid', db.Integer, db.ForeignKey('group.id')),
db.Column('uid', db.Integer, db.ForeignKey('member.id')))
# Meny-to-many relationship for privileges granted to groups
class GroupPrivilege(db.Model):
__tablename__ = 'group_privilege'
id = db.Column(db.Integer, primary_key=True)
gid = db.Column(db.Integer, db.ForeignKey('group.id'))
priv = db.Column(db.String(V5Config.PRIVS_MAXLEN))
def __init__(self, group, priv):
self.gid = group.id
self.priv = priv