Skip to content

Commit

Permalink
refactor: move custom field methods to be a sub element of member, to…
Browse files Browse the repository at this point in the history
…gether with a factory method taking in the member ID. This eliminates the need to dupicate CRUD code just to take in the member_id and better follows the API style of EasyVerein
  • Loading branch information
waza-ari committed Aug 19, 2024
1 parent 64c1d76 commit 7cdaab2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 88 deletions.
4 changes: 0 additions & 4 deletions easyverein/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
from .modules.invoice import InvoiceMixin
from .modules.invoice_item import InvoiceItemMixin
from .modules.member import MemberMixin
from .modules.member_custom_field import MemberCustomFieldMixin
from .modules.member_group import MemberMemberGroupMixin


class EasyvereinAPI:
Expand Down Expand Up @@ -42,5 +40,3 @@ def __init__(
self.invoice = InvoiceMixin(self.c, self.logger)
self.invoice_item = InvoiceItemMixin(self.c, self.logger)
self.member = MemberMixin(self.c, self.logger)
self.member_custom_field = MemberCustomFieldMixin(self.c, self.logger)
self.member_member_group = MemberMemberGroupMixin(self.c, self.logger)
84 changes: 17 additions & 67 deletions easyverein/modules/member_custom_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,83 +5,30 @@
import logging

from ..core.client import EasyvereinClient
from ..core.protocol import IsEVClientProtocol
from ..models import MemberCustomField, MemberCustomFieldCreate, MemberCustomFieldUpdate
from ..models.member_custom_field import MemberCustomFieldFilter
from ..models import (
CustomField,
MemberCustomField,
MemberCustomFieldCreate,
MemberCustomFieldFilter,
MemberCustomFieldUpdate,
)
from .mixins.crud import CRUDMixin
from .mixins.recycle_bin import RecycleBinMixin


class MemberCustomFieldMixin(
CRUDMixin[
MemberCustomField,
MemberCustomFieldCreate,
MemberCustomFieldUpdate,
MemberCustomFieldFilter,
],
RecycleBinMixin[MemberCustomField],
CRUDMixin[MemberCustomField, MemberCustomFieldCreate, MemberCustomFieldUpdate, MemberCustomFieldFilter]
):
def __init__(self, client: EasyvereinClient, logger: logging.Logger):
def __init__(self, client: EasyvereinClient, logger: logging.Logger, member_id: int):
self.return_type = MemberCustomField
self.c = client
self.logger = logger
self.member_id = None
self.member_id = member_id

@property
def endpoint_name(self) -> str:
return f"member/{self.member_id}/custom-fields"

def get(
self: IsEVClientProtocol,
member_id: int = None,
query: str = None,
limit: int = 10,
) -> list[MemberCustomField]:
if member_id:
self.member_id = member_id
return super().get(query=query, limit=limit)

def get_all(
self: IsEVClientProtocol,
member_id: int = None,
query: str = None,
limit_per_page: int = 10,
) -> list[MemberCustomField]:
if member_id:
self.member_id = member_id
return super().get_all(query=query, limit_per_page=limit_per_page)

def get_by_id(self: IsEVClientProtocol, obj_id: int, member_id: int = None, query: str = None) -> MemberCustomField:
if member_id:
self.member_id = member_id
return super().get_by_id(obj_id, query=query)

def create(self: IsEVClientProtocol, obj: MemberCustomFieldCreate, member_id: int = None) -> MemberCustomField:
if member_id:
self.member_id = member_id
return super().create(obj)

def update(
self: IsEVClientProtocol,
obj_id: int,
obj: MemberCustomFieldUpdate,
member_id: int = None,
) -> MemberCustomField:
if member_id:
self.member_id = member_id
return super().update(obj_id, obj)

def delete(
self: IsEVClientProtocol,
obj: MemberCustomField,
delete_from_recycle_bin: bool = False,
member_id: int = None,
):
if member_id:
self.member_id = member_id
return super().delete(obj, delete_from_recycle_bin)

def ensure_set(self, member_id: int, custom_field_id: int, value: str) -> MemberCustomField:
def ensure_set(self, custom_field_id: int, value: str) -> MemberCustomField:
"""
Convenience method to set the custom field value on a member, no matter if it was
set before already (PATCH) or not.
Expand All @@ -94,21 +41,24 @@ def ensure_set(self, member_id: int, custom_field_id: int, value: str) -> Member
value (str): New value the specified custom field should be set to
"""

self.member_id = member_id

# Get all custom fields this member has already set, use max limit available
query = "{id,value,customField{id}}"
all_member_custom_fields = self.get_all(limit_per_page=100, query=query)

# Extract custom field from that list if it exists
existing_custom_field = next(
(mcf for mcf in all_member_custom_fields if mcf.customField.id == custom_field_id),
(
mcf
for mcf in all_member_custom_fields
if isinstance(mcf.customField, CustomField) and mcf.customField.id == custom_field_id
),
None,
)

if existing_custom_field:
# It's already there, we need to patch the existing field
patch = MemberCustomFieldUpdate(value=value)
assert existing_custom_field.id
return self.update(existing_custom_field.id, patch)
else:
# It's not there, we need to create it
Expand Down
30 changes: 13 additions & 17 deletions tests/test_member_custom_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,46 @@

@pytest.fixture(scope="class")
def example_member_custom_field(ev_connection: EasyvereinAPI, example_member, example_custom_field):
member_custom_field = ev_connection.member_custom_field.create(
member_custom_field = ev_connection.member.custom_field(example_member.id).create(
MemberCustomFieldCreate(customField=example_custom_field.id, value="Example-Value"),
member_id=example_member.id,
)
yield member_custom_field
ev_connection.member_custom_field.delete(member_custom_field, member_id=example_member.id)
ev_connection.member.custom_field(example_member.id).delete(member_custom_field)


class TestMemberCustomField:
def test_create_custom_field(self, ev_connection: EasyvereinAPI, example_member, example_custom_field):
member_custom_field = ev_connection.member_custom_field.create(
MemberCustomFieldCreate(customField=example_custom_field.id, value="Example-Value"),
member_id=example_member.id,
member_custom_field = ev_connection.member.custom_field(example_member.id).create(
MemberCustomFieldCreate(customField=example_custom_field.id, value="Example-Value")
)
assert isinstance(member_custom_field, MemberCustomField)
assert member_custom_field.value == "Example-Value"

# Delete it again
ev_connection.member_custom_field.delete(member_custom_field)
ev_connection.member.custom_field(example_member.id).delete(member_custom_field)

def test_modify_custom_field(self, ev_connection: EasyvereinAPI, example_member_custom_field, example_member):
member_custom_field = ev_connection.member_custom_field.update(
example_member_custom_field.id,
MemberCustomFieldUpdate(value="Modified-Value"),
member_id=example_member.id,
member_custom_field = ev_connection.member.custom_field(example_member.id).update(
example_member_custom_field.id, MemberCustomFieldUpdate(value="Modified-Value")
)

# Check that the value was modified
assert member_custom_field.value == "Modified-Value"

def test_ensure_custom_field_value(self, ev_connection: EasyvereinAPI, example_custom_field, example_member):
member_custom_field = ev_connection.member_custom_field.ensure_set(
example_member.id, example_custom_field.id, "Ensured-Value"
member_custom_field = ev_connection.member.custom_field(example_member.id).ensure_set(
example_custom_field.id, "Ensured-Value"
)
assert member_custom_field.value == "Ensured-Value"

def test_ensure_custom_field_value_empty(self, ev_connection: EasyvereinAPI, example_custom_field, example_member):
member_custom_field = ev_connection.member_custom_field.ensure_set(
example_member.id, example_custom_field.id, "New-Ensured-Value"
member_custom_field = ev_connection.member.custom_field(example_member.id).ensure_set(
example_custom_field.id, "New-Ensured-Value"
)

assert member_custom_field.value == "New-Ensured-Value"

member_custom_field = ev_connection.member_custom_field.ensure_set(
example_member.id, example_custom_field.id, ""
member_custom_field = ev_connection.member.custom_field(example_member.id).ensure_set(
example_custom_field.id, ""
)
assert member_custom_field.value is None

0 comments on commit 7cdaab2

Please sign in to comment.