Skip to content
Snippets Groups Projects
Commit a5038856 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

after import is successfully performed create rename file to backup file name

parent ff59a5d7
No related branches found
No related tags found
1 merge request!231Resolve "auto-import of subject data"
# coding=utf-8
import datetime
import logging
import os
import os.path
import traceback
......@@ -22,8 +24,8 @@ class ImporterCronJob(CronJobBase):
@timeout_decorator.timeout(CRON_JOB_TIMEOUT)
def do(self):
title = "Daily import"
recipients = getattr(settings, "DEFAULT_FROM_EMAIL", None)
email_title = "Daily import"
email_recipients = getattr(settings, "DEFAULT_FROM_EMAIL", None)
filename = getattr(settings, "DAILY_IMPORT_FILE", None)
......@@ -32,22 +34,28 @@ class ImporterCronJob(CronJobBase):
return "import file not defined"
logger.info("Importing subjects from file: " + filename)
if not os.path.isfile(filename):
EmailSender().send_email(title,
EmailSender().send_email(email_title,
"<h3><font color='red'>File with imported data is not available in the system: " + filename + "</font></h3>",
recipients)
email_recipients)
return "import file not found"
try:
importer = Importer(settings.DAILY_IMPORT_FILE, CsvSubjectImportReader())
importer.execute()
body = importer.get_summary()
EmailSender().send_email(title,
"<h3>Data was successfully imported from file: " + filename + "</h3>" + body,
recipients)
email_body = importer.get_summary()
EmailSender().send_email(email_title,
"<h3>Data was successfully imported from file: " + filename + "</h3>" + email_body,
email_recipients)
self.backup_file(filename)
return "import is successful"
except:
tb = traceback.format_exc()
EmailSender().send_email(title,
EmailSender().send_email(email_title,
"<h3><font color='red'>There was a problem with importing data from file: " + filename + "</font></h3><pre>" + tb + "</pre>",
recipients)
email_recipients)
return "import crashed"
def backup_file(self, filename):
new_file = filename + "-" + datetime.datetime.now().strftime("%Y-%m-%d-%H-%M") + ".bac"
os.rename(filename, new_file)
return
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment