Angularjs: Event handler attached to all anchor tag 'a'    Posted:


Noticed today that angular modify the behavior of 'a' tag to include some event handler by default. Discover this after getting error Object #<g> has no method 'on' error in my dev console. The error actually showing up after I added jquery to the page but the fact that angular attaching event to all 'a' tag really suprising. I have some 'normal' links I don't want any fancy behavior, just a link. Found a post on how to create new directive to disable the behavior but for now I manage to escape from this by simply not using angular together with jquery.

Comments

Django: @transaction.commit_manually and accessing db from template    Posted:


Bumped into this issue twice and because the first time it happened was almost a year ago, in different project, I'd already lost my mental note about it when it happened again for second time. Our views functions are wrapped inside @transaction.commit_manually, which look like below:-

@transaction.commit_manually
def send_message(request):
    context = {}
    # do some stuff
    transaction.commit()
    return render(request, 'path/to/template.html', context)

This work flawlessly until recently, I need to add some logic in the template that need to check certain user's permission, before displaying the part. In the template, it just some function call that access db like:-

{% if user.can_see_this %}
<a href="#">Some link</a>
{% endif %}

Django now raise TransactionManagementError complaining I have pending commit/rollback. It doesn't make sense because I can verify that the line that executed transaction.commit() was reached in the views above. Only after reading through this ticket on django issue tracker, I'd immediately remember the real issue.

Turn out this caused by new behavior of django transaction in 1.3 and above. Before 1.3, transaction only marked as dirty if we did any write database operation but since 1.3 and above, any database operation will mark the transaction as dirty. So the following code will work before 1.3:-

@transaction.commit_manually
def sendmessage(request):
    transaction.rollback()
    user = User.objects.get(pk=1)
    return HttpResponse('OK')

All database operation now will mark the transaction as dirty and since we access the db in template - which happened during render after we called commit(), it obvious now why django complain we unclosed transaction.

It should be written like below:-

@transaction.commit_manually
def send_message(request):
    context = {}
    # do some stuff
    response = render(request, 'path/to/template.html', context)
    transaction.commit()
    return response

Comments

Django From Scratch    Posted:


There are a lot of tutorials out there on Django and the official documentation also has one. For this post, I decided not to go through the typical route on how to get started with django. Let's 'ignore' the best practices and focus on what actually work and hopefully we can learn something along the way. So let's get started by downloading Django itself from the website.

Read more…

Comments

Ansible: My Playbook    Posted:


This is my first playbook that I manage to get it working. It's an achievement for me as it showed that I started to get the concept around ansible:-

Read more…

Comments

Ansible-Playbook: Specify hosts on the command line    Posted:


While we can specify what hosts to run the command through ansible command-line program, it's does not apply to ansible-playbook program. We always need to specify the hosts: attribute in our YAML playbook. It mean if we were to run someone's else playbook, we always have to edit the playbook to customize the hardcoded hosts: attribute.

Googling for this, I found the workaround (which a great resource in itself to gain insight what ansible capable for). We can use variables. So instead of the following playbook:-

- hosts: myhostname
  tasks:
    - name: Add user
      user: name=kamal
            groups=admin
            shell=/bin/bash

We can write it as:-

- hosts: $hosts
  tasks:
    - name: Add user
      user: name=kamal
            groups=admin
            shell=/bin/bash

And then run our playbook as:-

ansible-playbook -u root --extra-vars="hosts=myhostname"

Comments

Hosting multiple apps at digital ocean droplet    Posted:


This is to document a setup that I plan on a digital ocean droplet. It should allow us to host applications of different platforms to co-exists side by side.
This initially inspired by dokku setup but dokku still has some rough edges making it not ready yet for production setup. This setup simply eliminate [docker] and run application natively on the host instead inside a container.

Based on diagram below, we'll use nginx as our frontend server. It'll not do much other than forwarding requests to the backend apps running on different port. Each app will run inside specific user account so in theory it should allow us to host apps for multiple users. Some planning on port assignment is needed however in case you want to go in this route. For example user1 will use port range 10000 and user2 using port 11000 space.

If you want to skip the write up and straightly get your hand dirty, just clone the github repo and fix all the path to suit your environment.

Read more…

Comments

About me

Web developer in Malaysia. Currently work at MARIMORE Inc building internet services using Python and Django web framework.

ImportError is an error message emitted by Python when it failed to load certain module as requested by programmer. It's a very common error when someone new to the language trying it out. This website on the same theme, will try provide help for newcomers on any technologies to overcome their first hurdle.

Try most of the examples you may find here on Digital Ocean cloud service. They provide excellent VPS at a very cheaper price. Using this referral link you'll get USD10 credits upon sign up. That's enough to run single VPS with 1GB RAM for a month.

Others

I can also be found at the following sites:-

  • http://k4ml.blogspot.com/
  • http://k4ml.github.io/
  • http://metak4ml.blogspot.com/
  • http://www.mydev.my/
  • http://github.com/k4ml/

Disclaimers

The postings on this site are my own and don't necessarily represent my employer's positions, strategies or opinions.

Share