-
Notifications
You must be signed in to change notification settings - Fork 0
/
botany_purger.py
89 lines (75 loc) · 4.22 KB
/
botany_purger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import logging
from specify_db import SpecifyDb
from attachment_utils import AttachmentUtils
from image_client import ImageClient
from get_configs import get_config
class BotanyPurger():
def __init__(self):
self.logger = logging.getLogger(f'Client.' + self.__class__.__name__)
self.botany_importer_config = get_config(config="Botany_PIC")
self.specify_db = SpecifyDb(self.botany_importer_config)
self.attachment_utils = AttachmentUtils(self.specify_db)
self.logger.debug(f"BotanyPurger setup complete")
self.image_client = ImageClient(config=self.botany_importer_config)
def purge(self):
# import csv
# with open('doomed.csv') as csv_file:
# csv_reader = csv.reader(csv_file, delimiter=',')
# line_count = 0
# for row in csv_reader:
# collection_object_id = row[0]
# collecting_event_id = row[1]
# print(f"Deleting {collecting_event_id}, {collection_object_id}")
# sql = f"delete from collectionobject where CollectionObjectID={collection_object_id}"
# self.specify_db.execute(sql)
#
# sql = f"delete from collectingevent where CollectingEventID={collecting_event_id}"
# self.specify_db.execute(sql)
self.purge_attachments_from_image_server()
self.purge_skeletons()
self.logger.debug(f"Remove collection object attachments")
sql = f"delete from collectionobjectattachment"
self.specify_db.execute(sql)
self.logger.debug(f"Remove attachments")
sql = f"delete from attachment"
self.specify_db.execute(sql)
def purge_attachments_from_image_server(self):
self.logger.debug("Pulling image records from image server")
sql = f"""select AttachmentLocation from attachment"""
attachment_locations = self.specify_db.get_records(sql)
for attachment_location in attachment_locations:
self.image_client.delete_from_image_server(attachment_location[0],
self.botany_importer_config.COLLECTION_NAME)
def purge_skeletons(self):
sql = (f"""
select collectionobjectattachment.CollectionObjectID,
determination.DeterminationID,
determination.TaxonID,
collectionobject.CatalogNumber,
collectionobjectattachment.AttachmentID,
collectionobjectattachment.CollectionObjectAttachmentID,
collectionobject.CollectingEventID,
attachmentlocation,
attachment.origFilename
from casbotany.collectionobjectattachment
INNER join casbotany.attachment ON attachment.AttachmentID = collectionobjectattachment.AttachmentID
INNER join casbotany.collectionobject
ON collectionobject.CollectionObjectID = collectionobjectattachment.CollectionObjectID
left join casbotany.determination
on casbotany.collectionobject.CollectionObjectID = casbotany.determination.CollectionObjectID
where determination.DeterminationID is NULL and collectionobject.version = 0;
""")
specify_list = self.specify_db.get_records(sql)
for remove_record in specify_list:
collecting_event_id = remove_record[6]
collection_object_id = remove_record[0]
barcode = remove_record[3]
self.logger.debug(f"Purging barcode: {barcode}")
self.logger.debug(f"Removing collecting event ID: {collecting_event_id}")
self.logger.debug(f"Find and remove attachments from collection object {collection_object_id}")
self.logger.debug(f"Removing collecting object ID: {collection_object_id}")
sql = f"delete from collectionobject where CollectionObjectID={collection_object_id}"
self.specify_db.execute(sql)
sql = f"delete from collectingevent where CollectingEventID={collecting_event_id}"
self.specify_db.execute(sql)
# query_skeletons = [int(item[3]) for item in specify_list]