1111
1212import arrow
1313from cds_rdm .legacy .resolver import get_pid_by_legacy_recid
14- from flask import current_app , url_for
14+ from flask import current_app
1515from invenio_access .permissions import system_identity
1616from invenio_accounts .models import User
1717from invenio_db .uow import UnitOfWork
@@ -49,7 +49,9 @@ def __init__(
4949
5050 def get_attached_files_for_comment (self , recid , comment_id ):
5151 """Get the attached files for the comment."""
52- attached_files_directory = os .path .join (self .dirpath , str (recid ), str (comment_id ))
52+ attached_files_directory = os .path .join (
53+ self .dirpath , str (recid ), str (comment_id )
54+ )
5355 if os .path .exists (attached_files_directory ):
5456 return os .listdir (attached_files_directory )
5557 return []
@@ -59,19 +61,26 @@ def get_oldest_record(self, parent_pid_value):
5961 identity = system_identity , id_ = parent_pid_value
6062 )
6163 search_result = current_rdm_records_service .scan_versions (
62- system_identity , latest_record ["id" ],
64+ system_identity ,
65+ latest_record ["id" ],
6366 )
6467 for hit in search_result .hits :
6568 self .all_record_versions [hit ["versions" ]["index" ]] = hit
6669 oldest_version_index = min (self .all_record_versions .keys ())
6770 return self .all_record_versions [oldest_version_index ]
6871
6972 def create_event (
70- self , request , data , community , record , uow , parent_comment_id = None
73+ self ,
74+ request ,
75+ data ,
76+ community ,
77+ uow ,
78+ legacy_recid ,
79+ parent_comment_id = None ,
7180 ):
7281 logger .info (
73- "Creating event for record ID<{}> request ID<{}> comment ID<{}> parent_comment ID<{}>" .format (
74- record [ "id" ] ,
82+ "Creating event for legacy recid ID<{}> request ID<{}> comment ID<{}> parent_comment ID<{}>" .format (
83+ legacy_recid ,
7584 request .id ,
7685 data .get ("comment_id" ),
7786 "self" if not parent_comment_id else parent_comment_id ,
@@ -89,20 +98,14 @@ def create_event(
8998 comment_status = data .get ("status" )
9099 event_type = CommentEventType
91100 if comment_status == "da" :
92- comment_payload ["payload" ].update (
93- {
94- "content" : "comment was deleted by the author." ,
95- "event_type" : "comment_deleted" ,
96- }
97- )
101+ comment_payload ["payload" ]["content" ] = "comment was deleted by the author."
102+ comment_payload ["payload" ]["event" ] = "comment_deleted"
98103 event_type = LogEventType
99104 elif comment_status == "dm" :
100- comment_payload ["payload" ].update (
101- {
102- "content" : "comment was deleted by the moderator." ,
103- "event_type" : "comment_deleted" ,
104- }
105- )
105+ comment_payload ["payload" ][
106+ "content"
107+ ] = "comment was deleted by the moderator."
108+ comment_payload ["payload" ]["event" ] = "comment_deleted"
106109 event_type = LogEventType
107110
108111 event = current_events_service .record_cls .create (
@@ -114,16 +117,17 @@ def create_event(
114117 file_relation = data .get ("file_relation" )
115118 file_id = file_relation .get ("file_id" )
116119 version = file_relation .get ("version" )
117- record_version = self .all_record_versions .get (str ( version ) , None )
120+ record_version = self .all_record_versions .get (version , None )
118121 if record_version :
119- record_url = url_for (
120- "invenio_app_rdm_records.record_detail" ,
121- pid_value = record_version ["id" ],
122- preview_file = file_id ,
122+ record_url = (
123+ current_app .config ["CDS_MIGRATOR_KIT_SITE_UI_URL" ]
124+ + "/records/"
125+ + record_version ["id" ]
126+ + f"?preview_file={ file_id } "
123127 )
124- version_link = f"<p><a href='{ record_url } '>See related record version { version } </a></p>"
128+ version_link_html = f"<p><a href='{ record_url } '>See related record version { version } </a></p>"
125129 comment_payload ["payload" ]["content" ] = (
126- version_link + "\n " + comment_payload ["payload" ]["content" ]
130+ version_link_html + "\n " + comment_payload ["payload" ]["content" ]
127131 )
128132
129133 if parent_comment_id :
@@ -145,20 +149,29 @@ def create_event(
145149 event .id ,
146150 )
147151 )
148- deep_link = f"<p><a href='{ current_app .config ['CDS_MIGRATOR_KIT_SITE_UI_URL' ]} /communities/{ community .slug } /requests/{ request .id } #commentevent-{ parent_comment_id } _{ mentioned_event_id } '>Link to the reply</a></p>"
152+ deep_link = (
153+ current_app .config ["CDS_MIGRATOR_KIT_SITE_UI_URL" ]
154+ + f"/communities/{ community .slug } /requests/{ request .id } #commentevent-{ parent_comment_id } _{ mentioned_event_id } "
155+ )
156+ deep_link_html = f"<p><a href='{ deep_link } '>Link to the reply</a></p>"
149157 comment_payload ["payload" ]["content" ] = (
150- deep_link + "\n " + comment_payload ["payload" ]["content" ]
158+ deep_link_html + "\n " + comment_payload ["payload" ]["content" ]
151159 )
152160
153161 # TODO: Add attached files to the event
154162 # https://github.com/CERNDocumentServer/cds-migrator-kit/issues/381
155163 # For now, if attached files are found, raise ManualImportRequired error
156164 attached_files = self .get_attached_files_for_comment (
157- record [ "id" ] , data .get ("comment_id" )
165+ legacy_recid , data .get ("comment_id" )
158166 )
159167 if attached_files :
160168 raise ManualImportRequired (
161- f"Attached files found for comment ID<{ data .get ('comment_id' )} >."
169+ message = f"Attached files found." ,
170+ field = data .get ("comment_id" ),
171+ value = attached_files ,
172+ stage = "load" ,
173+ recid = legacy_recid ,
174+ priority = "critical" ,
162175 )
163176
164177 event .update (comment_payload )
@@ -169,7 +182,12 @@ def create_event(
169182 )
170183 else :
171184 raise ManualImportRequired (
172- f"User not found for email: { data .get ('user_email' )} "
185+ message = f"User not found." ,
186+ field = data .get ("comment_id" ),
187+ value = data .get ("user_email" ),
188+ stage = "load" ,
189+ recid = legacy_recid ,
190+ priority = "critical" ,
173191 )
174192 created_at = arrow .get (data .get ("created_at" )).datetime .replace (tzinfo = None )
175193 event .model .created = created_at
@@ -182,6 +200,7 @@ def create_event(
182200
183201 def create_accepted_community_submission_request (
184202 self ,
203+ legacy_recid ,
185204 record ,
186205 community ,
187206 creator_user_id ,
@@ -228,34 +247,37 @@ def create_accepted_community_submission_request(
228247
229248 for comment_data in comments :
230249 comment_event = self .create_event (
231- request , comment_data , community , record , uow
250+ request , comment_data , community , uow , legacy_recid
232251 )
233252 for reply in comment_data .get ("replies" , []):
234253 reply_event = self .create_event (
235254 request ,
236255 reply ,
237256 community ,
238- record ,
239257 uow ,
258+ legacy_recid ,
240259 parent_comment_id = comment_event .id ,
241260 )
242261 self .LEGACY_REPLY_LINK_MAP [reply .get ("comment_id" )] = reply_event .id
243262
244- # Commit at the end to rollback if any error occurs not only for the request but also for the comments
263+ # Commit at the end so that rollback can be done if any error occurs not only for the request but also for the comments in the middle
245264 uow .commit ()
246265
247266 return request
248267
249- def _process_legacy_comments_for_recid (self , recid , comments ):
268+ def _process_legacy_comments_for_recid (self , recid , comments , dry_run ):
250269 """Process the legacy comments for the record."""
251270 logger .info (f"Processing legacy comments for recid: { recid } " )
252271 parent_pid = get_pid_by_legacy_recid (recid )
253272 oldest_record = self .get_oldest_record (parent_pid .pid_value )
254273 parent = RDMParent .pid .resolve (parent_pid .pid_value )
255274 community = parent .communities .default
256275 record_owner_id = parent .access .owned_by .owner_id
276+ if dry_run :
277+ logger .info (f"Dry loading legacy comments for recid: { recid } " )
278+ return None
257279 request = self .create_accepted_community_submission_request (
258- oldest_record , community , record_owner_id , comments
280+ recid , oldest_record , community , record_owner_id , comments
259281 )
260282 return request
261283
@@ -264,9 +286,17 @@ def _load(self, entry):
264286 if entry :
265287 recid , comments = entry
266288 try :
267- self ._process_legacy_comments_for_recid (recid , comments )
289+ self ._process_legacy_comments_for_recid (recid , comments , self .dry_run )
290+ except ManualImportRequired as ex :
291+ error_message = (
292+ f"Error: { ex .message } | "
293+ f"Value: { ex .value } | "
294+ f"Recid: { recid } | "
295+ f"Comment ID: { ex .field } "
296+ )
297+ logger .error (error_message )
268298 except Exception as ex :
269- logger .error (ex )
299+ logger .error (f"Error: { ex } | Recid: { recid } " )
270300
271301 def _cleanup (self , * args , ** kwargs ):
272302 """Cleanup the entries."""
0 commit comments