Skip to content

Commit

Permalink
[MIG] shopinvader_helpdesk to v16
Browse files Browse the repository at this point in the history
  • Loading branch information
paradoxxxzero committed Sep 13, 2023
1 parent 5c6f5fe commit 9d5058d
Show file tree
Hide file tree
Showing 14 changed files with 197 additions and 174 deletions.
19 changes: 16 additions & 3 deletions shopinvader_helpdesk/pydantic_models/attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,27 @@
from pydantic import BaseModel, Field


class AttachmentRequest(BaseModel, metaclass=ExtendableModelMeta):
class AttachmentRequest(BaseModel):
name: str = None


class AttachmentInfo(BaseModel, metaclass=ExtendableModelMeta):
class AttachmentInfo(BaseModel):
id: int
name: str

@classmethod
def from_rec(cls, odoo_rec):
return cls.model_construct(
id=odoo_rec.id,
name=odoo_rec.name,
)

class AttachableInfo(BaseModel, metaclass=ExtendableModelMeta):

class AttachableInfo(BaseModel):
attachments: List[IdAndNameInfo] = Field([], alias="attachment_ids")

@classmethod
def from_rec(cls, odoo_rec):
return cls.model_construct(
attachments=[IdAndNameInfo.from_rec(rec) for rec in odoo_rec.attachment_ids]
)
18 changes: 18 additions & 0 deletions shopinvader_helpdesk/pydantic_models/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from extendable_pydantic import ExtendableModelMeta

from odoo.addons.pydantic import utils
from pydantic import BaseModel, EmailStr, Field, ConfigDict

from pydantic import BaseModel

Expand All @@ -9,6 +10,23 @@ class IdAndNameInfo(BaseModel, metaclass=ExtendableModelMeta):
id: int
name: str

@classmethod
def from_rec(cls, odoo_rec):
if not odoo_rec:
return
return cls.model_construct(
id=odoo_rec.id,
name=odoo_rec.name,
)


class IdRequest(BaseModel, metaclass=ExtendableModelMeta):
id: int

@classmethod
def from_rec(cls, odoo_rec):
if not odoo_rec:
return
return cls.model_construct(
id=odoo_rec.id,
)
15 changes: 13 additions & 2 deletions shopinvader_helpdesk/pydantic_models/mail_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from datetime import datetime
from typing import List

from pydantic import BaseModel, Field

from odoo import fields, models
from pydantic import BaseModel, Field, ConfigDict
from .base import IdAndNameInfo, IdRequest
from odoo.addons.pydantic import utils

Expand All @@ -25,6 +25,17 @@ class MailMessageInfo(MailMessageBase):
date: datetime
author: IdAndNameInfo = Field(None, alias="author_id")

@classmethod
def from_rec(cls, odoo_rec):
return cls.model_construct(
id=odoo_rec.id,
date=fields.Datetime.context_timestamp(odoo_rec, odoo_rec.date)
if odoo_rec.date
else None,
body=odoo_rec.body,
author=IdAndNameInfo.from_rec(odoo_rec.author_id),
)


class MailThreadInfo(BaseModel):
messages: List[MailMessageInfo] = Field([], alias="message_ids")
45 changes: 41 additions & 4 deletions shopinvader_helpdesk/pydantic_models/ticket.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
# @author Pierrick Brun <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from typing import List
from typing import List, Optional
from datetime import datetime

from extendable_pydantic import ExtendableModelMeta
from pydantic import BaseModel, EmailStr, Field
from pydantic import BaseModel, EmailStr, Field, ConfigDict

from .attachment import AttachableInfo
from .base import IdAndNameInfo, IdRequest
from .mail_message import MailThreadInfo
from odoo.addons.pydantic import utils
from odoo import fields

from ..pydantic_models.mail_message import MailThreadInfo


class HelpdeskPartnerRequest(BaseModel):
Expand All @@ -22,6 +23,8 @@ class HelpdeskPartnerRequest(BaseModel):


class HelpdeskTicketSaleLineRequest(BaseModel):
model_config = ConfigDict(from_attributes=True)

sale_line_id: int
qty: int = None

Expand All @@ -31,8 +34,16 @@ class HelpdeskTicketSaleLineInfo(BaseModel):
qty: int
product_name: str = None

