Skip to content

Commit

Permalink
Contributions custom (#248)
Browse files Browse the repository at this point in the history
* add load river command

* use pg triggers

* use pg triggers

* use pg triggers

* use pg triggers

* use pg triggers

* use pg triggers

* use pg triggers

* use pg triggers

* use pg triggers

* use internal triggers

* use internal triggers

* use internal triggers

* use internal triggers

* use internal triggers

* use internal triggers

* use internal triggers

* use internal triggers

* use internal triggers

* use internal triggers

* use internal triggers

* use internal triggers

* use internal triggers

* use internal triggers

* fix command

* lint

* add load river command

* use pg triggers

* use pg triggers

* use pg triggers

* use pg triggers

* use pg triggers

* use pg triggers

* use pg triggers

* use pg triggers

* use pg triggers

* use internal triggers

* use internal triggers

* use internal triggers

* use internal triggers

* use internal triggers

* use internal triggers

* use internal triggers

* use internal triggers

* use internal triggers

* use internal triggers

* use internal triggers

* use internal triggers

* use internal triggers

* use internal triggers

* fix command

* lint

* Add custom contribution types (#244)

* Configure custom contribution type in admin

* hotfix pillow

* improve data perfs

* improve data perfs

* improve data perfs

* add custom contrib type description

* add station endpoint

* add station endpoint

* add station endpoint

* handle 500

* handle 500

* handle 500

* handle 500

* fix station endpoint

* fix station endpoint

* fix station endpoint

* fix station endpoint

* fix station endpoint

* fix station endpoint

* fix station endpoint

* fix station endpoint

* fix station endpoint

* revert default json

* fix and explicit default json in api responses

* fix and explicit default json in api responses

* fix and explicit default json in api responses

* fix and add contribution endpoint

* improve load_river command

* fix contributions

* fix contributions

* pep8

* pep8

* fix migration

* fix tests

* fix tests and station layer migration

* fix migration

* add shared contribution_at field

* add shared contribution_at field

* add station json / geojson urls

* add contributions by station endpoint

* add contributions by station endpoint

* add contributions by station endpoint

* add contributions by station endpoint

* add internal field

* add internal field

* fix translations

* add password management and allow send files

* fix password check

* Improve contribution API by station

* fix test

* add custom contributions endpoint and layer

* fix serializer

* add serializer for attachments

* add attachments in admin

* add attachments in admin

* add attachments in admin

* add attachments in admin

* fix signal

* fix river trigger

* fix test

* allow deploy develop branch

* set changelog
  • Loading branch information
submarcos authored Apr 29, 2024
1 parent f1bef05 commit e021632
Show file tree
Hide file tree
Showing 84 changed files with 3,181 additions and 1,274 deletions.
45 changes: 7 additions & 38 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ on:
push:
branches:
- master
- develop

release:
types:
- created
Expand All @@ -27,11 +29,12 @@ jobs:
uses: actions/setup-python@v5
with:
python-version: 3.9

- name: Install dependencies
run: |
echo "${{ github.event_name }}! ${{ github.event.action }}"
python -m pip install --upgrade pip
python -m pip install flake8
python -m pip install flake8 -c dev-requirements.txt
- name: Lint with flake8
run: |
flake8 georiviere
Expand Down Expand Up @@ -90,40 +93,6 @@ jobs:
verbose: true
fail_ci_if_error: true # optional (default = false)

build-and-push-dev-image:
runs-on: ubuntu-latest
permissions:
packages: write # required to publish docker image
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
if: ${{(github.base_ref == 'develop')}}
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}


build-and-push-image:
runs-on: ubuntu-latest
needs: [flake8, doc_build, unittests]
Expand All @@ -132,7 +101,7 @@ jobs:
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
if: ${{ (github.event_name == 'release' && github.event.action == 'created') || (github.ref == 'refs/heads/master' && github.event_name != 'pull_request')}}
if: ${{ (github.event_name == 'release' && github.event.action == 'created') || github.event_name != 'pull_request'}}
steps:
- name: Checkout repository
uses: actions/checkout@v4
Expand Down Expand Up @@ -163,7 +132,7 @@ jobs:
needs: [ build-and-push-image ]
permissions:
contents: write # required to attach zip to release
if: ${{ github.event_name == 'release' && github.event.action == 'created' }}
if: ${{ (github.event_name == 'release' && github.event.action == 'created' }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ RUN apt-get update -qq && apt-get install -y -qq \
USER django

RUN python3.9 -m venv /opt/venv
RUN /opt/venv/bin/pip install --no-cache-dir pip setuptools wheel -U
RUN /opt/venv/bin/pip install --no-cache-dir pip setuptools wheel -U
# geotrek setup fix : it required django before being installed... TODO: fix it in geotrek setup.py
RUN /opt/venv/bin/pip install --no-cache-dir django==2.2.*

Expand Down
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
deps:
docker compose run --rm web bash -c "pip-compile -q && pip-compile -q dev-requirements.in"
2 changes: 1 addition & 1 deletion dev-requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ coverage
django-debug-toolbar
django-extensions
factory-boy
flake8 # WARNING : CI always use last flake8 published version
flake8

# doc
sphinx
Expand Down
29 changes: 12 additions & 17 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
#
# pip-compile dev-requirements.in
#
alabaster==0.7.13
alabaster==0.7.16
# via sphinx
asgiref==3.3.1
# via
# -c requirements.txt
# django
babel==2.14.0
# via sphinx
build==1.0.3
build==1.2.1
# via pip-tools
certifi==2020.12.5
# via
Expand Down Expand Up @@ -56,7 +56,7 @@ faker==9.7.1
# via
# -c requirements.txt
# factory-boy
flake8==6.1.0
flake8==7.0.0
# via -r dev-requirements.in
freezegun==1.1.0
# via
Expand All @@ -68,7 +68,7 @@ idna==2.10
# requests
imagesize==1.4.1
# via sphinx
importlib-metadata==7.0.0
importlib-metadata==7.1.0
# via
# build
# sphinx
Expand All @@ -91,7 +91,7 @@ pip-tools==6.10.0
# via -r dev-requirements.in
pycodestyle==2.11.1
# via flake8
pyflakes==3.1.0
pyflakes==3.2.0
# via flake8
pygments==2.17.2
# via sphinx
Expand Down Expand Up @@ -126,27 +126,22 @@ sphinx==5.1.1
# via
# -r dev-requirements.in
# sphinx-rtd-theme
# sphinxcontrib-applehelp
# sphinxcontrib-devhelp
# sphinxcontrib-htmlhelp
# sphinxcontrib-jquery
# sphinxcontrib-qthelp
# sphinxcontrib-serializinghtml
sphinx-rtd-theme==2.0.0
# via -r dev-requirements.in
sphinxcontrib-applehelp==1.0.7
sphinxcontrib-applehelp==1.0.8
# via sphinx
sphinxcontrib-devhelp==1.0.5
sphinxcontrib-devhelp==1.0.6
# via sphinx
sphinxcontrib-htmlhelp==2.0.4
sphinxcontrib-htmlhelp==2.0.5
# via sphinx
sphinxcontrib-jquery==4.1
# via sphinx-rtd-theme
sphinxcontrib-jsmath==1.0.1
# via sphinx
sphinxcontrib-qthelp==1.0.6
sphinxcontrib-qthelp==1.0.7
# via sphinx
sphinxcontrib-serializinghtml==1.1.9
sphinxcontrib-serializinghtml==1.1.10
# via sphinx
sqlparse==0.4.1
# via
Expand All @@ -167,9 +162,9 @@ urllib3==1.26.3
# via
# -c requirements.txt
# requests
wheel==0.42.0
wheel==0.43.0
# via pip-tools
zipp==3.17.0
zipp==3.18.1
# via importlib-metadata

# The following packages are considered to be unsafe in a requirements file:
Expand Down
3 changes: 2 additions & 1 deletion docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ CHANGELOG

**New features**

- add load_rivers command
- Add load_rivers command
- Create custom contribution types from the admin with specific field schema

**Bug fix**

Expand Down
146 changes: 129 additions & 17 deletions georiviere/contribution/admin.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,131 @@
from admin_ordering.admin import OrderableAdmin
from django.contrib import admin
from django.contrib.admin.widgets import AdminFileWidget
from django.contrib.contenttypes.admin import GenericTabularInline
from django.db.models import FileField
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _
from leaflet.admin import LeafletGeoAdmin

from georiviere.contribution.models import (
SeverityType, LandingType, JamType, DiseaseType, DeadSpecies, InvasiveSpecies, HeritageSpecies, HeritageObservation,
FishSpecies, NaturePollution, TypePollution, ContributionStatus
)

admin.site.register(ContributionStatus, admin.ModelAdmin)
admin.site.register(SeverityType, admin.ModelAdmin)
admin.site.register(LandingType, admin.ModelAdmin)
admin.site.register(JamType, admin.ModelAdmin)
admin.site.register(DiseaseType, admin.ModelAdmin)
admin.site.register(DeadSpecies, admin.ModelAdmin)
admin.site.register(InvasiveSpecies, admin.ModelAdmin)
admin.site.register(HeritageSpecies, admin.ModelAdmin)
admin.site.register(HeritageObservation, admin.ModelAdmin)
admin.site.register(FishSpecies, admin.ModelAdmin)
admin.site.register(NaturePollution, admin.ModelAdmin)
admin.site.register(TypePollution, admin.ModelAdmin)
from . import models, forms
from ..main.models import Attachment

admin.site.register(models.ContributionStatus, admin.ModelAdmin)
admin.site.register(models.SeverityType, admin.ModelAdmin)
admin.site.register(models.LandingType, admin.ModelAdmin)
admin.site.register(models.JamType, admin.ModelAdmin)
admin.site.register(models.DiseaseType, admin.ModelAdmin)
admin.site.register(models.DeadSpecies, admin.ModelAdmin)
admin.site.register(models.InvasiveSpecies, admin.ModelAdmin)
admin.site.register(models.HeritageSpecies, admin.ModelAdmin)
admin.site.register(models.HeritageObservation, admin.ModelAdmin)
admin.site.register(models.FishSpecies, admin.ModelAdmin)
admin.site.register(models.NaturePollution, admin.ModelAdmin)
admin.site.register(models.TypePollution, admin.ModelAdmin)


class CustomFieldInline(OrderableAdmin, admin.TabularInline):
verbose_name = _("Field")
verbose_name_plural = _("Fields")
model = models.CustomContributionTypeField
ordering_field = "order"
ordering = ("order", "label")
form = forms.CustomContributionFieldInlineForm
fields = (
"label",
"internal_identifier",
"value_type",
"required",
"help_text",
"order",
)
extra = 0
show_change_link = True
popup_link = "change"


@admin.register(models.CustomContributionType)
class CustomContributionTypeAdmin(admin.ModelAdmin):
list_display = ("label",)
search_fields = ("label",)
filter_horizontal = ("stations",)
inlines = [
CustomFieldInline,
]


@admin.register(models.CustomContributionTypeField)
class CustomContributionTypeFieldAdmin(admin.ModelAdmin):
list_display = ("label", "key", "value_type", "required", "custom_type")
list_filter = ("custom_type", "value_type", "required")
search_fields = ("label", "key", "custom_type__label")
form = forms.CustomContributionFieldForm
fieldsets = (
(
None,
{
"fields": (
"custom_type",
"label",
"internal_identifier",
"key",
"value_type",
"required",
"help_text",
)
},
),
(
_("Customization"),
{
"fields": ("customization", "options"),
},
),
)

def get_readonly_fields(self, request, obj=None):
if obj and obj.pk:
return ["custom_type", "key", "options"]
return []

def has_add_permission(self, request):
"""Disable addition in list view"""
return False

def has_delete_permission(self, request, obj=None):
"""Disable deletion in list view"""
return False


class AdminImageWidget(AdminFileWidget):
def render(self, name, value, attrs=None, renderer=None):
output = []
if value and getattr(value, "url", None):
image_url = value.url
file_name = str(value)
output.append(
' <a href="%s" target="_blank"><img src="%s" alt="%s" style="max-height: 60px; max-width: 60px;"/></a> %s '
% (image_url, image_url, file_name, _(""))
)
output.append(super().render(name, value, attrs))
return mark_safe("".join(output))


class CustomContribAttachmentInline(GenericTabularInline):
model = Attachment
extra = 0
exclude = ("attachment_video", "attachment_link", "creator", "legend", "starred")
formfield_overrides = {FileField: {"widget": AdminImageWidget}}


@admin.register(models.CustomContribution)
class CustomContributionAdmin(LeafletGeoAdmin, admin.ModelAdmin):
list_display = ("custom_type", "portal", "validated", "date_insert", "date_update")
list_filter = ("custom_type", "portal", "validated")
form = forms.CustomContributionForm
inlines = [CustomContribAttachmentInline]

def get_readonly_fields(self, request, obj=None):
if not obj or not obj.pk:
return ("data",)
return []
Loading

0 comments on commit e021632

Please sign in to comment.