Skip to content

Commit

Permalink
Merge pull request #907 from aanil/gs_links
Browse files Browse the repository at this point in the history
Change project links to connect to new gs_links db
  • Loading branch information
aanil authored Jan 9, 2025
2 parents 72d755d + c7a584a commit 0a755a9
Showing 1 changed file with 46 additions and 19 deletions.
65 changes: 46 additions & 19 deletions status/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
from dateutil.relativedelta import relativedelta
from genologics import lims
from genologics.config import BASEURI, PASSWORD, USERNAME
from genologics.entities import Artifact, Project
from genologics.entities import Artifact
from ibm_cloud_sdk_core.api_exception import ApiException
from zenpy import ZenpyException

from status.util import SafeHandler, dthandler
Expand Down Expand Up @@ -1001,11 +1002,15 @@ class LinksDataHandler(SafeHandler):
"""

def get(self, project):
self.set_header("Content-type", "application/json")
p = Project(lims, id=project)
p.get(force=True)

links = json.loads(p.udf["Links"]) if "Links" in p.udf else {}
links_doc = {}
try:
links_doc = self.application.cloudant.get_document(
db="gs_links", doc_id=project
).get_result()
except ApiException as e:
if e.message == "not_found":
pass
links = links_doc.get("links", {})

# Sort by descending date, then hopefully have deviations on top
sorted_links = OrderedDict()
Expand All @@ -1014,6 +1019,8 @@ def get(self, project):
sorted_links = OrderedDict(
sorted(sorted_links.items(), key=lambda k: k[1]["type"])
)

self.set_header("Content-type", "application/json")
self.write(sorted_links)

def post(self, project):
Expand All @@ -1027,19 +1034,39 @@ def post(self, project):
self.set_status(400)
self.finish("<html><body>Link title and type is required</body></html>")
else:
p = Project(lims, id=project)
p.get(force=True)
links = json.loads(p.udf["Links"]) if "Links" in p.udf else {}
links[str(datetime.datetime.now())] = {
"user": user.name,
"email": user.email,
"type": a_type,
"title": title,
"url": url,
"desc": desc,
}
p.udf["Links"] = json.dumps(links)
p.put()
links_doc = {}
links = {}
try:
links_doc = self.application.cloudant.get_document(
db="gs_links", doc_id=project
).get_result()
except ApiException as e:
if e.message == "not_found":
links_doc["_id"] = project
links_doc["links"] = {}
links = links_doc.get("links", {})
links.update(
{
str(datetime.datetime.now()): {
"user": user.name,
"email": user.email,
"type": a_type,
"title": title,
"url": url,
"desc": desc,
}
}
)
links_doc["links"] = links

response = self.application.cloudant.post_document(
db="gs_links", document=links_doc
).get_result()

if not response.get("ok"):
self.set_status(500)
return

self.set_status(200)
# ajax cries if it does not get anything back
self.set_header("Content-type", "application/json")
Expand Down

0 comments on commit 0a755a9

Please sign in to comment.