@classmethod
def from_rec(cls, sol):
return cls.model_construct(
sale_line_id=sol.id, qty=sol.qty, product_name=sol.product_name
)


class HelpdeskTicketInfo(AttachableInfo, MailThreadInfo):
model_config = ConfigDict(from_attributes=True)

id: int
name: str
description: str
Expand All @@ -45,8 +56,34 @@ class HelpdeskTicketInfo(AttachableInfo, MailThreadInfo):
sale: IdAndNameInfo = Field(None, alias="sale_id")
sale_lines: List[HelpdeskTicketSaleLineInfo] = Field([], alias="sale_line_ids")

@classmethod
def from_rec(cls, odoo_rec):
return cls.model_construct(
id=odoo_rec.id,
name=odoo_rec.name or None,
description=odoo_rec.description or None,
create_date=fields.Datetime.context_timestamp(
odoo_rec, odoo_rec.create_date
)
if odoo_rec.create_date
else None,
last_stage_update=fields.Datetime.context_timestamp(
odoo_rec, odoo_rec.last_stage_update
)
if odoo_rec.last_stage_update
else None,
category=IdAndNameInfo.from_rec(odoo_rec.category_id),
team=IdAndNameInfo.from_rec(odoo_rec.team_id),
stage=IdAndNameInfo.from_rec(odoo_rec.stage_id),
sale=IdAndNameInfo.from_rec(odoo_rec.sale_id),
sale_lines=[
HelpdeskTicketSaleLineInfo.from_rec(sol)
for sol in odoo_rec.sale_line_ids
],
)


class HelpdeskTicketRequest(BaseModel, metaclass=ExtendableModelMeta):
class HelpdeskTicketRequest(BaseModel):
name: str
description: str
partner: HelpdeskPartnerRequest = Field(None)
Expand Down
1 change: 0 additions & 1 deletion shopinvader_helpdesk/services/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from . import attachment_mixin
from . import service
from . import abstract_mail_thread
from . import ticket
from . import settings
37 changes: 0 additions & 37 deletions shopinvader_helpdesk/services/abstract_mail_thread.py

This file was deleted.

6 changes: 3 additions & 3 deletions shopinvader_helpdesk/services/attachment_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,17 @@ def _get_attachment_response_data(self, attachment):
output_param=PydanticModel(AttachmentInfo),
)
def create_attachment(self, object_id=None, file=None, params=None):
record = self._get(object_id)
record = self._get_record(object_id)
vals = params.dict()
vals["res_id"] = record.id
vals["res_model"] = record._name
vals["raw"] = file.read()
if not vals.get("name"):
vals["name"] = os.path.basename(file.filename)
attachment = self.env["ir.attachment"].create(vals)
return AttachmentInfo.from_orm(attachment)
return AttachmentInfo.from_rec(attachment)

def _get(self, _id):
def _get_record(self, _id):
record = self.env[self._expose_model].browse(_id)
if not record:
raise MissingError(_("The record does not exist: {}".format(_id)))
Expand Down
2 changes: 1 addition & 1 deletion shopinvader_helpdesk/services/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def _get(self, _id):
return record

def _get_base_search_domain(self):
if not self.env.context.get("authenticated_partner_id"):
if not self.partner:
raise AccessError(
_("You should be connected to search for Helpdesk Tickets")
)
Expand Down
6 changes: 3 additions & 3 deletions shopinvader_helpdesk/services/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def categories(self):

def _get_categories(self):
categories = self.env["helpdesk.ticket.category"].search([])
result = [IdAndNameInfo.from_orm(categ) for categ in categories]
result = [IdAndNameInfo.from_rec(categ) for categ in categories]
return result

@restapi.method(
Expand All @@ -56,9 +56,9 @@ def _get_categories(self):
auth="public_or_default",
)
def teams(self):
return [IdAndNameInfo.from_orm(team) for team in self._get_teams()]
return [IdAndNameInfo.from_rec(team) for team in self._get_teams()]

def _get_teams(self):
teams = self.env["helpdesk.ticket.team"].search([])
result = [IdAndNameInfo.from_orm(team) for team in teams]
result = [IdAndNameInfo.from_rec(team) for team in teams]
return result
Loading

0 comments on commit 9d5058d

Please sign in to comment.