summaryrefslogtreecommitdiff
path: root/indoteknik_api/models/rest_api.py
blob: 65119b528de37d2bfdcb9ba08431f641c3806854 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from odoo import models
from odoo.http import request
import datetime
from pytz import timezone
import hashlib
import base64


class RestApi(models.TransientModel):
    _name = 'rest.api'

    def datetime_to_str(self, object, format):
        time = ''
        if isinstance(object, datetime.datetime):
            time = object.astimezone(timezone('Asia/Jakarta')).strftime(format)
        return time
    
    def md5_salt(self, value, salt):
        return hashlib.md5((salt + '$' + str(value)).encode()).hexdigest()

    def md5_salt_valid(self, value, salt, token):
        return hashlib.md5((salt + '$' + str(value)).encode()).hexdigest() == token

    def get_single_attachment(self, model, field, id):
        domain = [
            ('res_model', '=', model),
            ('res_field', '=', field),
            ('res_id', '=', id),
        ]
        fields = ['datas', 'mimetype']
        result = self.env['ir.attachment'].sudo().search_read(domain, fields)
        return result[0] if len(result) > 0 else None

    def response_attachment(self, data = {}):
        decode_content = data.get('decode_content', False)
        if decode_content:
            data['content'] = base64.b64decode(data['content'])

        return request.make_response(
            data['content'],
            [
                ('Content-Type', data['mimetype']),
                ('Content-Disposition', 'attachment; filename=%s' % data['filename']),
                ('Content-Length', len(data['content']))
            ]
        )