summaryrefslogtreecommitdiff
path: root/chimere/tasks.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@peacefrogs.net>2012-03-17 00:49:22 +0100
committerÉtienne Loks <etienne.loks@peacefrogs.net>2012-03-17 00:49:22 +0100
commit105149b7810cc459662368d6275e88ba328e44d0 (patch)
tree292c946e593ec6227854f2eee835720bee01ee3b /chimere/tasks.py
parent9a794edbffbb3a178138fac3ded0b516c4ced9a7 (diff)
downloadChimère-105149b7810cc459662368d6275e88ba328e44d0.tar.bz2
Chimère-105149b7810cc459662368d6275e88ba328e44d0.zip
Add import task using celery
Diffstat (limited to 'chimere/tasks.py')
-rw-r--r--chimere/tasks.py57
1 files changed, 57 insertions, 0 deletions
diff --git a/chimere/tasks.py b/chimere/tasks.py
new file mode 100644
index 0000000..ba88304
--- /dev/null
+++ b/chimere/tasks.py
@@ -0,0 +1,57 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# Copyright (C) 2012 Étienne Loks <etienne.loks_AT_peacefrogsDOTnet>
+
+# 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/>.
+
+# See the file COPYING for details.
+
+from celery.decorators import task
+
+from django.core.exceptions import ObjectDoesNotExist
+from django.utils.translation import ugettext_lazy as _
+
+from chimere.models import Importer
+
+IMPORT_MESSAGES = {'import_pending':[_(u"Import pending")],
+ 'import_process':[_(u"Import processing")],
+ 'import_done':[_(u"Import successfuly done"),
+ _(u" %(new)d new item(s), %(updated)d updated item(s)")],
+ 'import_failed':[_(u"Import failed"), "%s"],
+ 'import_cancel':[_(u"Import canceled")]
+ }
+
+@task()
+def importing(importer_pk):
+ try:
+ importer = Importer.objects.get(pk=importer_pk)
+ except ObjectDoesNotExist:
+ # importer doesn't exists anymore: too late!
+ return
+ if importer.state != IMPORT_MESSAGES['import_pending'][0]:
+ # import canceled or done
+ return
+ importer.state = unicode(IMPORT_MESSAGES['import_process'][0])
+ importer.save()
+ new_item, updated_item, error = importer.manager.get()
+ if error:
+ importer.state = unicode(IMPORT_MESSAGES['import_failed'][0]) \
+ + u" - " + unicode(IMPORT_MESSAGES['import_failed'][1]) % error
+ importer.save()
+ return
+ importer.state = unicode(IMPORT_MESSAGES['import_done'][0]) + u" - " \
+ + unicode(IMPORT_MESSAGES['import_done'][1]) % {'new':new_item,
+ 'updated':updated_item}
+ importer.save()
+ return True