Browse Source

[website_coupon] Apply python coding conventions

pull/83/head
Florent Cayré 7 years ago
parent
commit
7fc25c4ad2
  1. 126
      website_coupon/controllers/main.py
  2. 14
      website_coupon/models/gift_voucher.py

126
website_coupon/controllers/main.py

@ -2,7 +2,7 @@
from datetime import datetime
from dateutil import parser
from odoo import http, tools, _
from odoo import http
from odoo.http import request
@ -10,14 +10,17 @@ class WebsiteCoupon(http.Controller):
@http.route(['/shop/cart'], type='http', auth="public", website=True)
def cart(self, **post):
"""This function is overwritten because we need to pass the value 'coupon_not_available'
to the template, inorder to show the error message to the user that, 'this coupon is not available'. """
""" This function is overwritten because we need to pass the value
'coupon_not_available' to the template, in order to show the error
message to the user that, 'this coupon is not available'.
"""
order = request.website.sale_get_order()
if order:
from_currency = order.company_id.currency_id
to_currency = order.pricelist_id.currency_id
compute_currency = lambda price: from_currency.compute(price, to_currency)
compute_currency = lambda price: from_currency.compute(price,
to_currency)
else:
compute_currency = lambda price: price
@ -33,7 +36,9 @@ class WebsiteCoupon(http.Controller):
values['suggested_products'] = _order._cart_accessories()
if post.get('type') == 'popover':
return request.render("website_sale.cart_popover", values)
# force no-cache so IE11 doesn't cache this XHR
return request.render("website_sale.cart_popover", values,
headers={'Cache-Control': 'no-cache'})
if post.get('code_not_available'):
values['code_not_available'] = post.get('code_not_available')
@ -41,29 +46,39 @@ class WebsiteCoupon(http.Controller):
values['coupon_not_available'] = post.get('coupon_not_available')
return request.render("website_sale.cart", values)
@http.route(['/shop/gift_coupon'], type='http', auth="public", website=True)
@http.route(['/shop/gift_coupon'],
type='http', auth="public", website=True)
def gift_coupon(self, promo_voucher, **post):
"""This function will be executed when we click the apply button of the voucher code in the website.
It will verify the validity and availability of that coupon. If it can be applied, the coupon will be applied
and coupon balance will also be updated"""
""" This function will be executed when we click the apply button of
the voucher code in the website. It will verify the validity and
availability of that coupon. If it can be applied, the coupon will be
applied and coupon balance will also be updated.
"""
curr_user = request.env.user
coupon = request.env['gift.coupon'].sudo().search([('code', '=', promo_voucher)], limit=1)
coupon = request.env['gift.coupon'].sudo().search(
[('code', '=', promo_voucher)], limit=1)
flag = True
if coupon and coupon.total_avail > 0:
applied_coupons = request.env['partner.coupon'].sudo().search([('coupon', '=', promo_voucher),
('partner_id', '=', curr_user.partner_id.id)], limit=1)
applied_coupons = request.env['partner.coupon'].sudo().search(
[('coupon', '=', promo_voucher),
('partner_id', '=', curr_user.partner_id.id)],
limit=1)
# checking voucher date and limit for each user for this coupon---------------------
# checking voucher date and limit for each user for this coupon
if coupon.partner_id:
if curr_user.partner_id.id != coupon.partner_id.id:
flag = False
today = datetime.now().date()
if flag and applied_coupons.number < coupon.limit and today <= parser.parse(coupon.voucher.expiry_date).date():
# checking coupon validity ---------------------------
# checking date of coupon ------------
voucher_expiry = parser.parse(coupon.voucher.expiry_date).date()
if (flag
and applied_coupons.number < coupon.limit
and today <= voucher_expiry):
# checking coupon validity
# checking date of coupon
if coupon.start_date and coupon.end_date:
if today < parser.parse(coupon.start_date).date() or today > parser.parse(coupon.end_date).date():
if (today < parser.parse(coupon.start_date).date()
or today > parser.parse(coupon.end_date).date()):
flag = False
elif coupon.start_date:
if today < parser.parse(coupon.start_date).date():
@ -79,7 +94,8 @@ class WebsiteCoupon(http.Controller):
voucher_type = coupon.voucher.voucher_type
voucher_val = coupon.voucher_val
type = coupon.type
coupon_product = request.env['product.product'].sudo().search([('name', '=', 'Gift Coupon')], limit=1)
coupon_product = request.env['product.product'].sudo().search(
[('name', '=', 'Gift Coupon')], limit=1)
if coupon_product:
order = request.website.sale_get_order(force_create=1)
flag_product = False
@ -89,59 +105,73 @@ class WebsiteCoupon(http.Controller):
break
if flag and order.order_line:
if voucher_type == 'product':
# the voucher type is product ----------------------------
categ_id = coupon.voucher.product_id
# the voucher type is product
product_id = coupon.voucher.product_id
for line in order.order_line:
if line.product_id.name == categ_id.name:
if line.product_id.name == product_id.name:
flag_product = True
elif voucher_type == 'category':
# the voucher type is category ----------------------------
product_id = coupon.voucher.product_categ
for line in order.order_line:
if line.product_id.categ_id.name == product_id.name:
# the voucher type is category
voucher_cat = coupon.voucher.product_categ
for ol in order.order_line:
if ol.product_id.categ_id.name == voucher_cat.name:
flag_product = True
elif voucher_type == 'all':
# the voucher is applicable to all products ----------------------------
# the voucher is applicable to all products
flag_product = True
if flag_product:
# the voucher is applicable --------------------------------------
# the voucher is applicable
if type == 'fixed':
# coupon type is 'fixed'--------------------------------------
# coupon type is 'fixed'
if voucher_val < order.amount_total:
coupon_product.product_tmpl_id.write({'list_price': -voucher_val})
coupon_product.product_tmpl_id.write(
{'list_price': -voucher_val})
else:
return request.redirect("/shop/cart?coupon_not_available=3")
return request.redirect(
"/shop/cart?coupon_not_available=3")
elif type == 'percentage':
# coupon type is percentage -------------------------------------
# coupon type is percentage
amount_final = 0
factor = (voucher_val / 100)
if voucher_type == 'product':
for line in order.order_line:
if line.product_id.name == categ_id.name:
amount_final = (voucher_val / 100) * line.price_total
for ol in order.order_line:
if ol.product_id.name == product_id.name:
amount_final = factor * ol.price_total
break
elif voucher_type == 'category':
for line in order.order_line:
if line.product_id.categ_id.name == product_id.name:
amount_final += (voucher_val / 100) * line.price_total
for ol in order.order_line:
cat_name = ol.product_id.categ_id.name
if cat_name == voucher_cat.name:
amount_final += factor * ol.price_total
elif voucher_type == 'all':
amount_final = (voucher_val/100) * order.amount_total
coupon_product.product_tmpl_id.write({'list_price': -amount_final})
order._cart_update(product_id=coupon_product.id, set_qty=1, add_qty=1)
# updating coupon balance--------------
amount_final = factor * order.amount_total
coupon_product.product_tmpl_id.write(
{'list_price': -amount_final})
order._cart_update(product_id=coupon_product.id,
set_qty=1, add_qty=1)
# updating coupon balance
total = coupon.total_avail - 1
coupon.write({'total_avail': total})
# creating a record for this partner, i.e he is used this coupon once-----------
# creating a record for this partner,
# i.e he is used this coupon once
if not applied_coupons:
curr_user.partner_id.write({'applied_coupon': [(0, 0, {'partner_id': curr_user.partner_id.id,
'coupon': coupon.code,
'number': 1})]})
curr_user.partner_id.write({
'applied_coupon': [(0, 0, {
'partner_id': curr_user.partner_id.id,
'coupon': coupon.code,
'number': 1,
})]
})
else:
applied_coupons.write({'number': applied_coupons.number + 1})
applied_coupons.write(
{'number': applied_coupons.number + 1})
else:
return request.redirect("/shop/cart?coupon_not_available=1")
return request.redirect(
"/shop/cart?coupon_not_available=1")
else:
return request.redirect("/shop/cart?coupon_not_available=2")
return request.redirect(
"/shop/cart?coupon_not_available=2")
else:
return request.redirect("/shop/cart?coupon_not_available=1")

