1# -*- coding: utf-8 -*-
2# Part of Odoo. See LICENSE file for full copyright and licensing details.
3
4from odoo import api, models, tools, _
5from odoo.addons.website.models import ir_http
6from odoo.exceptions import UserError
7from odoo.http import request
8
9
10class Lang(models.Model):
11    _inherit = "res.lang"
12
13    def write(self, vals):
14        if 'active' in vals and not vals['active']:
15            if self.env['website'].search([('language_ids', 'in', self._ids)]):
16                raise UserError(_("Cannot deactivate a language that is currently used on a website."))
17        return super(Lang, self).write(vals)
18
19    @api.model
20    @tools.ormcache_context(keys=("website_id",))
21    def get_available(self):
22        website = ir_http.get_request_website()
23        if not website:
24            return super().get_available()
25        # Return the website-available ones in this case
26        return request.website.language_ids.get_sorted()
27