Skip to content

Commit

Permalink
SYS-539 bump apicrud to 0.0.65
Browse files Browse the repository at this point in the history
  • Loading branch information
instantlinux committed Apr 29, 2021
1 parent 6587e3f commit 758bef9
Show file tree
Hide file tree
Showing 20 changed files with 286 additions and 441 deletions.
2 changes: 1 addition & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ stages:
- Publish Packages
- Clean

image: instantlinux/python-builder:3.8.7-r1
image: instantlinux/python-builder:3.8.8-r0

before_script:
- export TAG=bld_$CI_PIPELINE_IID_${CI_COMMIT_SHA:0:7}
Expand Down
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ export APP_ENV ?= local
include Makefile.vars
include Makefile.i18n

VENV=python_env
VDIR=$(PWD)/$(VENV)

# Local dev

run_media: py_requirements
run_media: dev_requirements
. $(VDIR)/bin/activate && \
AMQ_HOST=$(RABBITMQ_IP) REDIS_HOST=$(REDIS_IP) \
PUBLIC_URL=http://$(FQDN):$(APP_PORT) \
Expand All @@ -26,9 +29,6 @@ media_worker:
celery -A media_worker worker -Q media_$(APP_ENV) \
-n media1@%h --loglevel=INFO

VENV=python_env
VDIR=$(PWD)/$(VENV)

.PHONY: qemu

analysis: flake8
Expand Down Expand Up @@ -78,7 +78,7 @@ test: dev_requirements media/.proto.sqlite \
--cov-report html \
--cov-report xml \
--cov-report term-missing \
--cov ../python_env/lib/python*/site-packages/apicrud/media \
--cov ~/.local/lib/python*/site-packages/apicrud/media \
--cov .)

media/.proto.sqlite:
Expand Down
7 changes: 4 additions & 3 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[packages]
apicrud = "==0.0.56"
Pillow = "==7.2.0"
apicrud = "==0.0.66"
Pillow = "==8.1.2"

# for faster builds, keep these versions in sync with images
# instantlinux/python-builder and instantlinux/python-uwsgi
Expand All @@ -9,7 +9,8 @@ celery = "==4.4.7"
connexion = "*"
"connexion[swagger-ui]" = "*"
dollar-ref = "*"
SQLAlchemy = "*"
# 1.4.1 throws shitloads of warnings
SQLAlchemy = "==1.3.23"
SQLAlchemy-Utils = "*"
swagger-ui-bundle = "*"

Expand Down
214 changes: 117 additions & 97 deletions Pipfile.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion media/_version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__version__ = '0.0.30'
__version__ = '0.0.31'
vcs_ref = 'unset'
build_date = 'unset'
4 changes: 3 additions & 1 deletion media/controllers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
created 14-jan-2020 by richb@instantlinux.net
"""

from . import album, file, metric, picture, storage
from apicrud.controllers import metric

from . import album, file, picture, storage


def resources():
Expand Down
41 changes: 0 additions & 41 deletions media/controllers/auth.py

This file was deleted.

38 changes: 0 additions & 38 deletions media/controllers/metric.py

This file was deleted.

54 changes: 9 additions & 45 deletions media/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,62 +4,29 @@
created 9-dec-2019 by richb@instantlinux.net
"""

import connexion
from datetime import datetime
from flask import g, request
from flask import g
from flask_babel import Babel
import os

from apicrud import AccessControl, AccountSettings, Metrics, ServiceConfig, \
ServiceRegistry, SessionManager, database, initialize
from apicrud import ServiceConfig, initialize

import controllers
from messaging import send_contact
import models

application = connexion.FlaskApp(__name__)
path = os.path.dirname(os.path.abspath(__file__))
config = ServiceConfig(
babel_translation_directories='i18n;%s' % os.path.join(path, 'i18n'),
reset=True, file=os.path.join(path, 'config.yaml'), models=models).config
initialize.app(application)
babel = Babel(application.app)


