Commits

Anonymous committed 52ab43b

[svn r33] Added mail view to datastore

added mailtools.py

Comments (0)

Files changed (2)

 from django.http import HttpResponseRedirect
 from django.shortcuts import render_to_response
 from django.template import RequestContext
+from webutils.mailtools import send_simple_email
 from webutils.datastore.models import *
 
 
 def process_form(request, template, formcl, redir, map={}):
-    ''' map should be a map of form fields to request.GET fields.
+    ''' View to store all form data into a text field (pickled)
+        map should be a map of form fields to request.GET fields.
         example: {'sale_id': 'oid'}
         This will set the initial value for "formcl" of the 
         field sale_id to whatever oid is set in request.GET
     return render_to_response(template, {
         'form': form,
         }, context_instance=RequestContext(request))
+
+
+def process_email_form(request, template, formcl, redir, map={}, defaults={}):
+    ''' View to email all form data to an email address.
+        Required form field: email_to, email_subject
+        all other options are same as process_form above
+    '''
+    if request.method == 'POST':
+        form = formcl(request.POST)
+        if form.is_valid():
+            data = {}
+            for key in form.fields.keys():
+                data[key] = form.cleaned_data.get(key, '')
+            
+            data['ip'] = request.META['REMOTE_ADDR']
+            msg = 'The following data was submitted:\n\n'
+            msg += '\n'.join(['%s: %s\n' % (k, v) for k, v in data.items()])
+            send_simple_email(
+                data['email_to'],
+                data['email_subject'],
+                {},
+                msg,
+                is_template_file=False,
+            )
+            return HttpResponseRedirect(redir)
+    else:
+        init = {}
+        for key in map.keys():
+            init[key] = request.GET.get(map[key], '')
+            init.update(defaults)
+        form = formcl(initial=init)
+
+    return render_to_response(template, {
+        'form': form,
+        }, context_instance=RequestContext(request))
+from django.conf import settings
+
+
+def send_simple_email(recip, subject, context, template,
+                from_email=None, is_template_file=True, fail_silently=False):
+    from django.template import Context, Template, loader
+    from django.core.mail import send_mail
+
+    if is_template_file:
+        # Load template from actual file in templates dir
+        msg_template = loader.get_template(template)
+    else:
+        # Template is a string, passed directly
+        msg_template = Template(template)
+
+    msg_context = Context(context)
+    msg_dict = {
+        'from_email': from_email or settings.DEFAULT_FROM_EMAIL,
+        'message': msg_template.render(msg_context),
+        'recipient_list': isinstance(recip, list) and recip or [recip],
+        'subject': subject,
+    }
+    send_mail(fail_silently=fail_silently, **msg_dict)