GithubHelp home page GithubHelp logo

django-wurfl's Introduction

django-wurfl in 15 seconds

How to install it

Dependencies

django-wurfl use the python-Levenshtein package. However, the hosting site is defunct, so

john@doe:~# sudo easy_install python-Levenshtein

probably won't work. You can however still get the source code from Michael Noll's website and install it manually.

Setup

Put the wurfl folder somewhere on your python path. Then add in your settings file :

# in settings.py
INSTALLED_APPS = (
    ...
    'wurfl',
    ...
)

Create the database tables with syncdb command. Then import WURFL data in the database using the custom command:

john@doe:~/mydjangoproject/# ./manage.py wurfl-load <path/to/wurfl.xml>

How to use it

django-wurfl provides a middleware, a decorator, and a context processor for you to use in your django powered application.

How to use the middleware :

# Add the middleware in your settings.py file
MIDDLEWARE_CLASSES = (
    ...
    'wurfl.middleware.DeviceMiddleware',
    ...
)

# You can now access the detected device in your views
def myview(request):
    markup_dict = request.device['markup'] # this is a group of
            # capabilities stored represented as a dictionnary

    if request.device['html_wi_w3_xhtmlbasic']:
        # this is a single capability
        # in this case, equivalent to markup_dict['html_wi_w3_xhtmlbasic']
        pass

The decorators allows you to activate the device detection only in specific views, whereas the middleware is application wide.

# In your views
from wurfl.decorators import device

@device
def myview(request):
    if request.device['html_wi_w3_xhtmlbasic']:
        # bla bla bla

And the context processor is helpful to have the device variable directly accessible in your template (if the device has been detected before hand using the middleware or a decorator).

# Add the context processor in your settings.py file
TEMPLATE_CONTEXT_PROCESSORS = (
    ...
    "wurfl.context_processors.device",
    ...
)

# Use the device in your templates
<p>Markup xhtmlbasic : {{ device.html_wi_w3_xhtmlbasic }}</p>

Reference

Settings reference

  • WURFL_USE_CACHE : if set to True, django-wurfl will use Django cache backend to cache user agents and associated capabilities. That will give a significant performance boost to your mobile application, and is highly recommended. See also WURFL_CACHE_PREFIX and WURFL_CACHE_TIMEOUT. Default to False.
  • WURFL_CACHE_PREFIX : Cache key in django-wurfl are user agent's MD5 hexadecimal digest. In order to prevent cache key collision, all keys are prefixed with the string in this setting (default to 'wurfl_').
  • WURFL_CACHE_TIMEOUT : timeout (in seconds) for cached user agent capabilities. If set to None, it will use the default timeout of your application. If set to 0, the cached values will never expire. Default to None.

Note:

The cache is not automatically invalidated when you update your WURFL data (patches, or main files), it is your responsibility to do so, or to set a relevant timeout value.

  • WURFL_USE_PATCH : if set to True, django-wurfl will take in account the patches to calculate the capabilities of the detected device. The patched capabilities are pre-processed using the wurfl-hybrid command (see below) Default to False.
  • WURFL_URL : the url to the XML wurfl file, not used in this version.
  • WURFL_UA_PREFIX_MATCHING : if set to True, django-wurfl will search for the device using only a prefix of the user agent in case it can't find the exact user agent in the database. Default to False.
  • WURFL_UA_PREFIX_MATCHING_MAX_DISTANCE : when searching with only a prefix of the user_agent, this setting is used to limit the sensitivity of the search. It indicates the maximum Levenshtein Distance that will be tolerated between the original user agent and the search results. Default to 20.

Commands reference

  • wurfl-load : load the data from a wurfl file and store it in the database. Takes 1 argument, the path to the WURLF XML file.
  • wurfl-hybrid : apply the patch to the current wurfl data and store the result in the hybrid table.
  • wurfl-add-patch : create a new patch from an XML file. You can also do that using the administration back office. See the documentation of this command for more information.

django-wurfl's People

Contributors

belovachap avatar clement avatar rbdcti avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

django-wurfl's Issues

bu

DevBook:mobile admin$ ./manage.py wurfl-load /Users/admin/Projects/example-mobile/src/mobile/wurfl-latest.xml
/Users/admin/Projects/example-mobile/src/mobile/wurfl/models.py:10: DeprecationWarning: the md5 module is deprecated; use hashlib instead
from md5 import new as md5

Error: sequence item 0: expected string, OperationalError found

What kind of bug is this sequence item 0: .. I must be doing something wrong...

BaseDevice._match_user_agent fails with updated iPad UserAgent

An updated iPad (1) user-agent reports as: Mozilla/5.0 (iPad; U; CPU OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5

The latest-and-greatest wurfl.xml (http://sourceforge.net/projects/wurfl/files/WURFL/) does not have that exact user-agent; however the official API (Tera-WURFL for the DB implementation, or really any of them) can still match this against the iPad (listed in wurfl.xml as Mozilla/5.0 (iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7D11) because of the way it does partial matches - in the newest Tera-WURFL this can be found in UserAgentMatchers/AppleUserAgentMatcher.php public function recoveryMatch() - notice it doesn't simply try to do a match against the beginning of the string but rather checks for certain key elements.

django-wurfl on the other hand tries to do a match against a third of the UA string and then uses Levenshtein distance to determine which match is the best to use. In this case, a third of the iPad UA string is "Mozilla/5.0 (iPad; U; CPU OS 4_3_3 like Mac OS" which still doesn't match anything (I was able to get a match by making it fallback to a sixth of the UA string, e.g., "Mozilla/5.0 (iPad; U; C").

The fix is unfortunately non-trivial - the official WURFL APIs solve this by having per-user-agent classes that handle the attempts at matching, instead of just trying to match against a fragment of the UA string. If django-wurfl is to be an actual replacement for the official WURFL APIs, however, this problem needs to be addressed.

a sample hello world project

Even though you have very good documentation, It would be nice if you could add a simple "hello world" project.

Much appreciated.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.