Skip to content

Commit

Permalink
Add server part
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed Sep 30, 2021
1 parent 96340b8 commit f09a6ee
Show file tree
Hide file tree
Showing 20 changed files with 181 additions and 119 deletions.
9 changes: 9 additions & 0 deletions .github/dependabot.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: 2
updates:
- package-ecosystem: pip
directory: custom
schedule:
interval: daily
time: '04:00'
ignore:
- dependency-name: none
3 changes: 3 additions & 0 deletions .hadolint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ignored:
- DL3008 # pin version: https://github.com/hadolint/hadolint/wiki/DL3008
- DL3003 # use workdir: https://github.com/hadolint/hadolint/wiki/DL3003
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.min.js
custom/Pipfile.lock
4 changes: 0 additions & 4 deletions ci/config.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
---
checks:
black: False
isort: False
codespell: False
eof: False
required_workflows: False
dependabot_config: False
setup: False

version:
Expand Down
46 changes: 46 additions & 0 deletions custom/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
FROM osgeo/gdal:ubuntu-small-3.2.1 AS base
LABEL maintainer Camptocamp "[email protected]"

# Fail on error on pipe, see: https://github.com/hadolint/hadolint/wiki/DL4006.
# Treat unset variables as an error when substituting.
# Print commands and their arguments as they are executed.
SHELL ["/bin/bash", "-o", "pipefail", "-cux"]

ENV DEBIAN_FRONTEND=noninteractive \
SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt

