def send(self): try: model_name = ContentType.objects.get_for_model(self.related_object) except: set_log('info', 'The related object has been deleted when trying to send mail to {0}'.format(self.email_addresses)) self.is_sent = True return True emails = self.email_addresses.split(',') sended = [] for email in emails: subject, text, html = getattr(self, '_'.join(['_pack', model_name.name.lower().replace(' ', '_')]))(email) message = EmailMultiAlternatives(subject, text, settings.DEFAULT_FROM_EMAIL, [email]) if html: message.attach_alternative(html, "text/html") try: message.send() except Exception, e: set_log('error', 'Mail Server eror: ' + str(e)) self.email_addresses = ','.join(set(emails) - set(sended)) return False else: sended.append(email) self.mark_sent()
def auto_handle(self, handler): if handler.is_valid() and handler.parse(): file_name = self.get_filename() if file_name: if len(handler.parsed) > 1: filenames = [str_crc32(file_name + str(i)) for i in range(len(handler.parsed))] else: filenames = [str_crc32(file_name)] else: filenames = [] ppics = handler.save(filenames=filenames, save_origin=self.save_origin, save_dims=self.save_dims) if not ppics: set_log('error', handler.sys_error) return JsonResponse(status=0, msg=u'发生错误,请重试', content_type='text/html') if hasattr(self, 'after_saving'): self.after_saving(ppics) return JsonResponse(status=1, data={'images': ppics}, content_type='text/html') if handler.sys_error: set_log('error', handler.sys_error) return JsonResponse(status=0, msg=handler.error, content_type='text/html')
def run(self): set_log('info', 'Email sending thread ({0}) started.'.format(self)) while True: try: if self.block: eid = conn.brpop(settings.EMAIL_PRESENDING_POOL)[1] else: eid = conn.rpop(settings.EMAIL_PRESENDING_POOL) if not eid: senders.remove(self) break self.is_working = True sending_attempt_count = 0 email = EmailWaiting.objects.get(id=int(eid)) while True: if not email.send() and sending_attempt_count <= 3: sending_attempt_count += 1 time.sleep(settings.THREAD_SEND_FAILED_SLEEPTIME) conn.lpush(settings.EMAIL_PRESENDING_POOL, eid) continue break except Exception, e: set_log('error', e) time.sleep(settings.THREAD_EXCEPTION_SLEEPTIME) if hasattr(self, 'eid'): conn.lpush(settings.EMAIL_PRESENDING_POOL, eid) finally:
def save(self, upfile, path): try: f = open(path, "wb") for chunk in upfile.chunks(): f.write(chunk) except Exception, e: set_log("error", u"Error in saving uploading file:" + e.message) return False
def save(self, upfile, path): try: f = open(path, 'wb') for chunk in upfile.chunks(): f.write(chunk) except Exception, e: set_log('error', u'Error in saving uploading file:' + e.message) return False
def get_image(self, url): print url status, data = ImageParser(url).load() print status, data if not status: set_log('error', data) return None return data
def save(self, image, basename): new = BaseImageParser([image], u_settings.IMAGES_CONF) if new.is_valid() and new.parse(): images = new.save([basename]) if images: return images[0] set_log('error', ' '.join([new.error, new.sys_error])) return None
def send_request(self): req = self.url_unparse(query=urllib.urlencode(self.query_dict)) try: res = urllib2.urlopen(req) result = json.loads(res.read()) except Exception, e: set_log('error', e) return {}
def node_pre_save(sender, **kwargs): curr = kwargs.get('instance') try: prev = Node.objects.get(id=curr.id) except Node.DoesNotExist: pass else: if not curr.file == prev.file: try: os.remove(prev.file.path) except Exception, e: set_log('error', str(e))
def remove(path, filename): '''remove file from the filesystem''' if not filename: return False fullpath = os.path.join(path, filename) try: os.remove(fullpath) return True except OSError: set_log('info', "delete file %s error" % fullpath) return False
def save(self, content, uploadpath): # TODO ... 加入对传入的 uploadpath 的支持 import base64 filename = str(uuid.uuid1()) + '.png' path = os.path.join(os.path.join(settings.MEDIA_ROOT, os.path.dirname(uploadpath))) try: if not os.path.exists(path): os.makedirs(path) f = open(os.path.join(path, filename), 'wb') f.write(base64.decodestring(content)) f.close() except Exception, e: set_log('error', str(e)) return JsonResponse(state='ERROR')
def save(self, content, uploadpath): # TODO ... 加入对传入的 uploadpath 的支持 import base64 filename = str(uuid.uuid1()) + '.png' path = os.path.join( os.path.join(settings.MEDIA_ROOT, os.path.dirname(uploadpath))) try: if not os.path.exists(path): os.makedirs(path) f = open(os.path.join(path, filename), 'wb') f.write(base64.decodestring(content)) f.close() except Exception, e: set_log('error', str(e)) return JsonResponse(state='ERROR')
def run(self): set_log('info', 'Email sending manager thead started.') self.init_sending_threads(settings.MIN_THREADS_NUM) while True: try: c_mails = conn.llen(settings.EMAIL_PRESENDING_POOL) #if not reduce(lambda x, y: x or y, [item.is_working for item in senders]: if not c_mails: mail_ids = EmailWaiting.objects.get_unconfirmed_email_ids() if mail_ids: conn.lpush(settings.EMAIL_PRESENDING_POOL, *mail_ids) c_threads = len(senders) if c_threads < c_mails / 10 and c_threads < settings.MAX_THREADS_NUM: self.init_tmp_threads(min(c_mails / 10, settings.MAX_THREADS_NUM)) set_log('debug', '{0} mail sending threads are working now.'.format(len(senders))) time.sleep(settings.MANAGER_SCANNING_SLEEPTIME) except Exception, e: set_log('error', str(e)) time.sleep(settings.MANAGER_EXCEPTION_SLEEPTIME)
def node_pre_delete(sender, **kwargs): instance = kwargs.get('instance') try: os.remove(instance.file.path) except Exception, e: set_log('error', str(e))