Browse Source

[PEP8] make code comply with PEP8

pull/1/head
houssine 6 years ago
parent
commit
3a8f60cc79
  1. 166
      easy_my_coop/controllers/main.py

166
easy_my_coop/controllers/main.py

@ -10,12 +10,18 @@ from openerp import http, SUPERUSER_ID
from openerp.http import request
from openerp.tools.translate import _
_TECHNICAL = ['view_from', 'view_callback'] # Only use for behavior, don't stock it
_BLACKLIST = ['id', 'create_uid', 'create_date', 'write_uid', 'write_date', 'user_id', 'active'] # Allow in description
# Only use for behavior, don't stock it
_TECHNICAL = ['view_from', 'view_callback']
# Allow in description
_BLACKLIST = ['id', 'create_uid', 'create_date', 'write_uid', 'write_date',
'user_id', 'active']
class WebsiteSubscription(http.Controller):
@http.route(['/page/become_cooperator','/become_cooperator'], type='http', auth="public", website=True)
@http.route(['/page/become_cooperator',
'/become_cooperator'],
type='http', auth="public", website=True)
def display_become_cooperator_page(self, **kwargs):
values = {}
if request.env.user.login != 'public':
@ -23,26 +29,28 @@ class WebsiteSubscription(http.Controller):
if partner.is_company:
return request.website.render("easy_my_coop.becomecompanycooperator", values)
values = self.fill_values(values,False,True)
for field in ['email','firstname','lastname','birthdate','iban','share_product_id','no_registre','address','city','zip_code','country_id','phone','lang','nb_parts','total_parts','error_msg']:
if kwargs.get(field):
values[field] = kwargs.pop(field)
values.update(kwargs=kwargs.items())
return request.website.render("easy_my_coop.becomecooperator", values)
@http.route(['/page/become_company_cooperator','/become_company_cooperator'], type='http', auth="public", website=True)
@http.route(['/page/become_company_cooperator',
'/become_company_cooperator'],
type='http', auth="public", website=True)
def display_become_company_cooperator_page(self, **kwargs):
values = {}
values = self.fill_values(values,True,True)
values = self.fill_values(values, True, True)
for field in ['is_company','company_register_number','company_name','company_email','company_type','email','firstname','lastname','birthdate','iban','share_product_id','no_registre','address','city','zip_code','country_id','phone','lang','nb_parts','total_parts','error_msg']:
if kwargs.get(field):
values[field] = kwargs.pop(field)
values.update(kwargs=kwargs.items())
return request.website.render("easy_my_coop.becomecompanycooperator", values)
def preRenderThanks(self, values, kwargs):
""" Allow to be overrided """
return {
@ -53,11 +61,11 @@ class WebsiteSubscription(http.Controller):
def get_subscription_response(self, values, kwargs):
values = self.preRenderThanks(values, kwargs)
return request.website.render(kwargs.get("view_callback", "easy_my_coop.cooperator_thanks"), values)
def get_date_string(self,birthdate):
def get_date_string(self, birthdate):
if birthdate:
birthdate = datetime.strptime(birthdate,"%Y-%m-%d")
return datetime.strftime(birthdate,"%d/%m/%Y")
birthdate = datetime.strptime(birthdate, "%Y-%m-%d")
return datetime.strftime(birthdate, "%d/%m/%Y")
return False
def get_values_from_user(self, values, is_company):
@ -65,7 +73,7 @@ class WebsiteSubscription(http.Controller):
if request.env.user.login != 'public':
values['logged'] = 'on'
partner = request.env.user.partner_id
if partner.member or partner.old_member:
values['already_cooperator'] = 'on'
if partner.bank_ids:
@ -74,7 +82,7 @@ class WebsiteSubscription(http.Controller):
values['zip_code'] = partner.zip
values['city'] = partner.city
values['country_id'] = partner.country_id.id
if is_company:
#company values
values['company_register_number'] = partner.company_register_number
@ -101,15 +109,15 @@ class WebsiteSubscription(http.Controller):
values['lang'] = partner.lang
values['phone'] = partner.phone
return values
def fill_values(self, values, is_company, load_from_user=False):
company = request.website.company_id
if load_from_user:
values = self.get_values_from_user(values,is_company)
values = self.get_values_from_user(values, is_company)
values['countries'] = self.get_countries()
values['langs'] = self.get_langs()
values['products'] = self.get_products_share(is_company)
fields_desc = request.env['subscription.request'].sudo().fields_get(['company_type','gender'])
fields_desc = request.env['subscription.request'].sudo().fields_get(['company_type', 'gender'])
values['company_types'] = fields_desc['company_type']['selection']
values['genders'] = fields_desc['gender']['selection']
values['company'] = company
@ -126,7 +134,7 @@ class WebsiteSubscription(http.Controller):
if company.default_country_id:
values['country_id'] = company.default_country_id.id
else:
values['country_id'] = '21'
values['country_id'] = '21'
if not values.get('activities_country_id'):
if company.default_country_id:
values['activities_country_id'] = company.default_country_id.id
@ -136,47 +144,55 @@ class WebsiteSubscription(http.Controller):
if company.default_lang_id:
values['lang'] = company.default_lang_id.code
return values
def get_products_share(self, is_company):
products = request.env['product.template'].sudo().get_web_share_products(is_company)
return products
def get_countries(self):
countries = request.env['res.country'].sudo().search([])
return countries
def get_langs(self):
langs = request.env['res.lang'].sudo().search([])
return langs
@http.route(['/subscription/get_share_product'], type='json', auth="public", methods=['POST'], website=True)
return langs
@http.route(['/subscription/get_share_product'],
type='json',
auth="public",
methods=['POST'], website=True)
def get_share_product(self, share_product_id, **kw):
product = request.env['product.template'].sudo().browse(int(share_product_id))
return {product.id: {'list_price':product.list_price,'min_qty':product.minimum_quantity,'force_min_qty':product.force_min_qty}}
@http.route(['/subscription/subscribe_share'], type='http', auth="public", website=True)
return {product.id: {'list_price': product.list_price, 'min_qty': product.minimum_quantity, 'force_min_qty': product.force_min_qty}}
@http.route(['/subscription/subscribe_share'],
type='http',
auth="public", website=True)
def share_subscription(self, **kwargs):
user_obj = request.env['res.users']
post_file = [] # List of file to add to ir_attachment once we have the ID
post_description = [] # Info to add after the message
# List of file to add to ir_attachment once we have the ID
post_file = []
# Info to add after the message
post_description = []
values = {}
for field_name, field_value in kwargs.items():
if hasattr(field_value, 'filename'):
post_file.append(field_value)
elif field_name in request.registry['subscription.request']._fields and field_name not in _BLACKLIST:
values[field_name] = field_value
elif field_name not in _TECHNICAL: # allow to add some free fields or blacklisted field like ID
# allow to add some free fields or blacklisted field like ID
elif field_name not in _TECHNICAL:
post_description.append("%s: %s" % (field_name, field_value))
logged = kwargs.get("logged")=='on'
logged = kwargs.get("logged") == 'on'
if logged:
partner = request.env.user.partner_id
values['partner_id'] = partner.id
values['already_cooperator'] = partner.member
redirect = "easy_my_coop.becomecooperator"
email = kwargs.get('email')
@ -185,25 +201,28 @@ class WebsiteSubscription(http.Controller):
is_company = True
redirect = "easy_my_coop.becomecompanycooperator"
email = kwargs.get('company_email')
values["is_company"] = is_company
if not kwargs.has_key('g-recaptcha-response') or not request.website.is_captcha_valid(kwargs['g-recaptcha-response']):
values = self.fill_values(values,is_company)
values["error_msg"] = _("the captcha has not been validated, please fill in the captcha")
return request.website.render(redirect, values)
if 'g-recaptcha-response' not in kwargs or not request.website.is_captcha_valid(kwargs['g-recaptcha-response']):
values = self.fill_values(values, is_company)
values["error_msg"] = _("the captcha has not been validated,"
" please fill in the captcha")
return request.website.render(redirect, values)
if not logged and email:
user = user_obj.sudo().search([('login','=',email)])
if user:
values = self.fill_values(values,is_company)
values.update(kwargs)
values["error_msg"] = _("There is an existing account for this mail address. Please login before fill in the form")
return request.website.render(redirect, values)
# fields validation : Check that required field from model subscription_request exists
user = user_obj.sudo().search([('login', '=', email)])
if user:
values = self.fill_values(values, is_company)
values.update(kwargs)
values["error_msg"] = _("There is an existing account for this"
" mail address. Please login before "
"fill in the form")
return request.website.render(redirect, values)
# Check that required field from model subscription_request exists
required_fields = request.env['subscription.request'].sudo().get_required_field()
error = set(field for field in required_fields if not values.get(field))
@ -212,34 +231,34 @@ class WebsiteSubscription(http.Controller):
values["error_msg"] = _("Some mandatory fields have not been filled")
values = dict(values, error=error, kwargs=kwargs.items())
return request.website.render(kwargs.get("view_from", redirect), values)
if kwargs.get("already_cooperator") == 'on':
already_cooperator = True
values["already_cooperator"] = True
lastname = kwargs.get("lastname").upper()
firstname = kwargs.get("firstname").title()
values["name"] = firstname + " " + lastname
values["lastname"] = lastname
values["firstname"] = firstname
values["birthdate"] = datetime.strptime(kwargs.get("birthdate"), "%d/%m/%Y").date()
values["source"] = "website"
if kwargs.get("share_product_id"):
product_id = kwargs.get("share_product_id")
product = request.env['product.template'].sudo().browse(int(product_id)).product_variant_ids[0]
values["share_product_id"] = product.id
#check the subscription's amount
# check the subscription's amount
company = request.website.company_id
max_amount = company.subscription_maximum_amount
total_amount = float(kwargs.get('total_parts'))
if max_amount > 0 and total_amount > max_amount:
values = self.fill_values(values,is_company)
values["error_msg"] = _("You can't subscribe for an amount that exceed ") + str(max_amount) + company.currency_id.symbol
return request.website.render("easy_my_coop.becomecooperator", values)
values = self.fill_values(values, is_company)
values["error_msg"] = _("You can't subscribe for an amount that exceed ") + str(max_amount) + company.currency_id.symbol
return request.website.render("easy_my_coop.becomecooperator", values)
if is_company:
if kwargs.get("company_register_number", is_company):
values["company_register_number"] = re.sub('[^0-9a-zA-Z]+', '', kwargs.get("company_register_number"))
@ -248,9 +267,9 @@ class WebsiteSubscription(http.Controller):
if kwargs.get("no_registre"):
values["no_registre"] = re.sub('[^0-9a-zA-Z]+', '', kwargs.get("no_registre"))
subscription_id = request.env['subscription.request'].sudo().create(values)
values.update(subscription_id = subscription_id)
values.update(subscription_id=subscription_id)
if subscription_id:
for field_value in post_file:
attachment_value = {
@ -261,7 +280,6 @@ class WebsiteSubscription(http.Controller):
'datas': base64.encodestring(field_value.read()),
'datas_fname': field_value.filename,
}
request.registry['ir.attachment'].create(request.cr, SUPERUSER_ID, attachment_value, context=request.context)
return self.get_subscription_response(values, kwargs)
request.registry['ir.attachment'].sudo().create(attachment_value)
return self.get_subscription_response(values, kwargs)
Loading…
Cancel
Save