Friday, May 24, 2013

Notification and confirm service using angular-ui bootstrap, phonegap and AngularJS

I've been playing with AngularJS lately when developing a new application using a Phonegap framework.

Phonegap is a framework which lets you create mobile apps for many platforms by just using HTML5 CSS and Javascript. These won't be as fast as native apps, but it makes developing faster.. I don't want to talk about Phonegap advantages and disadvantages here.

In my mobile applicatin I use alerts and confirmations windows many times.
Ripple - chrome extension that lets you emulate Phonegap someway doesn't provide confirmation windows (as far as I know) so I had to write my own fallback code for using native phonegap notifications on phone and html based notifications. For the second, I've used a great project angular-ui bootstrap which has a dialogs built in.

The code is a AngularJS service. I won't write here how AngularJS is great and awesome. Just look at the website and find yourself using this great framework. I think that Angular service is the right place to put this into work.

I should also mention  that I am using a great angular-phonegap-notification.

My intention was to make my service work similar to Phonegap notification API.


econtainersApp.factory("NotificationService", function ($dialog, notification) {
return {
showAlert: function (message, alertCallback, title, buttonName) {
if (navigator.notification) {
notification.alert(message, alertCallback, title, buttonName)
} else {
var buttonName = (typeof buttonName === "undefined") ? "Ok" : buttonName;
var msgbox = $dialog.messageBox(title, message, [{
label: buttonName
}
])
msgbox.open()
.then(function (result) {
if (alertCallback) {
alertCallback()
};
});
}
},
showConfirm: function (message, confirmCallback, title, buttonLabels) {
if (navigator.notification) {
notification.confirm(message, confirmCallback, title, buttonLabels)
} else {
var buttonLabels = (typeof buttonLabels === "undefined") ? ["Ok", "Cancel"] : buttonLabels;
var msgbox = $dialog.messageBox(title, message, (function () {
var buttons = [];
var result = 1;
angular.forEach(buttonLabels, function (value) {
buttons.push({
label: value,
result: result
});
result = result + 1;
});
return buttons;
})())
msgbox.open()
.then(function (result) {
confirmCallback(result);
})
}
}
}
})
view raw gistfile1.js hosted with ❤ by GitHub

Wednesday, February 6, 2013

Restricting django to one user concurrent session only

Here's the tiny code that helps you avoid multiple users logged using the same account.
# -*- coding: utf-8 -*

from django.conf import settings
from django.core.cache import cache, get_cache
from django.utils.importlib import import_module


class UserRestrictMiddleware(object):
    def process_request(self, request):
        """
        Checks if different session exists for user and deletes it.
        """
        if request.user.is_authenticated():
            cache = get_cache('default')
            cache_timeout = 86400
            cache_key = "user_pk_%s_restrict" % request.user.pk
            cache_value = cache.get(cache_key)

            if cache_value is not None:
                if request.session.session_key != cache_value:
                    engine = import_module(settings.SESSION_ENGINE)
                    session = engine.SessionStore(session_key=cache_value)
                    session.delete()
                    cache.set(cache_key, request.session.session_key, 
                              cache_timeout)
            else:
                cache.set(cache_key, request.session.session_key, cache_timeout)

# vim: ai ts=4 sts=4 et sw=4
Hope you like it.
Remember, to put UserRestrictMiddleware somewhere after Session Middleware in MIDDLEWARE_CLASSES (settings.py)