Commits

Joel Rivera committed bb423e4

Update locale messages and implement the backend (send email) of the contact form.

  • Participants
  • Parent commits 060502f

Comments (0)

Files changed (7)

 # http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/environment.html
 ###
 
+
+
 [app:main]
 use = egg:joelmx
-analytics = 'UA-3765019-12'
+analytics = UA-3765019-12
 mako.directories = joelmx:templates
 pyramid.reload_templates = true
 pyramid.debug_authorization = false
 pyramid.includes =  pyramid_debugtoolbar
 available_languages = en es
 
+
+
 # By default, the toolbar only appears for clients from IP addresses
 # '127.0.0.1' and '::1'.
 # debugtoolbar.hosts = 127.0.0.1 ::1

joelmx/__init__.py

 #import logging; log = logging.getLogger(__name__)
-
 from pyramid.config import Configurator
 
 
-
 def locale_businessman(req):
     avail_langs = req.registry.settings['available_languages'].split()
     if 'lang' in req.params \
     """
     config = Configurator(settings=settings,
                           locale_negotiator=locale_businessman)
+    config.include('pyramid_mailer')
     config.add_translation_dirs('joelmx:locale/')
     config.add_subscriber('joelmx.subscribers.add_renderer_globals',
                           'pyramid.events.BeforeRender')
                           'pyramid.events.NewRequest')
     config.add_static_view('/static', 'static', cache_max_age=3600)
     config.add_route('home', '/')
+    config.add_route('contact', '/contact')
     config.scan()
     return config.make_wsgi_app()

joelmx/locale/es/LC_MESSAGES/joelmx.mo

Binary file modified.

joelmx/locale/es/LC_MESSAGES/joelmx.po

 msgstr ""
 "Project-Id-Version: joelmx 0.0\n"
 "Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
-"POT-Creation-Date: 2013-02-10 03:25-0600\n"
-"PO-Revision-Date: 2013-02-10 03:31-0600\n"
+"POT-Creation-Date: 2013-02-12 00:59-0600\n"
+"PO-Revision-Date: 2013-02-12 01:16-0600\n"
 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
 "Language-Team: es <LL@li.org>\n"
 "Plural-Forms: nplurals=2; plural=(n != 1)\n"
 "Content-Transfer-Encoding: 8bit\n"
 "Generated-By: Babel 1.0dev\n"
 
-#: joelmx/templates/home.mako:6
+#: joelmx/templates/home.mako:14
 msgid "Hi!, my name is Joel Rivera."
 msgstr "¡Hola!, me llamo Joel Rivera."
 
-#: joelmx/templates/home.mako:7
+#: joelmx/templates/home.mako:15
 msgid ""
 "I'm a software developer from Monterrey Mexico with a particular taste to"
 " software libre, python and coffee."
-msgstr "Soy un desarrollador de software de Monterrey México con un gusto particular por el software libre, python y el café."
+msgstr ""
+"Soy un desarrollador de software de Monterrey México con un gusto "
+"particular por el software libre, python y el café."
 
-#: joelmx/templates/home.mako:8
+#: joelmx/templates/home.mako:20
 msgid "Here is my"
 msgstr "Aquí esta mí"
 
-#: joelmx/templates/home.mako:8
+#: joelmx/templates/home.mako:22
 msgid "and a bunch of other links"
 msgstr "y otro puñado de ligas"
 
+#: joelmx/templates/home.mako:50
+msgid "Contact me"
+msgstr "Contacto"
+
+#: joelmx/templates/home.mako:57
+msgid "Name"
+msgstr "Nombre"
+
+#: joelmx/templates/home.mako:61
+msgid "Email"
+msgstr "Email"
+
+#: joelmx/templates/home.mako:65
+msgid "Message"
+msgstr "Mensaje"
+
+#: joelmx/templates/home.mako:72
+msgid "Send"
+msgstr "Enviar"
+

joelmx/locale/joelmx.pot

 msgstr ""
 "Project-Id-Version: joelmx 0.0\n"
 "Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
-"POT-Creation-Date: 2013-02-10 03:25-0600\n"
+"POT-Creation-Date: 2013-02-12 00:59-0600\n"
 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
 "Language-Team: LANGUAGE <LL@li.org>\n"
 "Content-Transfer-Encoding: 8bit\n"
 "Generated-By: Babel 1.0dev\n"
 
-#: joelmx/templates/home.mako:6
+#: joelmx/templates/home.mako:14
 msgid "Hi!, my name is Joel Rivera."
 msgstr ""
 
-#: joelmx/templates/home.mako:7
+#: joelmx/templates/home.mako:15
 msgid ""
 "I'm a software developer from Monterrey Mexico with a particular taste to "
 "software libre, python and coffee."
 msgstr ""
 
-#: joelmx/templates/home.mako:8
+#: joelmx/templates/home.mako:20
 msgid "Here is my"
 msgstr ""
 
-#: joelmx/templates/home.mako:8
+#: joelmx/templates/home.mako:22
 msgid "and a bunch of other links"
 msgstr ""
 
+#: joelmx/templates/home.mako:50
+msgid "Contact me"
+msgstr ""
+
+#: joelmx/templates/home.mako:57
+msgid "Name"
+msgstr ""
+
+#: joelmx/templates/home.mako:61
+msgid "Email"
+msgstr ""
+
+#: joelmx/templates/home.mako:65
+msgid "Message"
+msgstr ""
+
+#: joelmx/templates/home.mako:72
+msgid "Send"
+msgstr ""
+

joelmx/templates/home.mako

     <h4>${_("I'm a software developer from Monterrey Mexico with a particular taste to software libre, python and coffee.")} </h4>
   </div>
   
-  
   <div id="links">
     <p>
       ${_("Here is my")}
     </p>  
     
     <div>
-      <a href="http://www.linkedin.com/in/joelriv" >
+      <a href="http://www.linkedin.com/in/joelriv/${request.locale_name}" >
 	<img alt="LinkedIn" src="${request.static_url('joelmx:static/img/linkedin.png')}" >
       </a>
       <a href="http://stackoverflow.com/users/298371/cyraxjoe">
   </a>
 
   <div id="contactme" class="hide">
-    <div class="formfield">
-      <label for="name">${_("Name")}</label>
-      <input type="text" id="name"  name="name" style="width: 10em;" value="">
-    </div> 
-    <div class="formfield">
-      <label for="email">${_("Email")}</label>
-    <input type="text" id="email" name="email" style="width: 10em;" value="" >
-    </div> 
-    <div class="formfield">
-      <label for="message">${_("Message")}</label>
-      <textarea style="width: 60%;" name="message" rows="5"></textarea>
-      
-    </div> 
-    
-    <input type="submit" value="${_('Send')}" style="float: right; margin-right: 20px;"/>
+    <form method="POST" id="contact" action="/contact">
+      <input type="hidden" name="cidx" value="${cidx}" />
+      <div class="formfield">
+	<label for="name">${_("Name")}</label>
+	<input type="text" id="name"  name="name" style="width: 10em;" value="">
+      </div> 
+      <div class="formfield">
+	<label for="email">${_("Email")}</label>
+	<input type="text" id="email" name="email" style="width: 10em;" value="" >
+      </div> 
+      <div class="formfield">
+	<label for="message">${_("Message")}</label>
+	<textarea style="width: 60%;" name="message" rows="5"></textarea>      
+      </div> 
+      <div class="formfield">
+	<label for="clg">${challenge}</label>
+	<input type="text" name="clg" id="clg" value="" />	
+      </div>
+      <input type="submit" value="${_('Send')}" style="float: right; margin-right: 20px;"/>
+    </form>
   </div>
-
   <div id="twitter">
     <%include file="_tw_widget.mako" />
   </div>
+import logging; log = logging.getLogger(__name__)
+import random
+
 from pyramid.view import view_config
+from pyramid.httpexceptions import HTTPClientError
+from pyramid_mailer import get_mailer
+from pyramid_mailer.message import Message
+
+CHALLENGES = {
+        'en': [("What's my first name?", 'joel'),
+               ("What's my last name?", 'rivera'),
+               ("Two plus one?", 'three', '3'),
+               ("Did Joel lives in Brazil?", 'no')],
+        'es': [('¿Cuál es mí primer nombre?', 'joel'),
+               ('¿Cuál es mí apellido?', 'rivera'),
+               ('¿Dos más uno?', ('three', '3')),
+               ('¿Joel vive en Brasil?', 'no')],
+        }
 
 
 @view_config(route_name='home', renderer='home.mako')
 def homepage(request):
-    return {}
+    challenges = _get_challenges(request)
+    idx = random.randint(0, len(challenges) - 1)
+    return {'challenge': challenges[idx][0],
+            'cidx': idx}
 
 
-#@view_config(route_name='contactme')
-#def contact_me(request):
-#    pass
+@view_config(route_name='contact', renderer='string', request_method='POST')
+def contact_me(request):
+    POST = request.POST
+    if _is_a_valid_contact_form(POST):
+        try:
+            cidx, response = int(POST['cidx']), POST['clg']
+            challenges = _get_challenges(request)
+            challenge = challenges[cidx]
+        except IndexError:
+            return HTTPClientError('Invalid challenge.')
+        else:
+            if not _is_a_robot(challenge, response):
+                return _send_email(request)
+            else:
+                return HTTPClientError(
+                    'This is not correct: "%s : %s"' %
+                    (challenge[0], response))
+    else:
+        return HTTPClientError('Invalid form fields.')
+
+
+
+def _get_challenges(request):
+    return CHALLENGES[request.locale_name]
+
+
+def _is_a_valid_contact_form(POST):
+    reqfields = {'name', 'email', 'message', 'clg', 'cidx'}
+    missing_fields =  reqfields - set(POST) 
+    return not missing_fields \
+           and POST['cidx'].isdigit()
+        
+
+def _is_a_robot(challenge, resp):
+    response = resp.strip().lower()
+    vresponses = challenge[1:]
+    for vresp  in vresponses:
+        if vresp == response:
+            return False
+    else:
+        return True
+
+    
+def _send_email(request):
+    POST = request.POST
+    settings = request.registry.settings
+    mailer = get_mailer(request)
+    recipients = (settings['mail_recipient'],)
+    message = Message(sender=settings['mail_sender'],
+                      recipients=recipients,
+                      subject='Contact via joel.mx'  ,
+                      extra_headers={'Reply-To':'%s<%s>' %
+                                     ((POST['name'], POST['email'])),},
+                      body=POST['message'])
+    mailer.send_immediately(message)
+    return 'ok'