diff --git a/mozaik_membership_rest/pydantic_models/membership_request.py b/mozaik_membership_rest/pydantic_models/membership_request.py index 5e5d0c296..28d329f1a 100644 --- a/mozaik_membership_rest/pydantic_models/membership_request.py +++ b/mozaik_membership_rest/pydantic_models/membership_request.py @@ -4,10 +4,17 @@ from datetime import datetime from typing import List +import pydantic from extendable_pydantic import ExtendableModelMeta from pydantic import BaseModel +class MembershipRequestAttachment(BaseModel, metaclass=ExtendableModelMeta): + name: str + mimetype: str + content: str = pydantic.Field(..., title="Content encoded in base64") + + class MembershipRequest(BaseModel, metaclass=ExtendableModelMeta): lastname: str firstname: str @@ -53,3 +60,4 @@ class MembershipRequest(BaseModel, metaclass=ExtendableModelMeta): force_auto_validate: bool = False auto_generate_reference: bool = False auto_validate_after_payment: bool = False + attachments: List[MembershipRequestAttachment] = [] diff --git a/mozaik_membership_rest/services/membership_request.py b/mozaik_membership_rest/services/membership_request.py index 3797dc88c..7ada7f8ef 100644 --- a/mozaik_membership_rest/services/membership_request.py +++ b/mozaik_membership_rest/services/membership_request.py @@ -181,6 +181,19 @@ def _get_protected_values(self, vals): protected_values[field_name] = field_value[0][2] return protected_values + def _create_membership_request_attachments(self, mr, attachments): + for attachment in attachments: + self.env["ir.attachment"].sudo().create( + { + "name": attachment["name"], + "type": "binary", + "mimetype": attachment["mimetype"], + "datas": attachment["content"], + "res_model": "membership.request", + "res_id": mr.id, + } + ) + def _create_membership_request(self, membership_request): vals = self._validate_membership_request_input(membership_request) protected_values = self._get_protected_values(vals) @@ -197,6 +210,9 @@ def _create_membership_request(self, membership_request): .with_context(mode="pre_process", protected_values=protected_values) .create(vals) ) + attachments = membership_request.dict().get("attachments", False) + if attachments: + self._create_membership_request_attachments(mr, attachments) # We validate the request if asked, and force auto-validation if asked mr._auto_validate_may_be_forced(membership_request.auto_validate) return mr