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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #! /usr/bin/python3
  2. from app import app, db
  3. from app.models.users import Member, Group, GroupPrivilege
  4. from app.models.privs import SpecialPrivilege
  5. from app.models.trophies import Trophy, Title, TrophyMember
  6. from app.utils import unicode_names
  7. import os
  8. import sys
  9. import yaml
  10. import readline
  11. help_msg = """
  12. This is the Planète Casio master shell. Type 'exit' or C-D to leave.
  13. Type a category name to see a list of elements. Available categories are:
  14. 'members' Registered community members
  15. 'groups' Privilege groups
  16. 'trophies' Trophies
  17. 'trophy-members' Trophies owned by members
  18. Type a category name followed by 'clear' to remove all entries in the category.
  19. Type 'create-groups-and-privs' to recreate all groups and privileges to the
  20. default. This function generates a minimal set of groups and members to prepare
  21. the database.
  22. 1. Deletes all groups
  23. 2. Creates groups 'Administrateur', 'Modérateur', 'Développeur', 'Rédacteur',
  24. 'Responsable Communauté', 'Partenaire', 'Compte communautaire', 'Robot', and
  25. 'Membre de CreativeCalc'
  26. 3. Grants privileges related to these groups
  27. 4. Recreates common accounts: 'Planète Casio' (community account) and 'GLaDOS'
  28. (robot)
  29. Type 'add-group <member> #<group-id>' to add a new member to a group.
  30. Type 'create-trophies' to reset trophies and titles.
  31. """
  32. #
  33. # Category viewers
  34. #
  35. def members(*args):
  36. if args == ("clear",):
  37. for m in Member.query.all():
  38. m.delete()
  39. db.session.commit()
  40. print("Removed all members.")
  41. return
  42. for m in Member.query.all():
  43. print(m)
  44. def groups(*args):
  45. if args == ("clear",):
  46. for g in Group.query.all():
  47. g.delete()
  48. db.session.commit()
  49. print("Removed all groups.")
  50. return
  51. for g in Group.query.all():
  52. print(f"#{g.id} {g.name}")
  53. def trophies(*args):
  54. if args == ("clear",):
  55. for t in Trophy.query.all():
  56. db.session.delete(t)
  57. db.session.commit()
  58. print("Removed all trophies.")
  59. return
  60. for t in Trophy.query.all():
  61. print(t)
  62. def trophy_members(*args):
  63. for t in Trophy.query.all():
  64. if t.owners == []:
  65. continue
  66. print(t)
  67. for m in t.owners:
  68. print(f" {m}")
  69. #
  70. # Creation and edition
  71. #
  72. def create_groups_and_privs():
  73. # Clean up groups
  74. groups("clear")
  75. # Create base groups
  76. gr = []
  77. with open(os.path.join(app.root_path, "data", "groups.yaml")) as fp:
  78. gr = yaml.safe_load(fp.read())
  79. for g in gr:
  80. g["obj"] = Group(g["name"], g["css"], g["descr"])
  81. db.session.add(g["obj"])
  82. db.session.commit()
  83. for g in gr:
  84. for priv in g.get("privs", "").split():
  85. db.session.add(GroupPrivilege(g["obj"], priv))
  86. db.session.commit()
  87. print(f"Created {len(gr)} groups.")
  88. # Clean up test members
  89. for name in "PlanèteCasio GLaDOS".split():
  90. m = Member.query.filter_by(name=name).first()
  91. if m is not None:
  92. m.delete()
  93. print("Removed test members.")
  94. # Create template members
  95. def addgroup(member, group):
  96. g = Group.query.filter_by(name=group).first()
  97. if g is not None:
  98. member.groups.append(g)
  99. m = Member("PlanèteCasio", "contact@planet-casio.com", "v5-forever")
  100. addgroup(m, "Compte communautaire")
  101. db.session.add(m)
  102. m = Member("GLaDOS", "glados@aperture.science", "v5-forever")
  103. m.xp = 1338
  104. addgroup(m, "Robot")
  105. db.session.add(m)
  106. db.session.commit()
  107. db.session.add(SpecialPrivilege(m, "edit-posts"))
  108. db.session.add(SpecialPrivilege(m, "shoutbox-ban"))
  109. db.session.commit()
  110. print(f"Created 2 test members with some privileges.")
  111. def create_trophies():
  112. # Clean up trophies
  113. trophies("clear")
  114. # Create base trophies
  115. tr = []
  116. with open(os.path.join(app.root_path, "data", "trophies.yaml")) as fp:
  117. tr = yaml.safe_load(fp.read())
  118. for t in tr:
  119. if t["is_title"]:
  120. trophy = Title(t["name"], t.get("css", ""))
  121. else:
  122. trophy = Trophy(t["name"])
  123. db.session.add(trophy)
  124. db.session.commit()
  125. print(f"Created {len(tr)} trophies.")
  126. def add_group(member, group):
  127. if group[0] != "#":
  128. print(f"error: group id {group} should start with '#'")
  129. return
  130. gid = int(group[1:])
  131. norm = unicode_names.normalize(member)
  132. g = Group.query.filter_by(id=gid).first()
  133. m = Member.query.filter_by(norm=norm).first()
  134. if m is None:
  135. print(f"error: no member has a normalized name of '{norm}'")
  136. return
  137. m.groups.append(g)
  138. db.session.add(m)
  139. db.session.commit()
  140. #
  141. # Main program
  142. #
  143. print(help_msg)
  144. commands = {
  145. "exit": lambda: sys.exit(0),
  146. "members": members,
  147. "groups": groups,
  148. "trophies": trophies,
  149. "trophy-members": trophy_members,
  150. "create-groups-and-privs": create_groups_and_privs,
  151. "create-trophies": create_trophies,
  152. "add-group": add_group,
  153. }
  154. while True:
  155. try:
  156. print("@> ", end="")
  157. cmd = input().split()
  158. except EOFError:
  159. sys.exit(0)
  160. if not cmd:
  161. continue
  162. if cmd[0] not in commands:
  163. print(f"error: unknown command '{cmd[0]}'")
  164. else:
  165. commands[cmd[0]](*cmd[1:])