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.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

privs.py 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # Planète Casio v5
  2. # models.privs: Database models for groups and privilege management
  3. from app import db
  4. from config import V5Config
  5. # Privileges are represented by strings (slugs), for instance "post-news" or
  6. # "delete-own-posts". Belonging to a group automatically grants a user the
  7. # privileges of that group; additionally, administrators (or any people with
  8. # the "grant-special-privileges" privilege) can grant privileges on a per-user
  9. # basis.
  10. # SpecialPrivilege: Privilege manually granted to a user
  11. class SpecialPrivilege(db.Model):
  12. __tablename__ = 'special_privilege'
  13. id = db.Column(db.Integer, primary_key=True)
  14. # Member that is granted the privilege
  15. mid = db.Column(db.Integer, db.ForeignKey('member.id'), index=True)
  16. # Privilege name
  17. priv = db.Column(db.String(V5Config.PRIVS_MAXLEN))
  18. def __init__(self, member, priv):
  19. self.mid = member.id
  20. self.priv = priv
  21. def __repr__(self):
  22. return f'<Privilege "{self.priv}" of member #{mid}>'
  23. # Group: User group, corresponds to a community role and a set of privileges
  24. class Group(db.Model):
  25. __tablename__ = 'group'
  26. # Unique group ID
  27. id = db.Column(db.Integer, primary_key=True)
  28. # Full name, such as "Administrateur" or "Membre d'honneur".
  29. name = db.Column(db.Unicode(50), unique=True)
  30. # The CSS code should not assume any specific layout and typically applies
  31. # to a text node. Use attributes like color, font-style, font-weight, etc.
  32. css = db.Column(db.UnicodeText)
  33. # Textual description
  34. description = db.Column(db.UnicodeText)
  35. # List of members (lambda delays evaluation)
  36. members = db.relationship('Member', secondary=lambda:GroupMember,
  37. back_populates='groups')
  38. def __init__(self, name, css, descr):
  39. self.name = name
  40. self.css = css
  41. self.description = descr
  42. self.members = []
  43. def delete(self):
  44. """
  45. Deletes the group and the associated information:
  46. * Group privileges
  47. """
  48. for gp in GroupPrivilege.query.filter_by(gid=self.id).all():
  49. db.session.delete(gp)
  50. db.session.commit()
  51. db.session.delete(self)
  52. db.session.commit()
  53. def privs(self):
  54. gps = GroupPrivilege.query.filter_by(gid=self.id).all()
  55. return sorted(gp.priv for gp in gps)
  56. def __repr__(self):
  57. return f'<Group "{self.name}">'
  58. # Many-to-many relation for users belonging to groups
  59. GroupMember = db.Table('group_member', db.Model.metadata,
  60. db.Column('gid', db.Integer, db.ForeignKey('group.id')),
  61. db.Column('uid', db.Integer, db.ForeignKey('member.id')))
  62. # Meny-to-many relationship for privileges granted to groups
  63. class GroupPrivilege(db.Model):
  64. __tablename__ = 'group_privilege'
  65. id = db.Column(db.Integer, primary_key=True)
  66. gid = db.Column(db.Integer, db.ForeignKey('group.id'))
  67. priv = db.Column(db.String(V5Config.PRIVS_MAXLEN))
  68. def __init__(self, group, priv):
  69. self.gid = group.id
  70. self.priv = priv