def create_welcome_email(user, request): # fish out all the relevant information about the user and # then create an unsent WelcomeEmail subject = u"Welcome to %s" % settings.PROJECT_NAME try: person = user.get_profile() except KungfuPerson.DoesNotExist: return None alu = AutoLoginKey.get_or_create(user) profile_url = reverse('person.view', args=(user.username, )) upload_photo_url = reverse('upload_profile_photo', args=(user.username, )) change_password_url = reverse("edit_password", args=(user.username, )) edit_style_url = reverse("edit_style", args=(user.username, )) edit_club_url = reverse("edit_club", args=(user.username, )) edit_profile_url = reverse("edit_profile", args=(user.username, )) data = locals() domain = RequestSite(request).domain base_url = 'http://%s' % domain # for every variable that ends with _url make it an absolute url # and add the _alu variable def aluify_url(url): if '?' in url: return url + '&alu=%s' % alu.uuid else: return url + '?alu=%s' % alu.uuid keys = list(data.keys()) for key in keys: if key.endswith('_url'): url = data[key] if url.startswith('/'): url = base_url + url data[key] = url data[key + '_alu'] = aluify_url(url) # now the interesting thing starts. We need to find out what they haven't # done with their profile and pester them about that. response = render(request, 'welcome-email.html', data) html = response.content html = Premailer( html, base_url=base_url, keep_style_tags=False, ).transform() return WelcomeEmail.objects.create( user=user, subject=subject, body=html, )
def create_welcome_email(user, request): # fish out all the relevant information about the user and # then create an unsent WelcomeEmail subject = u"Welcome to %s" % settings.PROJECT_NAME try: person = user.get_profile() except KungfuPerson.DoesNotExist: return None alu = AutoLoginKey.get_or_create(user) profile_url = reverse('person.view', args=(user.username,)) upload_photo_url = reverse('upload_profile_photo', args=(user.username,)) change_password_url = reverse("edit_password", args=(user.username,)) edit_style_url = reverse("edit_style", args=(user.username,)) edit_club_url = reverse("edit_club", args=(user.username,)) edit_profile_url = reverse("edit_profile", args=(user.username,)) data = locals() domain = RequestSite(request).domain base_url = 'http://%s' % domain # for every variable that ends with _url make it an absolute url # and add the _alu variable def aluify_url(url): if '?' in url: return url + '&alu=%s' % alu.uuid else: return url + '?alu=%s' % alu.uuid keys = list(data.keys()) for key in keys: if key.endswith('_url'): url = data[key] if url.startswith('/'): url = base_url + url data[key] = url data[key + '_alu'] = aluify_url(url) # now the interesting thing starts. We need to find out what they haven't # done with their profile and pester them about that. response = render(request, 'welcome-email.html', data) html = response.content html = Premailer(html, base_url=base_url, keep_style_tags=False, ).transform() return WelcomeEmail.objects.create(user=user, subject=subject, body=html, )
def process_request(self, request): if request.GET.get('alu'): uuid = request.GET.get('alu') user = AutoLoginKey.find_user_by_uuid(uuid) if user: # "forcibly" log in as this user from django.contrib.auth import load_backend, login for backend in settings.AUTHENTICATION_BACKENDS: if user == load_backend(backend).get_user(user.pk): user.backend = backend if hasattr(user, 'backend'): login(request, user) new_full_path = request.get_full_path().replace('alu=%s' % uuid, '') new_full_path = new_full_path.replace('?&','?').replace('&&','&') return HttpResponsePermanentRedirect(new_full_path) return None
def _append_autologin_urls(self, person, context): """context is a dict like this: {'first_name': u'Peter', 'profile_url': 'http://example.com/peterbe', ...} Now, for every variable that ends with '_url' add '?alu=550269bc-bc67-4085-ba1a-04f3f0290288' (or &alu=... if ? is already in the URL) """ alu = AutoLoginKey.get_or_create(person.user) for key in context.keys(): if key.endswith('_url'): url = context[key] else: continue key += '_alu' if '?' in url: url += '&alu=%s' % alu.uuid else: url += '?alu=%s' % alu.uuid context[key] = url
def test_send_newsletter_urls(self): """ create a newsletter, set a template text and render it """ # Create a KungfuPerson so it can send to someone user, person = self._create_person('bob', '*****@*****.**', first_name="Bob", last_name="Sponge") text_template = "Profile URL: {{ profile_url }}\n"\ "Site URL: {{ site_url }}\n"\ "Autologin Profile URL: {{ profile_url_alu }}\n"\ "Autologin Site URL: {{ site_url_alu }}\n" subject_template = "Newsletter no {{ newsletter_issue_no }}" n = Newsletter.objects.create(text_template=text_template, subject_template=subject_template) self.assertFalse(n.sent) n.send() sent_email = mail.outbox[0] # The body of the email should now contain full URLs # to the profile and to the site site_url_base = 'http://' + Site.objects.get_current().domain self.assertTrue((site_url_base + '/') in sent_email.body) self.assertTrue((site_url_base + person.get_absolute_url()) in sent_email.body) # the body should also contain "alu urls", e.g. # http://example.com/peterbe?alu=550269bc-bc67-4085-ba1a-04f3f0290288 alu_regex = re.compile(r'alu=([\w-]{36,})\b') uuids = alu_regex.findall(sent_email.body) self.assertEqual(len(uuids), 2) # but they should be equal self.assertEqual(uuids[0], uuids[1]) # with these it should be possible to get the user back self.assertEqual(user, AutoLoginKey.find_user_by_uuid(uuids[0]))