Skip to content

Commit

Permalink
inherit lint github action from newsroom-app (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
petrjasek authored Nov 13, 2024
1 parent 0d15cd5 commit 9e38713
Show file tree
Hide file tree
Showing 23 changed files with 165 additions and 184 deletions.
31 changes: 3 additions & 28 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,6 @@ name: "Lint"
on: [ push, pull_request ]

jobs:

flake8:
runs-on: ubuntu-latest

defaults:
run:
working-directory: server

steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- run: pip install flake8
- run: flake8

mypy:
runs-on: ubuntu-latest

defaults:
run:
working-directory: server

steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
with:
python-version: 3.8
- run: pip install -r mypy-requirements.txt
- run: mypy cp
lint:
name: "Lint"
uses: superdesk/newsroom-app/.github/workflows/lint.yml@develop
2 changes: 1 addition & 1 deletion server/cp/commands/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from . import fix_language # noqa
from . import fix_mediaformat # noqa
from . import fix_mediaformat # noqa
6 changes: 4 additions & 2 deletions server/cp/commands/fix_language.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import time

from superdesk import get_resource_service
Expand All @@ -9,7 +8,10 @@
def fix_language(resource="items", limit=50, sleep_secs=2):
service = get_resource_service(resource)

source = {"query": {"terms": {"language": ["en-CA", "en-US", "fr-CA"]}}, "size": 100}
source = {
"query": {"terms": {"language": ["en-CA", "en-US", "fr-CA"]}},
"size": 100,
}

for i in range(int(limit)):
items = service.search(source)
Expand Down
23 changes: 14 additions & 9 deletions server/cp/common_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
from newsroom.web.default_settings import AUTH_PROVIDERS
from newsroom.types import AuthProviderType

AUTH_PROVIDERS.extend([{
"_id": "gip",
"name": lazy_gettext("Firebase"),
"auth_type": AuthProviderType.FIREBASE,
}, {
"_id": "azure",
"name": lazy_gettext("Azure"),
"auth_type": AuthProviderType.SAML,
}])
AUTH_PROVIDERS.extend(
[
{
"_id": "gip",
"name": lazy_gettext("Firebase"),
"auth_type": AuthProviderType.FIREBASE,
},
{
"_id": "azure",
"name": lazy_gettext("Azure"),
"auth_type": AuthProviderType.SAML,
},
]
)
6 changes: 3 additions & 3 deletions server/cp/mgmt_api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

app = get_app()

if __name__ == '__main__':
host = '0.0.0.0'
port = int(os.environ.get('MGMTAPI_PORT', '5500'))
if __name__ == "__main__":
host = "0.0.0.0"
port = int(os.environ.get("MGMTAPI_PORT", "5500"))
app.run(host=host, port=port, debug=True, use_reloader=True)
36 changes: 19 additions & 17 deletions server/cp/mgmt_api/default_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# at https://www.sourcefabric.org/superdesk/license

from superdesk.default_settings import urlparse
from newsroom.web.default_settings import ( # noqa
from newsroom.web.default_settings import ( # noqa
env,
ELASTICSEARCH_URL,
ELASTICSEARCH_SETTINGS,
Expand All @@ -21,42 +21,44 @@
)
import os

MGMTAPI_URL = env('MGMTAPI_URL', 'http://localhost:5500/api')
MGMTAPI_URL = env("MGMTAPI_URL", "http://localhost:5500/api")
server_url = urlparse(MGMTAPI_URL)
URL_PREFIX = env("MGMTAPI_URL_PREFIX", server_url.path.strip("/")) or ""

BLUEPRINTS = []

CORE_APPS = [
'cp.mgmt_api.companies',
'cp.mgmt_api.users',
'cp.mgmt_api.products',
'cp.mgmt_api.navigations',
'cp.mgmt_api.topics',
"cp.mgmt_api.companies",
"cp.mgmt_api.users",
"cp.mgmt_api.products",
"cp.mgmt_api.navigations",
"cp.mgmt_api.topics",
]

INSTALLED_APPS = []

LANGUAGES = ['en', 'fi', 'cs', 'fr_CA']
DEFAULT_LANGUAGE = 'en'
LANGUAGES = ["en", "fi", "cs", "fr_CA"]
DEFAULT_LANGUAGE = "en"

# newsroom default db and index names
MONGO_DBNAME = env('MONGO_DBNAME', 'newsroom')
MONGO_DBNAME = env("MONGO_DBNAME", "newsroom")
# mongo
MONGO_URI = env('MONGO_URI', f'mongodb://localhost/{MONGO_DBNAME}')
CONTENTAPI_MONGO_URI = env('CONTENTAPI_MONGO_URI', f'mongodb://localhost/{MONGO_DBNAME}')
MONGO_URI = env("MONGO_URI", f"mongodb://localhost/{MONGO_DBNAME}")
CONTENTAPI_MONGO_URI = env(
"CONTENTAPI_MONGO_URI", f"mongodb://localhost/{MONGO_DBNAME}"
)
# elastic
ELASTICSEARCH_INDEX = env('ELASTICSEARCH_INDEX', MONGO_DBNAME)
CONTENTAPI_ELASTICSEARCH_INDEX = env('CONTENTAPI_ELASTICSEARCH_INDEX', MONGO_DBNAME)
ELASTICSEARCH_INDEX = env("ELASTICSEARCH_INDEX", MONGO_DBNAME)
CONTENTAPI_ELASTICSEARCH_INDEX = env("CONTENTAPI_ELASTICSEARCH_INDEX", MONGO_DBNAME)

FILTER_AGGREGATIONS = False
ELASTIC_DATETIME_FORMAT = '%Y-%m-%dT%H:%M:%S'
ELASTIC_DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%S"

CACHE_TYPE = os.environ.get('CACHE_TYPE', 'simple') # in-memory cache
CACHE_TYPE = os.environ.get("CACHE_TYPE", "simple") # in-memory cache
# The default timeout that is used if no timeout is specified in sec
CACHE_DEFAULT_TIMEOUT = 3600
# Redis host (used only if CACHE_TYPE is redis)
CACHE_REDIS_URL = os.environ.get('REDIS_URL', 'redis://localhost:6379')
CACHE_REDIS_URL = os.environ.get("REDIS_URL", "redis://localhost:6379")
# fix superdesk cache config
CACHE_URL = CACHE_REDIS_URL

Expand Down
87 changes: 49 additions & 38 deletions server/cp/mgmt_api/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,21 @@


class NewsroomMGMTAPI(BaseNewsroomApp):
INSTANCE_CONFIG = 'settings_mgmtapi.py'
INSTANCE_CONFIG = "settings_mgmtapi.py"
AUTH_SERVICE = JWTAuth

def __init__(self, import_name=__package__, config=None, **kwargs):
if not hasattr(self, 'settings'):
self.settings = flask.Config('.')
if not hasattr(self, "settings"):
self.settings = flask.Config(".")

if config and config.get('BEHAVE'):
if config and config.get("BEHAVE"):
# ``superdesk.tests.update_config`` adds ``planning`` to ``INSTALLED_APPS``
# So if we're running behave tests, reset this config here
config['INSTALLED_APPS'] = []
config["INSTALLED_APPS"] = []

super(NewsroomMGMTAPI, self).__init__(import_name=import_name, config=config, **kwargs)
super(NewsroomMGMTAPI, self).__init__(
import_name=import_name, config=config, **kwargs
)

def load_app_default_config(self):
"""
Expand All @@ -48,7 +50,7 @@ def load_app_default_config(self):
# default config from `content_api.app.settings`
super().load_app_default_config()
# default config from `cp.mgmt_api.default_settings`
self.config.from_object('cp.mgmt_api.default_settings')
self.config.from_object("cp.mgmt_api.default_settings")

def load_app_instance_config(self):
"""
Expand All @@ -57,52 +59,61 @@ def load_app_instance_config(self):
# config from newsroom-app settings_newsapi.py file
super().load_app_instance_config()
# config from env var
self.config.from_envvar('MGMT_API_SETTINGS', silent=True)
self.config.from_envvar("MGMT_API_SETTINGS", silent=True)

def run(self, host=None, port=None, debug=None, **options):
if not self.config.get('MGMT_API_ENABLED', False):
raise RuntimeError('Management API is not enabled')
if not self.config.get("MGMT_API_ENABLED", False):
raise RuntimeError("Management API is not enabled")

super(NewsroomMGMTAPI, self).run(host, port, debug, **options)

def setup_error_handlers(self):
def json_error(err):
return flask.jsonify(err), err['code']
return flask.jsonify(err), err["code"]

def handle_werkzeug_errors(err):
return json_error({
'error': str(err),
'message': getattr(err, 'description') or None,
'code': getattr(err, 'code') or 500
})
return json_error(
{
"error": str(err),
"message": getattr(err, "description") or None,
"code": getattr(err, "code") or 500,
}
)

def superdesk_api_error(err):
return json_error({
'error': err.message or '',
'message': err.payload,
'code': err.status_code or 500,
})
return json_error(
{
"error": err.message or "",
"message": err.payload,
"code": err.status_code or 500,
}
)

def assertion_error(err):
return json_error({
'error': err.args[0] if err.args else 1,
'message': str(err),
'code': 400
})
return json_error(
{
"error": err.args[0] if err.args else 1,
"message": str(err),
"code": 400,
}
)

def base_exception_error(err):
if type(err) is ElasticRequestError and err.error == 'search_phase_execution_exception':
return json_error({
'error': 1,
'message': 'Invalid search query',
'code': 400
})

return json_error({
'error': err.args[0] if err.args else 1,
'message': str(err),
'code': 500
})
if (
type(err) is ElasticRequestError
and err.error == "search_phase_execution_exception"
):
return json_error(
{"error": 1, "message": "Invalid search query", "code": 400}
)

return json_error(
{
"error": err.args[0] if err.args else 1,
"message": str(err),
"code": 500,
}
)

for cls in HTTPException.__subclasses__():
self.register_error_handler(cls, handle_werkzeug_errors)
Expand Down
4 changes: 3 additions & 1 deletion server/cp/mgmt_api/navigations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@

def init_app(app):
NavigationsResource.internal_resource = False
superdesk.register_resource('navigations', NavigationsResource, NavigationsService, _app=app)
superdesk.register_resource(
"navigations", NavigationsResource, NavigationsService, _app=app
)
2 changes: 1 addition & 1 deletion server/cp/mgmt_api/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

def init_app(app):
ProductsResource.internal_resource = False
superdesk.register_resource('products', ProductsResource, ProductsService, _app=app)
superdesk.register_resource("products", ProductsResource, ProductsService, _app=app)
30 changes: 18 additions & 12 deletions server/cp/mgmt_api/topics.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,38 @@

def init_app(app):
TopicsResource.internal_resource = False
superdesk.register_resource('topics', GlobalTopicsResource, GlobalTopicsService, _app=app)
superdesk.register_resource('topic_folders', FoldersResource, FoldersService, _app=app)
superdesk.register_resource(
"topics", GlobalTopicsResource, GlobalTopicsService, _app=app
)
superdesk.register_resource(
"topic_folders", FoldersResource, FoldersService, _app=app
)


class GlobalTopicsResource(TopicsResource):
url = 'topics'
resource_methods = ['GET', 'POST', 'DELETE']
url = "topics"
resource_methods = ["GET", "POST", "DELETE"]


class GlobalTopicsService(TopicsService):
def on_create(self, docs):
super().on_create(docs)
for doc in docs:
user = doc.get('user')
user = doc.get("user")
if user:
doc['original_creator'] = user
doc['version_creator'] = user
elif not doc.get('is_global'):
message = ("Please set is_global True, or provide user in the body.")
raise SuperdeskApiError.badRequestError(message=message, payload=message)
doc["original_creator"] = user
doc["version_creator"] = user
elif not doc.get("is_global"):
message = "Please set is_global True, or provide user in the body."
raise SuperdeskApiError.badRequestError(
message=message, payload=message
)

def on_created(self, docs):
super().on_created(docs)
for doc in docs:
app.cache.set(str(doc['_id']), doc)
app.cache.set(str(doc["_id"]), doc)

def on_update(self, updates, original):
super().on_update(updates, original)
app.cache.delete(str(original['_id']))
app.cache.delete(str(original["_id"]))
Loading

0 comments on commit 9e38713

Please sign in to comment.