diff --git a/app/js/project_name.js b/app/js/project_name.js index 62129c8..d289ccb 100644 --- a/app/js/project_name.js +++ b/app/js/project_name.js @@ -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'); diff --git a/db/project_name/settings/base.py b/db/project_name/settings/base.py index ac55dd8..6fef59a 100644 --- a/db/project_name/settings/base.py +++ b/db/project_name/settings/base.py @@ -41,6 +41,7 @@ 'wq.app',{% endif %} # Project apps + '{{ project_name }}_survey', ] MIDDLEWARE = [ @@ -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 diff --git a/db/project_name_survey/__init__.py b/db/project_name_survey/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/db/project_name_survey/migrations/0001_initial.py b/db/project_name_survey/migrations/0001_initial.py new file mode 100644 index 0000000..4e0a1eb --- /dev/null +++ b/db/project_name_survey/migrations/0001_initial.py @@ -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'], + }, + ), + ] diff --git a/db/project_name_survey/migrations/__init__.py b/db/project_name_survey/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/db/project_name_survey/models.py b/db/project_name_survey/models.py new file mode 100644 index 0000000..db08bfc --- /dev/null +++ b/db/project_name_survey/models.py @@ -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'] diff --git a/db/project_name_survey/rest.py b/db/project_name_survey/rest.py new file mode 100644 index 0000000..90da537 --- /dev/null +++ b/db/project_name_survey/rest.py @@ -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 %} +) diff --git a/db/project_name_survey/serializers.py b/db/project_name_survey/serializers.py new file mode 100644 index 0000000..dfbd29f --- /dev/null +++ b/db/project_name_survey/serializers.py @@ -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}, + }