Repository: carpentries/amy

Issue:
Add "awarded by" field to badges
We should track who awarded instructor badges to new instructors.



Discussion:

Resolving patch:
diff --git a/workshops/forms.py b/workshops/forms.py
--- a/workshops/forms.py
+++ b/workshops/forms.py
@@ -527,6 +527,14 @@ class BadgeAwardForm(forms.ModelForm):
         widget=selectable.AutoComboboxSelectWidget,
     )
 
+    awarded_by = selectable.AutoCompleteSelectField(
+        lookup_class=lookups.PersonLookup,
+        label='Awarded by',
+        required=False,
+        help_text=AUTOCOMPLETE_HELP_TEXT,
+        widget=selectable.AutoComboboxSelectWidget,
+    )
+
     class Meta:
         model = Award
         fields = '__all__'
@@ -543,6 +551,14 @@ class PersonAwardForm(forms.ModelForm):
         widget=selectable.AutoComboboxSelectWidget,
     )
 
+    awarded_by = selectable.AutoCompleteSelectField(
+        lookup_class=lookups.PersonLookup,
+        label='Awarded by',
+        required=False,
+        help_text=AUTOCOMPLETE_HELP_TEXT,
+        widget=selectable.AutoComboboxSelectWidget,
+    )
+
     class Meta:
         model = Award
         fields = '__all__'
diff --git a/workshops/migrations/0080_award_awarded_by.py b/workshops/migrations/0080_award_awarded_by.py
new file mode 100644
--- /dev/null
+++ b/workshops/migrations/0080_award_awarded_by.py
@@ -0,0 +1,22 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.9.2 on 2016-02-29 09:04
+from __future__ import unicode_literals
+
+from django.conf import settings
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('workshops', '0079_eventsubmission'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='award',
+            name='awarded_by',
+            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='awarded_set', to=settings.AUTH_USER_MODEL),
+        ),
+    ]
diff --git a/workshops/models.py b/workshops/models.py
--- a/workshops/models.py
+++ b/workshops/models.py
@@ -238,7 +238,9 @@ class Person(AbstractBaseUser, PermissionsMixin):
     notes = models.TextField(default="", blank=True)
     affiliation = models.CharField(max_length=STR_LONG, default='', blank=True)
 
-    badges = models.ManyToManyField("Badge", through="Award")
+    badges = models.ManyToManyField(
+        "Badge", through="Award",
+        through_fields=('person', 'badge'))
     lessons = models.ManyToManyField(
         "Lesson",
         through="Qualification",
@@ -1160,6 +1162,9 @@ class Award(models.Model):
     awarded    = models.DateField()
     event      = models.ForeignKey(Event, null=True, blank=True,
                                    on_delete=models.PROTECT)
+    awarded_by = models.ForeignKey(
+        Person, null=True, blank=True, on_delete=models.PROTECT,
+        related_name='awarded_set')
 
     class Meta:
         unique_together = ("person", "badge", )


Test patch:
diff --git a/workshops/test/test_badge.py b/workshops/test/test_badge.py
--- a/workshops/test/test_badge.py
+++ b/workshops/test/test_badge.py
@@ -42,6 +42,8 @@ def test_badge_award(self):
         values['person_0'] = ''
         values['event_1'] = ''
         values['event_0'] = ''
+        values['awarded_by_0'] = ''
+        values['awarded_by_1'] = ''
 
         assert self.swc_instructor.award_set.count() == 3
 
diff --git a/workshops/test/test_person.py b/workshops/test/test_person.py
--- a/workshops/test/test_person.py
+++ b/workshops/test/test_person.py
@@ -176,6 +176,9 @@ def test_person_award_badge(self):
 
         values['award-badge'] = self.swc_instructor.pk
         values['award-event_1'] = ''
+        values['award-awarded_by_0'] = ''
+        values['award-awarded_by_1'] = ''
+
         rv = self.client.post(url, data=values)
         assert rv.status_code == 302, \
             'After awarding a badge we should be redirected to the same page, got {} instead'.format(rv.status_code)