def updateGroupAltNames(group, altnames, deleteother=True): print("Alt names:", altnames) altnames = [name.strip() for name in altnames] cleaned = {} for name in altnames: if name.lower().strip(): cleaned[name.lower().strip()] = name havenames = AlternateTranslatorNames.query.filter(AlternateTranslatorNames.group==group.id).order_by(AlternateTranslatorNames.name).all() havenames = {name.name.lower().strip() : name for name in havenames} for name in cleaned.keys(): if name in havenames: havenames.pop(name) else: newname = AlternateTranslatorNames( name = bleach.clean(cleaned[name], strip=True), cleanname = nt.prepFilenameForMatching(cleaned[name]), group = group.id, changetime = datetime.datetime.now(), changeuser = getCurrentUserId() ) db.session.add(newname) if deleteother: for key, value in havenames.items(): db.session.delete(value) db.session.commit()
def add_group(form): name = form.name.data.strip() have = AlternateTranslatorNames.query.filter( AlternateTranslatorNames.cleanname == nt.prepFilenameForMatching( name)).scalar() if have: flash(gettext('Group already exists!')) return redirect(url_for('renderGroupId', sid=have.group)) else: new = Translators( name=name, changetime=datetime.datetime.now(), changeuser=g.user.id, ) db.session.add(new) db.session.commit() newname = AlternateTranslatorNames( name=name, cleanname=nt.prepFilenameForMatching(name), group=new.id, changetime=datetime.datetime.now(), changeuser=g.user.id) db.session.add(newname) db.session.commit() flash(gettext('Group Created!')) return redirect(url_for('renderGroupId', sid=new.id))
def get_create_group(groupname, changeuser): groupname = groupname[:500] cleanName = nt.prepFilenameForMatching(groupname) # If the group name collapses down to nothing when cleaned, search for it without cleaning. if len(cleanName): have = AlternateTranslatorNames.query.filter( AlternateTranslatorNames.cleanname == cleanName).all() else: have = AlternateTranslatorNames.query.filter( AlternateTranslatorNames.name == groupname).all() if not have: print("Need to create new translator entry for ", groupname) new = Translators(name=groupname, changeuser=changeuser, changetime=datetime.datetime.now()) db.session.add(new) db.session.commit() newalt = AlternateTranslatorNames( group=new.id, name=new.name, cleanname=nt.prepFilenameForMatching(new.name), changetime=datetime.datetime.now(), changeuser=changeuser, ) db.session.add(newalt) db.session.commit() return new else: if len(have) == 1: group = have[0] assert group.group_row is not None, ( "Wat? Row: '%s', '%s', '%s'" % (group.id, group.name, group.group_row)) elif len(have) > 1: group = pick_best_match(have, groupname) else: raise ValueError("Wat for groupname: '%s'" % groupname) row = group.group_row return row