Repository: DDMAL/CantusDB

Issue:
Cannot retrieve "created by" information from old Cantus
In the new Cantus, we keep track of who created or updated the source, using fields like `created_by` and `updated_by`. However, this information is not included in the old Cantus' json-export and will be lost if we don't find another way to retrieve it. 


Discussion:
Seeing as I remember us looking pretty hard for this and not finding it, it seems likely to me that OldCantus simply does not store this information. Do we have a compelling reason to think that OldCantus has been keeping a record of this?

Going to leave this issue open for now in case some relevant new information emerges, but we may simply need to give up on this one.
The creating information does seem to exist in oldCantus: here's the "authoring information' of one source visible in https://cantus.uwaterloo.ca/node/699507/edit
<img width="488" alt="image" src="https://user-images.githubusercontent.com/67451875/229235306-7fbb4c14-2bd3-489e-a216-67773acb7278.png">

It's in a different place for chants, and indicated as a number rather than the user's name (also visible from the chant's view page and not just the edit page)
<img width="366" alt="image" src="https://user-images.githubusercontent.com/67451875/229235853-97569704-a488-4258-b8ea-14cab711a9ae.png">

Neither shows up for me unless I'm logged in, so maybe it's only viewable to admins. I also don't seem to see a full list of change/updates, just the date of the most recent one.

Hmm... I wonder why we weren't able to find any of this in the database dump we were looking at last year. Maybe there's a way to scrape this from the HTML, even if we can't find it in the DB? But it must be stored somewhere... :/
> The creating information does seem to exist in oldCantus: here's the "authoring information' of one source visible in https://cantus.uwaterloo.ca/node/699507/edit

I'm able to view this page, but can't find anything resembling the "authoring information" section in your screenshot:
![Screenshot 2023-04-04 at 9 20 05 AM](https://user-images.githubusercontent.com/58090591/229805813-abd94fa9-52ef-46ee-ab91-b80228d708a2.png)

In your second screenshot from the chant detail page, I see there's an "Import date" row. If we're unable to find "created by" information anywhere, it might be worth adding a field like this one to NewCantus - it will at least indicate that the chant was initially added to OldCantus, and give us a record of when it entered NewCantus.
not sure what to do with this one - it would certainly be great if we could figure this out, but it's been such a long time that no one has been able to figure this out. We might just need to resign ourselves to not being able to get this information.

@lucasmarchd01, assigning you to this - when you get a chance, maybe spend some time poking around in this direction (perhaps with @annamorphism) - perhaps you'll be able to find something that everyone else has missed.
In a video call with Debra yesterday, she said she would check in with Jan to ask where/how we can access this information.
I seems that the "created by" information can be accessed from the json-node "uid" entry based on Jan's email. Is this data being accessed/used already in NewCantus?

Resolving patch:
diff --git a/django/cantusdb_project/main_app/management/commands/sync_chants.py b/django/cantusdb_project/main_app/management/commands/sync_chants.py
--- a/django/cantusdb_project/main_app/management/commands/sync_chants.py
+++ b/django/cantusdb_project/main_app/management/commands/sync_chants.py
@@ -75,6 +75,12 @@ def get_new_chant(chant_id):
             error_file.write("\n")
         return
 
+    try:
+        author_id = json_response["uid"]
+        author = get_user_model().objects.get(id=author_id)
+    except (KeyError, TypeError, ObjectDoesNotExist):
+        author = None
+
     try:
         incipit = json_response["title"]
     except KeyError:
@@ -268,6 +274,7 @@ def get_new_chant(chant_id):
     chant_obj, created = Chant.objects.update_or_create(
         id=chant_id,
         defaults={
+            "created_by": author,
             "incipit": incipit,
             "visible_status": status,
             "source": source,
diff --git a/django/cantusdb_project/main_app/management/commands/sync_sequences.py b/django/cantusdb_project/main_app/management/commands/sync_sequences.py
--- a/django/cantusdb_project/main_app/management/commands/sync_sequences.py
+++ b/django/cantusdb_project/main_app/management/commands/sync_sequences.py
@@ -3,6 +3,7 @@
 from django.core.exceptions import ObjectDoesNotExist, ValidationError
 from django.core.validators import URLValidator
 import requests, json
+from django.contrib.auth import get_user_model
 
 SEQUENCE_ID_FILE = "sequence_list.txt"
 USER_AGENTS = [
@@ -49,6 +50,12 @@ def get_new_sequence(seq_id):
             error_file.write("\n")
         return
 
+    try:
+        author_id = json_response["uid"]
+        author = get_user_model().objects.get(id=author_id)
+    except (KeyError, TypeError, ObjectDoesNotExist):
+        author = None
+
     try:
         title = json_response["title"]
     except KeyError:
@@ -161,6 +168,7 @@ def get_new_sequence(seq_id):
         id=seq_id,
         defaults={
             "visible_status": status,
+            "created_by": author,
             "title": title,
             "siglum": siglum,
             "incipit": incipit,
diff --git a/django/cantusdb_project/main_app/management/commands/sync_sources.py b/django/cantusdb_project/main_app/management/commands/sync_sources.py
--- a/django/cantusdb_project/main_app/management/commands/sync_sources.py
+++ b/django/cantusdb_project/main_app/management/commands/sync_sources.py
@@ -50,6 +50,12 @@ def get_new_source(source_id):
     json_response = json.loads(response.content)
     title = json_response["title"]
 
+    try:
+        author_id = json_response["uid"]
+        author = get_user_model().objects.get(id=author_id)
+    except (KeyError, TypeError, ObjectDoesNotExist):
+        author = None
+
     try:
         siglum = json_response["field_siglum"]["und"][0]["value"]
     except (KeyError, TypeError):
@@ -287,6 +293,7 @@ def get_new_source(source_id):
         id=source_id,
         defaults={
             "title": title,
+            "created_by": author,
             "published": published,
             "siglum": siglum,
             "rism_siglum": rism_siglum,


Test patch: