def detail(request, pk): """ View a buddy profile """ buddy = get_object_or_404(Buddy, pk=pk) # if not viewing own profile, restrict to organisations try: request.user.buddy.get(pk=pk) except Buddy.DoesNotExist: try: organisation = request.user.organisation.all()[0] except IndexError: return HttpResponseForbidden() try: organisation = request.user.organisation.all()[0] except IndexError: organisation = None if organisation: message_form = MessageForm(request.POST or None, initial={ 'message': '''Dear <name>, Would you like to help out doing ....... If you are able to help, you would need to be able to .... Please reply with your email address and phone number so that we can get in touch with you. Kind regards, <name>''' }) if message_form.is_valid(): contact = ContactLog( **{ 'service': organisation, 'buddy': buddy, 'message': message_form.cleaned_data['message'] }) contact.save() subject = 'You have a new message from refugeebuddy.org' message = render_to_string('buddies/email/buddy_message.txt', {'contact': contact}) send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [buddy.email]) messages.success(request, 'Your message has been sent') return redirect('.') else: message_form = None return render_to_response('buddies/detail.html', { 'buddy': buddy, 'message_form': message_form }, context_instance=RequestContext(request))
def test_contactlog(self): user = users.User(email='*****@*****.**') rs = Ruleset(name='Foo Bar') rs.put() cl = ContactLog(name='Quux', ruleset=rs, owner=user) cl.put() cl2 = db.get(cl.key()) self.assertEquals('Quux', cl.name) self.assertEquals(rs, cl.ruleset) self.assertEquals(user, cl.owner) self.assertTrue(cl.when_created)
def detail(request, pk): """ View a buddy profile """ buddy = get_object_or_404(Buddy, pk=pk) # if not viewing own profile, restrict to organisations try: request.user.buddy.get(pk=pk) except Buddy.DoesNotExist: try: organisation = request.user.organisation.all()[0] except IndexError: return HttpResponseForbidden() try: organisation = request.user.organisation.all()[0] except IndexError: organisation = None if organisation: message_form = MessageForm(request.POST or None, initial = {'message' : '''Dear <name>, Would you like to help out doing ....... If you are able to help, you would need to be able to .... Please reply with your email address and phone number so that we can get in touch with you. Kind regards, <name>'''}) if message_form.is_valid(): contact = ContactLog(**{ 'service': organisation, 'buddy': buddy, 'message': message_form.cleaned_data['message'] }) contact.save() subject = 'You have a new message from refugeebuddy.org' message = render_to_string('buddies/email/buddy_message.txt', {'contact': contact}) send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [buddy.email]) messages.success(request, 'Your message has been sent') return redirect('.') else: message_form = None return render_to_response('buddies/detail.html', { 'buddy': buddy, 'message_form': message_form }, context_instance=RequestContext(request))