Skip to content

Commit

Permalink
[ADD] mozaik_membership_request attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
qgroulard authored and marielejeune committed Sep 19, 2023
1 parent d8b093f commit 9e92cff
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
8 changes: 8 additions & 0 deletions mozaik_membership_rest/pydantic_models/membership_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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] = []
16 changes: 16 additions & 0 deletions mozaik_membership_rest/services/membership_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 9e92cff

Please sign in to comment.