Posts de ‘Fábio M. Costa’

[Fabio M Costa] django-meio-easytags 0.4 released!

Wednesday, March 2nd, 2011

I just released the version 0.4 of the django-meio-easytags. I added a subclass of django’s default template tags Library. And now it’s easier to create a template tag. Take a look.

Projects page

Read the Docs

Download from Github

[Fabio M Costa] django-meio-easytags 0.4 released!

Wednesday, March 2nd, 2011

I just released the version 0.4 of the django-meio-easytags. I added a subclass of django’s default template tags Library. And now it’s easier to create a template tag. Take a look.

Projects page

Read the Docs

Download from Github

[Fabio M Costa] django-meio-easytags 0.3 released!

Saturday, February 26th, 2011

I released today the version 0.3 of django-meio-easytags. Now it supports template tags that accepts positional arguments (*args) and keyword arguments (**kwargs). If you don’t know how to use this, take a look at Python FAQ. In this release I included some documentation and created a page for the project.

Any doubts, suggestions, feature requests, feel free to mail me, open an issue at Github or leave a comment.

[Fabio M Costa] django-meio-easytags 0.3 released!

Saturday, February 26th, 2011

I released today the version 0.3 of django-meio-easytags. Now it supports template tags that accepts positional arguments (*args) and keyword arguments (**kwargs). If you don’t know how to use this, take a look at Python FAQ. In this release I included some documentation and created a page for the project.

Any doubts, suggestions, feature requests, feel free to mail me, open an issue at Github or leave a comment.

[Fabio M Costa] django-meio-easytags released!

Monday, February 21st, 2011

I just released a project I have been working today. A long time ago, I was tired of parsing my template tags by hand, then I created a BaseNode to do this work for me and all I had to do was subclass it and define the method to render the context.

Last friday, my friend Diego Fleury asked me for some help developing a template tag and I talked to him about my old BaseNode. When I showed the code to him, he didn’t understand it so did I. So I wanted to make it cleaner and developed the project django-meio-easytags. It’s hosted at Github (http://github.com/vbmendes/django-meio-easytags/).

It’s very easy to create your own template tags with it. Take a look: https://github.com/vbmendes/django-meio-easytags/blob/master/README

Any comments and contributions are welcome.

[Fabio M Costa] django-meio-easytags released!

Monday, February 21st, 2011

I just released a project I have been working today. A long time ago, I was tired of parsing my template tags by hand, then I created a BaseNode to do this work for me and all I had to do was subclass it and define the method to render the context.

Last friday, my friend Diego Fleury asked me for some help developing a template tag and I talked to him about my old BaseNode. When I showed the code to him, he didn’t understand it so did I. So I wanted to make it cleaner and developed the project django-meio-easytags. It’s hosted at Github (http://github.com/vbmendes/django-meio-easytags/).

It’s very easy to create your own template tags with it. Take a look: https://github.com/vbmendes/django-meio-easytags/blob/master/README

Any comments and contributions are welcome.

[Fabio M Costa] Domain redirect keeping the URL path with .htaccess

Friday, July 16th, 2010

I had a site hosted at a domain and the client asked me to change the domain. But it’s a very bad idea to simply change a site’s address without thinking about all the users who have bookmarked the old site. It means losing all of them. So I thought that i could do a redirect from the old domain to the new one. This redirect should be using HTTP status code 301 to identify that the site has moved permanently. There are many ways to do such a thing. But one took my attention. Why not use Apache .htaccess file associated with mod_rewrite? Most of the hosting services supports this. Firstly I tried this:

RewriteEngine On
RewriteBase /
Redirect 301 / http://mynewdomain.com/

This way, all requests to the root of my old domain get redirected to the root of my new domain. But what if I try yo access some path inside my old domain? It would raise a 404 error because it does not have the content anymore. I want that if the user access http://myolddomain.com/path/ it redirects him to http://mynewdomain.com/path/. So I ended up with this:

RewriteEngine On
RewriteBase /
RedirectMatch 301 (.*)$ http://mynewdomain.com$1

[Fabio M Costa] Domain redirect keeping the URL path with .htaccess

Friday, July 16th, 2010

I had a site hosted at a domain and the client asked me to change the domain. But it’s a very bad idea to simply change a site’s address without thinking about all the users who have bookmarked the old site. It means losing all of them. So I thought that i could do a redirect from the old domain to the new one. This redirect should be using HTTP status code 301 to identify that the site has moved permanently. There are many ways to do such a thing. But one took my attention. Why not use Apache .htaccess file associated with mod_rewrite? Most of the hosting services supports this. Firstly I tried this:

RewriteEngine On
RewriteBase /
Redirect 301 / http://mynewdomain.com/

This way, all requests to the root of my old domain get redirected to the root of my new domain. But what if I try yo access some path inside my old domain? It would raise a 404 error because it does not have the content anymore. I want that if the user access http://myolddomain.com/path/ it redirects him to http://mynewdomain.com/path/. So I ended up with this:

RewriteEngine On
RewriteBase /
RedirectMatch 301 (.*)$ http://mynewdomain.com$1

[Fabio M Costa] get_first_object_or_none shortcut for Django

Sunday, March 28th, 2010

I will talk about a shortcut I developed for the Django framework and always use in my apps. This code is aimed in making a quik shortcut to obtain the first object of a queryset if it exists or None otherwise.

It’s very useful when you want to display the last news in the first page of your website, but don’t want it to break if there isn’t one to show. It’s just a use case for such a code, but there are many more.

In the past I used to write something like this:

try:
    first_user = User.objects.all()[:1][0]
except IndexError:
    first_user = None

This is four lines to do a very simple task and I wanted to evolve this to use only one line of code. So I developed a function that do this to me. It’s inspired in the django.shortcuts.get_object_or_404. Let’s take a look at the code:

def get_first_object_or_none(queryset):
    try:
        return queryset[:1][0]
    except IndexError:
        return None

I’ve put this code in a module called shortcuts inside my project and now everytime I want to use it I can write this code:

from shortcuts import get_first_object_or_none
first_user = get_first_object_or_none(User.objects.all())

I think it’s a very good result and helps a lot in my coding. It also made my code more legible and easy to understand. It increased my productivity and I am publishing to increase others productivity also. Any doubts, questions os suggestions, please leave a comment.

[Fabio M Costa] get_first_object_or_none shortcut for Django

Sunday, March 28th, 2010

I will talk about a shortcut I developed for the Django framework and always use in my apps. This code is aimed in making a quik shortcut to obtain the first object of a queryset if it exists or None otherwise.

It’s very useful when you want to display the last news in the first page of your website, but don’t want it to break if there isn’t one to show. It’s just a use case for such a code, but there are many more.

In the past I used to write something like this:

try:
    first_user = User.objects.all()[:1][0]
except IndexError:
    first_user = None

This is four lines to do a very simple task and I wanted to evolve this to use only one line of code. So I developed a function that do this to me. It’s inspired in the django.shortcuts.get_object_or_404. Let’s take a look at the code:

def get_first_object_or_none(queryset):
    try:
        return queryset[:1][0]
    except IndexError:
        return None

I’ve put this code in a module called shortcuts inside my project and now everytime I want to use it I can write this code:

from shortcuts import get_first_object_or_none
first_user = get_first_object_or_none(User.objects.all())

I think it’s a very good result and helps a lot in my coding. It also made my code more legible and easy to understand. It increased my productivity and I am publishing to increase others productivity also. Any doubts, questions os suggestions, please leave a comment.