Tuesday, December 18, 2012

Advertise SFTP on Network using Avahi

I just realized that I can broadcast / advertise my SFTP server to other computers connected to the network.

Go to /etc/avahi/services
and create a file named sftp.service, which contains:

   %h SFTP

Now, you can see your SFTP server in Nautilus.

This guy has more complete list on what can be broadcast using avah i

Wednesday, October 17, 2012

Django Login and Sign-Up Page Together

It is very common to combine login and sign-up form in one page, so I implemented it in django. Here's how I do it.

First, create UserForm class which contains whatever information you need, such as: username, password, full name, address, email, gender, etc.

Second, add login url to your urls.py, something like the following:
(r'^/login/$', 'django.contrib.auth.views.login', { 'template_name': 'myapp/login.html', 'reg_form': UserForm() }),

Third, create in your template folder myapp/login.html which content is like shown in docs.djangoproject.com/en/dev/topics/auth/ (search for sample registration/login.html)
Make sure you use the exact same variable name (form, next, etc), unless you know what you are doing.
Also, in this template, add another html form and render your UserForm object (in my case the variable name is reg_form.

Fourth, create sign_up view, which perform whatever it is necessary in order to save the new user. Make sure you add { 'form': django.contrib.auth.forms.AuthenticationForm() } to the context data.

Sixth, add sign_up url to your urls.py, something like the following:
(r'^/sign-up/$', myapp.views.sign_up ),

I created my own User class with additional information that django user (auth_user) does not provide. In order to do that, I create (custom) User with a OneToOne relationship with django user which is also the primary_key.

That's how I do it. Hopefully, give you some ideas if you want to implement something similar.

Saturday, August 4, 2012

Consuming Django Tastypie RESTful WS with Apache HTTPClient and GSON

Creating RESTful Web Service is so simple using Django Tastypie. It's pretty much just about registering which model you want to expose. Even more amazing is that filtering feature. Read here to get more info about it.

I decided to create Java frontend to replace the existing, which uses Python Gtk. To keep with the existing look and feel I use java-gnome binding. The transition from python gtk to java-gnome is quite smooth. Java-gnome has such a good design, so the only thing that I need to for the transition is just adapting to the java paradigm, which is wonderful.

To get the JSON object from the web service, I chose Apache HTPClient, just to make things simple. I know that there are a lot of java REST client, such as restlet, jersey, etc. I stick with the simplest one.
The next thing is to figure out how to transform JSON result from the RESTful web service to POJO.  Here comes GSON to the rescue. I did not want to use python attribute name convention in my java codes. So, this one I need to solve. Fortunately, GSON comes with clever and handy feature named FieldNamingPolicy. Obviously they have really good experience in different programming language hence this feature available.

To give example on how to use it, taken from the site:

SomeObject someObject = new SomeObject("first", "second");
Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
String jsonRepresentation = gson.toJson(someObject);

Awesome! That simple and you can keep the java attribute naming convention and make your java codes look good.

Friday, July 6, 2012

Debian with Ubuntu Fonts

Here is how I configure my Debian system to use the same font config as Ubuntu.
Simply download, fontconfig-config_2.8.0-3ubuntu9_all.deb. The link will take you to Ubuntu 12.04 fontconfig-config deb package.
After you saved that deb file, extract it to get its contents:
$ dpkg-deb -x fontconfig-config_2.8.0-3ubuntu9_all.deb /tmp/extract/

then replace the content of Debian's /etc/fonts/* with data from /tmp/extract/etc/fonts/*

$ sudo cp -r /tmp/extract/etc/fonts/* /etc/fonts/

Logout and log back in to see the change.

Thursday, May 3, 2012

Using CSS to Make Your HTML Page Printer Friendly

Besides for styling for regular media (such as screen / pc monitor), css can also be used to make the web layout suitable for printing.
Perhaps you have a web page which has logo, menu, navigation, videos, and other things on it. Also there is a table or other information which you actually need. You would not want those irrelevant elements cluttering all over when you print it, right?

To solve it, simple use specific stylesheet for printing purpose and define it for "print" media. Here is an example of using external stylesheet for "print" media:

Next step is to eliminate those unnecessary elements using display: none. Here's an example:
div#menu {
display: none;

Now, as a bonus, I found out a way to make the default printing orientation as landscape. Here's it is:

@page {
size: landscape;

Another interesting thing that I found during my work is that !important rule defined in other css for any other media other than "print" will also effects the "print" media layout. I did not expect that, since I assume that !important rules defined for "screen" media, for example, would only effect that particular media. So, make sure you override the !important rule in the "print" css.

Monday, April 9, 2012

Django Blog Zinnia Limit User to Own Entry/Post

I just need a simple yet flexible django based blog application, and found django-blog-zinnia. I really admire how simple it is. The only major adjustment that I need is to limit user to his/her own blog entry/post.

Combining basic django (admin) knowledge with soome info from django-blog-zinnia documentation, I found a simple solution. Of course this is related with django-blog-zinnia's admin interface, and there is a documentation about how to make some modification to the EntryAdmin. Read more from here.

The idea is to create a django application and create admin.py on that app's top level directory (the same location with models.py).

Here is how my admin.py codes look like:
############# admin.py #################

from django.contrib import admin
from zinnia.models import Entry
from zinnia.admin.entry import EntryAdmin

class FilteredEntryAdmin(EntryAdmin):
    def queryset(self, request):
        qs = super(FilteredEntryAdmin, self).queryset(request)
        if request.user.is_superuser:
            return qs
        return qs.filter(authors=request.user)

admin.site.register(Entry, FilteredEntryAdmin)

############# end of admin.py #################

Remember to add the newly created app in settings.py INSTALLED_APPS, and make sure you put it below zinnia.

Django's documentation is quite complete and comprehensive. To get more information about ModelAdmin, queryset, etc, go to this link. From there I got the idea of overriding the queryset and use filter to limit the entry based on user.