<s> import os <EOL> try : <EOL> from celery import Task <EOL> except ImportError : <EOL> Task = object <EOL> from django . core . cache import cache <EOL> from data_importer . core import default_settings <EOL> from django . core . mail import EmailMessage <EOL> from django . utils . safestring import mark_safe <EOL> try : <EOL> from django . conf import settings <EOL> except ImportError as e : <EOL> settings = None <EOL> try : <EOL> LOCK_EXPIRE = settings . DATA_IMPORTER_TASK_LOCK_EXPIRE <EOL> except AttributeError : <EOL> LOCK_EXPIRE = default_settings . DATA_IMPORTER_TASK_LOCK_EXPIRE <EOL> try : <EOL> DATA_IMPORTER_QUEUE = settings . DATA_IMPORTER_QUEUE <EOL> except AttributeError : <EOL> DATA_IMPORTER_QUEUE = default_settings . DATA_IMPORTER_QUEUE <EOL> acquire_lock = lambda lock_id : cache . add ( lock_id , "<STR_LIT:true>" , LOCK_EXPIRE ) <EOL> release_lock = lambda lock_id : cache . delete ( lock_id ) <EOL> class DataImpoterTask ( Task ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> queue = DATA_IMPORTER_QUEUE <EOL> time_limit = <NUM_LIT> * <NUM_LIT:15> <EOL> @ staticmethod <EOL> def send_email ( subject = '<STR_LIT>' , body = "<STR_LIT>" , owner = None ) : <EOL> email = EmailMessage ( subject = subject , <EOL> body = body , <EOL> to = [ owner . email ] , <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT>' } ) <EOL> email . send ( ) <EOL> def run ( self , importer = None , source = "<STR_LIT>" , owner = None , message = "<STR_LIT>" , <EOL> send_email = True , ** kwargs ) : <EOL> if not importer : <EOL> return <EOL> self . parser = importer ( source = source ) <EOL> lock_id = "<STR_LIT>" % ( self . name ) <EOL> if acquire_lock ( lock_id ) : <EOL> """<STR_LIT>""" <EOL> self . parser . is_valid ( ) <EOL> self . parser . save ( ) <EOL> message += "<STR_LIT:\n>" <EOL> if owner and owner . email and self . parser . errors : <EOL> message += mark_safe ( self . parser . errors ) <EOL> elif owner and owner . email and not self . parser . errors : <EOL> message = "<STR_LIT>" <EOL> if hasattr ( owner , '<STR_LIT:email>' ) and send_email : <EOL> self . send_email ( body = message , owner = owner ) <EOL> release_lock ( lock_id ) <EOL> else : <EOL> return <NUM_LIT:0> </s>