@application.app.before_first_request
def setup_db(db_url=None, redis_conn=None):
"""Database setup
Args:
db_url (str): URL with db host, credentials and db name
redis_conn (obj): connection to redis
"""
db_url = db_url or config.DB_URL
ServiceRegistry().register(controllers.resources())
if __name__ in ['__main__', 'main', 'uwsgi_file_main']:
database.initialize_db(db_url=db_url, redis_conn=redis_conn)
Metrics(redis_conn=redis_conn, func_send=send_contact.delay).store(
'api_start_timestamp', value=int(datetime.now().timestamp()))


@application.app.before_request
def before_request():
"""Request-setup function - get sessions to database and auth
"""
g.db = database.get_session()
g.session = SessionManager()
g.request_start_time = datetime.utcnow()
initialize.before_request()


@application.app.after_request
def add_header(response):
"""All responses get a cache-control header"""
response.cache_control.max_age = config.HTTP_RESPONSE_CACHE_MAX_AGE
Metrics().store(
'api_request_seconds_total', value=datetime.utcnow().timestamp() -
g.request_start_time.timestamp())
return response
return initialize.after_request(response)


@application.app.teardown_appcontext
Expand All @@ -71,14 +38,11 @@ def cleanup(resp_or_exc):

@babel.localeselector
def get_locale():
acc = AccessControl()
if acc.auth and acc.uid:
locale = AccountSettings(acc.account_id,
uid=acc.uid, db_session=g.db).locale
if locale:
return locale
return request.accept_languages.best_match(config.LANGUAGES)
return initialize.get_locale()


if __name__ in ('__main__', 'uwsgi_file_main', 'media.main'):
initialize.app(application, controllers, models, os.path.dirname(
os.path.abspath(__file__)), func_send=send_contact.delay)
if __name__ == '__main__':
application.run(port=config.APP_PORT)
application.run(port=ServiceConfig().config.APP_PORT)
19 changes: 13 additions & 6 deletions media/models/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
# coding: utf-8

# from geoalchemy2 import Geometry
from sqlalchemy import BOOLEAN, Column, Enum, ForeignKey, INTEGER, String, \
TEXT, TIMESTAMP, Unicode, UniqueConstraint
from sqlalchemy import BOOLEAN, Column, Enum, ForeignKey, INTEGER, \
LargeBinary, String, TEXT, TIMESTAMP, Unicode, UniqueConstraint
from sqlalchemy import func
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import relationship, backref
from sqlalchemy_utils.types.encrypted.encrypted_type import AesEngine, \
StringEncryptedType
Expand All @@ -24,8 +25,9 @@ class Account(AsDictMixin, Base):
__table_args__ = (
UniqueConstraint(u'id', u'uid', name='uniq_account_user'),
)
__rest_exclude__ = ('password', 'totp_secret', 'invalid_attempts',
'last_invalid_attempt')
__rest_exclude__ = ('backup_codes', 'password', 'totp_secret',
'invalid_attempts', 'last_invalid_attempt')
__rest_hybrid__ = ('totp',)

id = Column(String(16), primary_key=True, unique=True)
name = Column(String(32), nullable=False, unique=True)
Expand All @@ -36,7 +38,8 @@ class Account(AsDictMixin, Base):
password_must_change = Column(BOOLEAN, nullable=False,
server_default="False")
totp_secret = Column(StringEncryptedType(Unicode, aes_secret, AesEngine,
'pkcs5', length=48))
'pkcs5', length=64))
backup_codes = Column(LargeBinary(256))
is_admin = Column(BOOLEAN, nullable=False, server_default="False")
settings_id = Column(ForeignKey(u'settings.id'), nullable=False)
last_login = Column(TIMESTAMP)
Expand All @@ -50,6 +53,10 @@ class Account(AsDictMixin, Base):
'account_uid', cascade='all, delete-orphan'))
settings = relationship('Settings')

