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.

master.py 6.7KB

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