<commit_before># -*- coding: utf-8 -*-
# © 2016 Coop IT Easy (http://www.coopiteasy.be)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import logging

import datetime
from openerp import http
from openerp.http import request
from openerp import SUPERUSER_ID
from openerp.addons.website_sale.controllers.main import QueryURL
from openerp.tools.translate import _
from openerp import tools
from openerp.addons.website_sale.controllers.main import website_sale
from openerp.addons.website_portal_sale.controllers.main import website_account

_logger = logging.getLogger(__name__)

class WebsiteSale(website_sale):

    @http.route(['/shop',
                 '/shop/page/<int:page>',
                 '/shop/category/<model("product.public.category"):category>',
                 '/shop/category/<model("product.public.category"):category>/page/<int:page>',
                 '/shop/time_frame'
    ],type='http',auth='user',website=True)
    def shop(self, page=0, category=None, time_frame=None, search='', **post):
#         if time_frame:
#             context = dict(request.env.context)
#             context.setdefault('time_frame_id', int(time_frame))
#             request.env.context = context
        time_frames = request.env['time.frame'].sudo().search([('state','=','open')])
        if len(time_frames) == 0:
            return request.website.render("distribution_circuits_website_sale.shop_closed", post)
        else:
            return super(WebsiteSale, self).shop(page=page, category=category,
                                             time_frame=time_frame, search=search,
                                             **post)


    @http.route(['/shop/set_current_time_frame'], type='json', auth="public", methods=['POST'], website=True)
    def set_current_time_frame(self, time_frame_id=None, **kw):
        env  = request.env
        if time_frame_id :
            time_frame = env['time.frame'].sudo().browse(int(time_frame_id))
            if request.website.sale_get_order():
                order = request.website.sale_get_order()
                order.sudo().write({'time_frame_id':time_frame.id})
            request.session['selected_time_frame'] = time_frame.id
            return {time_frame.id: time_frame.name}
        else:
            request.session['selected_time_frame'] = None
        return {0:""}
    
    @http.route('/shop/payment/validate', type='http', auth="public", website=True)
    def payment_validate(self, transaction_id=None, sale_order_id=None, **post):
        if transaction_id is None:
            tx = request.website.sale_get_transaction()
        else:
            tx = request.env['payment.transaction'].sudo().browse(transaction_id)

        if sale_order_id is None:
            order = request.website.sale_get_order()
        else:
            order = request.env['sale.order'].sudo().browse(sale_order_id)
            assert order.id == request.session.get('sale_last_order_id')
        
        #sometime without knowing yet why we loose the order so we add this last attempt to get the current sale order
        if not order:
            order = request.website.sale_get_order()
            #if it still not defined we redirect to the shop
            if not order:
                _logger.error('The order is not defined')
                return request.redirect('/shop')    
        
        if order.check_customer_credit():
            if tx:
                tx.write({'state':'done'})
            return super(WebsiteSale, self).payment_validate(transaction_id, sale_order_id, **post)
        else:
            _logger.error('The customer credit is not sufficient to cover the payment %s set as error' % (tx.reference))
            return request.redirect('/shop')
    
    def get_delivery_points(self):
        return request.env['res.partner'].sudo().get_delivery_points()<commit_msg>[FIX] fix error when no order is defined<commit_after># -*- coding: utf-8 -*-
# © 2016 Coop IT Easy (http://www.coopiteasy.be)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import logging

import datetime
from openerp import http
from openerp.http import request
from openerp import SUPERUSER_ID
from openerp.addons.website_sale.controllers.main import QueryURL
from openerp.tools.translate import _
from openerp import tools
from openerp.addons.website_sale.controllers.main import website_sale
from openerp.addons.website_portal_sale.controllers.main import website_account

_logger = logging.getLogger(__name__)

class WebsiteSale(website_sale):

    @http.route(['/shop',
                 '/shop/page/<int:page>',
                 '/shop/category/<model("product.public.category"):category>',
                 '/shop/category/<model("product.public.category"):category>/page/<int:page>',
                 '/shop/time_frame'
    ],type='http',auth='user',website=True)
    def shop(self, page=0, category=None, time_frame=None, search='', **post):
#         if time_frame:
#             context = dict(request.env.context)
#             context.setdefault('time_frame_id', int(time_frame))
#             request.env.context = context
        time_frames = request.env['time.frame'].sudo().search([('state','=','open')])
        if len(time_frames) == 0:
            return request.website.render("distribution_circuits_website_sale.shop_closed", post)
        else:
            return super(WebsiteSale, self).shop(page=page, category=category,
                                             time_frame=time_frame, search=search,
                                             **post)


    @http.route(['/shop/set_current_time_frame'], type='json', auth="public", methods=['POST'], website=True)
    def set_current_time_frame(self, time_frame_id=None, **kw):
        env  = request.env
        if time_frame_id :
            time_frame = env['time.frame'].sudo().browse(int(time_frame_id))
            if request.website.sale_get_order():
                order = request.website.sale_get_order()
                order.sudo().write({'time_frame_id':time_frame.id})
            request.session['selected_time_frame'] = time_frame.id
            return {time_frame.id: time_frame.name}
        else:
            request.session['selected_time_frame'] = None
        return {0:""}
    
    @http.route('/shop/payment/validate', type='http', auth="public", website=True)
    def payment_validate(self, transaction_id=None, sale_order_id=None, **post):
        if transaction_id is None:
            tx = request.website.sale_get_transaction()
        else:
            tx = request.env['payment.transaction'].sudo().browse(transaction_id)

        if sale_order_id is None:
            order = request.website.sale_get_order()
        else:
            order = request.env['sale.order'].sudo().browse(sale_order_id)
            assert order.id == request.session.get('sale_last_order_id')
        
        #sometime without knowing yet why we loose the order so we add this last attempt to get the current sale order
        if not order:
            order = request.website.sale_get_order()
            #if it still not defined we redirect to the shop
            if not order:
                _logger.error('The order is not defined')
                if tx:
                    _logger.error('The transaction was reference %s' % (tx.reference))
                return request.redirect('/shop')    
        
        if order.check_customer_credit():
            if tx:
                tx.write({'state':'done'})
            return super(WebsiteSale, self).payment_validate(transaction_id, sale_order_id, **post)
        else:
            _logger.error('The customer credit is not sufficient to cover the payment %s set as error' % (tx.reference))
            return request.redirect('/shop')
    
    def get_delivery_points(self):
        return request.env['res.partner'].sudo().get_delivery_points()
{'fraction_non_alphanumeric': 0.12042547802963151, 'fraction_numerical': 0.0025326073192351527, 'mean_word_length': 2.5528565002249213, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '672847', 'n_tokens_mistral': 2307, 'n_tokens_neox': 2185, 'n_words': 470}