示例#1
0
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])
示例#2
0
    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)
示例#3
0
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)