abidibo.net

Weblog

Serve private media with angular, DRF, CORS and token authentication

angularjs django drf rest

A long time has passed since my last post, I was fighting hard with a project learning django REST framework and angularjs, and now time has come to share some interesting knowledges.

This entry talks about serving private media from a django REST webservice, developed with django REST framework, to angularjs, in a scenario which uses a token authentication schema and CORS requests.

The problems to face

Let's see the problems we have to deal with.

  • Media with django are normally served directly by the web server, so they are out of the authentication/permission logic of the application
  • The token authentication schema needs every request to contain an header 'Authentication' part providing the token key (also when requesting files ...

read the full post


Decorators with Python

decorator patterns python

Decorators in python can be quite easy to use and understand, but flexible decorators using the new syntax introduced by python 2.4 can be a bit more complex.

Here I'll try to explain (especially to myself) the world of decorators in python, starting from simple cases and ending with complex ones. Ideas and notions taken from the Pro Django book by Marty Alchin.

Decorators without extra arguments

Let's create a simple cache decorator you can use to store complex operations:

>>> def cache(func):
...   cache_dict = {}
...   def wrapper(*args, **kwargs):
...     key = '%s-%s' % (repr(args), repr(kwargs))
...     if key not in cache_dict:
...       cache_dict[key] = func(*args, **kwargs)
...       print 'cached'
...     return cache_dict[key]
...   return wrapper

A simple decorator function takes ...

read the full post


Python's excess arguments

programming python

What I've always really appreciated of python is the ability to give a function an arbitrary number of positional and keyword arguments, something that in PHP is not possible and is indeed very powerful.

When declaring a function, we can define 4 different types of arguments:

  • required arguments
  • optional arguments (a default is provided)
  • excess positional arguments
  • excess keyword arguments

for example:

def myfunc(a, b=3, *c, **d)
  • a is a required argument
  • b is an optional one since it's default value it's 3
  • the single asterisk before the c argument allows the function to accept any number of positional arguments
  • the two asterisks before the d argument allow the function to support arbitrary kewyword arguments

The order of ...

read the full post


How to get the best of your U8 smartwatch

android smartwatch tasker u8

Yesterday my new U8 smartwatch arrived to me after a long trip around the world.
I'm happy with it, ok it is not as smart as the samsung gear or the sony smartwatch, but it's a super cheap product and you can't expect too much from it.

Nevertheless it has some nice functionalities:

  • receive and make calls from your watch
  • receive phone notifications
  • control your camera and take photos
  • sync your phonebook and call log
  • play music
  • anti lost feature
  • barometer, altimeter and pedometer

Wow, but it's not my intention to talk here about such things, there are trillions of youtube videos reviewing them on youtube.

Here I want to deepen how we can use two of such functions in ...

read the full post


How to exclude choices of m2m raw_id_field in django admin

django programming django-admin

This procedure took a lot of effort to be understood, but now it seems quite easy.

Scenario

Imagine we need to create a recursive m2m relationship, something like a "related posts" field assigned to a Post model. It would be nice if we could exclude the current editing post from the available choices.

This is quite straightforward if we go with the default select multiple widget. In fact we only need to create a custom form class and override the queryset property of the related_posts field in the __init__ function, excluding the id of the current instance, and then assign such form class in the ModelAdmin class, see this SO question for more info.

But things become more ...

read the full post


Lightscribe on ubuntu 14.04 64-bit, what worked for me

lightscribe linux

Lightscribe technology is cool, but almost noone uses it. Actually it can be hard also to find writable disks,  at least in Italy.

I think this is the reason why having this technology working in ubuntu 64-bit can be a mess. You may find some discussions about the installation of the necessary software, but it seems that there isn't a "widely working" solution, so let's add another "worked for me" solution.

Install the ia32-libs

This package contains runtime libraries for the ia32/i386 architecture, configured for use on an ...

read the full post


Pretty raw_id_fields with django-salmonella and django-grappelli

django django-admin programming tips grappelli

Sometimes, when dealing with m2m relationships, we need to use the raw_id_fields property of the ModelAdmin class, in order to choose the related objects in a new page, where we can sort and filter the available items.

Without doubt the information that the widget used by django to treat such fields gives us is a bit poor. We only see the related objects's ids, it would be nicer to have the string representation of the object.

Here comes django-salmonella:

A raw_id_fields widget replacement that handles display of an object's string ...

read the full post


Responsive menu for bootstrap

bootstrap css3 javascript jquery

This is a nice one.

Bootstrap is a nice framework, the great advantage of using it is that it saves you a lot of time while building a responsive web site (the same can be said of other css frameworks out there).
The drawback is that the web sites coming out in the last years very often looks the same, all of them have the same navbar and the same menu behavior which collapses for extra-small devices, showing a menu button which reveals the content when pressed.

A good way to differentiate your web site from the others could be a change in the menu collapsing behavior.

Datmenu is a nice responsive menu plugin for Wordpress, so the idea is ...

read the full post


Add links to django admin changelist view

django django-admin programming tips

Sometimes you may need to add one or more links for each table row in your admin changelist view. There are many ways to do it, and here we'ss see a simple one. So this post is nothing more than a tip.

In my case I had to add a link opening a pdf view of the model item detail, here comes my implementation:

- app/models.py

from django.db import models
from django.core.urlresolvers import reverse
from django.utils.html import mark_safe

class MyModel(models.Model):
    title = models.CharField('title', max_length=255)
    # ...

    def pdf_link(self):
        return mark_safe('<a class="grp-button" href="%s" target="blank">view pdf</a>' % reverse('archiviocr-opera-pdf', args=[self.id]))
    pdf_link.short_description = _('PDF')

- app/admin ...

read the full post


Paginating the results of a Django form POST request

django forms pagination

This is quite a common situation, we have a search form which leads to a result page. The number of found results can be big enough to require pagination. If we use the classic pagination code, the posted parameters will be lost when changing page and our search gets broken.

There are several ways to solve the problem, as tux21b wrote in this stack overflow answer.

Here I will talk about the first method, using session.
In the same stack overflow page you can see an answer by rvnovaes, which surely works but in my opinion has some eliminable drawbacks. The first is that if there are many search fields the code we have to write is too long (yes ...

read the full post


Your Smartwatch Loves Tasker!

Your Smartwatch Loves Tasker!

Now available for purchase!

Featured