@hybrid_property
def totp(self):
return True if self.totp_secret else False


class APIkey(AsDictMixin, Base):
__tablename__ = 'apikeys'
Expand Down Expand Up @@ -361,7 +368,7 @@ class Profile(AsDictMixin, Base):
id = Column(String(16), primary_key=True, unique=True)
uid = Column(ForeignKey(u'people.id', ondelete='CASCADE'), nullable=False)
item = Column(String(32), nullable=False)
value = Column(String(32))
value = Column(String(96))
location_id = Column(ForeignKey(u'locations.id'))
tz_id = Column(ForeignKey(u'time_zone_name.id'))
privacy = Column(String(8), nullable=False, server_default=u'public')
Expand Down
5 changes: 5 additions & 0 deletions media/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
from apicrud import ServiceConfig

Base = declarative_base()
# TODO set this value after initialize.app not at import time
aes_secret = ServiceConfig().config.DB_AES_SECRET


class AsDictMixin(object):

def as_dict(self):
"""Returns a serializable dict from an instance of the model
Expand All @@ -31,5 +33,8 @@ def as_dict(self):
if hasattr(self, '__rest_related__'):
for key in self.__rest_related__:
retval[key] = [rec.id for rec in getattr(self, key)]
if hasattr(self, '__rest_hybrid__'):
for key in self.__rest_hybrid__:
retval[key] = getattr(self, key)
retval.pop('_sa_instance_state', None)
return retval
15 changes: 0 additions & 15 deletions media/models/media.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,6 @@ class AlbumContent(Base):
rank = Column(Float)
created = Column(TIMESTAMP, nullable=False, server_default=func.now())

album = relationship('Album', foreign_keys=[album_id], backref=backref(
'albumcontents', cascade='all, delete-orphan'))
picture = relationship('Picture', backref=backref(
'albumcontents', cascade='all, delete-orphan'))


class File(AsDictMixin, Base):
__tablename__ = 'files'
Expand Down Expand Up @@ -114,11 +109,6 @@ class ListFile(Base):
nullable=False, index=True)
created = Column(TIMESTAMP, nullable=False, server_default=func.now())

file = relationship('File', foreign_keys=[file_id], backref=backref(
'listfiles', cascade='all, delete-orphan'))
list = relationship('List', backref=backref(
'listfiles', cascade='all, delete-orphan'))


# Message attachments
class MessageFile(Base):
Expand All @@ -133,11 +123,6 @@ class MessageFile(Base):
nullable=False, index=True)
created = Column(TIMESTAMP, nullable=False, server_default=func.now())

file = relationship('File', foreign_keys=[file_id], backref=backref(
'messagefiles', cascade='all, delete-orphan'))
message = relationship('Message', backref=backref(
'messagefiles', cascade='all, delete-orphan'))


class Picture(Base):
__tablename__ = 'pictures'
Expand Down
4 changes: 2 additions & 2 deletions media/openapi/account.path.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ auth:
tags:
- auth
x-codegen-request-body-name: body
x-openapi-router-controller: controllers.auth
x-openapi-router-controller: apicrud.controllers.auth
logout:
get:
summary: Log out
Expand All @@ -29,4 +29,4 @@ logout:
description: Invalid input
tags:
- auth
x-openapi-router-controller: controllers.auth
x-openapi-router-controller: apicrud.controllers.auth
4 changes: 2 additions & 2 deletions media/openapi/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ components:
basic:
type: http
scheme: basic
x-basicInfoFunc: apicrud.session_auth.basic
x-basicInfoFunc: apicrud.auth.local_user.basic
apikey:
in: header
name: X-API-KEY
type: apiKey
x-apikeyInfoFunc: apicrud.session_auth.api_key
x-apikeyInfoFunc: apicrud.auth.apikey.auth

paths:
/album:
Expand Down

0 comments on commit 758bef9

Please sign in to comment.