From 33a77c6f0629b41d3f4bef761888409a7ce697e1 Mon Sep 17 00:00:00 2001
From: "J.-S. Caux" <J.S.Caux@uva.nl>
Date: Mon, 26 Aug 2019 09:47:27 +0200
Subject: [PATCH] Add TOTPDevice to admin

---
 scipost/admin.py                              | 15 ++++++++++++++-
 scipost/migrations/0027_auto_20190826_0924.py | 17 +++++++++++++++++
 scipost/models.py                             |  1 +
 scipost/totp.py                               |  8 ++++----
 4 files changed, 36 insertions(+), 5 deletions(-)
 create mode 100644 scipost/migrations/0027_auto_20190826_0924.py

diff --git a/scipost/admin.py b/scipost/admin.py
index d9c18ea88..95dcfdec2 100644
--- a/scipost/admin.py
+++ b/scipost/admin.py
@@ -8,7 +8,7 @@ from django import forms
 from django.contrib.auth.admin import UserAdmin
 from django.contrib.auth.models import User, Permission
 
-from scipost.models import Contributor, Remark,\
+from scipost.models import TOTPDevice, Contributor, Remark,\
                            AuthorshipClaim, PrecookedEmail,\
                            EditorialCollege, EditorialCollegeFellowship, UnavailabilityPeriod
 
@@ -17,6 +17,12 @@ from production.admin import ProductionUserInline
 from submissions.models import Submission
 
 
+class TOTPDeviceAdmin(admin.ModelAdmin):
+    search_fields = ['user',]
+
+admin.site.register(TOTPDevice)
+
+
 admin.site.register(UnavailabilityPeriod)
 
 
@@ -34,9 +40,16 @@ class ContributorInline(admin.StackedInline):
     min_num = 0
 
 
+class TOTPDeviceInline(admin.StackedInline):
+    model = TOTPDevice
+    extra = 0
+    min_num = 0
+
+
 class UserAdmin(UserAdmin):
     inlines = [
         ContributorInline,
+        TOTPDeviceInline,
         ContactInline,
         ProductionUserInline
         ]
diff --git a/scipost/migrations/0027_auto_20190826_0924.py b/scipost/migrations/0027_auto_20190826_0924.py
new file mode 100644
index 000000000..c43fc5cda
--- /dev/null
+++ b/scipost/migrations/0027_auto_20190826_0924.py
@@ -0,0 +1,17 @@
+# Generated by Django 2.1.8 on 2019-08-26 07:24
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('scipost', '0026_auto_20190522_1120'),
+    ]
+
+    operations = [
+        migrations.AlterModelOptions(
+            name='totpdevice',
+            options={'default_related_name': 'devices', 'verbose_name': 'TOTP Device'},
+        ),
+    ]
diff --git a/scipost/models.py b/scipost/models.py
index be2d448c1..ee3de6178 100644
--- a/scipost/models.py
+++ b/scipost/models.py
@@ -52,6 +52,7 @@ class TOTPDevice(models.Model):
 
     class Meta:
         default_related_name = 'devices'
+        verbose_name = 'TOTP Device'
 
     def __str__(self):
         return '{}: {}'.format(self.user, self.name)
diff --git a/scipost/totp.py b/scipost/totp.py
index 3af23ed5f..35d39901d 100644
--- a/scipost/totp.py
+++ b/scipost/totp.py
@@ -23,8 +23,8 @@ class TOTPVerification:
         Verify a time-dependent code for a certain User.
         """
         try:
-            # Try to see if input token is convertable to integer.
-            # Do not actually make it a integer, because it'll loose the leading 0s.
+            # Try to see if input token is convertible to integer.
+            # Do not actually make it an integer, because it'll lose the leading 0s.
             assert int(code) > 0
         except (ValueError, AssertionError):
             # return False, if token could not be converted to an integer
@@ -59,8 +59,8 @@ class TOTPVerification:
         Independently verify a secret_key/code combination at current time.
         """
         try:
-            # Try to see if input token is convertable to integer.
-            # Do not actually make it a integer, because it'll loose the leading 0s.
+            # Try to see if input token is convertible to integer.
+            # Do not actually make it an integer, because it'll lose the leading 0s.
             assert int(code) > 0
         except (ValueError, AssertionError):
             # return False, if token could not be converted to an integer
-- 
GitLab