-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_xml.py
110 lines (97 loc) · 3.76 KB
/
update_xml.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#########################################################################
#
# Copyright (C) 2016 OSGeo
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#########################################################################
import errno
import logging
from django.conf import settings
from django.db.models import signals
from lxml import etree
from owslib.etree import etree as dlxml
from geonode.layers.models import Dataset
from geonode.documents.models import Document
from geonode.catalogue import get_catalogue
from geonode.base.models import Link, ResourceBase
LOGGER = logging.getLogger(__name__)
def custom_catalogue_post_save(instance, **kwargs):
"""Get information from catalogue"""
_id = (
instance.resourcebase_ptr.id
if hasattr(instance, "resourcebase_ptr")
else instance.id
)
resources = ResourceBase.objects.filter(id=_id)
# Update the Catalog
try:
catalogue = get_catalogue()
catalogue.create_record(instance)
record = catalogue.get_record(instance.uuid)
except OSError as err:
msg = f'Could not connect to catalogue to save information for layer "{instance.name}"'
if err.errno == errno.ECONNREFUSED:
LOGGER.warn(msg, err)
return
else:
raise err
if not record:
msg = f"Metadata record for {instance.title} does not exist, check the catalogue signals."
LOGGER.warning(msg)
return
if not hasattr(record, "links"):
msg = f"Metadata record for {instance.title} should contain links."
raise Exception(msg)
# Create the different metadata links with the available formats
if resources.exists():
for mime, name, metadata_url in record.links["metadata"]:
try:
Link.objects.get_or_create(
resource=resources.get(),
url=metadata_url,
defaults=dict(
name=name, extension="xml", mime=mime, link_type="metadata"
),
)
except Exception:
_d = dict(name=name, extension="xml", mime=mime, link_type="metadata")
Link.objects.filter(
resource=resources.get(),
url=metadata_url,
extension="xml",
link_type="metadata",
).update(**_d)
print("custom cat did run")
instance.extra_metadata = [
{extra.metadata.get("name", ""): extra.metadata.get("value", "")}
for extra in instance.metadata.all()
]
# generate an XML document (GeoNode's default is ISO)
if instance.metadata_uploaded and instance.metadata_uploaded_preserve:
md_doc = etree.tostring(dlxml.fromstring(instance.metadata_xml))
else:
md_doc = catalogue.catalogue.csw_gen_xml(
instance, "custom_metadata/full_metadata.xml"
)
try:
csw_anytext = catalogue.catalogue.csw_gen_anytext(md_doc)
except Exception as e:
LOGGER.exception(e)
csw_anytext = ""
resources.update(
metadata_xml=md_doc,
csw_wkt_geometry=instance.geographic_bounding_box,
csw_anytext=csw_anytext,
)