Repository: carpentries/amy

Issue:
Assign admin when creating new event
You should be able to assign admin from this page:
https://amy.software-carpentry.org/workshops/events/add/

Right now you can't assign admin until you save and edit.



Discussion:
@maneesha 
When you say 'admin', do you refer to the field `administrator` that relates to a host or the field `assigned_to` field that links to a person?

This refers to `assigned_to`  -- so that I can assign an event to myself when creating it.

The issue here is that current assignment machinery works on existing objects, and @maneesha wants to assign admins when the event is not yet created.

We should be able to achieve that by extending event form with assignment field.


Resolving patch:
diff --git a/workshops/forms.py b/workshops/forms.py
--- a/workshops/forms.py
+++ b/workshops/forms.py
@@ -261,6 +261,14 @@ class EventForm(forms.ModelForm):
         widget=selectable.AutoComboboxSelectWidget,
     )
 
+    assigned_to = selectable.AutoCompleteSelectField(
+        lookup_class=lookups.AdminLookup,
+        label='Assigned to',
+        required=False,
+        help_text=AUTOCOMPLETE_HELP_TEXT,
+        widget=selectable.AutoComboboxSelectWidget,
+    )
+
     language = selectable.AutoCompleteSelectField(
         lookup_class=lookups.LanguageLookup,
         label='Language',
@@ -341,11 +349,11 @@ class Meta:
         model = Event
         # reorder fields, don't display 'deleted' field
         fields = ('slug', 'completed', 'start', 'end', 'host', 'administrator',
-                  'tags', 'url', 'language', 'reg_key', 'admin_fee',
-                  'invoice_status', 'attendance', 'contact', 'notes',
-                  'country', 'venue', 'address', 'latitude', 'longitude',
-                  'learners_pre', 'learners_post', 'instructors_pre',
-                  'instructors_post', 'learners_longterm')
+                  'assigned_to', 'tags', 'url', 'language', 'reg_key',
+                  'admin_fee', 'invoice_status', 'attendance', 'contact',
+                  'notes', 'country', 'venue', 'address', 'latitude',
+                  'longitude', 'learners_pre', 'learners_post',
+                  'instructors_pre', 'instructors_post', 'learners_longterm')
         # WARNING: don't change put any fields between 'country' and
         #          'longitude' that don't relate to the venue of the event
 


Test patch:
diff --git a/workshops/test/test_event.py b/workshops/test/test_event.py
--- a/workshops/test/test_event.py
+++ b/workshops/test/test_event.py
@@ -328,6 +328,24 @@ def test_unique_slug(self):
             Event.objects.create(host=self.test_host,
                                  slug='testing-unique-slug')
 
+    def test_assign_to_field_populated(self):
+        """Ensure that we can assign an admin to an event
+        from the `event_add` view."""
+        data = {
+            'slug': '2016-07-09-test',
+            'host': self.test_host.id,
+            'tags': [self.test_tag.id],
+            'assigned_to': self.admin.pk,
+            'invoice_status': 'unknown',
+        }
+        response = self.client.post(reverse('event_add'), data, follow=True)
+        event = Event.objects.get(slug='2016-07-09-test')
+        self.assertRedirects(
+            response,
+            reverse('event_details', kwargs={'event_ident': event.slug}),
+        )
+        self.assertEqual(event.assigned_to, self.admin)
+
     def test_unique_non_empty_slug(self):
         """Ensure events with no slugs are *not* saved to the DB.
         """