diff --git a/src/caosadvancedtools/serverside/examples/example_script.py b/src/caosadvancedtools/serverside/examples/example_script.py index 148d8cbae22a62865c3c619f14e459c7556df957..d06d25c9cfe204181445d7c229f656d93cfc7d7b 100755 --- a/src/caosadvancedtools/serverside/examples/example_script.py +++ b/src/caosadvancedtools/serverside/examples/example_script.py @@ -40,12 +40,44 @@ import numpy as np from caosadvancedtools.cfood import assure_property_is from caosadvancedtools.guard import INSERT, UPDATE from caosadvancedtools.guard import global_guard as guard +from caosadvancedtools.serverside.helper import send_mail as main_send_mail logger = logging.getLogger(__name__) guard.set_level(level=UPDATE) +def send_mail(self, changes): + """ calls sendmail in order to send a mail to the curator about pending + changes + + Parameters: + ----------- + changes: The CaosDB entities in the version after the update. + """ + + caosdb_config = db.configuration.get_config() + text = """Dear Curator, +The following changes where done automatically. + +{changes} + """.format(changes="\n".join(changes)) + try: + fro = caosdb_config["advancedtools"]["automated_updates.from_mail"] + to = caosdb_config["advancedtools"]["automated_updates.to_mail"] + except KeyError: + logger.error("Server Configuration is missing a setting for " + "sending mails. The administrator should check " + "'from_mail' and 'to_mail'.") + return + + main_send_mail( + from_addr=fro, + to=to, + subject="Automated Update", + body=text) + + def main(args): # TODO (maybe) can these checks be replaced by a more declaritive appoach? try: @@ -84,19 +116,20 @@ def main(args): # ####################################################################### # # Insert the result plot - # TODO (must): how do we find a good file path?? fig = db.File(file=filename, - path="/uploaded/something3/"+str(datetime.now())+filename) + path="/Analysis/results/"+str(datetime.now())+"/"+filename) fig.insert() # Add the result to the analysis Record # An update should only be done if necessary: assure_property_is should be # used instead of direct calls to 'update'. + to_be_updated = [] assure_property_is( dataAnalysisRecord, "mean_value", mean, + to_be_updated=to_be_updated ) # TODO (maybe) this is not really meaningful since an uploaded file will always # be different.... Compare checksums of files? @@ -104,7 +137,12 @@ def main(args): dataAnalysisRecord, "results", fig.id, + to_be_updated=to_be_updated ) + + if len(to_be_updated) > 0: + send_mail(to_be_updated) + # TODO (must) what should be done with the old file? Removed if not referenced? # TODO (maybe) inform about updates (reuse stuff from crawler.py?)