Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wazo 3442 phonebook source name auto #195

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions wazo_dird/database/models.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Copyright 2016-2023 The Wazo Authors (see the AUTHORS file)
# Copyright 2016-2024 The Wazo Authors (see the AUTHORS file)
# SPDX-License-Identifier: GPL-3.0-or-later

from sqlalchemy import Column, ForeignKey, Integer, Sequence, String, Text, schema, text
from sqlalchemy.dialects.postgresql import ARRAY, HSTORE, JSON, UUID
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy.orm import relationship, validates

Base = declarative_base()

Expand Down Expand Up @@ -263,6 +263,21 @@ class Source(Base):
lambda: Phonebook, foreign_keys=[phonebook_uuid], lazy='joined'
)

@validates('name')
def ensure_phonebook_name(self, key, value):
assert key == 'name'
if not self.backend == 'phonebook':
return value
else:
assert self.phonebook_uuid and self.phonebook
if self.phonebook.name == value:
return value
else:
raise ValueError(
f'Source name {value} does not correspond to underlying '
f'phonebook name {self.phonebook.name}'
)


class Tenant(Base):
__tablename__ = 'dird_tenant'
Expand Down
8 changes: 6 additions & 2 deletions wazo_dird/plugins/phonebook_backend/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ paths:
- $ref: '#/parameters/tenantuuid'
- name: body
in: body
description: The display configuration body
description: The phonebook source configuration body
required: true
schema:
$ref: '#/definitions/PhonebookSource'
Expand Down Expand Up @@ -152,14 +152,18 @@ definitions:
allOf:
- $ref: '#/definitions/Source'
- properties:
name:
readOnly: true
phonebook_uuid:
type: string
readOnly: true
phonebook_name:
type: string
readOnly: true
phonebook_description:
type: string
readOnly: true
- required:
- name
- phonebook_uuid
PhonebookSourceItems:
title: PhonebookSourceItems
Expand Down
3 changes: 2 additions & 1 deletion wazo_dird/plugins/phonebook_backend/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import TypedDict

from marshmallow import post_load
from xivo.mallow import fields
from xivo.mallow import fields, validate
from xivo.mallow_helpers import ListSchema as _ListSchema

from wazo_dird.schemas import BaseSourceSchema
Expand All @@ -16,6 +16,7 @@ class SourceSchema(BaseSourceSchema):
phonebook_uuid = fields.UUID(required=True)
phonebook_name = fields.String(dump_only=True)
phonebook_description = fields.String(dump_only=True)
name = fields.String(validate=validate.Length(min=1, max=512), dump_only=True)

@post_load
def stringify_uuid(self, data, **kwargs):
Expand Down