django-geosource 0.5.3
Django geographic asynchrone data sources loading

[![Build Status](https://travis-ci.org/Terralego/django-geosource.svg?branch=master)](https://travis-ci.org/Terralego/django-geosource)
[![codecov](https://codecov.io/gh/Terralego/django-geosource/branch/master/graph/badge.svg)](https://codecov.io/gh/Terralego/django-geosource)
[![PyPi version](https://pypip.in/v/django-geosource/badge.png)](https://pypi.org/project/django-geosource/)

# Django GeoSource

This django application provide a Rest Framework API that allow to manage many geo data
sources and integrate that data wherever you need, like a Django model or any output
pipeline. Its provided with necessary celery jobs that do the job.

## Settings

You can define the setting `GEOSOURCE_MAX_TASK_RUNTIME` that allow to define the max run time of a task before it can be launched one more
time. It allow to prevent when a task is stuck and disallow launching one more.

## Configure and run Celery

You must define in your project settings the variables CELERY_BROKER_URL and CELERY_RESULT_BACKEND as specified in Celery documentation.
You also need to create the celery app following this [documentation](https://docs.celeryproject.org/en/stable/django/first-steps-with-django.html#django-first-steps).

Then to run the celery worker:
`$ celery worker -A django_geosource -l info`

To run periodic tasks, use celery beat:

```python

from celery import Celery
from celery.schedules import crontab

app = Celery()

@app.after_finalize.connect
def setup_periodic_tasks(sender, **kwargs):
    # Calls refresh check every 30 minutes
    sender.add_periodic_task(60.0 * 30, run_auto_refresh_source.s())

@app.task
def run_auto_refresh_source():
    from django_geosource.periodics import auto_refresh_source

    auto_refresh_source()
```

Then run celery beat worker that allow to synchronize periodically sources, launch this command:
`$ celery beat -A django_geosource -l info`

## Configure data destination

Now, you must set the callback methods that are used to insert data in your destination database.

If you use django-geostore, we provide a set of callback in the `geostore_callbacks` module, else you can define your
own callbacks.

### GEOSOURCE_LAYER_CALLBACK

The callback signature receive as first argument the SourceModel object, and must return your Layer object.
Example:

```python
def layer_callback(geosource):
    return Layer.objects.get_or_create(name=geosource.name)[0]
```

### GEOSOURCE_FEATURE_CALLBACK

This one, define a feature creation callback method.
Example:

```python
def feature_callback(geosource, layer, identifier, geometry, attributes):
    return Feature.objects.get_or_create(layer=layer, identifier=identifier, geom=geometry, properties=attributes)[0]
```

### GEOSOURCE_CLEAN_FEATURE_CALLBACK

This callback is called when the refresh is done, to clear old features that are not anymore present in the database.
It receives as parametter the geosource, layer and begin update date, so you can advise what to do depending of your
models.
Example:

```python
def clear_features(geosource, layer, begin_date):
    return layer.features.filter(updated_at__lt=begin_date).delete()
```

### GEOSOURCE_DELETE_LAYER_CALLBACK

This is called when a Source is deleted, so you are able to do what you want with the loaded content in database, when
the source doesn't exist anymore. It's executed before real deletion.
Example:

```python
def delete_layer(geosource, layer):
    if layer.features.count() > 0:
        layer.features.delete()
    return layer.delete()
```

## To start a dev instance

Define settings you wants in `test_geosource` django project.

```sh
docker-compose build
docker-compose up
```

First start should failed as the database need to be initialized. Just launch the same command twice.

Then initialize the database:

```sh
docker-compose run web ./manage.py migrate
```

You can now edit your code. A django runserver is launched internally so the
this is an autoreload server.

You can access to the api on http://localhost:8000/api/

## Test and coverage

To run test suite, just launch:

```sh
docker-compose run --rm web ./manage.py test
```

To run test suite with coverage, execute this commande:

```sh
docker-compose run web /code/src/coverage.sh
```



0.5.3 / 2022-03-04
==================

  * add ci job to check pending migrations (#42)

  * Add missing migrations

0.5.2 / 2022-02-02
==================

  * Simplify WMTS source serializer validation

0.5.1 / 2021-10-13
==================

  * Add hack to handle mondrian exception with logging proxy

0.5.0 / 2021-09-14
==================

  * Change way periodic tasks are launched

0.4.12 / 2021-09-08
==================

  * Fix scheduler

0.4.11 / 2021-02-04
==================
  * Disable record validation in CSVSourceSerialiser

0.4.10 / 2020-12-01
==================

  * Add property values list endpoint
  * [Bug] Force order update & add it to default

0.4.9 / 2020-10-15
==================

  * Add error reporting on source

0.4.8 / 2020-10-07
==================

  * Fields order is kept from the source
  * Add credit field on Source
  * Add date as a field type

0.4.7 / 2020-07-01
==================

  * Csv empty cell are recorded as None value
  * Update test, CSVSource settings are not write only anymore
  * Only update csvsource settings and make it readable

0.4.6 / 2020-05-14
==================

  * Add refresh_data assertion in get_records tests
  * Serializer do not return None value to representation
  * Records name are string even with no header

0.4.5 / 2020-05-13
==================

  * Ignored columns are removed from records

0.4.4 / 2020-05-11
==================

  * let pyexcel handle file type

0.4.3 / 2020-05-11
==================

  * Correctly extract srid from data
  * fix typo in separators name
  * Ensure correct csv decoding

0.4.2 / 2020-05-07
==================

  * Add CSVSource source

0.4.1 / 2020-03-24
==================

  * Fix wmts geom_type mandatory

0.4.0 / 2020-03-19
==================

  * BREAKING CHANGE : change way celery is working to allow using celery in another app

0.3.7 / 2020-03-17
==================

  * Enhance tests to valide search and filter
  * Add option to sync sources to have more control
  * Add zipfile shapefilesource

0.3.6 / 2019-12-19
==================

  * Fix bug with FileSourceSerializer

0.3.5 / 2019-12-18
==================

  * Add ordering and filtering for sources
  * Add flake8 linting to CI

0.3.4 / 2019-12-16
==================

### Improves

  * Improve documentation
  * Fix python3.8, django 3.0 and DRF 3.11 compatibility

0.3.3 / 2019-11-06
==================

### Improves

  * Define MANIFEST.in

0.3.1 / 2019-11-06
==================

### Improves

  * Improve error message when identifier field is not found in the source
  * Improve error message when geojson features has bad geometries
  * Use black for linting in pipelines

0.3.0 / 2019-10-18
==================

### Release

  * First release