14
website_coupon/models/gift_voucher.py

@ -20,8 +20,10 @@
# If not, see <https://www.gnu.org/licenses/>.
#
##############################################################################
import string
import random
from odoo import models, fields, api, _
from odoo.exceptions import UserError
@ -38,7 +40,8 @@ class GiftVoucher(models.Model):
], string="Applicable on ", default='product'
)
product_id = fields.Many2one('product.product', string="Product")
product_categ = fields.Many2one('product.category', string="Product Category")
product_categ = fields.Many2one('product.category',
string="Product Category")
min_value = fields.Integer(string="Minimum Voucher Value", required=True)
max_value = fields.Integer(string="Maximum Voucher Value", required=True)
expiry_date = fields.Date(string="Expiry Date", required=True)
@ -61,7 +64,8 @@ class GiftCoupon(models.Model):
voucher = fields.Many2one('gift.voucher', string="Voucher", required=True)
start_date = fields.Date(string="Start Date")
end_date = fields.Date(string="End Date")
partner_id = fields.Many2one('res.partner', string="Limit to a Single Partner")
partner_id = fields.Many2one('res.partner',
string="Limit to a Single Partner")
limit = fields.Integer(string="Total Available For Each User", default=1)
total_avail = fields.Integer(string="Total Available", default=1)
voucher_val = fields.Float(string="Voucher Value")
@ -72,7 +76,8 @@ class GiftCoupon(models.Model):
@api.onchange('voucher_val')
def check_val(self):
if self.voucher_val > self.voucher.max_value or self.voucher_val < self.voucher.min_value:
if (self.voucher_val > self.voucher.max_value
or self.voucher_val < self.voucher.min_value):
raise UserError(_("Please check the voucher value"))
@ -87,4 +92,5 @@ class CouponPartner(models.Model):
class PartnerExtended(models.Model):
_inherit = 'res.partner'
applied_coupon = fields.One2many('partner.coupon', 'partner_id', string="Coupons Applied")
applied_coupon = fields.One2many('partner.coupon', 'partner_id',
string="Coupons Applied")

Loading…
Cancel
Save