def code_data(request): ctx = project_settings() ctx.update({ 'full_page_url': quote((settings.URL_PREFIX+request.get_full_path()).encode('utf8')), 'PROFILE': json.dumps(request.PROFILE, ensure_ascii=False), }) return ctx
def send_email(recipient, subject, template, ctx, type, from_email='noreply', reply_to=None, to_email=None): """ To send emails to admin account set recipient=None """ # TODO: generate unsubscribe link with hash (page with confirmation); default place for it in base template context = project_settings() context.update(ctx) context['recipient'] = recipient html = render_to_string(template, context) # TODO: convert html to text # TODO: replace <a href="url">text</a> with 'text url', no GA tracking in it text = html name = str(recipient.user_id) if recipient else 'grakon' hash = hashlib.md5(name+' '+str(datetime.now())).hexdigest()[:20] params = urlencode({'mh': hash, 'utm_campaign': type, 'utm_medium': 'email', 'utm_source': 'main'}) xml = fromstring(html) for a in xml.findall('.//a'): url = a.get('href') if url.startswith(settings.URL_PREFIX): # If hash is inside url - move it to the end of the newly generated link if '#' in url: start, end = url.split('#') url = start + ('&' if '?' in url else '?') + params + '#' + end else: url += ('&' if '?' in url else '?') + params a.set('href', url) img1x1 = xml.find(".//img[@id='img1x1']") if img1x1 is not None: img1x1.set('src', img1x1.get('src')+'?mh='+hash) html = tostring(xml) from_str = u'%s <%s>' % settings.EMAILS[from_email] if to_email is None: to_email = recipient.user.email if recipient else settings.ADMIN_EMAIL # TODO: check that email has appropriate format (like no dot at the end) headers = {} if reply_to: headers['Reply-To'] = reply_to msg = EmailMultiAlternatives(subject, text, from_str, [to_email], headers=headers) msg.attach_alternative(html, "text/html") message = msg.message().as_string() # TODO: set priority email = Email(recipient=recipient, hash=hash, type=type, raw_msg=message, from_email=from_email, to_email=to_email, priority=0) email.save() send_email_task(email) # TODO: run it in celery (use select_related)
def code_data(request): ctx = {"resources": json.dumps(RESOURCE_CHOICES, ensure_ascii=False)} ctx.update(project_settings()) # Include mustache templates partials = {} for name, path in comments_templates.iteritems(): partials[name] = get_mustache_template(path) ctx["mustache_partials"] = json.dumps(partials, ensure_ascii=False).replace("\\n", " ") return render_to_response("code_data.js", ctx, mimetype="application/javascript")
def code_data(request): ctx = {} ctx.update(project_settings()) # Include mustache templates partials = {} for name, path in comments_templates.iteritems(): partials[name] = get_mustache_template(path) ctx['mustache_partials'] = json.dumps(partials, ensure_ascii=False).replace('\\n', ' ') return render_to_response('code_data.js', ctx, mimetype='application/javascript')