From 63bec7d84cbf1acd3a9a357b58b47584b1701229 Mon Sep 17 00:00:00 2001 From: Sawyer Bergeron Date: Thu, 23 May 2019 13:30:37 -0400 Subject: Refactor selector step logic Change-Id: I61e361e63da7453b2eee0e0c162a6f4e48460128 Signed-off-by: Sawyer Bergeron --- src/booking/forms.py | 19 ++++++++----------- src/booking/lib.py | 36 ++++++++++++++++++++++++++++++++++++ src/booking/quick_deployer.py | 9 ++------- src/booking/views.py | 2 +- 4 files changed, 47 insertions(+), 19 deletions(-) create mode 100644 src/booking/lib.py (limited to 'src/booking') diff --git a/src/booking/forms.py b/src/booking/forms.py index de427ab..e48b293 100644 --- a/src/booking/forms.py +++ b/src/booking/forms.py @@ -10,12 +10,13 @@ import django.forms as forms from django.forms.widgets import NumberInput from workflow.forms import ( - SearchableSelectMultipleWidget, MultipleSelectFilterField, MultipleSelectFilterWidget, FormUtils) from account.models import UserProfile from resource_inventory.models import Image, Installer, Scenario +from workflow.forms import SearchableSelectMultipleField +from booking.lib import get_user_items, get_user_field_opts class QuickBookingForm(forms.Form): @@ -27,16 +28,11 @@ class QuickBookingForm(forms.Form): scenario = forms.ModelChoiceField(queryset=Scenario.objects.all(), required=False) def __init__(self, data=None, user=None, *args, **kwargs): - chosen_users = [] if "default_user" in kwargs: default_user = kwargs.pop("default_user") else: default_user = "you" self.default_user = default_user - if "chosen_users" in kwargs: - chosen_users = kwargs.pop("chosen_users") - elif data and "users" in data: - chosen_users = data.getlist("users") super(QuickBookingForm, self).__init__(data=data, **kwargs) @@ -44,12 +40,13 @@ class QuickBookingForm(forms.Form): Image.objects.filter(public=True) | Image.objects.filter(owner=user) ) - self.fields['users'] = forms.CharField( - widget=SearchableSelectMultipleWidget( - attrs=self.build_search_widget_attrs(chosen_users, default_user=default_user) - ), - required=False + self.fields['users'] = SearchableSelectMultipleField( + queryset=UserProfile.objects.select_related('user').exclude(user=user), + items=get_user_items(exclude=user), + required=False, + **get_user_field_opts() ) + attrs = FormUtils.getLabData(0) attrs['selection_data'] = 'false' self.fields['filter_field'] = MultipleSelectFilterField(widget=MultipleSelectFilterWidget(attrs=attrs)) diff --git a/src/booking/lib.py b/src/booking/lib.py new file mode 100644 index 0000000..8132c75 --- /dev/null +++ b/src/booking/lib.py @@ -0,0 +1,36 @@ +############################################################################## +# Copyright (c) 2019 Parker Berberian, Sawyer Bergeron, and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +from account.models import UserProfile + + +def get_user_field_opts(): + return { + 'show_from_noentry': False, + 'show_x_results': 5, + 'results_scrollable': True, + 'selectable_limit': -1, + 'placeholder': 'Search for other users', + 'name': 'users', + 'disabled': False + } + + +def get_user_items(exclude=None): + qs = UserProfile.objects.select_related('user').exclude(user=exclude) + items = {} + for up in qs: + item = { + 'id': up.id, + 'expanded_name': up.full_name, + 'small_name': up.user.username, + 'string': up.email_addr + } + items[up.id] = item + return items diff --git a/src/booking/quick_deployer.py b/src/booking/quick_deployer.py index 763c8a0..ac69c8c 100644 --- a/src/booking/quick_deployer.py +++ b/src/booking/quick_deployer.py @@ -12,7 +12,6 @@ import json import uuid import re from django.db.models import Q -from django.contrib.auth.models import User from datetime import timedelta from django.utils import timezone from account.models import Lab @@ -321,12 +320,8 @@ def create_from_form(form, request): ) booking.pdf = PDFTemplater.makePDF(booking) - users_field = users_field[2:-2] - if users_field: # may be empty after split, if no collaborators entered - users_field = json.loads(users_field) - for collaborator in users_field: - user = User.objects.get(id=collaborator['id']) - booking.collaborators.add(user) + for collaborator in users_field: # list of UserProfiles + booking.collaborators.add(collaborator.user) booking.save() diff --git a/src/booking/views.py b/src/booking/views.py index 8211a0c..13e9d01 100644 --- a/src/booking/views.py +++ b/src/booking/views.py @@ -47,7 +47,7 @@ def quick_create(request): context['lab_profile_map'] = profiles - context['form'] = QuickBookingForm(initial={}, chosen_users=[], default_user=request.user.username, user=request.user) + context['form'] = QuickBookingForm(default_user=request.user.username, user=request.user) context.update(drop_filter(request.user)) -- cgit 1.2.3-korg