Skip to content
Snippets Groups Projects
Commit 807e4334 authored by konykon's avatar konykon
Browse files

Add landing_page buttons to res_company view

parent b14f2c8c
No related branches found
No related tags found
4 merge requests!138Hotfix setup name error,!134Release v14.0.1.1.6,!129Draft: Release v14.0.1.1.5,!111Feature/Landing page
......@@ -37,6 +37,7 @@
],
'data': [
'security/ir_rule_data.xml',
'security/ir.model.access.csv',
'security/res_users_role_data.xml',
'data/utm_data.xml',
'data/crm_lead_tag.xml',
......@@ -44,7 +45,9 @@
'data/auth_oauth_provider_data.xml',
'data/ir_cron.xml',
'views/crm_lead_views.xml',
'views/landing_page_view.xml',
'views/res_company_views.xml',
'views/res_config_settings.xml',
'views/res_partner_views.xml',
'views/website_subscription_template.xml',
'views/ce_views.xml',
......
......@@ -4,7 +4,10 @@ from odoo import api, models, fields, _
from odoo.exceptions import ValidationError
from datetime import datetime
import re
import os
from odoo.exceptions import UserError
from ..pywordpress_client.resources.authenticate import Authenticate
from ..pywordpress_client.resources.landing_page import LandingPage
_HIERARCHY_LEVEL_VALUES = [
('instance', _('Instance')),
......@@ -35,18 +38,24 @@ class ResCompany(models.Model):
parent_id_filtered_ids = fields.One2many('res.company', compute=_compute_parent_id_filtered_ids, readonly=True,
store=False)
ce_tag_ids = fields.Many2many('crm.tag', string='Energy Community Services')
_inherit = "res.company"
coordinator = fields.Boolean(
string="Platform coordinator",
help="Flag to indicate that this company has the rol of 'Coordinator'(=Administrator) for the current 'Comunitats Energètiques' Platform",
)
ce_tag_ids = fields.Many2many("crm.tag", string="Energy Community Services")
cooperator_journal = fields.Many2one(
"account.journal",
string="Cooperator Journal",
domain="[('type','=','sale'),('active','=',True)]",
help="This journal will be"
" the default one as the"
" receivable journal for the"
" cooperators"
" the default one as the"
" receivable journal for the"
" cooperators",
)
foundation_date = fields.Date('Foundation date')
social_telegram = fields.Char('Telegram Account')
foundation_date = fields.Date("Foundation date")
social_telegram = fields.Char("Telegram Account")
initial_subscription_share_amount = fields.Float(
'Initial Subscription Share Amount', digits='Product Price')
allow_new_members = fields.Boolean(
......@@ -56,6 +65,13 @@ class ResCompany(models.Model):
default=False)
voluntary_share_id = fields.Many2one(comodel_name='product.template', domain=[('is_share', '=', True)],
string='Voluntary share to show on website')
landing_page_id = fields.Many2one("landing.page", string=_("Landing Page"))
wordpress_db_credentials_admin_username = fields.Char(
string=_("Wordpress DB Admin Username")
)
wordpress_db_credentials_admin_password = fields.Char(
string=_("Wordpress DB Admin Password")
)
@api.constrains('hierarchy_level', 'parent_id')
def _check_hierarchy_level(self):
......@@ -86,52 +102,65 @@ class ResCompany(models.Model):
@api.model
def get_real_ce_company_id(self, api_param_odoo_compant_id):
if api_param_odoo_compant_id == self.API_PARAM_ID_VALUE_FOR_COORDINADORA:
return self.search([('coordinator', '=', True)], limit=1) or None
return self.search([("coordinator", "=", True)], limit=1) or None
else:
return self.search([('id', '=', api_param_odoo_compant_id)]) or None
return self.search([("id", "=", api_param_odoo_compant_id)]) or None
def check_ce_has_admin(self):
self.ensure_one()
admin_roles_ids = [r['odoo_role_id']
for r in self.env['res.users'].ce_user_roles_mapping().values() if r['is_admin']]
admin_roles_ids = [
r["odoo_role_id"]
for r in self.env["res.users"].ce_user_roles_mapping().values()
if r["is_admin"]
]
company_user_ids = self.get_ce_members().ids
admins_user_ids = []
for admin_role in self.env['res.users.role'].sudo().browse(admin_roles_ids):
for admin_role in self.env["res.users.role"].sudo().browse(admin_roles_ids):
for role_line in admin_role.line_ids:
admins_user_ids.append(role_line.user_id.id)
return any([user in admins_user_ids for user in company_user_ids])
def get_ce_members(self, domain_key='in_kc_and_active'):
domains_dict = {'in_kc_and_active': [
('company_id', '=', self.id), ('oauth_uid', '!=', None), ('active', '=', True)]}
members = self.env['res.users'].sudo().search(
domains_dict['in_kc_and_active'])
def get_ce_members(self, domain_key="in_kc_and_active"):
domains_dict = {
"in_kc_and_active": [
("company_id", "=", self.id),
("oauth_uid", "!=", None),
("active", "=", True),
]
}
members = self.env["res.users"].sudo().search(domains_dict["in_kc_and_active"])
return members
@api.model
def _is_not_unique(self, vals):
# check for VAT
if vals.get('vat', False) and vals.get('vat'):
sanit_vat = re.sub(r"[^a-zA-Z0-9]", "", vals['vat']).lower()
if sanit_vat in [re.sub(r"[^a-zA-Z0-9]", "", c.vat).lower() for c in self.search([]) if c.vat]:
if vals.get("vat", False) and vals.get("vat"):
sanit_vat = re.sub(r"[^a-zA-Z0-9]", "", vals["vat"]).lower()
if sanit_vat in [
re.sub(r"[^a-zA-Z0-9]", "", c.vat).lower()
for c in self.search([])
if c.vat
]:
raise UserError(
_("Unable to create new company because there is an allready existing company with this VAT number: {}").format(
vals['vat']))
_(
"Unable to create new company because there is an allready existing company with this VAT number: {}"
).format(vals["vat"])
)
# check for name
if vals.get('name', False) and vals.get('name'):
if vals.get("name", False) and vals.get("name"):
# sanit_name = slugify(vals['name'])
sanit_name = vals['name']
sanit_name = vals["name"]
# if sanit_name in [slugify(c.name) for c in self.search([]) if c.name]:
if sanit_name in [c.name for c in self.search([]) if c.name]:
raise UserError(
_("Unable to create new company because there is an allready existing company with this NAME: {}").format(
vals['name']))
_(
"Unable to create new company because there is an allready existing company with this NAME: {}"
).format(vals["name"])
)
@api.model
def create(self, vals):
# check that we are not creating duplicate companies by vat or by name
self._is_not_unique(vals)
......@@ -144,16 +173,56 @@ class ResCompany(models.Model):
self.ensure_one()
res = []
for tag in self.ce_tag_ids:
res.append({
'id': tag.id,
'name': tag.name,
})
res.append(
{
"id": tag.id,
"name": tag.name,
}
)
return res
def get_public_web_landing_url(self):
# TODO Get from community_maps
return 'https://somcomunitats.coop/ce/comunitat-energetica-prova/'
return "https://somcomunitats.coop/ce/comunitat-energetica-prova/"
def get_keycloak_odoo_login_url(self):
login_provider_id = self.env.ref('energy_communities.keycloak_login_provider')
login_provider_id = self.env.ref("energy_communities.keycloak_login_provider")
return login_provider_id.get_auth_link()
def create_landing(self):
landing_page = self.env["landing.page"]
vals = {"company_id": self.id, "name": self.name, "status": "publish"}
new_landing = landing_page.create(vals)
context = {
"__last_update": {},
"active_model": "landing.page",
"active_id": new_landing.id,
}
self.write({"landing_page_id": new_landing.id})
self.action_create_wp_landing()
return {
"type": "ir.actions.act_window",
"view_type": "form",
"view_mode": "form",
"res_model": "landing.page",
"res_id": new_landing.id,
"target": "current",
"context": context,
}
def action_create_wp_landing(self, fields=None):
auth = Authenticate().authenticate()
token = "Bearer %s" % auth["token"]
landing_page_data = self.landing_page_id.to_dict()
landing_page = LandingPage.create(token, landing_page_data)
self.landing_page_id.write({"wp_landing_page_id": landing_page.id})
def get_landing_page_form(self):
return {
"type": "ir.actions.act_window",
"view_type": "form",
"view_mode": "form",
"res_model": "landing.page",
"res_id": self.landing_page_id.id,
"target": "current",
}
......@@ -8,6 +8,29 @@
<xpath expr="//page[@name='general_info']//field[@name='parent_id']" position="attributes">
<attribute name="invisible">True</attribute>
</xpath>
<xpath expr="//sheet" position="before">
<header>
<field name="landing_page_id" invisible="1"/>
<button
name="create_landing"
type="object"
string="Create landing page"
attrs="{'invisible': [('landing_page_id','=',True)]}"
/>
</header>
</xpath>
<xpath expr="//field[@name='logo']" position="before">
<div class='oe_button_box'>
<button
name="get_landing_page_form"
type="object"
string="My landing page"
class="oe_stat_button"
icon="fa-globe"
attrs="{'invisible': [('landing_page_id','=',False)]}"
/>
</div>
</xpath>
<xpath expr="//notebook" position="inside">
<page string="Energy Communities">
<group>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment