def promote(role_name: str=None) -> str: """Promote the user accessing this page.""" if not current_user().is_authenticated: abort(401, 'You need to be logged in to promote an account!') part = Participant.get_from_user(current_user()) if part and part.role.name == 'Owner' and g.queue.get_num_owners() <= 1: abort(401, 'You cannot demote yourself from owner until another owner' ' has been added.') if not role_name: return render_queue( 'roles.html', title='Promotion Form', message='Welcome. Please select a role below.', roles=g.queue.get_roles_for_promotion()) form = PromotionForm(request.form) if request.method == 'POST' or g.queue.get_code_for_role(role_name) == '*': if not g.queue.is_promotion_valid(role_name, request.form['code']): form.errors.setdefault('code', []).append('Incorrect code.') return render_queue( 'form.html', form=form, submit='Promote', back=url_for('queue.promote')) Participant.update_or_create(current_user(), role_name) return render_queue( 'confirm.html', title='Promotion Success', message='You have been promoted to %s' % role_name, action='Onward', url=url_for('admin.home')) return render_queue( 'form.html', form=form, submit='Promote', back=url_for('queue.promote'))
def pull_queue_url(endpoint, values): g.queue_url = values.pop('queue_url') g.queue = Queue.query.filter_by(url=g.queue_url).one_or_none() if not g.queue: abort(404) if current_user().is_authenticated: g.participant = Participant.query.filter_by(user_id=current_user().id, queue_id=g.queue.id).one_or_none()
def pull_queue_url(endpoint: str, values: dict) -> None: """Pull values from URL automatically.""" g.queue_url = values.pop('queue_url') g.queue = Queue.query.filter_by(url=g.queue_url).one_or_none() if not g.queue: abort(404) if current_user().is_authenticated: g.participant = Participant.query.filter_by( user_id=current_user().id, queue_id=g.queue.id).one_or_none()
def create_queue(): """create queue form""" form = QueueForm(request.form) if request.method == 'POST' and form.validate(): queue = Queue.from_request().save().load_roles( default_queue_roles[request.form['category']]).save() current_user().join(queue, role='Owner') queue.load_settings('whitelist') return redirect(url_for('queue.home', queue_url=queue.url)) return render_dashboard('form_public.html', title='Create Queue', submit='create', form=form)
def maybe_promote_current_user(): """Whitelist the current user if the user is on the whitelist.""" whitelist = g.queue.setting('whitelist').value if whitelist: entries = {} for entry in whitelist.split(','): entry = tuple(s.strip() for s in entry.split('(')) if len(entry) == 2: entries[entry[0]] = entry[1][:-1] else: entries[entry[0]] = 'Staff' if current_user().is_authenticated and \ current_user().email in entries: current_user().set_role(entries[current_user().email])
def help_latest(location=None, category=None): """automatically selects next inquiry""" if not category or category == 'all': inquiry = Inquiry.latest(location=location) else: inquiry = Inquiry.latest(location=location, category=category) delayed_id, delayed = request.args.get('delayed_id', None), None if not inquiry: return redirect(url_for('admin.home', notification=NOTIF_HELP_DONE)) if g.queue.setting('inquiry_types').enabled and not category and g.queue.setting('inquiry_type_selection').enabled: categories = [(cat, Inquiry.query.filter_by( category=cat, status='unresolved', location=location, queue_id=g.queue.id).count()) for cat in g.queue.setting('inquiry_types').value.split(',')] categories = [c for c in categories if c[1]] if len(categories) > 1: return render_admin('categories.html', title='Request Type', location=location, categories=categories) if delayed_id: delayed = Inquiry.query.get(delayed_id) delayed.unlock() inquiry.lock() inquiry.link(current_user()) return redirect(url_for('admin.help_inquiry', id=inquiry.id, location=location))
def home() -> str: """List all unresolved inquiries for the homepage.""" if current_user().can('help'): return redirect(url_for('admin.home')) return render_queue( 'landing.html', num_inquiries=Inquiry.get_num_unresolved(), ttr=g.queue.ttr())
def home(): """list all unresolved inquiries for the homepage""" if current_user().can('help'): return redirect(url_for('admin.home')) return render_queue('landing.html', num_inquiries=Inquiry.query.filter_by( status='unresolved', queue_id=g.queue.id).count(), ttr=g.queue.ttr())
def inquiry() -> str: """Place a new request. This request which may be authored by either a system user or an anonymous user. """ user = current_user() if not user.is_authenticated and \ g.queue.setting(name='require_login').enabled: return render_queue( 'confirm.html', title='Login Required', message='Login to add an inquiry, and start using this queue.') form = InquiryForm(request.form, obj=user) n = int(g.queue.setting(name='max_requests').value) if User.get_num_current_requests(request.form.get('name', None)) >= n: if not current_user().is_authenticated: message = 'If you haven\'t submitted a request, try' ' logging in and re-requesting.' else: message = 'Would you like to cancel your oldest request?' return render_queue( 'confirm.html', title='Oops', message='Looks like you\'ve reached the maximum number of times ' 'you can add yourself to the queue at once (<code>%d</code>). ' '%s' % (n, message), action='Cancel Oldest Request', url=url_for('queue.cancel')) form.location.choices = choicify(g.queue.setting('locations').value) form.category.choices = choicify(g.queue.setting('inquiry_types').value) if request.method == 'POST' and form.validate() and \ g.queue.is_valid_assignment(request, form): inquiry = Inquiry(**request.form).update(queue_id=g.queue.id) if current_user().is_authenticated: inquiry.owner_id = current_user().id inquiry.save() emitQueueInfo(g.queue) return redirect(url_for('queue.waiting', inquiry_id=inquiry.id)) return render_queue( 'form.html', form=form, title='Request Help', submit='Request Help')
def inquiry(): """ Place a new request, which may be authored by either a system user or an anonymous user. """ user, form = flask_login.current_user, InquiryForm(request.form) if user.is_authenticated: form = InquiryForm(request.form, obj=user) elif g.queue.setting(name='require_login').enabled: return render_queue('confirm.html', title='Login Required', message='Login to add an inquiry, and start using this queue.') n = int(g.queue.setting(name='max_requests').value) filter_id = User.email == current_user().email if \ current_user().is_authenticated else User.name == request.form.get('name', None) not_logged_in_max = '' if Inquiry.query.join(User).filter( filter_id, Inquiry.status=='unresolved', Inquiry.queue_id==g.queue.id ).count() >= n: if not current_user().is_authenticated: not_logged_in_max = 'If you haven\'t submitted a request, try logging in and re-requesting.' return render_queue('confirm.html', title='Oops', message='Looks like you\'ve reached the maximum number of times you can add yourself to the queue at once (<code>%d</code>). %s' % (n, not_logged_in_max or 'Would you like to cancel your oldest request?'), action='Cancel Oldest Request', url=url_for('queue.cancel')) form.location.choices = choicify( g.queue.setting('locations').value.split(',')) form.category.choices = choicify( g.queue.setting('inquiry_types').value.split(',')) if request.method == 'POST' and form.validate() and \ g.queue.is_valid_assignment(request, form): inquiry = Inquiry(**request.form) inquiry.queue_id = g.queue.id if current_user().is_authenticated: inquiry.owner_id = current_user().id inquiry.save() emitQueueInfo(g.queue) return redirect(url_for('queue.waiting', inquiry_id=inquiry.id)) return render_queue('form.html', form=form, title='Request Help', submit='Request Help')
def render_queue(template, *args, **kwargs): """Special rendering for queue""" whitelist = g.queue.setting('whitelist').value if whitelist: entries = {} for entry in whitelist.split(','): entry = tuple(s.strip() for s in entry.split('(')) if len(entry) == 2: entries[entry[0]] = entry[1][:-1] else: entries[entry[0]] = 'Staff' if current_user().is_authenticated and \ current_user().email in entries: current_user().set_role(entries[current_user().email]) for k in default_queue_settings: setting = g.queue.setting(k) kwargs.update({'q_%s' % k: (setting.value or setting.enabled) if setting.enabled else False }) kwargs.setdefault('queue', g.queue) return render(template, *args, **kwargs)
def cancel(inquiry_id=None): """Cancel placed request""" inquiry = Inquiry.query.get(inquiry_id) if inquiry_id else Inquiry.query.filter_by( owner_id=current_user().id, status='unresolved', queue_id=g.queue.id).first() anon = not current_user().is_authenticated and not inquiry.owner_id non_anon = current_user().is_authenticated and inquiry.owner_id == current_user().id if anon or non_anon: inquiry.update(status='closed').save() else: return render_queue('error.html', code='404', message='You cannot cancel another user\'s request. This incident has been logged.', url=url_for('queue.home'), action='Back Home') emitQueuePositions(inquiry) emitQueueInfo(inquiry.queue) return redirect(url_for('queue.home'))
def help_latest(location: str=None, category: str=None) -> str: """Automatically select next inquiry.""" if g.queue.show_inquiry_types() and not category: categories = Inquiry.get_categories_unresolved(location=location) if len(categories) > 1: return render_admin( 'categories.html', title='Request Type', location=location, categories=categories) inquiry = Inquiry.get_current_or_latest( location=location, category=category) Inquiry.maybe_unlock_delayed() if not inquiry: return redirect(url_for('admin.home', notification=NOTIF_HELP_DONE)) inquiry.lock().link(current_user()) return redirect(url_for( 'admin.help_inquiry', id=inquiry.id, location=location))
def help_inquiry(id, location=None): """automatically selects next inquiry or reloads inquiry """ inquiry = Inquiry.query.get(id) if not inquiry.resolution: inquiry.lock() inquiry.link(current_user()) if request.method == 'POST': delayed_id=None inquiry.resolution.close() # emit new queue positions emitQueuePositions(inquiry) emitQueueInfo(inquiry.queue) if request.form['status'] == 'unresolved': delayed_id = inquiry.id else: inquiry.close() if request.form['load_next'] != 'y': if delayed_id: delayed = Inquiry.query.get(delayed_id) delayed.unlock() return redirect(url_for('admin.home')) if not location: return redirect(url_for('admin.help_latest', delayed_id=delayed_id)) return redirect(url_for('admin.help_latest', location=location, delayed_id=delayed_id)) return render_admin('help_inquiry.html', inquiry=inquiry, inquiries=Inquiry.query.filter_by(name=inquiry.name).limit(10).all(), hide_event_nav=True, group=Inquiry.query.filter( Inquiry.status == 'unresolved', Inquiry.queue_id == g.queue.id, Inquiry.assignment == inquiry.assignment, Inquiry.problem == inquiry.problem, Inquiry.owner_id != inquiry.owner_id ).all(), wait_time=strfdelta( inquiry.resolution.created_at-inquiry.created_at, '%h:%m:%s'))
def waiting(inquiry_id=None): """Screen shown after user has placed request and is waiting""" if inquiry_id: current_inquiry = Inquiry.query.get(inquiry_id) else: current_inquiry = Inquiry.query.filter_by( owner_id=current_user().id, status='unresolved', queue_id=g.queue.id).first() return render_queue('waiting.html', position=Inquiry.query.filter( Inquiry.status == 'unresolved', Inquiry.queue_id == g.queue.id, Inquiry.created_at <= current_inquiry.created_at ).count(), details='Location: %s, Assignment: %s, Problem: %s, Request: %s' % (current_inquiry.location, current_inquiry.assignment, current_inquiry.problem, current_inquiry.created_at.humanize()), group=Inquiry.query.filter( Inquiry.status == 'unresolved', Inquiry.queue_id == g.queue.id, Inquiry.assignment == current_inquiry.assignment, Inquiry.problem == current_inquiry.problem, Inquiry.id != current_inquiry.id ).all(), inquiry=current_inquiry)
def home(): """User dashboard, contains all involved queues.""" return render_dashboard( 'dashboard/index.html', queues=current_user().queues(), empty='You aren\'t participating in any queues!')
def home(): """user dashboard""" return render_dashboard('dashboard/index.html', queues=current_user().queues(), empty='You aren\'t participating in any queues!')
def promote(role_name=None): """Promote the user accessing this page.""" if not current_user().is_authenticated: return render_queue('error.html', code='Oops', message='You need to be logged in to promote an account!') part = Participant.query.filter_by( queue_id=g.queue.id, user_id=current_user().id ).one_or_none() n_owners = Participant.query.join(QueueRole).filter( QueueRole.name == 'Owner', Participant.queue_id == g.queue.id ).count() if part and part.role.name == 'Owner' and n_owners <= 1: return render_queue('error.html', code='Oops', message='You cannot demote yourself from owner until another owner has been added.') promotion_setting = g.queue.setting(name='self_promotion', default=None) if not promotion_setting or not promotion_setting.enabled: abort(404) tuples = [s.split(':') for s in promotion_setting.value.splitlines()] codes = dict((k.strip().lower(), v.strip()) for k, v in tuples) role_names = [role.name for role in g.queue.roles] if n_owners == 0: role_name = 'Owner' roles = [s.lower() for s in role_names + list(codes.keys())] if role_name and role_name.lower() not in roles: abort(404) if not role_name: return render_queue('roles.html', title='Promotion Form', message='Welcome. Please select a role below.', roles=[name for name in role_names if name.lower() in codes]) try: if n_owners == 0: code = '*' else: code = codes[role_name.lower()] except KeyError: abort(404) form = PromotionForm(request.form) if request.method == 'POST' or code == '*': if not (code == '*' or request.form['code'] == code): form.errors.setdefault('code', []).append('Incorrect code.') return render_queue('form.html', form=form, submit='Promote', back=url_for('queue.promote')) role = QueueRole.query.filter_by( name=role_name, queue_id=g.queue.id).one() part = Participant.query.filter_by( user_id=current_user().id, queue_id=g.queue.id).one_or_none() if part: part.update(role_id = role.id).save() else: Participant( user_id=current_user().id, queue_id=g.queue.id, role_id=role.id ).save() return render_queue('confirm.html', title='Promotion Success', message='You have been promoted to %s' % role_name, action='Onward', url=url_for('admin.home')) return render_queue('form.html', form=form, submit='Promote', back=url_for('queue.promote'))