Compare commits

..

8 Commits

10 changed files with 357 additions and 273 deletions

4
.gitignore vendored
View File

@ -1,2 +1,6 @@
__pycache__/
ENV
*~
~*
.*~

37
.gitlab-ci.yml Normal file
View File

@ -0,0 +1,37 @@
stages:
- check
- dockerize
variables:
IMAGE_NAME: $CI_REGISTRY/$CI_PROJECT_PATH
check:
image: registry.hottis.de/dockerized/base-build-env:latest
stage: check
tags:
- hottis
- linux
- docker
rules:
- if: $CI_COMMIT_TAG
script:
- checksemver.py -v
--versionToValidate "$CI_COMMIT_TAG"
--validateMessage
--messageToValidate "$CI_COMMIT_MESSAGE"
dockerize:
image: registry.hottis.de/dockerized/docker-bash:latest
stage: dockerize
tags:
- hottis
- linux
- docker
rules:
- if: $CI_COMMIT_TAG
script:
- docker build --tag $IMAGE_NAME:latest --tag $IMAGE_NAME:$CI_COMMIT_TAG .
- docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
- docker push $IMAGE_NAME:latest
- docker push $IMAGE_NAME:$CI_COMMIT_TAG

View File

@ -20,9 +20,7 @@ RUN \
pip3 install connexion && \
pip3 install connexion[swagger-ui] && \
pip3 install uwsgi && \
pip3 install flask-cors && \
pip3 install python-jose[cryptography] && \
pip3 install six
pip3 install flask-cors
RUN \
mkdir -p ${APP_DIR} && \

View File

@ -21,6 +21,29 @@ SELECT m.id as id,
w.id = m.wohnung
""", [], "Mieter")
def get_mieters_active():
return getMany("""
SELECT m.id as id,
o.id as objekt,
w.id as wohnung,
w.shortname as wohnung_shortname,
o.shortname as objekt_shortname,
COALESCE(m.anrede, '-') as anrede,
COALESCE(m.vorname, '-') as vorname,
m.nachname as nachname,
COALESCE(m.strasse, '-') as strasse,
COALESCE(m.plz, '-') as plz,
COALESCE(m.ort, '-') as ort,
COALESCE(m.telefon, '-') as telefon,
m.einzug as einzug,
COALESCE(m.auszug, '-') as auszug
FROM wohnung w, objekt o, mieter m
WHERE o.id = w.objekt AND
w.id = m.wohnung AND
m.einzug <= curdate() and
(m.auszug is null or m.auszug > curdate())
""", [], "Mieter")
def get_mieter(id=None):
return getOne("""
SELECT m.id as id,

View File

