master: update groups without deleting them

This commit is contained in:
Lephe 2021-02-20 17:34:35 +01:00
parent 85dfde3811
commit c8661ca50f
Signed by: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
1 changed files with 46 additions and 37 deletions

View File

@ -10,6 +10,7 @@ import os
import sys
import yaml
import slugify
import readline
from PIL import Image
@ -27,23 +28,11 @@ Type a category name to see a list of elements. Available categories are:
For each category, an argument can be specified:
* 'clear' will remove all entries in the category (destroys a lot of data!)
* 'update' will update from the model in app/data/, when applicable
(currently available on: forums)
Type 'create-groups-and-privs' to recreate all groups and privileges to the
default. This function generates a minimal set of groups and members to prepare
the database.
1. Deletes all groups
2. Creates groups 'Administrateur', 'Modérateur', 'Développeur', 'Rédacteur',
'Responsable Communauté', 'Partenaire', 'Compte communautaire', 'Robot', and
'Membre de CreativeCalc'
3. Grants privileges related to these groups
4. Recreates common accounts: 'Planète Casio' (community account) and 'GLaDOS'
(robot)
(currently available on: forums, groups)
Type 'create-common-accounts' to recreate 'Planète Casio' and 'GLaDOS'
Type 'add-group <member> #<group-id>' to add a new member to a group.
Type 'create-trophies' to reset trophies and titles and their icons.
Type 'enable-user' to enable a email-disabled account.
"""
@ -70,6 +59,10 @@ def groups(*args):
print("Removed all groups.")
return
if args == ("update",):
update_groups()
return
for g in Group.query.all():
print(f"#{g.id} {g.name}")
@ -114,37 +107,56 @@ def forums(*args):
# Creation and edition
#
def create_groups_and_privs():
# Clean up groups
groups("clear")
def update_groups():
existing = Group.query.all()
# Create base groups
gr = []
with open(os.path.join(app.root_path, "data", "groups.yaml")) as fp:
gr = yaml.safe_load(fp.read())
for g in gr:
g["obj"] = Group(g["name"], g["css"], g["descr"])
db.session.add(g["obj"])
db.session.commit()
for group_info in gr:
name = group_info["name"]
css = group_info.get("css", "")
descr = group_info.get("descr", "")
privs = group_info.get("privs", "").split()
for g in gr:
for priv in g.get("privs", "").split():
db.session.add(GroupPrivilege(g["obj"], priv))
db.session.commit()
g = Group.query.filter_by(name=name).first()
print(f"Created {len(gr)} groups.")
# Update an existing group
if g is not None:
changes = (g.css != css) or (g.description != descr) or \
(set(g.privs()) != set(privs))
g.css = css
g.description = descr
# Clean up test members
for gpriv in GroupPrivilege.query.filter_by(gid=g.id):
db.session.delete(gpriv)
for priv in privs:
db.session.add(GroupPrivilege(g, priv))
if changes:
db.session.add(g)
print(f"[group] Updated {g.name}")
# Create a new one
else:
g = Group(name, css, descr)
db.session.add(g)
db.session.commit()
for priv in privs:
db.session.add(GroupPrivilege(g, priv))
print(f"[group] Created {g.name}")
def create_common_accounts():
# Clean up common accounts
for name in "PlanèteCasio GLaDOS".split():
m = Member.query.filter_by(name=name).first()
if m is not None:
m.delete()
print("Removed test members.")
# Create template members
# Recreate theme
def addgroup(member, group):
g = Group.query.filter_by(name=group).first()
if g is not None:
@ -167,8 +179,6 @@ def create_groups_and_privs():
db.session.commit()
print(f"Created 2 test members with some privileges.")
def create_trophies():
# Clean up trophies
@ -315,7 +325,7 @@ commands = {
"trophies": trophies,
"trophy-members": trophy_members,
"forums": forums,
"create-groups-and-privs": create_groups_and_privs,
"create-common-accounts": create_common_accounts,
"create-trophies": create_trophies,
"create-trophies-icons": create_trophies_icons,
"add-group": add_group,
@ -338,8 +348,7 @@ else:
while True:
try:
print("@> ", end="")
cmd = input().split()
cmd = input("@> ").split()
except EOFError:
sys.exit(0)