New in version 0.3.
LibreGeosocial is designed for support external applications. Create an external application is as easy as create a django application (please, refer to django documentation for more information about how to create a new django application).
Once the application is created, you can use it just adding it to the apps dir in the project, when you restart the engine, the application will be automatically discovered and used. If you do not want to use some applications in this directory, you can avoid autodiscovery just adding ‘.’ or ‘_’ at the beginning of the directory name.
There are some important issues to know about external applications:
- If you want to use the core models in you application, as related objects or parent classes of you own models, you must remember to set your ‘objects’ attribute as a GeoManager or a child of it. Otherwise you will not be able to make geo queries. Please, refer to geodjango documentation for more information.
- It is highly recommended to use Social_node or any of its children as parents for your own classes. This way it will be easier to manage all objects and all the social network will be more uniform.
In this section we are going to explain how to create a new application that extends the REST API of the core and uses its functional api. This sample application will show all the users names in the network (it does not use any login nor privacy systems).
First of all you need to create a new django application, this is easy using django-admin utilities. Just execute:
cd /apps/
django-admin startapp
This creates a new directory with three files whose name is the application name, in my case test but we need another one that specifies the urls that will be served by this application. Edit a file called urls.py and add the following content to it:
from django.conf.urls.defaults import *
urlpatterns = patterns('',
url(r'^list_users/$', 'apps.test.views.list_users', name="list_users"),
)
This file configures a new url that your application will serve, in this case: http://your_server/test/list_users. When this URL is requested, the function list_users in your views module will be executed to create a response, so let’s create it. Edit the views.py file and add the following content:
from format.utils import getResponseFormat, generateResponse
from social.core import api
def list_users(request):
format = getResponseFormat (request)
users=api.user.get_all()
data = {"code" : '200',
"users" : users }
return generateResponse(format, data, "test/list")
This will try to use two different templates depending on the format (XML or JSON): test/list.xml or test/list.json. So let’s create them!
First you should create a directory called templates inside your application directory inside it create another directory called test. Then create a file called list.json inside the templates/test directory with the following content:
[
{% for user in users %}
"{{ user.username }}",
{% endfor %}
]
And finally, create another file caller list.xml with this content:
{% extends "base.xml" %}
{% block type %}user_names_list{% endblock %}
{% block data %}
{% for user in users %}
{{ user.username }}
{% endfor %}
{% endblock %}
Now the test application is ready, if you want to test it just run:
cd /
manage.py runserver
Then, with your browser access to http://localhost:8000/test/list_users/?format=JSON and http://localhost:8000/test/list_users/ to view the results.
It is also possible to use external libraries that are not standard and that they are not in the path. Simply add them to the ‘libs’ directory and they will be added to the path automatically.