@ -1,4 +1,4 @@
from dbpool import getConnection, getOne, getMany, putOne
from dbpool import getConnection, getOne, getMany, putOne, call
import datetime
import decimal
import dateparser
@ -106,11 +106,22 @@ WHERE mieter = ? AND
"zahlungen": float(sumZ)
}
def put_zahlung(zahlung):
print("Input of put_zahlung: {} {}".format(type(zahlung), zahlung))
datum_soll = dateparser.parse(zahlung["datum_soll"], languages=["de"])
datum_ist = dateparser.parse(zahlung["datum_ist"], languages=["de"])
def put_zahlung(**args):
try:
body = args["body"]
datum_soll_raw = body["datum_soll"]
datum_ist_raw = body["datum_ist"]
print("Input of put_zahlung: {}".format(body))
datum_soll = dateparser.parse(datum_soll_raw, languages=["de"])
datum_ist = dateparser.parse(datum_ist_raw, languages=["de"])
return putOne("""
INSERT INTO zahlung (datum_soll, datum_ist, mieter, betrag, kommentar)
VALUES(?, ?, ?, ?, ?)
""", [ datum_soll, datum_ist, zahlung["mieter"], zahlung["betrag"], zahlung["kommentar"] ], "Zahlung")
""", [ datum_soll, datum_ist, body["mieter"], body["betrag"], body["kommentar"] ], "Zahlung")
except KeyError as e:
print("Some parameter missing: {}".format(e))
return str(e), 500
def insertAllForMonth():
return call("insert_monatl_miet_forderung")

44
auth.py
View File

@ -1,44 +0,0 @@
import time
import connexion
import six
from werkzeug.exceptions import Unauthorized
from jose import JWTError, jwt
JWT_ISSUER = 'de.hottis.hausverwaltung'
JWT_SECRET = 'streng_geheim'
JWT_LIFETIME_SECONDS = 600
JWT_ALGORITHM = 'HS256'
def generate_token(user_id):
timestamp = _current_timestamp()
payload = {
"iss": JWT_ISSUER,
"iat": int(timestamp),
"exp": int(timestamp + JWT_LIFETIME_SECONDS),
"sub": str(user_id),
}
return jwt.encode(payload, JWT_SECRET, algorithm=JWT_ALGORITHM)
def decode_token(token):
try:
return jwt.decode(token, JWT_SECRET, algorithms=[JWT_ALGORITHM])
except JWTError as e:
six.raise_from(Unauthorized, e)
def get_secret(user, token_info) -> str:
return '''
You are user_id {user} and the secret is 'wbevuec'.
Decoded token claims: {token_info}.
'''.format(user=user, token_info=token_info)
def _current_timestamp() -> int:
return int(time.time())

View File

@ -1,7 +1,7 @@
#!/bin/bash
IMAGE_NAME="registry.hottis.de/hv/hv-service"
VERSION=0.0.3
VERSION=0.0.4
docker build -t ${IMAGE_NAME}:${VERSION} .
docker push ${IMAGE_NAME}:${VERSION}

View File

@ -83,6 +83,7 @@ def putOne(stmt, params, objName):
except mariadb.Error as err:
dbh.rollback()
print("Database error in putOne({}): {}".format(objName, err))
return str(err), 500
except Exception as err:
dbh.rollback()
print("Error in putOne({}): {}".format(objName, err))
@ -94,3 +95,26 @@ def putOne(stmt, params, objName):
if dbh:
dbh.close()
def call(procName):
dbh = None
cur = None
try:
dbh = getConnection()
cur = dbh.cursor(dictionary=True)
cur.execute("CALL {}(null)".format(procName))
dbh.commit()
return "{} successfully called".format(procName), 202
except mariadb.Error as err:
dbh.rollback()
print("Database error in call {}: {}".format(procName, err))
return str(err), 500
except Exception as err:
dbh.rollback()
print("Some error in call {}: {}".format(procName, err))
return str(err), 500
finally:
print("return connection in call {}".format(procName))
if cur:
cur.close()
if dbh:
dbh.close()

View File

@ -4,3 +4,4 @@ wsgi-file = server.py
processes = 4
stats = :9191

View File

@ -1,7 +1,7 @@
openapi: 3.0.0
info:
title: Hausverwaltung-JWT
version: "0.2"
title: Hausverwaltung
version: "0.1"
paths:
/hv/objekte:
@ -12,10 +12,12 @@ paths:
responses:
200:
description: Successful response.
content:
'application/json':
schema:
type: array
items:
$ref: '#/components/Objekt'
$ref: '#/components/schemas/Objekt'
404:
description: No Objekte available
500:
@ -28,13 +30,16 @@ paths:
parameters:
- name: id
in: path
type: integer
required: true
schema:
type: integer
responses:
200:
description: Successful response.
content:
'application/json':
schema:
$ref: '#/components/Objekt'
$ref: '#/components/schemas/Objekt'
404:
description: Objekt not found
500:
@ -47,10 +52,12 @@ paths:
responses:
200:
description: Successful response.
content:
'application/json':
schema:
type: array
items:
$ref: '#/components/Wohnung'
$ref: '#/components/schemas/Wohnung'
404:
description: No Wohnung available
500:
@ -63,15 +70,18 @@ paths:
parameters:
- name: id
in: path
type: integer
required: true
schema:
type: integer
responses:
200:
description: Successful response.
content:
'application/json':
schema:
type: array
items:
$ref: '#/components/Wohnung'
$ref: '#/components/schemas/Wohnung'
404:
description: No Wohnung available
500:
@ -84,13 +94,16 @@ paths:
parameters:
- name: id
in: path
type: integer
required: true
schema:
type: integer
responses:
200:
description: Successful response.
content:
'application/json':
schema:
$ref: '#/components/Wohnung'
$ref: '#/components/schemas/Wohnung'
404:
description: Wohnung not found
500:
@ -103,10 +116,30 @@ paths:
responses:
200:
description: Successful response.
content:
'application/json':
schema:
type: array
items:
$ref: '#/components/Mieter'
$ref: '#/components/schemas/Mieter'
404:
description: No Mieter available
500:
description: Some server error
/hv/mieters/active:
get:
tags: [ "Mieter" ]
operationId: Mieter.get_mieters_active
summary: Returns all currently active Mieters
responses:
200:
description: Successful response.
content:
'application/json':
schema:
type: array
items:
$ref: '#/components/schemas/Mieter'
404:
description: No Mieter available
500:
@ -119,13 +152,16 @@ paths:
parameters:
- name: id
in: path
type: integer
required: true
schema:
type: integer
responses:
200:
description: Successful response.
content:
'application/json':
schema:
$ref: '#/components/Mieter'
$ref: '#/components/schemas/Mieter'
404:
description: Mieter not found
500:
@ -138,13 +174,16 @@ paths:
parameters:
- name: id
in: path
type: integer
required: true
schema:
type: integer
responses:
200:
description: Successful response.
content:
'application/json':
schema:
$ref: '#/components/Forderung'
$ref: '#/components/schemas/Forderung'
404:
description: Forderung not found
500:
@ -157,15 +196,18 @@ paths:
parameters:
- name: mieter_id
in: path
type: integer
required: true
schema:
type: integer
responses:
200:
description: Successful response.
content:
'application/json':
schema:
type: array
items:
$ref: '#/components/Forderung'
$ref: '#/components/schemas/Forderung'
404:
description: No Forderung available
500:
@ -178,13 +220,16 @@ paths:
parameters:
- name: id
in: path
type: integer
required: true
schema:
type: integer
responses:
200:
description: Successful response.
content:
'application/json':
schema:
$ref: '#/components/Zahlung'
$ref: '#/components/schemas/Zahlung'
404:
description: Zahlung not found
500:
@ -197,15 +242,18 @@ paths:
parameters:
- name: mieter_id
in: path
type: integer
required: true
schema:
type: integer
responses:
200:
description: Successful response.
content:
'application/json':
schema:
type: array
items:
$ref: '#/components/Zahlung'
$ref: '#/components/schemas/Zahlung'
404:
description: No Zahlung available
500:
@ -218,19 +266,23 @@ paths:
parameters:
- name: mieter_id
in: path
type: integer
required: true
schema:
type: integer
- name: year
in: path
type: integer
required: true
schema:
type: integer
responses:
200:
description: Successful response
content:
'application/json':
schema:
type: array
items:
$ref: '#/components/ZahlungForderung'
$ref: '#/components/schemas/ZahlungForderung'
404:
description: No ZahlungForderung available
500:
@ -243,17 +295,21 @@ paths:
parameters:
- name: mieter_id
in: path
type: integer
required: true
schema:
type: integer
- name: year
in: path
type: integer
required: true
schema:
type: integer
responses:
200:
description: Successful response
content:
'application/json':
schema:
$ref: '#/components/Saldo'
$ref: '#/components/schemas/Saldo'
404:
description: Neither Forderungen nor Zahlungen available
500:
@ -263,52 +319,32 @@ paths:
tags: [ "Zahlung" ]
operationId: ZahlungenForderungen.put_zahlung
summary: Inserts a new Zahlung
parameters:
- name: zahlung
in: body
requestBody:
description: Zahlung
content:
application/json:
schema:
$ref: '#/components/Zahlung'
$ref: '#/components/schemas/Zahlung'
responses:
202:
description: Zahlung successfully inserted
500:
description: Some server or database error
/auth/{user_id}:
get:
tags: [ "jwt" ]
summary: Return JWT token
operationId: auth.generate_token
parameters:
- name: user_id
description: User unique identifier
in: path
required: true
example: 12
schema:
type: integer
/hv/forderung/insertAllForMonth:
post:
tags: [ "Forderung" ]
operationId: ZahlungenForderungen.insertAllForMonth
summary: Insert the Forderungen for the insertAllForMonth
responses:
'200':
description: JWT token
content:
'text/plain':
schema:
type: string
/secret:
get:
tags: [ "jwt" ]
summary: Return secret string
operationId: auth.get_secret
responses:
'200':
description: secret response
content:
'text/plain':
schema:
type: string
security:
- jwt: ['secret']
202:
description: Forderungen successfully inserted
500:
description: Some server or database error
components:
schemas:
Objekt:
description: Objekt type
type: object
@ -427,9 +463,3 @@ components:
type: number
saldo:
type: number
securitySchemes:
jwt:
type: http
scheme: bearer
bearerFormat: JWT
x-bearerInfoFunc: auth.decode_token