-
-
Notifications
You must be signed in to change notification settings - Fork 292
/
models.py
43 lines (35 loc) · 1.32 KB
/
models.py
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
# -*- coding: utf-8 -*-
from random import choice
import hashlib
from odoo import fields, api
from odoo import models
def _attachment2url(att):
sha = hashlib.sha1(getattr(att, '__last_update')).hexdigest()[0:7]
return '/web/image/%s-%s' % (att.id, sha)
class IRAttachmentBackground(models.Model):
_inherit = 'ir.attachment'
use_as_background = fields.Boolean("Use as login page background", default=False)
@api.multi
def check(self, mode, values=None):
ids = self.ids
cr = self.env.cr
if ids and mode == 'read':
if isinstance(ids, (int, long)):
ids = [ids]
ids = ids[:] # make a copy
cr.execute('SELECT id,use_as_background FROM ir_attachment WHERE id = ANY (%s)', (ids,))
for attachment_id, use_as_background in cr.fetchall():
if use_as_background:
ids.remove(attachment_id)
if not ids:
return
return super(IRAttachmentBackground, self).check(mode, values=values)
@api.model
def get_background_pic(self):
pictures = self.search([('use_as_background', '=', True)])
if pictures:
p = choice(pictures)
picture_url = p.url or _attachment2url(p)
return picture_url
else:
return False