Skip to content

Commit

Permalink
add example survey models, streamline config
Browse files Browse the repository at this point in the history
(fixes #27, wq/wq#54)
  • Loading branch information
sheppard committed Jan 12, 2022
1 parent 02c7ced commit 3dcba94
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 29 deletions.
37 changes: 8 additions & 29 deletions app/js/project_name.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,16 @@
import wq from './wq.js';
import config from './data/config.js';
import version from './data/version.js';
// import config from '/config.js'; // Load directly from wq.db

wq.use({
context() {
return { version };
}
});

wq.init({
...config,
router: {
'base_url': ''
},
store: {
'service': '',
'defaults': {'format': 'json'}
},
material: {
theme: {
primary: '#7500ae',
secondary: '#0088bd'
}
},
map: {
bounds: [[-93.6, 44.7], [-92.8, 45.2]]
async function init() {
await wq.init(config);
await wq.prefetchAll();
if (config.debug) {
window.wq = wq;
}
}).then(() => {
wq.prefetchAll();
});

if (config.debug) {
window.wq = wq;
}

init();

navigator.serviceWorker.register('/service-worker.js');
12 changes: 12 additions & 0 deletions db/project_name/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
'wq.app',{% endif %}

# Project apps
'{{ project_name }}_survey',
]

MIDDLEWARE = [
Expand Down Expand Up @@ -148,6 +149,17 @@
WQ_APP_TEMPLATE = BASE_DIR / 'htdocs' / 'index.html'
VERSION_TXT = BASE_DIR / 'version.txt'
MEDIA_URL = '/media/'
WQ_CONFIG = {
"material": {
"theme": {
"primary": "#7500ae",
"secondary": "#0088bd"
}
},
"map": {
"bounds": [[-180, -70], [180, 70]]
}
}

# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
Expand Down
Empty file.
43 changes: 43 additions & 0 deletions db/project_name_survey/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from django.conf import settings{% if with_gis %}
import django.contrib.gis.db.models.fields{% endif %}
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name='Category',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=255)),
('description', models.TextField(blank=True, null=True)),
],
options={
'verbose_name_plural': 'categories',
},
),
migrations.CreateModel(
name='Observation',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('date', models.DateField(blank=True, help_text='The date when the observation was taken', null=True, verbose_name='Date')),{% if with_gis %}
('geometry', django.contrib.gis.db.models.fields.PointField(help_text='The location of the observation', srid=4326, verbose_name='Location')),{% endif %}
('photo', models.ImageField(blank=True, help_text='Photo of the observation', null=True, upload_to='observations', verbose_name='Photo')),
('notes', models.TextField(blank=True, help_text='Field observations and notes', null=True, verbose_name='Notes')),
('category', models.ForeignKey(blank=True, help_text='Observation type', null=True, on_delete=django.db.models.deletion.PROTECT, to='{{ project_name }}_survey.category', verbose_name='Category')),
('user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL, verbose_name='submitted by')),
],
options={
'verbose_name_plural': 'observations',
'ordering': ['-date'],
},
),
]
Empty file.
71 changes: 71 additions & 0 deletions db/project_name_survey/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{% if with_gis %}from django.contrib.gis.db import models
{% else %}from django.db import models
{% endif %}from wq.db.patterns.models import LabelModel
from django.conf import settings


"""
Example survey app for {{ project_name }}. The models below are examples, and
are not required for wq to work. Feel free to modify, rewrite, or delete and
replace this folder with a completely new Django app.
https://wq.io/guides/describe-your-data-model
"""


class Category(LabelModel):
name = models.CharField(max_length=255)
description = models.TextField(null=True, blank=True)

wq_label_template = "{% templatetag openvariable %}name{% templatetag closevariable %}"

class Meta:
verbose_name_plural = "categories"


class Observation(LabelModel):
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.PROTECT,
null=True,
blank=True,
verbose_name="submitted by",
)
date = models.DateField(
null=True,
blank=True,
verbose_name="Date",
help_text="The date when the observation was taken",
)
category = models.ForeignKey(
Category,
on_delete=models.PROTECT,
null=True,
blank=True,
verbose_name="Category",
help_text="Observation type",
){% if with_gis %}
geometry = models.PointField(
srid=4326,
verbose_name="Location",
help_text="The location of the observation",
){% endif %}
photo = models.ImageField(
upload_to="observations",
null=True,
blank=True,
verbose_name="Photo",
help_text="Photo of the observation",
)
notes = models.TextField(
null=True,
blank=True,
verbose_name="Notes",
help_text="Field observations and notes",
)

wq_label_template = "{% templatetag openvariable %}date{% templatetag closevariable %}"

class Meta:
verbose_name_plural = "observations"
ordering = ['-date']
31 changes: 31 additions & 0 deletions db/project_name_survey/rest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from wq.db import rest
from .models import Category, Observation
from .serializers import ObservationSerializer


rest.router.register_model(
Category,
fields="__all__",
cache="all",
background_sync=False,
)

rest.router.register_model(
Observation,
serializer=ObservationSerializer,
cache="first_page",
background_sync=True,{% if with_gis %}
map=[{
'mode': 'list',
'autoLayers': True,
'layers': [],
}, {
'mode': 'detail',
'autoLayers': True,
'layers': [],
}, {
'mode': 'edit',
'layers': [],
}],
{% endif %}
)
16 changes: 16 additions & 0 deletions db/project_name_survey/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from wq.db.rest.serializers import ModelSerializer
from rest_framework import serializers
from .models import Observation


class ObservationSerializer(ModelSerializer):
user = serializers.HiddenField(
default=serializers.CurrentUserDefault()
)

class Meta:
fields = "__all__"
model = Observation
wq_field_config = {
"notes": {"multiline": True},
}

0 comments on commit 3dcba94

Please sign in to comment.