# hadolint ignore=SC1091
RUN . /etc/os-release && \
apt-get update && \
apt-get install --assume-yes --no-install-recommends \
python3-pip python3-dev python3-wheel python3-pkgconfig \
libpq-dev binutils gcc && \
apt-get clean && \
rm --recursive --force /var/lib/apt/lists/*

COPY requirements.txt /tmp/
RUN python3 -m pip install --disable-pip-version-check --no-cache-dir --requirement=/tmp/requirements.txt && \
rm --recursive --force /tmp/*

COPY Pipfile Pipfile.lock /tmp/

RUN cd /tmp && PIP_NO_BINARY=fiona,rasterio,shapely PROJ_DIR=/usr/local/ pipenv sync --system --clear && \
rm --recursive --force /usr/local/lib/python3.*/dist-packages/tests/ /tmp/* /root/.cache/* && \
strip /usr/local/lib/python3.*/dist-packages/*/*.so && \
python3 -m compileall -q /usr/local/lib/python3.* -x '/(ptvsd|pipenv|.*pydev.*|networkx)/'

# hadolint ignore=DL3059
RUN apt-get remove --autoremove --assume-yes gcc

WORKDIR /app
COPY . /app
RUN python3 -m pip install --disable-pip-version-check --no-cache-dir --editable=/app/ && \
python3 -m compileall -q /app/custom
# hadolint ignore=DL3059
RUN mkdir -p /etc/gunicorn/ && \
mv gunicorn_config.py /etc/gunicorn/config.py

CMD [ "/usr/local/bin/gunicorn", "--paste", "production.ini" ]

ARG GIT_HASH
ENV GIT_HASH=${GIT_HASH}
1 change: 1 addition & 0 deletions custom/custom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ def main(global_config, **settings):
"""This function returns a Pyramid WSGI application."""
with Configurator(settings=settings) as config:
config.include("pyramid_mako")
config.include("cornice")
config.include(".routes")
config.include(".models")
config.scan()
Expand Down
3 changes: 1 addition & 2 deletions custom/custom/alembic/env.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
"""Pyramid bootstrap environment. """
from alembic import context
from custom.models.meta import Base
from pyramid.paster import get_appsettings, setup_logging
from sqlalchemy import engine_from_config

from custom.models.meta import Base

config = context.config

setup_logging(config.config_file_name)
Expand Down
18 changes: 14 additions & 4 deletions custom/custom/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import os

import zope.sqlalchemy
from sqlalchemy import engine_from_config
from sqlalchemy import Column, Integer, String, engine_from_config
from sqlalchemy.orm import configure_mappers, sessionmaker

# Import or define all models here to ensure they are attached to the
# ``Base.metadata`` prior to any initialization routines.
from .mymodel import MyModel # flake8: noqa
from .meta import Base

# Run ``configure_mappers`` after defining all of the models to ensure
# all relationships can be setup.
Expand Down Expand Up @@ -120,3 +120,13 @@ def dbsession(request):
return dbsession

config.add_request_method(dbsession, reify=True)


class Feedback(Base):
__tablename__ = "feedback"
__table_args__ = {"schema": os.environ.get("PGSCHEMA", "main")}
id_feedback = Column(Integer, primary_key=True)
ua = Column(String(250))
permalink = Column(String(5000))
text = Column(String(1000))
email = Column(String(100))
13 changes: 0 additions & 13 deletions custom/custom/models/mymodel.py

This file was deleted.

15 changes: 15 additions & 0 deletions custom/custom/util/send_mail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import smtplib
from email.message import EmailMessage
from email.utils import formatdate


def send_mail(mail_list, text, subject):
msg = EmailMessage()
msg["From"] = "[email protected]"
msg["To"] = ", ".join(mail_list)
msg["Date"] = formatdate(localtime=True)
msg["Subject"] = subject
msg.set_content(text)
s = smtplib.SMTP("smtp.ne.ch")
s.send_message(msg)
s.quit()
31 changes: 0 additions & 31 deletions custom/custom/views/default.py

This file was deleted.

64 changes: 64 additions & 0 deletions custom/custom/views/feedback.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import os

import pyramid.request
from cornice import Service
from pyramid.httpexceptions import HTTPBadRequest
from pyramid.view import view_config

from c2cgeoportal_commons.models import DBSession

from .. import models
from ..models import Feedback
from ..util.sendmail import send_mail

feedback = Service(
name="feedback",
description="The feedback service",
path="/feedback",
cors_origins=(os.environ.get("VISIBLE_WEB_HOST", "*"),),
)


@feedback.post()
def feedback_post(request: pyramid.request.Request) -> None:
if (
"permalink" not in request.params
or "ua" not in request.params
or "email" not in request.params
or "email_optional" not in request.params
or "feedback" not in request.params
):
return HTTPBadRequest(detail="parameter missing")

new_feedback = Feedback()
new_feedback.ua = request.params["ua"]
new_feedback.permalink = request.params["permalink"]
new_feedback.email = request.params["email"]
email_optional = request.params["email_optional"]
new_feedback.text = request.params["feedback"]
DBSession.add(new_feedback)
DBSession.flush()

if "admin_email" in request.registry.settings:
mail_list = [request.registry.settings["admin_email"]]

if email_optional is not None and email_optional != "":
mail_list.append(email_optional)
instance = request.params["permalink"].split("?")[0]
text = "\n\n".join(
[
"Ceci est un email automatique. Un nouveau feedback a été inséré dans la BD.",
"Cela concerne l'instance : " + instance,
"Son identifiant est le : " + str(new_feedback.id_feedback),
"User agent : " + new_feedback.ua,
"Permalink : " + new_feedback.permalink,
"User email : " + new_feedback.email,
"User text : " + new_feedback.text,
]
)
subject = "Feedback - Guichet cartographique"

if mail_list[0] != "[email protected]":
send_mail(mail_list, text, subject)

return {"success": True}
5 changes: 5 additions & 0 deletions custom/development.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

[app:main]
use = egg:custom
filter-with = proxy-prefix

pyramid.reload_templates = true
pyramid.debug_authorization = false
Expand Down Expand Up @@ -39,6 +40,10 @@ file_template = %%(year)d%%(month).2d%%(day).2d_%%(rev)s
use = egg:waitress#main
listen = localhost:6543

[filter:proxy-prefix]
use = egg:PasteDeploy#prefix
prefix = /custom/

###
# logging configuration
# https://docs.pylonsproject.org/projects/pyramid/en/latest/narr/logging.html
Expand Down
2 changes: 2 additions & 0 deletions custom/gunicorn_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
accesslog = "-"
access_log_format = '%(H)s %({Host}i)s %(m)s %(U)s?%(q)s "%(f)s" "%(a)s" %(s)s %(B)s %(D)s %(p)s'
5 changes: 5 additions & 0 deletions custom/production.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

[app:main]
use = egg:custom
filter-with = proxy-prefix

pyramid.reload_templates = false
pyramid.debug_authorization = false
Expand Down Expand Up @@ -33,6 +34,10 @@ file_template = %%(year)d%%(month).2d%%(day).2d_%%(rev)s
use = egg:waitress#main
listen = *:6543

[filter:proxy-prefix]
use = egg:PasteDeploy#prefix
prefix = /custom/

###
# logging configuration
# https://docs.pylonsproject.org/projects/pyramid/en/latest/narr/logging.html
Expand Down
1 change: 1 addition & 0 deletions custom/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pipenv==2021.5.29
61 changes: 0 additions & 61 deletions custom/setup.py

This file was deleted.

5 changes: 2 additions & 3 deletions custom/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
import pytest
import transaction
import webtest
from custom import main, models
from custom.models.meta import Base
from pyramid.paster import get_appsettings
from pyramid.scripting import prepare
from pyramid.testing import DummyRequest, testConfig

from custom import main, models
from custom.models.meta import Base


def pytest_addoption(parser):
parser.addoption("--ini", action="store", metavar="INI_FILE")
Expand Down
10 changes: 10 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ services:
environment:
- PGSCHEMA

custom:
image: ${DOCKER_BASE}-custom:${DOCKER_TAG}
build:
context: custom
args:
GIT_HASH: ${GIT_HASH}
environment:
- GUNICORN_CMD_ARGS=${GUNICORN_PARAMS}
- VISIBLE_WEB_HOST

front:
extends:
file: docker-compose-lib.yaml
Expand Down
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[isort]
known_first_party=c2cgeoportal_commons,c2cgeoportal_geoportal,c2cgeoportal_admin,geomapfish_geoportal
known_third_party=webtest
known_first_party=c2cgeoportal_commons,c2cgeoportal_geoportal,c2cgeoportal_admin,geomapfish_geoportal,custom
multi_line_output=3
include_trailing_comma=1
force_grid_wrap=0
Expand Down

0 comments on commit f09a6ee

Please sign in to comment.