def import_json(): for g in grouper(1000,sys.stdin): try: Model.database.bulk_save([json.loads(l) for l in g if l]) except BulkSaveError as err: if any(d['error']!='conflict' for d in err.errors): raise else: logging.warn("conflicts for %r",[d['id'] for d in err.errors])
def _send_emails(emails, template_name, subject, sender=None, context=None, fail_silently=False, attachments=None, headers=None): """ Sends an email to a list of emails using a given template name """ if context is None: context = {} if attachments is None: attachments = [] if not settings.ENABLE_EMAILS: return text_template = get_template("emails/%s.txt" % template_name) html_template = get_template("emails/%s.html" % template_name) context = Context(context) text_content = text_template.render(context) html_content = html_template.render(context) sender = "%s <%s>" % (settings.EMAIL_SENDER_NAME, settings.SENDER_EMAIL) emails_in_groups_of_5 = utils.grouper(emails, 5) for emails in emails_in_groups_of_5: msg = EmailMultiAlternatives(subject, text_content, sender, emails, headers=headers) for attachment in attachments: attachment.seek(0) msg.attach(attachment.name, attachment.read(), 'application/pdf') msg.attach_alternative(html_content, "text/html") # do not send emails if in testing if not settings.TEST: return try: msg.send(fail_silently=fail_silently) except: time.sleep(1) msg.send(fail_silently=fail_silently)
def send_email_to(email_list, template_name, subject, sender=None, template_vars=None, fail_silently=True, attachments=None): """ Sends an email to a list of emails using a given template name """ if template_vars is None: template_vars = {} if attachments is None: attachments = [] if not settings.ENABLE_EMAILS: return text_template = get_template("emails/%s.txt" % template_name) html_template = get_template("emails/%s.html" % template_name) context = Context(template_vars) text_content = text_template.render(context) html_content = html_template.render(context) sender = "%s <%s>" % (settings.EMAIL_SENDER_NAME, settings.SENDER_EMAIL) emails_in_groups_of_5 = utils.grouper(email_list, 5) for emails in emails_in_groups_of_5: msg = EmailMultiAlternatives(subject, text_content, sender, emails) for attachment in attachments: attachment.seek(0) msg.attach(attachment.name, attachment.read(), 'application/pdf') msg.attach_alternative(html_content, "text/html") try: msg.send(fail_silently=fail_silently) except: time.sleep(1) msg.send(fail_silently=fail_silently)
def import_mongo(cls_name,path=None): Cls = globals()[cls_name] for g in grouper(1000,read_json(path)): Cls.bulk_save(Cls(from_dict=d) for d in g if d)