application.py
113 lines
| 3.5 KiB
| text/x-python
|
PythonLexer
r0 | # -*- coding: utf-8 -*- | |||
r112 | # Copyright 2010 - 2017 RhodeCode GmbH and the AppEnlight project authors | |||
r0 | # | |||
r112 | # Licensed under the Apache License, Version 2.0 (the "License"); | |||
# you may not use this file except in compliance with the License. | ||||
# You may obtain a copy of the License at | ||||
r0 | # | |||
r112 | # http://www.apache.org/licenses/LICENSE-2.0 | |||
r0 | # | |||
r112 | # Unless required by applicable law or agreed to in writing, software | |||
# distributed under the License is distributed on an "AS IS" BASIS, | ||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
# See the License for the specific language governing permissions and | ||||
# limitations under the License. | ||||
r0 | ||||
import uuid | ||||
import logging | ||||
import sqlalchemy as sa | ||||
from appenlight.models.resource import Resource | ||||
from sqlalchemy.orm import aliased | ||||
log = logging.getLogger(__name__) | ||||
def generate_api_key(): | ||||
r153 | uid = str(uuid.uuid4()).replace("-", "") | |||
r0 | return uid[0:32] | |||
class Application(Resource): | ||||
""" | ||||
Resource of application type | ||||
""" | ||||
r153 | __tablename__ = "applications" | |||
__mapper_args__ = {"polymorphic_identity": "application"} | ||||
r0 | ||||
# lists configurable possible permissions for this resource type | ||||
r153 | __possible_permissions__ = ("view", "update_reports") | |||
resource_id = sa.Column( | ||||
sa.Integer(), | ||||
sa.ForeignKey("resources.resource_id", onupdate="CASCADE", ondelete="CASCADE"), | ||||
primary_key=True, | ||||
) | ||||
domains = sa.Column(sa.UnicodeText(), nullable=False, default="") | ||||
api_key = sa.Column( | ||||
sa.String(32), nullable=False, unique=True, index=True, default=generate_api_key | ||||
) | ||||
public_key = sa.Column( | ||||
sa.String(32), nullable=False, unique=True, index=True, default=generate_api_key | ||||
) | ||||
default_grouping = sa.Column( | ||||
sa.Unicode(20), nullable=False, default="url_traceback" | ||||
) | ||||
r0 | error_report_threshold = sa.Column(sa.Integer(), default=10) | |||
slow_report_threshold = sa.Column(sa.Integer(), default=10) | ||||
r153 | allow_permanent_storage = sa.Column(sa.Boolean(), default=False, nullable=False) | |||
r0 | ||||
r153 | @sa.orm.validates("default_grouping") | |||
r0 | def validate_default_grouping(self, key, grouping): | |||
""" validate if resouce can have specific permission """ | ||||
r153 | assert grouping in ["url_type", "url_traceback", "traceback_server"] | |||
r0 | return grouping | |||
r153 | report_groups = sa.orm.relationship( | |||
"ReportGroup", | ||||
cascade="all, delete-orphan", | ||||
passive_deletes=True, | ||||
passive_updates=True, | ||||
lazy="dynamic", | ||||
backref=sa.orm.backref("application", lazy="joined"), | ||||
) | ||||
postprocess_conf = sa.orm.relationship( | ||||
"ApplicationPostprocessConf", | ||||
cascade="all, delete-orphan", | ||||
passive_deletes=True, | ||||
passive_updates=True, | ||||
backref="resource", | ||||
) | ||||
logs = sa.orm.relationship( | ||||
"Log", | ||||
lazy="dynamic", | ||||
backref="application", | ||||
passive_deletes=True, | ||||
passive_updates=True, | ||||
) | ||||
integrations = sa.orm.relationship( | ||||
"IntegrationBase", | ||||
backref="resource", | ||||
cascade="all, delete-orphan", | ||||
passive_deletes=True, | ||||
passive_updates=True, | ||||
) | ||||
r0 | ||||
def generate_api_key(self): | ||||
return generate_api_key() | ||||
def after_update(mapper, connection, target): | ||||
from appenlight.models.services.application import ApplicationService | ||||
r153 | ||||
log.info("clearing out ApplicationService cache") | ||||
r0 | ApplicationService.by_id_cached().invalidate(target.resource_id) | |||
ApplicationService.by_api_key_cached().invalidate(target.api_key) | ||||
r153 | sa.event.listen(Application, "after_update", after_update) | |||
sa.event.listen(Application, "after_delete", after_update) | ||||