<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><channel><title>Samir Mamude</title><link>http://samirmamude.com/</link><description /><language>en-us</language><lastBuildDate>Tue, 14 Jul 2009 03:53:07 -0000</lastBuildDate><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/samirmamude" type="application/rss+xml" /><item><title>Off-Topic: It&amp;#39;s time to move on
</title><link>http://feedproxy.google.com/~r/samirmamude/~3/VXbCTwIth_Q/</link><description>&lt;p&gt;Hi, my blog is not dead! At the middle of last month I had a serious health problem, I had to do a surgical procedure (intestine infection). BUT thank God I'm feeling healty and back to my normal life.&lt;/p&gt;

&lt;p&gt;In the subject of this post, I'm pretend to explain a little of my experience about a huge project that I worked for over one year based on Ruby on Rails.&lt;/p&gt;

&lt;p&gt;Definitely I could tell that was a big experience for my career as programmer and person. I had a chance to know smart people, new friendships, improve my English (I still think it's bad), learn more about Ruby and other cool stuff. Of course there were bad things, but was a big learning curve.&lt;/p&gt;

&lt;p&gt;Anyway, my contract was ended, now I have two options: find another remote job with Ruby on Rails, even though worldwide crisis, or I should give more attention to my studies, as improving more my knowlegde in Python, dedicate more time to my company developing some product and get some customers.&lt;/p&gt;
</description><guid isPermaLink="false">http://samirmamude.com/2009/4/23/off-topic-its-time-to-move-on/</guid><feedburner:origLink>http://samirmamude.com/2009/4/23/off-topic-its-time-to-move-on/</feedburner:origLink></item><item><title>Goodbye PC, finally I got a Macbook!
</title><link>http://feedproxy.google.com/~r/samirmamude/~3/ZetHhmDOG2Q/</link><description>&lt;img src="http://samirmamude.com/media/img/posts/macbook.jpg" class="alignright"/&gt;
&lt;p&gt;As been for a while, I was thinking to buy a Mac, but as it is very expensive here in Brazil, I was waiting for a big reason to spend my hard money.&lt;/p&gt;

&lt;p&gt;Last Saturday (Feb 21) my old PC stopped working, don't know what happened, anyway...As I don't like to losing my time fighting with a machine, on Sunday I went to buy my Macbook.&lt;/p&gt;

&lt;p&gt;After one week using it, I can say that has been an amazing experience. Everything works properly and my current environment of work is so much better.&lt;/p&gt;

Some tools that I'm using.
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://www.macromates.com"&gt;Textmate&lt;/a&gt; - perfect, just it.&lt;/li&gt;
&lt;li&gt;&lt;a href="http://git-scm.com/"&gt;Git&lt;/a&gt; - version control system&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.phg-home.com/index_mac.html"&gt;Minuteur&lt;/a&gt; - timesheet.&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.sequelpro.com/"&gt;Sequel Pro&lt;/a&gt; - Database Management&lt;/li&gt;
&lt;li&gt;&lt;a href="http://cyberduck.ch/"&gt;Cyberduck&lt;/a&gt; - GUI for FTP, SSH, etc..&lt;/li&gt;
&lt;/ul&gt;
More suggestion are welcome!
</description><guid isPermaLink="false">http://samirmamude.com/2009/3/2/goodbye-pc-finally-i-got-a-macbook/</guid><feedburner:origLink>http://samirmamude.com/2009/3/2/goodbye-pc-finally-i-got-a-macbook/</feedburner:origLink></item><item><title>Making Django more DRY (Don&amp;#39;t Repeat Yourself)
</title><link>http://feedproxy.google.com/~r/samirmamude/~3/vzG3vwwWPAQ/</link><description>&lt;p&gt;One thing I don't like about Django is that you always needs  to write things like that:&lt;/p&gt;
&lt;pre name="code" class="py"&gt;
from django.shortcuts import render_to_response
from django.template import RequestContext

def index(request):
     render_to_response('index.html', context_instance=RequestContext(request))

def products(request):
     render_to_response('products.html', context_instance=RequestContext(request))
&lt;/pre&gt;

&lt;p&gt;
But thanks to Python, Django has a great flexibility to handle different things without making any kind of bizarre code and I guess sometimes is much better and easier than Ruby on Rails (my current environment). So, to resolve this issue, I made a research to find some ideas.
&lt;/p&gt;
&lt;p&gt; First you should create a new Middleware call ThreadLocals &lt;a href="http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser"&gt;[1]&lt;/a&gt;.&lt;/p&gt;
&lt;pre name="code" class="py"&gt;
# /yourproject/middleware/threadlocals.py
try:
    from threading import local
except ImportError:
    from django.utils._threading_local import local

_thread_locals = local()

def get_request():
    return getattr(_thread_locals, 'request', None)

class ThreadLocals(object):
    def process_request(self, request):
        _thread_locals.request = request
&lt;/pre&gt;

&lt;p&gt;The big trick here, is get an instance of the object HttpRequest &lt;a href="http://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest"&gt;[2]&lt;/a&gt;, with this, you can create a lot of useful things to deal in your project.&lt;/p&gt;
&lt;p&gt;After created the Middleware, now create a new module (http.py) with some methods.&lt;/p&gt;

&lt;pre name="code" class="py"&gt;
#  yourproject/utils/http.py

from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponse, HttpResponseRedirect
from django.utils import simplejson
from yourproject.middleware import threadlocals as tl

def render(template_name, dictionary=None):    
    dictionary = dictionary or dict()
    return render_to_response(template_name + ".html", dictionary,
            context_instance=RequestContext(tl.get_request()))

def render_json(json):
    return HttpResponse(simplejson.dumps(json), mimetype='application/json')

def httprr(url, message=""):
    if message:
        tl.get_current_user().message_set.create(message=message)
    return HttpResponseRedirect(url)
&lt;/pre&gt;

&lt;p&gt;In settings.py, don't forget to append the line 5 in the tuple MIDDLEWARE_CLASSES.&lt;/p&gt;

&lt;pre name="code" class="py"&gt;
MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'yourproject.middleware.threadlocals.ThreadLocals',
)
&lt;/pre&gt;

