def handle(self, *args, **options): verbosity = int(options.get('verbosity')) if verbosity > 0: self.stdout.write('Syncing e-mails') directory = os.path.join(settings.EMAIL_HTML_DATA_DIRECTORY) files_with_email_html_body = [ filename for filename in os.listdir(directory) if filename.endswith('.html') ] for filename in files_with_email_html_body: try: template_obj = get_email_template_model().objects.get( slug=filename[:-5]) with codecs.open(os.path.join(directory, filename), 'r', encoding='utf-8-sig') as file: template_obj.change_and_save(body=file.read()) except ObjectDoesNotExist: if verbosity > 0: self.stderr.write( 'Template model with slug "{}" does not exists'.format( filename[:-5])) if verbosity > 0: self.stdout.write( self.style.SUCCESS('Synced "{}" e-mail templates'.format( len(files_with_email_html_body))))
def dump_template_model(self, directory, indent): print(directory) EmailTemplate = get_email_template_model() if EmailTemplate.objects.exists(): emails_directory = os.path.join(directory) if not os.path.exists(emails_directory): os.makedirs(emails_directory) data = serializers.serialize('python', EmailTemplate.objects.all()) for obj in data: with codecs.open(os.path.join(emails_directory, '{}.html'.format(obj['pk'])), 'w', encoding='utf-8-sig') as file: file.write(obj['fields']['body']) del obj['fields']['body'] with codecs.open(os.path.join(emails_directory, 'data.json'), 'w') as file: file.write( json.dumps(data, cls=serializers.json.DjangoJSONEncoder, indent=indent)) self.stdout.write('Stored: "{}" records'.format(len(data))) else: self.stdout.write('No records')
def send_template(recipient, slug, context_data, related_objects=None, attachments=None, tag=None): """ Helper for building and sending e-mail message from a template. :param recipient: e-mail address of the receiver :param slug: slug of the e-mail template :param context_data: dict of data that will be sent to the template renderer :param related_objects: list of related objects that will be linked with the e-mail message with generic relation :param attachments: list of files that will be sent with the message as attachments :param tag: string mark that will be saved with the message :return: e-mail message object or None if template cannot be sent """ return _send_template( recipient=recipient, slug=slug, context_data=context_data, related_objects=related_objects, tag=tag, template_model=get_email_template_model(), attachments=attachments, )