Repository: regro/regolith

Issue:
ENH: contact updater can take an id as an optional argument
When updating (rather than adding) it would be nice to be able to specify an id rather than first/last name only.

Come to think of it, it would be nice to be able to specify an id in any case, for example, with chinese names (and possibly other nationalities in the future) our "standard" behavior is to put two initials from the first name (so Songsheng Tao is sstao, not stao)


Discussion:

Patch:
diff --git a/news/fix_u_contact.rst b/news/fix_u_contact.rst
new file mode 100644
index 00000000..992c96a9
--- /dev/null
+++ b/news/fix_u_contact.rst
@@ -0,0 +1,13 @@
+**Added:** None
+
+**Changed:**
+ * u_contact now only requires institution when adding, but not when updating
+ * u_contact can take an id as an optional argument
+
+**Deprecated:** None
+
+**Removed:** None
+
+**Fixed:** None
+
+**Security:** None
diff --git a/regolith/helpers/u_contacthelper.py b/regolith/helpers/u_contacthelper.py
index 89352695..641bc779 100644
--- a/regolith/helpers/u_contacthelper.py
+++ b/regolith/helpers/u_contacthelper.py
@@ -16,9 +16,12 @@ TARGET_COLL = "contacts"
 
 def subparser(subpi):
     subpi.add_argument("name", help="first name space last name in quotes")
-    subpi.add_argument("institution", help="person's institution.  Can be inf"
-                                            "short form such as columbiau and will "
-                                            "be retrieved from institutions collection")
+    subpi.add_argument("-d", "--id", help="id of the person, e.g., first letter first name "
+                                            "plus last name, but unique")
+    subpi.add_argument("-i", "--institution", help="person's institution. It can be "
+                                                   "institution id or anything in the "
+                                                   "aka or name from institutions collection. "
+                                                   "it is required to create a new contact")
     #FIXME
     # subpi.add_argument("-e", "--email",
     #                    help="email address")
@@ -72,7 +75,10 @@ class ContactUpdaterHelper(DbHelperBase):
             now = dt.datetime.now()
         else:
             now = date_parser.parse(rc.date).date()
-        key = str(name.first[0].lower().replace(" ", "") + name.last.lower().replace(" ", ""))
+        if not rc.id:
+            key = str(name.first[0].lower().replace(" ", "") + name.last.lower().replace(" ", ""))
+        else:
+            key = rc.id
         filterid = {'_id': key}
         coll = self.gtx[rc.coll]
         pdoc = {'_id': key}
@@ -82,11 +88,14 @@ class ContactUpdaterHelper(DbHelperBase):
             notes = current.get('notes', [])
             aliases = current.get('aka', [])
         else:
-            pdoc.update({"date": dt.today()})
+            if not rc.institution:
+                raise RuntimeError("Institution is required to create a new contact")
+            pdoc.update({"date": dt.date.today()})
+            pdoc.update({"institution": rc.institution})
             notes = []
             aliases = []
-            UUID = str(uuid.uuid4())
-            pdoc.update({'uuid': UUID})
+            uniqueidentifier = str(uuid.uuid4())
+            pdoc.update({'uuid': uniqueidentifier})
         #FIXME
         # if rc.e_email:
         #     pdoc.update({"email": rc.email})
@@ -98,9 +107,7 @@ class ContactUpdaterHelper(DbHelperBase):
             notes.extend(rc.notes)
         pdoc.update({"aka": aliases})
         pdoc.update({"notes": notes})
-        pdoc.update({"name": name.full_name,
-                    "institution": rc.institution,
-                    })
+        pdoc.update({"name": name.full_name})
         pdoc.update({'updated': now})
 
         rc.client.update_one(rc.database, rc.coll, filterid, pdoc)


Test patch:
diff --git a/tests/test_helpers.py b/tests/test_helpers.py
index 4f08b907..22b0e4b1 100644
--- a/tests/test_helpers.py
+++ b/tests/test_helpers.py
@@ -49,14 +49,13 @@ helper_map = [
     (["helper", "l_contacts", "run", "-n", "ny", "-i", "col", "-o", "coffee", "-d", "2020-01-15", "-r", "2"],
      "name: Anthony B Friend, institution: columbiau, email: friend@deed.com\n"
      ),
-    (["helper", "u_contact", "Anthony B Friend", "columbiau", "-a", "Friend", "--date", "2020-01-02",
+    (["helper", "u_contact", "Anthony B Friend","--id", "afriend", "-a", "Friend", "--date", "2020-01-02",
       "-n", "Test note"],
      "Anthony B Friend has been added/updated in contacts\n"
      ),
     (["helper", "l_contacts", "run", "-n", "ny", "-i", "col", "-o", "coffee", "-d", "2019-01-15", "-r", "2"],
      ""
      ),
-
     ]