&lt;p&gt;As you can see, in the file views.py we have less code to deal and more DRY if you know what I mean :)&lt;/p&gt;
&lt;pre name="code" class="py"&gt;
from yourproject.utils.http import render, httprr

def index(request):
     render('index')

def products(request):
     products = Product.objects.all()
     render('products', dict(products=products))
&lt;/pre&gt;

&lt;p&gt;Pretty cool isn't ?&lt;/p&gt;

&lt;strong&gt;References:&lt;/strong&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser"&gt;http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest"&gt;http://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description><guid isPermaLink="false">http://samirmamude.com/2009/2/16/making-django-more-dry-dont-repeat-yourself/</guid><feedburner:origLink>http://samirmamude.com/2009/2/16/making-django-more-dry-dont-repeat-yourself/</feedburner:origLink></item><item><title>Log system for Django
</title><link>http://feedproxy.google.com/~r/samirmamude/~3/P_W1c1Zll-w/</link><description>&lt;p&gt;
I really like the log system of Ruby on Rails, it's very powerful to find bugs and understand how works HTTP requests.
&lt;/p&gt;
&lt;p&gt;
To get the same idea, I wrote this middleware to register all HTTP and SQL requests to a file (development.txt) and tracking this file from a terminal.
&lt;/p&gt;
&lt;pre name="code" class="py"&gt;
# middleware.py
from django.db import connection
from django.conf import settings
from datetime import datetime

class EnforceLogger(object):
  
  def process_response(self, request, response):
    file = open('log/development.txt','a')
    if not request.path_info.startswith('/media/'):
      # Http request
      text = '-' * 120
      text += '\nProcessing URL %s (for %s at %s)' % (request.path, request.META.get('REMOTE_ADDR'), datetime.now())
      text += '\n Session ID: %s' % request.COOKIES.get('sessionid')
      text += '\n Parameters: [%s] %s %s' % (request.method, request.raw_post_data, request.META.get('QUERY_STRING'))
      
      # SQL Queries
      for q in connection.queries:
        sql, time = q['sql'], q['time']
        text += ' &gt; %s\n DB Time: %s\n' % (sql, time)
          
      print &gt;&gt; file, text    
    file.close()
    return response
