<commit_before># -*- coding: utf-8 -*-
##############################################################################
#
#    Copyright (C) 2018 Compassion CH (http://www.compassion.ch)
#    Releasing children from poverty in Jesus' name
#    @author: <NAME> <<EMAIL>>
#    @author: <NAME> <<EMAIL>>
#    @author: <NAME> <<EMAIL>>
#    @author: <NAME> <<EMAIL>>
#
#    The licence is in the file __manifest__.py
#
##############################################################################
import werkzeug

from ..mappings.compassion_login_mapping import MobileLoginMapping
from ..mappings.app_banner_mapping import AppBannerMapping
from odoo import http, _
from odoo.http import request
from werkzeug.exceptions import NotFound, MethodNotAllowed, Unauthorized
from odoo.addons.base.ir.ir_mail_server import MailDeliveryException


def _get_lang(req, params):
    """ Fetches the lang from the request parameters. """
    lang_mapping = {'fr': 'fr_CH', 'en': 'en_US', 'de': 'de_DE', 'it': 'it_IT'}
    app_lang = params.get('language', '')[:2]
    return lang_mapping.get(app_lang, req.env.lang)


class RestController(http.Controller):

    @http.route('/mobile-app-api/login', type='json', methods=['GET'],
                auth='public')
    def mobile_app_login(self, username, password, **kwargs):
        """
        This is the first entry point for logging, which will setup the
        session for the user so that he can later call the main entry point.

        :param username: odoo user login
        :param password: <PASSWORD>
        :param view: mobile app view from which the request was made
        :return: json user data
        """
        request.session.authenticate(
            request.session.db, username, password)
        mapping = MobileLoginMapping(request.env)
        return mapping.get_connect_data(request.env.user)

    @http.route('/mobile-app-api/firebase.registration/register',
                auth='public', method='POST', type='json')
    def firebase_registration(self, **parameters):
        """
        Handle firebase registration coming from the app when no user is logged
        in.
        :param parameters: Request params
        :return: Request results
        """
        return request.env.get('firebase.registration').mobile_register(
            request.jsonrequest, **parameters)

    @http.route('/mobile-app-api/contact_us',
                type='json', auth='public', methods=['POST'])
    def mobile_app_contact_us(self, **parameters):
        """
        This route is used when the user is logged out
        :param parameters: request parameters
        :return: json dict as expected by the app
        """
        return request.env['crm.claim'].mobile_contact_us(request.jsonrequest)

    @http.route(['/mobile-app-api/<string:model>/<string:method>',
                 '/mobile-app-api/<string:model>/<string:method>/'
                 '<request_code>'],
                type='json', auth='user', methods=['GET', 'POST'])
    def mobile_app_handler(self, model, method, **parameters):
        """
        Main entry point for all authenticated calls (user is logged in).
        :param model: odoo model object in which we are requesting something
        :param method: method that will be called on the odoo model
        :param parameters: all other optional parameters sent by the request
        :return: json data for mobile app
        """
        odoo_obj = request.env.get(model).with_context(
            lang=_get_lang(request, parameters))
        model_method = 'mobile_' + method
        if odoo_obj is None or not hasattr(odoo_obj, model_method):
            raise NotFound("Unknown API path called.")<commit_msg>AP-253 Not logged in users can access FAQ and Privacy Statement (#1144)<commit_after># -*- coding: utf-8 -*-
##############################################################################
#
#    Copyright (C) 2018 Compassion CH (http://www.compassion.ch)
#    Releasing children from poverty in Jesus' name
#    @author: <NAME> <<EMAIL>ion.ch>
#    @author: <NAME> <<EMAIL>>
#    @author: <NAME> <<EMAIL>>
#    @author: <NAME> <<EMAIL>>
#
#    The licence is in the file __manifest__.py
#
##############################################################################
import werkzeug

from ..mappings.compassion_login_mapping import MobileLoginMapping
from ..mappings.app_banner_mapping import AppBannerMapping
from odoo import http, _
from odoo.http import request
from werkzeug.exceptions import NotFound, MethodNotAllowed, Unauthorized
from odoo.addons.base.ir.ir_mail_server import MailDeliveryException


def _get_lang(req, params):
    """ Fetches the lang from the request parameters. """
    lang_mapping = {'fr': 'fr_CH', 'en': 'en_US', 'de': 'de_DE', 'it': 'it_IT'}
    app_lang = params.get('language', '')[:2]
    return lang_mapping.get(app_lang, req.env.lang)


class RestController(http.Controller):

    @http.route('/mobile-app-api/login', type='json', methods=['GET'],
                auth='public')
    def mobile_app_login(self, username, password, **kwargs):
        """
        This is the first entry point for logging, which will setup the
        session for the user so that he can later call the main entry point.

        :param username: odoo user login
        :param password: <PASSWORD>
        :param view: mobile app view from which the request was made
        :return: json user data
        """
        request.session.authenticate(
            request.session.db, username, password)
        mapping = MobileLoginMapping(request.env)
        return mapping.get_connect_data(request.env.user)

    @http.route('/mobile-app-api/firebase.registration/register',
                auth='public', method='POST', type='json')
    def firebase_registration(self, **parameters):
        """
        Handle firebase registration coming from the app when no user is logged
        in.
        :param parameters: Request params
        :return: Request results
        """
        return request.env.get('firebase.registration').mobile_register(
            request.jsonrequest, **parameters)

    @http.route('/mobile-app-api/contact_us',
                type='json', auth='public', methods=['POST'])
    def mobile_app_contact_us(self, **parameters):
        """
        This route is used when the user is logged out
        :param parameters: request parameters
        :return: json dict as expected by the app
        """
        return request.env['crm.claim'].mobile_contact_us(request.jsonrequest)

    @http.route('/mobile-app-api/frequently.asked.questions/get_faq',
                type='json', auth='public', methods=['GET'])
    def mobile_app_get_faq(self, **parameters):
        """
        FAQ entry point for not logged in users
        :param parameters: all other optional parameters sent by the request
        :return: json data for mobile app
        """
        return request.env.get('frequently.asked.questions').mobile_get_faq(
            **parameters)

    @http.route(
        '/mobile-app-api/privacy.statement.agreement/get_privacy_notice',
        type='json', auth='public', methods=['GET'])
    def mobile_app_get_privacy_statement(self, **parameters):
        """
        Privacy statement entry point for not logged in users
        :param parameters: all other optional parameters sent by the request
        :return: json data for mobile app
        """
        return request.env.get('privacy.statement.agreement').\
            mobile_get_privacy_notice(**parameters)

    @http.route(['/mobile-app-api/<string:model>/<string:method>',
                 '/mobile-app-api/<string:model>/<string:method>/'
                 '<request_code>'],
                type='json', auth='user', methods=['GET', 'POST'])
    def mobile_app_handler(self, model, method, **parameters):
        """
        Main entry point for all authenticated calls (user is logged in).
        :param model: odoo model object in which we are requesting something
        :param method: method that will be called on the odoo model
        :param parameters: all other optional parameters sent by the request
        :return: json data for mobile app
        """
        odoo_obj = request.env.get(model).with_context(
            lang=_get_lang(request, parameters))
        model_method = 'mobile_' + method
        if odoo_obj is None or not hasattr(odoo_obj, model_method):
            raise NotFound("Unknown API path called.")
{'fraction_non_alphanumeric': 0.14906685565792582, 'fraction_numerical': 0.0022442712024568863, 'mean_word_length': 3.0338256312529777, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '977688', 'n_tokens_mistral': 2338, 'n_tokens_neox': 2271, 'n_words': 678}