def edit_package(name): package = db.query(Package).filter_by(name=name).first_or_404() form = request.form try: for key, prev_val in form.items(): if key.startswith('group-prev-'): group = db.query(PackageGroup).get_or_404(int(key[len('group-prev-'):])) new_val = form.get('group-{}'.format(group.id)) if bool(new_val) != (prev_val == 'true'): if not group.editable: abort(403) if new_val: rel = PackageGroupRelation(package_name=package.name, group_id=group.id) db.add(rel) else: db.query(PackageGroupRelation)\ .filter_by(group_id=group.id, package_name=package.name)\ .delete(synchronize_session=False) if 'manual_priority' in form: new_priority = int(form['manual_priority']) package.manual_priority = new_priority if 'arch_override' in form: package.arch_override = form['arch_override'].strip() or None flash("Package modified") except (KeyError, ValueError): abort(400) db.commit() return redirect(url_for('package_detail', name=package.name))
def edit_package(name): package = db.query(Package).filter_by(name=name).first_or_404() form = request.form try: for key, prev_val in form.items(): if key.startswith('group-prev-'): group = db.query(PackageGroup).get_or_404( int(key[len('group-prev-'):])) new_val = form.get('group-{}'.format(group.id)) if bool(new_val) != (prev_val == 'true'): if not group.editable: abort(403) if new_val: rel = PackageGroupRelation(package_name=package.name, group_id=group.id) db.add(rel) else: db.query(PackageGroupRelation)\ .filter_by(group_id=group.id, package_name=package.name)\ .delete(synchronize_session=False) if 'manual_priority' in form: new_priority = int(form['manual_priority']) package.manual_priority = new_priority if 'arch_override' in form: package.arch_override = form['arch_override'].strip() or None flash("Package modified") except (KeyError, ValueError): abort(400) db.commit() return redirect(url_for('package_detail', name=package.name))
def create_or_login(response): flask.session["openid"] = response.identity_url username = openid_to_username(response.identity_url) user = get_or_create(db, User, name=username) user.email = response.email user.timezone = response.timezone db.commit() flask.g.user = user return flask.redirect(openid.get_next_url())
def process_group_form(group=None): if request.method == 'GET': if group: obj = dict(name=group.name, owners=[u.name for u in group.owners], packages=[p.name for p in group.packages]) form = GroupForm(**obj) else: form = GroupForm(owners=[g.user.name]) return render_template('edit-group.html', group=group, form=form) form = GroupForm() # check permissions if group and not group.editable: flash("You don't have permission to edit this group") return redirect( url_for('group_detail', name=group.name, namespace=group.namespace)) # check form validity if not form.validate_or_flash(): return render_template('edit-group.html', group=group, form=form) names = set(form.packages.data) users = [ get_or_create(db, User, name=name) for name in set(form.owners.data) ] db.commit() user_ids = [u.id for u in users] packages = db.query(Package).filter(Package.name.in_(names)) found_names = {p.name for p in packages} if len(found_names) != len(names): flash("Packages don't exist: " + ', '.join(names - found_names)) return render_template('edit-group.html', group=group, form=form) created = not group if created: group = PackageGroup(namespace=g.user.name) db.add(group) group.name = form.name.data try: db.flush() except IntegrityError: db.rollback() flash("Group already exists") return render_template('edit-group.html', group=group, form=form) if not created: db.query(PackageGroupRelation)\ .filter_by(group_id=group.id).delete() db.query(GroupACL)\ .filter_by(group_id=group.id).delete() rels = [dict(group_id=group.id, package_name=name) for name in found_names] acls = [dict(group_id=group.id, user_id=user_id) for user_id in user_ids] db.execute(PackageGroupRelation.__table__.insert(), rels) db.execute(GroupACL.__table__.insert(), acls) db.commit() flash("Group created" if created else "Group modified") return redirect( url_for('group_detail', name=group.name, namespace=group.namespace))
def delete_group(name, namespace=None): group = db.query(PackageGroup)\ .options(joinedload(PackageGroup.packages))\ .filter_by(name=name, namespace=namespace).first_or_404() if request.method == 'POST': if EmptyForm().validate_or_flash() and group.editable: db.delete(group) db.commit() return redirect(url_for('groups_overview')) return render_template('edit-group.html', group=group) return redirect(url_for('groups_overview'))
def delete_group(name, namespace=None): group = db.query(PackageGroup)\ .options(joinedload(PackageGroup.packages))\ .filter_by(name=name, namespace=namespace).first_or_404() if request.method == 'POST': if EmptyForm().validate_or_flash() and group.editable: db.delete(group) db.commit() return redirect(url_for('groups_overview')) return render_template('edit-group.html', group=group) return redirect(url_for('groups_overview'))
def create_or_login(response): flask.session["openid"] = response.identity_url username = openid_to_username(response.identity_url) user = db.query(User).filter_by(name=username).first() if not user: user = User(name=username) db.add(user) user.email = response.email user.timezone = response.timezone db.commit() flask.g.user = user return flask.redirect(openid.get_next_url())
def create_or_login(response): flask.session["openid"] = response.identity_url username = openid_to_username(response.identity_url) user = db.query(User).filter_by(name=username).first() if not user: user = User(name=username) db.add(user) user.email = response.email user.timezone = response.timezone db.commit() flask.g.user = user return flask.redirect(openid.get_next_url())
def process_group_form(group=None): if request.method == 'GET': if group: obj = dict(name=group.name, owners=[u.name for u in group.owners], packages=[p.name for p in group.packages]) form = GroupForm(**obj) else: form = GroupForm(owners=[g.user.name]) return render_template('edit-group.html', group=group, form=form) form = GroupForm() # check permissions if group and not group.editable: flash("You don't have permission to edit this group") return redirect(url_for('group_detail', name=group.name, namespace=group.namespace)) # check form validity if not form.validate_or_flash(): return render_template('edit-group.html', group=group, form=form) names = set(form.packages.data) users = [get_or_create(db, User, name=name) for name in set(form.owners.data)] db.commit() user_ids = [u.id for u in users] packages = db.query(Package).filter(Package.name.in_(names)) found_names = {p.name for p in packages} if len(found_names) != len(names): flash("Packages don't exist: " + ', '.join(names - found_names)) return render_template('edit-group.html', group=group, form=form) created = not group if created: group = PackageGroup(namespace=g.user.name) db.add(group) group.name = form.name.data try: db.flush() except IntegrityError: db.rollback() flash("Group already exists") return render_template('edit-group.html', group=group, form=form) if not created: db.query(PackageGroupRelation)\ .filter_by(group_id=group.id).delete() db.query(GroupACL)\ .filter_by(group_id=group.id).delete() rels = [dict(group_id=group.id, package_name=name) for name in found_names] acls = [dict(group_id=group.id, user_id=user_id) for user_id in user_ids] db.execute(PackageGroupRelation.__table__.insert(), rels) db.execute(GroupACL.__table__.insert(), acls) db.commit() flash("Group created" if created else "Group modified") return redirect(url_for('group_detail', name=group.name, namespace=group.namespace))
def cancel_build(build_id): if not g.user.admin: abort(403) build = db.query(Build).filter_by(id=build_id).first_or_404() if EmptyForm().validate_or_flash(): if build.state != Build.RUNNING: flash("Only running builds can be canceled.") elif build.cancel_requested: flash("Build already has pending cancelation request.") else: flash("Cancelation request sent.") build.cancel_requested = True db.commit() return redirect(url_for('package_detail', name=build.package.name))
def cancel_build(build_id): if not g.user.admin: abort(403) build = db.query(Build).filter_by(id=build_id).first_or_404() if EmptyForm().validate_or_flash(): if build.state != Build.RUNNING: flash("Only running builds can be canceled.") elif build.cancel_requested: flash("Build already has pending cancelation request.") else: flash("Cancelation request sent.") build.cancel_requested = True db.commit() return redirect(url_for('package_detail', name=build.package.name))
def add_packages(): form = AddPackagesForm() if request.method == 'POST': if not form.validate_or_flash(): return render_template("add-packages.html", form=form) names = set(form.packages.data) existing = db.query(Package).filter(Package.name.in_(names)).all() nonexistent = names - {p.name for p in existing} if nonexistent: flash("Packages don't exist: " + ','.join(nonexistent)) return render_template("add-packages.html", form=form) if form.collection.data == '_all': coll_ids = [c.id for c in g.collections] else: coll_ids = [ c.id for c in g.collections if c.name == form.collection.data ] if not coll_ids: abort(404) db.query(Package).filter(Package.name.in_(names))\ .filter(Package.collection_id.in_(coll_ids))\ .update({'tracked': True}) if form.group.data: name, _, namespace = reversed(form.group.data.partition('/')) group = db.query(PackageGroup)\ .filter_by(namespace=namespace or None, name=name)\ .first() or abort(400) if not group.editable: abort(400) rels = [ dict(group_id=group.id, package_name=name) for name in names ] if rels: db.execute(PackageGroupRelation.__table__.insert(), rels) added = ' '.join(names) log.info("%s added %s", g.user.name, added) flash("Packages added: {}".format(added)) db.commit() return redirect(request.form.get('next') or url_for('frontpage')) return render_template("add-packages.html", form=form)
def login(): if bypass_login: identity = "none" user_name = bypass_login else: identity = request.environ.get('REMOTE_USER') or abort(501) user_name = re.match(user_re, identity).group(1) user = db.query(m.User).filter_by(name=user_name).first() if not user: user = m.User(name=user_name, admin=bool(bypass_login)) db.add(user) db.commit() flash('New user "{}" was registered.'.format(user_name)) session['user'] = user_name flash('Logged in as user "{}" with identity "{}".' .format(user_name, identity)) if user.admin: flash('You have admin privileges.') next_url = request.values.get("next", url_for('frontpage')) return redirect(next_url)
def login(): if bypass_login: identity = "none" user_name = bypass_login else: identity = request.environ.get('REMOTE_USER') or abort(501) user_name = re.match(user_re, identity).group(1) user = db.query(m.User).filter_by(name=user_name).first() if not user: user = m.User(name=user_name, admin=bool(bypass_login)) db.add(user) db.commit() flash('New user "{}" was registered.'.format(user_name)) session['user'] = user_name flash('Logged in as user "{}" with identity "{}".'.format( user_name, identity)) if user.admin: flash('You have admin privileges.') next_url = request.values.get("next", url_for('frontpage')) return redirect(next_url)
def add_packages(): form = AddPackagesForm() if request.method == 'POST': if not form.validate_or_flash(): return render_template("add-packages.html", form=form) names = set(form.packages.data) existing = db.query(Package).filter(Package.name.in_(names)).all() nonexistent = names - {p.name for p in existing} if nonexistent: flash("Packages don't exist: " + ','.join(nonexistent)) return render_template("add-packages.html", form=form) if form.collection.data == '_all': coll_ids = [c.id for c in g.collections] else: coll_ids = [c.id for c in g.collections if c.name == form.collection.data] if not coll_ids: abort(404) db.query(Package).filter(Package.name.in_(names))\ .filter(Package.collection_id.in_(coll_ids))\ .update({'tracked': True}) if form.group.data: name, _, namespace = reversed(form.group.data.partition('/')) group = db.query(PackageGroup)\ .filter_by(namespace=namespace or None, name=name)\ .first() or abort(400) if not group.editable: abort(400) rels = [dict(group_id=group.id, package_name=name) for name in names] if rels: db.execute(PackageGroupRelation.__table__.insert(), rels) added = ' '.join(names) log.info("%s added %s", g.user.name, added) flash("Packages added: {}".format(added)) db.commit() return redirect(request.form.get('next') or url_for('frontpage')) return render_template("add-packages.html", form=form)