&lt;/pre&gt;
&lt;p&gt;
Maybe I'm reinventing the wheel, there are many solutions for log on Django, but this particular trick works pretty good to me.
&lt;/p&gt; 
&lt;p&gt;
To use, just add this new line at MIDDLEWARE_CLASSES in settings.py.&lt;/p&gt;

&lt;pre name="code" class="py"&gt;
MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
    'middleware.EnforceLogger',
)
&lt;/pre&gt;

&lt;p&gt;
Feel free to use, modify, etc...
&lt;/p&gt;

</description><guid isPermaLink="false">http://samirmamude.com/2009/2/3/log-system-for-django/</guid><feedburner:origLink>http://samirmamude.com/2009/2/3/log-system-for-django/</feedburner:origLink></item><item><title>My environment for Rails
</title><link>http://feedproxy.google.com/~r/samirmamude/~3/7REVs8sNpk4/</link><description>&lt;p&gt;I spent some months to get the right environment for work with Rails, I worked with NetBeans, Aptana, Jedit and Komodo but any of this tools I can really say "Finally a good environment for work!". They are too heavy!&lt;/p&gt;

&lt;p&gt;I think the most of programmers that are using Linux or Mac, like to use simple things like a terminal and a text-editor. As I'm using Ubuntu Linux on my environment, I decided spend some hours to configure properly some tools.&lt;/p&gt;

&lt;p&gt;You can see a preview of my desktop right here.&lt;/p&gt;
&lt;a href="http://samirmamude.com/media/img/posts/desktop.png"&gt;&lt;img src="http://samirmamude.com/media/img/posts/desktop-min.png"/&gt;&lt;/a&gt;

&lt;p&gt;As you can see, I'm using GVim with some plugins,  a terminal for tracking the log files, ssh for remote connections, Git for control version and other common tasks. For databases, I like use Squirrel, it's a good tool and when I have to deal with HTML I like to use Firebug, it's very useful.&lt;/p&gt;

&lt;p&gt;I had some difficult to handle the shortcuts of GVim at the beginning, but was just a matter of time how to deal with it.&lt;/p&gt;

&lt;p&gt;I made a conclusion that you don't need to use a IDE for programming in Ruby, since the language doesn't need to be compiled, but I have to admit that can be useful for debugging and other issues.&lt;/p&gt;
</description><guid isPermaLink="false">http://samirmamude.com/2009/1/26/my-environment-for-rails/</guid><feedburner:origLink>http://samirmamude.com/2009/1/26/my-environment-for-rails/</feedburner:origLink></item><item><title>Hello world
</title><link>http://feedproxy.google.com/~r/samirmamude/~3/RFaTEzeOWoo/</link><description>&lt;p&gt;Hi my name is Samir and I would like to present myself. I live in São José do Rio Preto - SP / Brazil, I have a little company called SAM Web Solutions.  I do outsourcing with Ruby on Rails to a foreign company located on USA.&lt;/p&gt;

&lt;p&gt;Find a job with it here in Brazil and where I live is kinda impossible, but I believe that on 2009 things will going change. Ruby on Rails is becoming more big and we have a great community behind it, this is just a matter of time to companies start to leave yours legacy applications and realize how is possible use Rails for real applications.&lt;/p&gt;

&lt;p&gt;Before you ask me, why this blog was built with Django and not with Rails, well..I've been studing Python for a while and I think the best way to learn is making a blog right? So that's it. &lt;/p&gt;

&lt;p&gt;About my blog, I hope to share some of my experiences with Ruby on Rails and another tecnologies that I like.&lt;/p&gt;
</description><guid isPermaLink="false">http://samirmamude.com/2009/1/19/hello-world/</guid><feedburner:origLink>http://samirmamude.com/2009/1/19/hello-world/</feedburner:origLink></item></channel></rss>
