def add_membership(user_id): user = get_user_or_404(user_id) form = UserAddGroupMembership() if form.validate_on_submit(): if form.begins_at.date.data: begins_at = datetime.combine(form.begins_at.date.data, utc.time_min()) else: begins_at = session.utcnow() if not form.ends_at.unlimited.data: ends_at = datetime.combine(form.ends_at.date.data, utc.time_min()) else: ends_at = None make_member_of(user, form.group.data, current_user, closed(begins_at, ends_at)) session.session.commit() flash(u'Nutzer wurde der Gruppe hinzugefügt.', 'success') return redirect( url_for(".user_show", user_id=user_id, _anchor='groups')) return render_template( 'user/add_membership.html', page_title=u"Neue Gruppenmitgliedschaft für Nutzer {}".format(user_id), user_id=user_id, form=form)
def user_hosts_json(user_id): user = get_user_or_404(user_id) list_items = [] for host in user.hosts: if host.room: patch_ports = host.room.connected_patch_ports switches = ', '.join( p.switch_port.switch.host.name for p in patch_ports) ports = ', '.join(p.switch_port.name for p in patch_ports) else: switches = None ports = None list_items.append({ 'id': host.id, 'name': host.name, 'switch': switches, 'port': ports, 'actions': [{'href': url_for('.host_edit', host_id=host.id, user_id=user_id), 'title': "Bearbeiten", 'icon': 'fa-edit', 'btn-class': 'btn-link'}, {'href': url_for('.host_delete', host_id=host.id), 'title': "Löschen", 'icon': 'fa-trash', 'btn-class': 'btn-link'}], 'interfaces_table_link': url_for('.interface_table', host_id=host.id), 'interface_create_link': url_for('.interface_create', host_id=host.id), }) return jsonify(items=list_items)
def unblock(user_id): user = get_user_or_404(user_id) try: lib.user.unblock(user=user, processor=current_user) session.session.commit() except ValueError as e: flash(str(e), 'error') else: flash(u'Nutzer entsperrt.', 'success') return redirect(url_for('.user_show', user_id=user_id))
def move(user_id): user = get_user_or_404(user_id) form = UserMoveForm() refill_form_data = False if form.validate_on_submit(): if user.room == Room.q.filter_by( number=form.room_number.data, level=form.level.data, building_id=form.building.data.id).one(): flash(u"Nutzer muss in anderes Zimmer umgezogen werden!", "error") refill_form_data = True else: when = session.utcnow() if form.now.data else datetime.combine( form.when.data, utc.time_min()) _, success = web_execute(lib.user.move, None, user=user, building_id=form.building.data.id, level=form.level.data, room_number=form.room_number.data, processor=current_user, when=when) if success: session.session.commit() if when > session.utcnow(): flash(u'Der Umzug wurde vorgemerkt.', 'success') else: flash(u'Benutzer umgezogen', 'success') sheet = lib.user.store_user_sheet( user, '********', generation_purpose='user moved') session.session.commit() flask_session['user_sheet'] = sheet.id return redirect(url_for('.user_show', user_id=user.id)) if not form.is_submitted() or refill_form_data: if user.room is not None: refill_room_data(form, user.room) return render_template('user/user_move.html', user_id=user_id, form=form)
def move_in(user_id): form = UserMoveInForm() user = get_user_or_404(user_id) if user.room is not None: flash("Nutzer {} ist nicht ausgezogen!".format(user_id), 'error') abort(404) if form.validate_on_submit(): when = session.utcnow() if form.now.data else datetime.combine( form.when.data, utc.time_min()) _, success = web_execute( lib.user.move_in, None, user=user, building_id=form.building.data.id, level=form.level.data, room_number=form.room_number.data, mac=form.mac.data, birthdate=form.birthdate.data, begin_membership=form.begin_membership.data, processor=current_user, when=when, ) if success: session.session.commit() if when > session.utcnow(): flash("Der Einzug wurde vorgemerkt.", 'success') else: flash("Benutzer eingezogen.", 'success') return redirect(url_for('.user_show', user_id=user_id)) if not form.is_submitted(): form.birthdate.data = user.birthdate form.begin_membership.data = True return render_template('user/user_move_in.html', form=form, user_id=user_id)
def user_hosts_json(user_id): user = get_user_or_404(user_id) T = HostTable list_items = [] for host in user.hosts: if host.room: patch_ports = host.room.connected_patch_ports switches = ', '.join(p.switch_port.switch.host.name for p in patch_ports) ports = ', '.join(p.switch_port.name for p in patch_ports) else: switches = None ports = None list_items.append({ 'id': host.id, 'name': host.name, 'switch': switches, 'port': ports, 'actions': [ T.actions.single_value(href=url_for('.host_edit', host_id=host.id, user_id=user_id), title="Bearbeiten", icon='fa-edit', btn_class='btn-link'), T.actions.single_value(href=url_for('.host_delete', host_id=host.id), title="Löschen", icon='fa-trash', btn_class='btn-link') ], 'interfaces_table_link': url_for('.interface_table', host_id=host.id), 'interface_create_link': url_for('.interface_create', host_id=host.id), }) return jsonify(items=list_items)
def edit_user(user_id): user = get_user_or_404(user_id) form = UserEditForm() if not form.is_submitted(): form.name.data = user.name form.email.data = user.email form.birthdate.data = user.birthdate if form.validate_on_submit(): edited_user = lib.user.edit_name(user, form.name.data, current_user) edited_user = lib.user.edit_email(edited_user, form.email.data, current_user) edited_user = lib.user.edit_birthdate(edited_user, form.birthdate.data, current_user) session.session.commit() flash(u'Änderungen gespeichert', 'success') return redirect(url_for('.user_show', user_id=edited_user.id)) return render_template('user/user_edit.html', user_id=user_id, form=form)
def user_show_logs_json(user_id, logtype="all"): user = get_user_or_404(user_id) log_sources = [] # list of iterators if logtype in ["user", "all"]: log_sources.append( (format_user_log_entry(e) for e in user.log_entries)) if logtype in ["room", "all"] and user.room: log_sources.append( (format_room_log_entry(e) for e in user.room.log_entries)) if logtype in ["tasks", "all"]: log_sources.append( (format_task_log_entry(e) for e in user.task_log_entries)) if logtype in ["hades", "all"]: log_sources.append(formatted_user_hades_logs(user)) return jsonify(items=list( sorted(chain(*log_sources), key=operator.itemgetter('raw_created_at'), reverse=True)))
def host_create(): user = get_user_or_404(request.args.get('user_id', None)) form = HostForm(owner_id=user.id) def default_response(): form_args = { 'form': form, 'cancel_to': url_for('user.user_show', user_id=user.id) } return render_template('generic_form.html', page_title="Host erstellen", form_args=form_args, form=form) if not form.is_submitted(): refill_room_data(form, user.room) if not form.validate_on_submit(): return default_response() room = Room.q.filter_by(number=form.room_number.data, level=form.level.data, building=form.building.data).one() owner = User.get(form.owner_id.data) try: with handle_errors(session.session): host = lib_host.host_create(owner, room, form.name.data, processor=current_user) session.session.commit() except PycroftException: return default_response() return redirect( url_for('.interface_create', user_id=host.owner_id, host_id=host.id, _anchor='hosts'))
def move_out(user_id): form = UserMoveOutForm() user = get_user_or_404(user_id) if not user.room: flash("Nutzer {} ist aktuell nirgends eingezogen!".format(user_id), 'error') abort(404) if form.validate_on_submit(): when = session.utcnow() if form.now.data else datetime.combine( form.when.data, utc.time_min()) _, success = web_execute( lib.user.move_out, None, user=user, comment=form.comment.data, processor=current_user, when=session.utcnow() if form.now.data else datetime.combine( form.when.data, utc.time_min()), end_membership=form.end_membership.data) if success: session.session.commit() if when > session.utcnow(): flash("Der Auszug wurde vorgemerkt.", "success") else: flash(u'Benutzer ausgezogen.', 'success') return redirect(url_for('.user_show', user_id=user.id)) if not form.is_submitted(): form.end_membership.data = True return render_template('user/user_move_out.html', form=form, user_id=user_id)
def block(user_id): form = UserSuspendForm() myUser = get_user_or_404(user_id) if form.validate_on_submit(): if form.ends_at.unlimited.data: ends_at = None else: ends_at = datetime.combine(form.ends_at.date.data, utc.time_min()) try: during = closedopen(session.utcnow(), ends_at) blocked_user = lib.user.block(user=myUser, reason=form.reason.data, processor=current_user, during=during, violation=form.violation.data) session.session.commit() except ValueError as e: flash(str(e), 'error') else: flash(u'Nutzer gesperrt.', 'success') return redirect(url_for('.user_show', user_id=user_id)) return render_template('user/user_block.html', form=form, user_id=user_id)
def end_membership(user_id, membership_id): user = get_user_or_404(user_id) membership = Membership.q.get(membership_id) if membership is None: flash( u"Gruppenmitgliedschaft mit ID {} existiert nicht!".format( membership.id), 'error') abort(404) if membership.user.id != user_id: flash( u"Gruppenmitgliedschaft {} gehört nicht zu Nutzer {}!".format( membership.id, user_id), 'error') return abort(404) remove_member_of(user, membership.group, current_user, closedopen(session.utcnow(), None)) session.session.commit() flash(u'Mitgliedschaft in Gruppe beendet', 'success') return redirect( url_for(".user_show", user_id=membership.user_id, _anchor='groups'))
def host_create(): user = get_user_or_404(request.args.get('user_id', None)) form = HostForm(owner_id=user.id) if not form.is_submitted(): refill_room_data(form, user.room) if form.validate_on_submit(): room = Room.q.filter_by(number=form.room_number.data, level=form.level.data, building=form.building.data).one() owner = User.q.filter_by(id=form.owner_id.data).one() host, success = web_execute(lib_host.host_create, "Host erfolgreich erstellt.", owner, room, form.name.data, current_user) if success: session.session.commit() return redirect( url_for('.interface_create', user_id=host.owner_id, host_id=host.id, _anchor='hosts')) form_args = { 'form': form, 'cancel_to': url_for('user.user_show', user_id=user.id) } return render_template('generic_form.html', page_title="Host erstellen", form_args=form_args, form=form)
def user_account(user_id): user = get_user_or_404(user_id) return redirect( url_for("finance.accounts_show", account_id=user.account_id))
def user_show(user_id): user = get_user_or_404(user_id) room = user.room form = UserLogEntry() if form.validate_on_submit(): lib.logging.log_user_event(form.message.data, author=current_user, user=user) session.session.commit() flash(u'Kommentar hinzugefügt', 'success') balance = user.account.balance _log_endpoint = partial(url_for, ".user_show_logs_json", user_id=user.id) _membership_endpoint = partial(url_for, ".user_show_groups_json", user_id=user.id) _finance_table_kwargs = { 'data_url': url_for("finance.accounts_show_json", account_id=user.account_id), 'user_id': user.id, 'table_args': { 'data-page-size': 5 }, 'inverted': True, 'saldo': balance, } is_blocked = False for group in get_blocked_groups(): if user.member_of(group): is_blocked = True user_not_there = not user.member_of(config.member_group) try: if flask_session['user_sheet'] and lib.user.get_user_sheet( flask_session['user_sheet']): flash( Markup( u'Es ist ein <a href="{}" target="_blank">Nutzerdatenblatt</a> verfügbar!' .format(url_for('.user_sheet')))) except KeyError: pass return render_template( 'user/user_show.html', # Q: Can ports of a room point to different access switches? user=user, user_not_there=user_not_there, user_id_new=encode_type2_user_id(user.id), user_id_old=encode_type1_user_id(user.id), balance=-balance, hosts_rooms={host.room for host in user.hosts}, log_table_all=LogTableExtended(data_url=_log_endpoint()), log_table_user=LogTableSpecific(data_url=_log_endpoint( logtype="user")), log_table_room=LogTableSpecific(data_url=_log_endpoint( logtype="room")), log_table_hades=LogTableSpecific(data_url=_log_endpoint( logtype="hades")), log_table_tasks=LogTableSpecific(data_url=_log_endpoint( logtype="tasks")), membership_table_all=MembershipTable( user_id=user.id, data_url=_membership_endpoint(), ), membership_table_active=MembershipTable( user_id=user.id, data_url=_membership_endpoint(group_filter="active"), ), host_table=HostTable(data_url=url_for("host.user_hosts_json", user_id=user.id), user_id=user.id), task_table=TaskTable(data_url=url_for("task.json_tasks_for_user", user_id=user.id), hidden_columns=['user']), finance_table_regular=FinanceTable(**_finance_table_kwargs), finance_table_splitted=FinanceTableSplitted(**_finance_table_kwargs), room=room, form=form, flags=infoflags(user), json_url=url_for("finance.accounts_show_json", account_id=user.account_id), is_blocked=is_blocked, granted_properties=sorted(p.property_name for p in user.current_properties), revoked_properties=sorted( p.property_name for p in set(user.current_properties_maybe_denied) - set(user.current_properties)))
def json_tasks_for_user(user_id): user = get_user_or_404(user_id) return jsonify(items=[task_row(task) for task in user.tasks])