def add_group_form(form): """ Create a new Group table :param form: :return: if the group can be added, return the id of the group, else return False """ name = form.name.data if Group.from_name(name) is not None: flash("Ce groupe existe déjà", "danger") return False is_private = (form.is_private.data == 'private') password = form.password.data if not password and is_private: flash("Vous devez mettre un mot de passe pour un groupe privé", "danger") return False elif len(password) > 10: flash("Veuillez rentrer un mot de passe de moins de 10 caractères", "warning") return False db.session.add( Group(name=name, is_private=is_private, password=password, manager_id=current_user.id)) group_id = Group.from_name(name).id db.session.add(Participate(group_id=group_id, member_id=current_user.id)) db.session.commit() return group_id
def loaddb_groups(filename): """ Populates the database with groups and group-related relationships from a yml file. Requires loaddb_users. """ groups = yaml.safe_load(open(filename)) # premier tour de boucle, creation des groupes for g in groups: if Group.from_name(g["name"]) is None: group_object = Group(name=g["name"], is_private=g["is_private"], password=g["password"], manager_id=User.from_username( g["manager"]).id) db.session.add(group_object) print("V", g["name"]) else: print("X", g["name"]) db.session.commit() # deuxieme tour de boucle, creation des relations UserXGroup for g in groups: g_id = Group.from_name(g["name"]).id load_relationship(g, 'group_id', g_id, 'members', Participate, User.from_username, 'member_id') db.session.commit()