Thursday, October 16, 2008

SAY NO TO FLASH POPUPS!

I've been a Firefox user for a long time.

After Google Chrome has been published I decided to check it out.
After 1 day I had to switch back to Firefox.

The reason?
https://addons.mozilla.org/firefox/addon/10 (Firefox AdBlock plugin)

It's IMPOSSIBLE to watch the web without this plugin.
Flash plugins everywhere ... This is horrible.

People! Use this plugin, do not let manipulate and waste your time on
pages using this harmful type of advertising.

Boycote it! Let others know to not use it!
Admins, block ad hosts too!

I do not mean to fight with ads. I can watch ads, as long as it does not eat 100% of my CPU and some fuckin* flash poup appears! AARGH!!

THIS IS CRAZY!

Thursday, October 9, 2008

django-tables generic way for displaying tables (pagination included)

In thnis short post I would like to let you know about the great project
django-tables.

You can read more about it at:
http://blog.elsdoerfer.name/2008/07/09/django-tables-a-queryset-renderer/

In a couple of words it lets you:
- display the table using one template which lets you sort & paginate easily (filtering in plans!)

By reading the text below you will find out how to create table with sort & pagination quickly.

In usage it's very similar do django newforms (forms as of 1.0)
Example: (views.py)

class MyTable(tables.ModelTable):

  class Meta:
      model = SomeModel

def view_something(request):
  table = MyTable(queryset, order_by=request.GET.get('sort','some_field))

  return render_to_response('templates/table.html', {'table': table, 'rows' : table.rows})
# we need to return table, and  rows separately to use django-pagination (though django-tables provides its own pagination mechanism)

Now let's get to template:

{% load pagination_tags %}
{% autopaginate rows 10 %}



  {% for column in table.columns %}

  {% endfor %}


{% for row in rows %}

  {% for value in row %}

{% endfor %}


{% if column.sortable %}
{{ column }}
{% if column.is_ordered_reverse %}

{% else %}

{% endif %}
{% else %}
{{ column }}
{% endif %}
{{ value }}
{% endfor %}
{% paginate %}


That's it!

This way you get generic way of displaying tables + sorting ability.
By default, django-tables takes field names for table headers when using ModelTable (verbose_name is in plans as well).
You can exclude displaying fields too!

As for now, you can specify your own names in MyTable class.

Let's get back to our class:

class MyTable(tables.ModelTable):
  class Meta:
      model = SomeModel

  field1 = tables.Column(name="Name")
  field_we_want_to_exclude = tables.Column(visiable=Fale, sortable=False)


NOTE:
Django tables is also aware of ForeignKey relations.

Check it out!

Monday, March 3, 2008

Django ModelForm - replacement for form_for_model & form_for_instance

Each commit, Django gets more amazing.

I wrote about newforms library before. The library was a big step in Django form displaying and validating.

The first approach was to use forms.form_for_model() and forms.form_for_instance() respectively.

As Django programmer I found this a little confusing. Both were similar. The only difference was that form_for_instance() took an object instance for saving instead
of creating a new one. Recently django developers came up with a great idea - forms.ModelForm which combines both into one. It's great!

Just take a look at the example below.
Assume you want to create a new view function for creating & editing an object.
You only want to edit 3 fields (name, last_name, status). Also, you've created a
customized field type for "status", and want to use that.

Scenario #1: (forms.form_for_model && forms.form_for_instance)


def f_callback(field, **kwargs):
if field.name == "status":
return MyStatusField(**kwargs)
else:
return field.formfield(**kwargs)

def create_edit_customer(request, customer_id=None):

# check if object_id is None and if object exists
if object_id is None:
CustomerForm = forms.form_for_model(Customer,
formfield_callback=f_callback,
fields=('name','last_name','status')
else:
customer = get_object_or_404(Customer, id=customer_id)
CustomerForm = forms.form_for_instance(customer,
formfield_callback=f_callback,
fields=('name','last_name','status')

if request.method == "POST":
form = CustomerForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('customer/added/')
else:
form = CustomerForm()

return render_to_response("customers/customer_form.html", {'form' : form })




You could create your own form_for_model & form_for_instance subclass (using forms.BaseForm for example), but it would be too complicated. Hope you don't use this form often :-)

Scenario #2 (ModelForm)

1. Longer version.


class CustomerModelForm(forms.ModelForm):

class Meta:
model = Customer
fields = ('name','last_name','status')

status = MyCustomerField()

def create_edit_customer(request, customer_id=None):

if customer_id is not None:
customer = get_object_or_404(Customer, id=customer_id)
else:
customer = None


if request.method == "POST":
form = CustomerModelForm(data=request.POST, instance=customer)
if form.is_valid():
form.save()
return HttpResponseRedirect("/customer/added/")
else:
form = CustomerModelForm(instance=customer)

return render_to_response("customers/customer_form.html", {'form' : form })



2. Shorter version.


class CustomerModelForm(forms.ModelForm):

class Meta:
model = Customer
fields = ('name','last_name','status')

status = MyCustomerField()

def create_edit_customer(request, customer_id=None):

if customer_id is not None:
customer = get_object_or_404(Customer, id=customer_id)
else:
customer = None

form = CustomerModelForm(data=request.POST or None, instance=customer)

if form.is_valid():
form.save()
return HttpResponseRedirect("/customer/added/")

return render_to_response("customers/customer_form.html", {'form' : form }



We are using one form class for editing & creating objects.
We can also subclass CustomerModelForm and edit/change the way the form looks
and behave (by adding field clean() method.

Django forms library looks better each day :-)

Sunday, March 2, 2008

Vote for Gdynia in Monopoly Game

I was born in Gdynia, I grew up in Gdynia.. and I am happy to live in Gdynia.

Now it's chance for anybody to vote for Gdynia in the Worldwide Monopoly
city competition.

Log in at:
http://www.monopolyworldvote.com/pl_PL/world and vote.
You can give your vote once a day.

Thanks!

---
Urodziłem się w Gdyni, dorastałem w Gdyni i nadal mieszkam w Gdyni.
To cudowne miasto ma teraz niepowtarzalną okazję zaistnieć w
ogólnoświatowej wersji gry Monopol, w którą grają miliony
ludzi na świecie.
To niepowtarzalna szansa dla promocji naszego miasta, ale też i kraju,
czy też rejonu.

Oddaj swój głos na stronie:
http://www.monopolyworldvote.com/pl_PL/world

Możesz głosować codziennie 1 raz!

Dzięki !

Friday, February 29, 2008

FreeBSD 7.0 arrived

FreeBSD 7.0 offers new great features. You can read about them all here

With this release we can see a huge performance
improvements. Just take a look at these MySQL tests! It looks very promising.

It would be great if ULE scheduler was stable enough to be included in 7.1 release.

During my work at the company where MySQL was widely used, I had to use linuxthreads
package to get similar to linux performance (well, that's because we used FreeBSD 4.X then). Linux (mostly 2.6) is treated as a better
Operating system for MySQL. Is it high time to change it? :-). Go FreeBSD!

Take a look at graphs at:

http://people.freebsd.org/~kris/scaling/mysql.html

Monday, February 25, 2008

Some very usable django apps

I was looking for some easy to plug-in django apps
for my new service.

Here are some I decided to use:

django-tagging
django-registration
django-threadedcomments
django-voting

They seem to be rock stable, and easy to use.

Check back soon for more info about the new website.