# -*- coding: utf-8 -*-
# Copyright 2020 Green Valley Belgium NV
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# @@license_version:1.7@@

import cloudstorage
import logging
from babel.dates import format_datetime
from datetime import datetime
from google.appengine.ext import ndb, deferred, db
from typing import List
from xlwt import Worksheet, Workbook, XFStyle

from mcfw.cache import invalidate_cache
from mcfw.consts import REST_TYPE_TO
from mcfw.exceptions import HttpBadRequestException, HttpForbiddenException, HttpNotFoundException
from mcfw.restapi import rest
from mcfw.rpc import returns, arguments
from rogerthat.bizz.gcs import get_serving_url
from rogerthat.bizz.service import re_index_map_only
from rogerthat.consts import FAST_QUEUE
from rogerthat.models import ServiceIdentity
from rogerthat.models.settings import ServiceInfo
from rogerthat.rpc import users
from rogerthat.rpc.users import get_current_session
from rogerthat.utils import parse_date
from rogerthat.utils.service import create_service_identity_user
from shop.models import Customer
from solutions import translate
from solutions.common.bizz import SolutionModule, broadcast_updates_pending
from solutions.common.bizz.campaignmonitor import send_smart_email_without_check
from solutions.common.consts import OCA_FILES_BUCKET
from solutions.common.dal import get_solution_settings
from solutions.common.integrations.cirklo.cirklo import get_city_id_by_service_email, whitelist_merchant, \
    list_whitelisted_merchants, list_cirklo_cities
from solutions.common.integrations.cirklo.models import CirkloCity, CirkloMerchant, SignupLanguageProperty, \
    SignupMails, CirkloAppInfo
from solutions.common.integrations.cirklo.to import CirkloCityTO, CirkloVoucherListTO, CirkloVoucherServiceTO, \
    WhitelistVoucherServiceTO
from solutions.common.restapi.services import _check_is_city


def _check_permission(city_sln_settings):
    if SolutionModule.CIRKLO_VOUCHERS not in city_sln_settings.modules:
        raise HttpForbiddenException()

    if len(city_sln_settings.modules) != 1:
        _check_is_city(city_sln_settings.service_user)


@rest('/common/vouchers/cities', 'get', silent_result=True)
@returns([dict])
@arguments(staging=bool)
def api_list_cirklo_cities(staging=False):
    return list_cirklo_cities(staging)


@rest('/common/vouchers/services', 'get', silent_result=True)
@returns(CirkloVoucherListTO)
@arguments()
def get_cirklo_vouchers_services():
    city_service_user = users.get_current_user()
    city_sln_settings = get_solution_settings(city_service_user)
    _check_permission(city_sln_settings)

    to = CirkloVoucherListTO()
    to.total = 0
    to.results = []
    to.cursor = None
    to.more = False

    cirklo_city = CirkloCity.get_by_service_email(city_service_user.email())
    if not cirklo_city:
        return to

    cirklo_merchants = list_whitelisted_merchants(cirklo_city.city_id)
    cirklo_dict = {}
    cirklo_emails = []
    for merchant in cirklo_merchants:
        if merchant['email'] in cirklo_emails:
            logging.error('Duplicate found %s', merchant['email'])
            continue
        cirklo_emails.append(merchant['email'])
        cirklo_dict[merchant['email']] = merchant

    qry = CirkloMerchant.list_by_city_id(cirklo_city.city_id)  # type: List[CirkloMerchant]
    osa_merchants = []
    for merchant in qry:
        if merchant.service_user_email:
            osa_merchants.append(merchant)
        else:
            cirklo_merchant = cirklo_dict.get(merchant.data['company']['email'])
            if cirklo_merchant:
                if merchant.data['company']['email'] in cirklo_emails:
                    cirklo_emails.remove(merchant.data['company']['email'])
                if not merchant.whitelisted:
                    merchant.whitelisted = True
                    merchant.put()
            elif merchant.whitelisted:
                merchant.whitelisted = False
                merchant.put()

            whitelist_date = cirklo_merchant['createdAt'] if cirklo_merchant else None
            merchant_registered = 'shopInfo' in cirklo_merchant if cirklo_merchant else False
            to.results.append(
                CirkloVoucherServiceTO.from_model(merchant, whitelist_date, merchant_registered, u'Cirklo signup'))

    if osa_merchants:
        customer_to_get = [Customer.create_key(merchant.customer_id) for merchant in osa_merchants]
        customers_dict = {customer.id: customer for customer in db.get(customer_to_get)}
        info_keys = [ServiceInfo.create_key(users.User(merchant.service_user_email), ServiceIdentity.DEFAULT)
                     for merchant in osa_merchants]
        models = ndb.get_multi(info_keys)

        for service_info, merchant in zip(models, osa_merchants):
            customer = customers_dict[merchant.customer_id]
            if not customer.service_user:
                merchant.key.delete()
                continue
            cirklo_merchant = cirklo_dict.get(customer.user_email)
            should_save = False
            if cirklo_merchant:
                if customer.user_email in cirklo_emails:
                    cirklo_emails.remove(customer.user_email)
                if not merchant.whitelisted:
                    merchant.whitelisted = True
                    should_save = True
            elif merchant.whitelisted:
                merchant.whitelisted = False
                should_save = True

            if should_save:
                merchant.put()
                service_identity_user = create_service_identity_user(customer.service_user)
                deferred.defer(re_index_map_only, service_identity_user)

            whitelist_date = cirklo_merchant['createdAt'] if cirklo_merchant else None
            merchant_registered = 'shopInfo' in cirklo_merchant if cirklo_merchant else False
            service_to = CirkloVoucherServiceTO.from_model(merchant, whitelist_date, merchant_registered, u'OSA signup')
            service_to.populate_from_info(service_info, customer)
            to.results.append(service_to)

    for email in cirklo_emails:
        cirklo_merchant = cirklo_dict[email]
        to.results.append(CirkloVoucherServiceTO.from_cirklo_info(cirklo_merchant))
    return to


@rest('/common/vouchers/services/whitelist', 'put', type=REST_TYPE_TO)
@returns(CirkloVoucherServiceTO)
@arguments(data=WhitelistVoucherServiceTO)
def whitelist_voucher_service(data):
    city_service_user = users.get_current_user()
    city_sln_settings = get_solution_settings(city_service_user)
    _check_permission(city_sln_settings)

    cirklo_city = CirkloCity.get_by_service_email(city_service_user.email())  # type: CirkloCity
    if not cirklo_city:
        raise HttpNotFoundException('No cirklo settings found.')
    is_cirklo_only_merchant = '@' not in data.id
    if is_cirklo_only_merchant:
        merchant = CirkloMerchant.create_key(long(data.id)).get()  # type: CirkloMerchant
        language = merchant.get_language()
    else:
        merchant = CirkloMerchant.create_key(data.id).get()
        language = get_solution_settings(users.User(merchant.service_user_email)).main_language
    if data.accepted:
        email_id = cirklo_city.get_signup_accepted_mail(language)
        if not email_id:
            raise HttpBadRequestException('City settings aren\'t fully setup yet.')
        whitelist_merchant(cirklo_city.city_id, data.email)
        deferred.defer(send_smart_email_without_check, email_id, [data.email], _countdown=1,
                       _queue=FAST_QUEUE)
    else:
        email_id = cirklo_city.get_signup_accepted_mail(language)
        if not email_id:
            raise HttpBadRequestException('City settings aren\'t fully setup yet.')
        deferred.defer(send_smart_email_without_check, email_id, [data.email], _countdown=1,
                       _queue=FAST_QUEUE)

    whitelist_date = datetime.now().isoformat() + 'Z' if data.accepted else None
    if not is_cirklo_only_merchant:
        if data.accepted:
            merchant.whitelisted = True
        else:
            merchant.denied = True
        merchant.put()

        service_info = ServiceInfo.create_key(users.User(merchant.service_user_email), ServiceIdentity.DEFAULT).get()
        customer = Customer.get_by_id(merchant.customer_id)  # type: Customer

        if data.accepted:
            service_identity_user = create_service_identity_user(customer.service_user)
            deferred.defer(re_index_map_only, service_identity_user)
        to = CirkloVoucherServiceTO.from_model(merchant, whitelist_date, False, u'OSA signup')
        to.populate_from_info(service_info, customer)
        return to
    else:
        if data.accepted:
            merchant.whitelisted = True
        else:
            merchant.denied = True
        merchant.put()

        return CirkloVoucherServiceTO.from_model(merchant, whitelist_date, False, u'Cirklo signup')


@rest('/common/vouchers/cirklo', 'get')
@returns(CirkloCityTO)
@arguments()
def api_vouchers_get_cirklo_settings():
    service_user = users.get_current_user()
    city = CirkloCity.get_by_service_email(service_user.email())
    return CirkloCityTO.from_model(city)


@rest('/common/vouchers/cirklo', 'put')
@returns(CirkloCityTO)
@arguments(data=CirkloCityTO)
def api_vouchers_save_cirklo_settings(data):
    service_user = users.get_current_user()
    if not get_current_session().shop:
        lang = get_solution_settings(service_user).main_language
        raise HttpForbiddenException(translate(lang, 'no_permission'))
    other_city = CirkloCity.get_by_service_email(service_user.email())  # type: CirkloCity
    if not data.city_id:
        if other_city:
            other_city.key.delete()
        return CirkloCityTO.from_model(None)

    key = CirkloCity.create_key(data.city_id)
    city = key.get()
    if not city:
        city = CirkloCity(key=key, service_user_email=service_user.email())
    elif city.service_user_email != service_user.email():
        raise HttpBadRequestException('City id %s is already in use by another service' % data.city_id)
    if other_city and other_city.key != key:
        other_city.key.delete()
    invalidate_cache(get_city_id_by_service_email, service_user.email())
    city.logo_url = data.logo_url
    city.signup_enabled = data.signup_enabled
    city.signup_logo_url = data.signup_logo_url
    city.signup_names = None
    city.signup_mail = SignupMails.from_to(data.signup_mail)

    if data.signup_name_nl and data.signup_name_fr:
        city.signup_names = SignupLanguageProperty(nl=data.signup_name_nl,
                                                   fr=data.signup_name_fr)
    elif data.signup_name_nl:
        city.signup_names = SignupLanguageProperty(nl=data.signup_name_nl,
                                                   fr=data.signup_name_nl)
    elif data.signup_name_fr:
        city.signup_names = SignupLanguageProperty(nl=data.signup_name_fr,
                                                   fr=data.signup_name_fr)
    og_info = city.app_info and city.app_info.to_dict()
    info = CirkloAppInfo(enabled=data.app_info.enabled,
                         title=data.app_info.title,
                         buttons=data.app_info.buttons)
    sln_settings = get_solution_settings(service_user)
    if info.to_dict() != og_info and not sln_settings.ciklo_vouchers_only():
        city.app_info = info
        sln_settings.updates_pending = True
        sln_settings.put()
        broadcast_updates_pending(sln_settings)
    city.put()
    return CirkloCityTO.from_model(city)


@rest('/common/vouchers/cirklo/export', 'post')
@returns(dict)
@arguments()
def api_export_cirklo_services():
    service_user = users.get_current_user()
    city_sln_settings = get_solution_settings(service_user)
    _check_permission(city_sln_settings)
    all_services = get_cirklo_vouchers_services()
    if all_services.cursor:
        raise NotImplementedError()
    book = Workbook(encoding='utf-8')
    sheet = book.add_sheet('Cirklo')  # type: Worksheet
    language = city_sln_settings.main_language
    sheet.write(0, 0, translate(language, 'reservation-name'))
    sheet.write(0, 1, translate(language, 'Email'))
    sheet.write(0, 2, translate(language, 'address'))
    sheet.write(0, 3, translate(language, 'Phone number'))
    sheet.write(0, 4, translate(language, 'created'))
    sheet.write(0, 5, translate(language, 'merchant_registered'))

    date_format = XFStyle()
    date_format.num_format_str = 'dd/mm/yyyy'
    row = 0

    for service in all_services.results:
        row += 1
        sheet.write(row, 0, service.name)
        sheet.write(row, 1, service.email)
        sheet.write(row, 2, service.address)
        sheet.write(row, 3, service.phone_number)
        sheet.write(row, 4, parse_date(service.creation_date), date_format)
        sheet.write(row, 5, translate(language, 'Yes') if service.merchant_registered else translate(language, 'No'))

    date = format_datetime(datetime.now(), format='medium', locale='en_GB')
    gcs_path = '/%s/tmp/cirklo/export-cirklo-%s.xls' % (OCA_FILES_BUCKET, date.replace(' ', '-'))
    content_type = 'application/vnd.ms-excel'
    with cloudstorage.open(gcs_path, 'w', content_type=content_type) as gcs_file:
        book.save(gcs_file)

    deferred.defer(cloudstorage.delete, gcs_path, _countdown=86400)

    return {
        'url': get_serving_url(gcs_path),
    }

[((2628, 2686), 'mcfw.restapi.rest', 'rest', (['"""/common/vouchers/cities"""', '"""get"""'], {'silent_result': '(True)'}), "('/common/vouchers/cities', 'get', silent_result=True)\n", (2632, 2686), False, 'from mcfw.restapi import rest\n'), ((2688, 2703), 'mcfw.rpc.returns', 'returns', (['[dict]'], {}), '([dict])\n', (2695, 2703), False, 'from mcfw.rpc import returns, arguments\n'), ((2705, 2728), 'mcfw.rpc.arguments', 'arguments', ([], {'staging': 'bool'}), '(staging=bool)\n', (2714, 2728), False, 'from mcfw.rpc import returns, arguments\n'), ((2814, 2874), 'mcfw.restapi.rest', 'rest', (['"""/common/vouchers/services"""', '"""get"""'], {'silent_result': '(True)'}), "('/common/vouchers/services', 'get', silent_result=True)\n", (2818, 2874), False, 'from mcfw.restapi import rest\n'), ((2876, 2904), 'mcfw.rpc.returns', 'returns', (['CirkloVoucherListTO'], {}), '(CirkloVoucherListTO)\n', (2883, 2904), False, 'from mcfw.rpc import returns, arguments\n'), ((2906, 2917), 'mcfw.rpc.arguments', 'arguments', ([], {}), '()\n', (2915, 2917), False, 'from mcfw.rpc import returns, arguments\n'), ((6760, 6829), 'mcfw.restapi.rest', 'rest', (['"""/common/vouchers/services/whitelist"""', '"""put"""'], {'type': 'REST_TYPE_TO'}), "('/common/vouchers/services/whitelist', 'put', type=REST_TYPE_TO)\n", (6764, 6829), False, 'from mcfw.restapi import rest\n'), ((6831, 6862), 'mcfw.rpc.returns', 'returns', (['CirkloVoucherServiceTO'], {}), '(CirkloVoucherServiceTO)\n', (6838, 6862), False, 'from mcfw.rpc import returns, arguments\n'), ((6864, 6905), 'mcfw.rpc.arguments', 'arguments', ([], {'data': 'WhitelistVoucherServiceTO'}), '(data=WhitelistVoucherServiceTO)\n', (6873, 6905), False, 'from mcfw.rpc import returns, arguments\n'), ((9433, 9471), 'mcfw.restapi.rest', 'rest', (['"""/common/vouchers/cirklo"""', '"""get"""'], {}), "('/common/vouchers/cirklo', 'get')\n", (9437, 9471), False, 'from mcfw.restapi import rest\n'), ((9473, 9494), 'mcfw.rpc.returns', 'returns', (['CirkloCityTO'], {}), '(CirkloCityTO)\n', (9480, 9494), False, 'from mcfw.rpc import returns, arguments\n'), ((9496, 9507), 'mcfw.rpc.arguments', 'arguments', ([], {}), '()\n', (9505, 9507), False, 'from mcfw.rpc import returns, arguments\n'), ((9701, 9739), 'mcfw.restapi.rest', 'rest', (['"""/common/vouchers/cirklo"""', '"""put"""'], {}), "('/common/vouchers/cirklo', 'put')\n", (9705, 9739), False, 'from mcfw.restapi import rest\n'), ((9741, 9762), 'mcfw.rpc.returns', 'returns', (['CirkloCityTO'], {}), '(CirkloCityTO)\n', (9748, 9762), False, 'from mcfw.rpc import returns, arguments\n'), ((9764, 9792), 'mcfw.rpc.arguments', 'arguments', ([], {'data': 'CirkloCityTO'}), '(data=CirkloCityTO)\n', (9773, 9792), False, 'from mcfw.rpc import returns, arguments\n'), ((12090, 12136), 'mcfw.restapi.rest', 'rest', (['"""/common/vouchers/cirklo/export"""', '"""post"""'], {}), "('/common/vouchers/cirklo/export', 'post')\n", (12094, 12136), False, 'from mcfw.restapi import rest\n'), ((12138, 12151), 'mcfw.rpc.returns', 'returns', (['dict'], {}), '(dict)\n', (12145, 12151), False, 'from mcfw.rpc import returns, arguments\n'), ((12153, 12164), 'mcfw.rpc.arguments', 'arguments', ([], {}), '()\n', (12162, 12164), False, 'from mcfw.rpc import returns, arguments\n'), ((2783, 2810), 'solutions.common.integrations.cirklo.cirklo.list_cirklo_cities', 'list_cirklo_cities', (['staging'], {}), '(staging)\n', (2801, 2810), False, 'from solutions.common.integrations.cirklo.cirklo import get_city_id_by_service_email, whitelist_merchant, list_whitelisted_merchants, list_cirklo_cities\n'), ((2978, 3002), 'rogerthat.rpc.users.get_current_user', 'users.get_current_user', ([], {}), '()\n', (3000, 3002), False, 'from rogerthat.rpc import users\n'), ((3027, 3067), 'solutions.common.dal.get_solution_settings', 'get_solution_settings', (['city_service_user'], {}), '(city_service_user)\n', (3048, 3067), False, 'from solutions.common.dal import get_solution_settings\n'), ((3119, 3140), 'solutions.common.integrations.cirklo.to.CirkloVoucherListTO', 'CirkloVoucherListTO', ([], {}), '()\n', (3138, 3140), False, 'from solutions.common.integrations.cirklo.to import CirkloCityTO, CirkloVoucherListTO, CirkloVoucherServiceTO, WhitelistVoucherServiceTO\n'), ((3363, 3410), 'solutions.common.integrations.cirklo.cirklo.list_whitelisted_merchants', 'list_whitelisted_merchants', (['cirklo_city.city_id'], {}), '(cirklo_city.city_id)\n', (3389, 3410), False, 'from solutions.common.integrations.cirklo.cirklo import get_city_id_by_service_email, whitelist_merchant, list_whitelisted_merchants, list_cirklo_cities\n'), ((3737, 3788), 'solutions.common.integrations.cirklo.models.CirkloMerchant.list_by_city_id', 'CirkloMerchant.list_by_city_id', (['cirklo_city.city_id'], {}), '(cirklo_city.city_id)\n', (3767, 3788), False, 'from solutions.common.integrations.cirklo.models import CirkloCity, CirkloMerchant, SignupLanguageProperty, SignupMails, CirkloAppInfo\n'), ((6967, 6991), 'rogerthat.rpc.users.get_current_user', 'users.get_current_user', ([], {}), '()\n', (6989, 6991), False, 'from rogerthat.rpc import users\n'), ((7016, 7056), 'solutions.common.dal.get_solution_settings', 'get_solution_settings', (['city_service_user'], {}), '(city_service_user)\n', (7037, 7056), False, 'from solutions.common.dal import get_solution_settings\n'), ((9567, 9591), 'rogerthat.rpc.users.get_current_user', 'users.get_current_user', ([], {}), '()\n', (9589, 9591), False, 'from rogerthat.rpc import users\n'), ((9668, 9697), 'solutions.common.integrations.cirklo.to.CirkloCityTO.from_model', 'CirkloCityTO.from_model', (['city'], {}), '(city)\n', (9691, 9697), False, 'from solutions.common.integrations.cirklo.to import CirkloCityTO, CirkloVoucherListTO, CirkloVoucherServiceTO, WhitelistVoucherServiceTO\n'), ((9857, 9881), 'rogerthat.rpc.users.get_current_user', 'users.get_current_user', ([], {}), '()\n', (9879, 9881), False, 'from rogerthat.rpc import users\n'), ((10288, 10323), 'solutions.common.integrations.cirklo.models.CirkloCity.create_key', 'CirkloCity.create_key', (['data.city_id'], {}), '(data.city_id)\n', (10309, 10323), False, 'from solutions.common.integrations.cirklo.models import CirkloCity, CirkloMerchant, SignupLanguageProperty, SignupMails, CirkloAppInfo\n'), ((10930, 10967), 'solutions.common.integrations.cirklo.models.SignupMails.from_to', 'SignupMails.from_to', (['data.signup_mail'], {}), '(data.signup_mail)\n', (10949, 10967), False, 'from solutions.common.integrations.cirklo.models import CirkloCity, CirkloMerchant, SignupLanguageProperty, SignupMails, CirkloAppInfo\n'), ((11598, 11704), 'solutions.common.integrations.cirklo.models.CirkloAppInfo', 'CirkloAppInfo', ([], {'enabled': 'data.app_info.enabled', 'title': 'data.app_info.title', 'buttons': 'data.app_info.buttons'}), '(enabled=data.app_info.enabled, title=data.app_info.title,\n    buttons=data.app_info.buttons)\n', (11611, 11704), False, 'from solutions.common.integrations.cirklo.models import CirkloCity, CirkloMerchant, SignupLanguageProperty, SignupMails, CirkloAppInfo\n'), ((11770, 11805), 'solutions.common.dal.get_solution_settings', 'get_solution_settings', (['service_user'], {}), '(service_user)\n', (11791, 11805), False, 'from solutions.common.dal import get_solution_settings\n'), ((12057, 12086), 'solutions.common.integrations.cirklo.to.CirkloCityTO.from_model', 'CirkloCityTO.from_model', (['city'], {}), '(city)\n', (12080, 12086), False, 'from solutions.common.integrations.cirklo.to import CirkloCityTO, CirkloVoucherListTO, CirkloVoucherServiceTO, WhitelistVoucherServiceTO\n'), ((12218, 12242), 'rogerthat.rpc.users.get_current_user', 'users.get_current_user', ([], {}), '()\n', (12240, 12242), False, 'from rogerthat.rpc import users\n'), ((12267, 12302), 'solutions.common.dal.get_solution_settings', 'get_solution_settings', (['service_user'], {}), '(service_user)\n', (12288, 12302), False, 'from solutions.common.dal import get_solution_settings\n'), ((12469, 12495), 'xlwt.Workbook', 'Workbook', ([], {'encoding': '"""utf-8"""'}), "(encoding='utf-8')\n", (12477, 12495), False, 'from xlwt import Worksheet, Workbook, XFStyle\n'), ((12966, 12975), 'xlwt.XFStyle', 'XFStyle', ([], {}), '()\n', (12973, 12975), False, 'from xlwt import Worksheet, Workbook, XFStyle\n'), ((13803, 13866), 'google.appengine.ext.deferred.defer', 'deferred.defer', (['cloudstorage.delete', 'gcs_path'], {'_countdown': '(86400)'}), '(cloudstorage.delete, gcs_path, _countdown=86400)\n', (13817, 13866), False, 'from google.appengine.ext import ndb, deferred, db\n'), ((2500, 2524), 'mcfw.exceptions.HttpForbiddenException', 'HttpForbiddenException', ([], {}), '()\n', (2522, 2524), False, 'from mcfw.exceptions import HttpBadRequestException, HttpForbiddenException, HttpNotFoundException\n'), ((2578, 2624), 'solutions.common.restapi.services._check_is_city', '_check_is_city', (['city_sln_settings.service_user'], {}), '(city_sln_settings.service_user)\n', (2592, 2624), False, 'from solutions.common.restapi.services import _check_is_city\n'), ((5187, 5211), 'google.appengine.ext.ndb.get_multi', 'ndb.get_multi', (['info_keys'], {}), '(info_keys)\n', (5200, 5211), False, 'from google.appengine.ext import ndb, deferred, db\n'), ((7234, 7284), 'mcfw.exceptions.HttpNotFoundException', 'HttpNotFoundException', (['"""No cirklo settings found."""'], {}), "('No cirklo settings found.')\n", (7255, 7284), False, 'from mcfw.exceptions import HttpBadRequestException, HttpForbiddenException, HttpNotFoundException\n'), ((7870, 7921), 'solutions.common.integrations.cirklo.cirklo.whitelist_merchant', 'whitelist_merchant', (['cirklo_city.city_id', 'data.email'], {}), '(cirklo_city.city_id, data.email)\n', (7888, 7921), False, 'from solutions.common.integrations.cirklo.cirklo import get_city_id_by_service_email, whitelist_merchant, list_whitelisted_merchants, list_cirklo_cities\n'), ((7930, 8037), 'google.appengine.ext.deferred.defer', 'deferred.defer', (['send_smart_email_without_check', 'email_id', '[data.email]'], {'_countdown': '(1)', '_queue': 'FAST_QUEUE'}), '(send_smart_email_without_check, email_id, [data.email],\n    _countdown=1, _queue=FAST_QUEUE)\n', (7944, 8037), False, 'from google.appengine.ext import ndb, deferred, db\n'), ((8250, 8357), 'google.appengine.ext.deferred.defer', 'deferred.defer', (['send_smart_email_without_check', 'email_id', '[data.email]'], {'_countdown': '(1)', '_queue': 'FAST_QUEUE'}), '(send_smart_email_without_check, email_id, [data.email],\n    _countdown=1, _queue=FAST_QUEUE)\n', (8264, 8357), False, 'from google.appengine.ext import ndb, deferred, db\n'), ((8771, 8811), 'shop.models.Customer.get_by_id', 'Customer.get_by_id', (['merchant.customer_id'], {}), '(merchant.customer_id)\n', (8789, 8811), False, 'from shop.models import Customer\n'), ((9027, 9112), 'solutions.common.integrations.cirklo.to.CirkloVoucherServiceTO.from_model', 'CirkloVoucherServiceTO.from_model', (['merchant', 'whitelist_date', '(False)', 'u"""OSA signup"""'], {}), "(merchant, whitelist_date, False,\n    u'OSA signup')\n", (9060, 9112), False, 'from solutions.common.integrations.cirklo.to import CirkloCityTO, CirkloVoucherListTO, CirkloVoucherServiceTO, WhitelistVoucherServiceTO\n'), ((9345, 9433), 'solutions.common.integrations.cirklo.to.CirkloVoucherServiceTO.from_model', 'CirkloVoucherServiceTO.from_model', (['merchant', 'whitelist_date', '(False)', 'u"""Cirklo signup"""'], {}), "(merchant, whitelist_date, False,\n    u'Cirklo signup')\n", (9378, 9433), False, 'from solutions.common.integrations.cirklo.to import CirkloCityTO, CirkloVoucherListTO, CirkloVoucherServiceTO, WhitelistVoucherServiceTO\n'), ((10247, 10276), 'solutions.common.integrations.cirklo.to.CirkloCityTO.from_model', 'CirkloCityTO.from_model', (['None'], {}), '(None)\n', (10270, 10276), False, 'from solutions.common.integrations.cirklo.to import CirkloCityTO, CirkloVoucherListTO, CirkloVoucherServiceTO, WhitelistVoucherServiceTO\n'), ((11049, 11119), 'solutions.common.integrations.cirklo.models.SignupLanguageProperty', 'SignupLanguageProperty', ([], {'nl': 'data.signup_name_nl', 'fr': 'data.signup_name_fr'}), '(nl=data.signup_name_nl, fr=data.signup_name_fr)\n', (11071, 11119), False, 'from solutions.common.integrations.cirklo.models import CirkloCity, CirkloMerchant, SignupLanguageProperty, SignupMails, CirkloAppInfo\n'), ((11991, 12030), 'solutions.common.bizz.broadcast_updates_pending', 'broadcast_updates_pending', (['sln_settings'], {}), '(sln_settings)\n', (12016, 12030), False, 'from solutions.common.bizz import SolutionModule, broadcast_updates_pending\n'), ((12621, 12660), 'solutions.translate', 'translate', (['language', '"""reservation-name"""'], {}), "(language, 'reservation-name')\n", (12630, 12660), False, 'from solutions import translate\n'), ((12684, 12712), 'solutions.translate', 'translate', (['language', '"""Email"""'], {}), "(language, 'Email')\n", (12693, 12712), False, 'from solutions import translate\n'), ((12736, 12766), 'solutions.translate', 'translate', (['language', '"""address"""'], {}), "(language, 'address')\n", (12745, 12766), False, 'from solutions import translate\n'), ((12790, 12825), 'solutions.translate', 'translate', (['language', '"""Phone number"""'], {}), "(language, 'Phone number')\n", (12799, 12825), False, 'from solutions import translate\n'), ((12849, 12879), 'solutions.translate', 'translate', (['language', '"""created"""'], {}), "(language, 'created')\n", (12858, 12879), False, 'from solutions import translate\n'), ((12903, 12945), 'solutions.translate', 'translate', (['language', '"""merchant_registered"""'], {}), "(language, 'merchant_registered')\n", (12912, 12945), False, 'from solutions import translate\n'), ((13495, 13509), 'datetime.datetime.now', 'datetime.now', ([], {}), '()\n', (13507, 13509), False, 'from datetime import datetime\n'), ((13697, 13756), 'cloudstorage.open', 'cloudstorage.open', (['gcs_path', '"""w"""'], {'content_type': 'content_type'}), "(gcs_path, 'w', content_type=content_type)\n", (13714, 13756), False, 'import cloudstorage\n'), ((13896, 13921), 'rogerthat.bizz.gcs.get_serving_url', 'get_serving_url', (['gcs_path'], {}), '(gcs_path)\n', (13911, 13921), False, 'from rogerthat.bizz.gcs import get_serving_url\n'), ((3552, 3606), 'logging.error', 'logging.error', (['"""Duplicate found %s"""', "merchant['email']"], {}), "('Duplicate found %s', merchant['email'])\n", (3565, 3606), False, 'import logging\n'), ((4846, 4887), 'shop.models.Customer.create_key', 'Customer.create_key', (['merchant.customer_id'], {}), '(merchant.customer_id)\n', (4865, 4887), False, 'from shop.models import Customer\n'), ((6377, 6476), 'solutions.common.integrations.cirklo.to.CirkloVoucherServiceTO.from_model', 'CirkloVoucherServiceTO.from_model', (['merchant', 'whitelist_date', 'merchant_registered', 'u"""OSA signup"""'], {}), "(merchant, whitelist_date,\n    merchant_registered, u'OSA signup')\n", (6410, 6476), False, 'from solutions.common.integrations.cirklo.to import CirkloCityTO, CirkloVoucherListTO, CirkloVoucherServiceTO, WhitelistVoucherServiceTO\n'), ((6685, 6741), 'solutions.common.integrations.cirklo.to.CirkloVoucherServiceTO.from_cirklo_info', 'CirkloVoucherServiceTO.from_cirklo_info', (['cirklo_merchant'], {}), '(cirklo_merchant)\n', (6724, 6741), False, 'from solutions.common.integrations.cirklo.to import CirkloCityTO, CirkloVoucherListTO, CirkloVoucherServiceTO, WhitelistVoucherServiceTO\n'), ((7796, 7860), 'mcfw.exceptions.HttpBadRequestException', 'HttpBadRequestException', (['"""City settings aren\'t fully setup yet."""'], {}), '("City settings aren\'t fully setup yet.")\n', (7819, 7860), False, 'from mcfw.exceptions import HttpBadRequestException, HttpForbiddenException, HttpNotFoundException\n'), ((8176, 8240), 'mcfw.exceptions.HttpBadRequestException', 'HttpBadRequestException', (['"""City settings aren\'t fully setup yet."""'], {}), '("City settings aren\'t fully setup yet.")\n', (8199, 8240), False, 'from mcfw.exceptions import HttpBadRequestException, HttpForbiddenException, HttpNotFoundException\n'), ((8893, 8944), 'rogerthat.utils.service.create_service_identity_user', 'create_service_identity_user', (['customer.service_user'], {}), '(customer.service_user)\n', (8921, 8944), False, 'from rogerthat.utils.service import create_service_identity_user\n'), ((8957, 9013), 'google.appengine.ext.deferred.defer', 'deferred.defer', (['re_index_map_only', 'service_identity_user'], {}), '(re_index_map_only, service_identity_user)\n', (8971, 9013), False, 'from google.appengine.ext import ndb, deferred, db\n'), ((9893, 9914), 'rogerthat.rpc.users.get_current_session', 'get_current_session', ([], {}), '()\n', (9912, 9914), False, 'from rogerthat.rpc.users import get_current_session\n'), ((9936, 9971), 'solutions.common.dal.get_solution_settings', 'get_solution_settings', (['service_user'], {}), '(service_user)\n', (9957, 9971), False, 'from solutions.common.dal import get_solution_settings\n'), ((10023, 10055), 'solutions.translate', 'translate', (['lang', '"""no_permission"""'], {}), "(lang, 'no_permission')\n", (10032, 10055), False, 'from solutions import translate\n'), ((10510, 10603), 'mcfw.exceptions.HttpBadRequestException', 'HttpBadRequestException', (["('City id %s is already in use by another service' % data.city_id)"], {}), "('City id %s is already in use by another service' %\n    data.city_id)\n", (10533, 10603), False, 'from mcfw.exceptions import HttpBadRequestException, HttpForbiddenException, HttpNotFoundException\n'), ((11229, 11299), 'solutions.common.integrations.cirklo.models.SignupLanguageProperty', 'SignupLanguageProperty', ([], {'nl': 'data.signup_name_nl', 'fr': 'data.signup_name_nl'}), '(nl=data.signup_name_nl, fr=data.signup_name_nl)\n', (11251, 11299), False, 'from solutions.common.integrations.cirklo.models import CirkloCity, CirkloMerchant, SignupLanguageProperty, SignupMails, CirkloAppInfo\n'), ((13301, 13334), 'rogerthat.utils.parse_date', 'parse_date', (['service.creation_date'], {}), '(service.creation_date)\n', (13311, 13334), False, 'from rogerthat.utils import parse_date\n'), ((4696, 4798), 'solutions.common.integrations.cirklo.to.CirkloVoucherServiceTO.from_model', 'CirkloVoucherServiceTO.from_model', (['merchant', 'whitelist_date', 'merchant_registered', 'u"""Cirklo signup"""'], {}), "(merchant, whitelist_date,\n    merchant_registered, u'Cirklo signup')\n", (4729, 4798), False, 'from solutions.common.integrations.cirklo.to import CirkloCityTO, CirkloVoucherListTO, CirkloVoucherServiceTO, WhitelistVoucherServiceTO\n'), ((4983, 5006), 'google.appengine.ext.db.get', 'db.get', (['customer_to_get'], {}), '(customer_to_get)\n', (4989, 5006), False, 'from google.appengine.ext import ndb, deferred, db\n'), ((5052, 5091), 'rogerthat.rpc.users.User', 'users.User', (['merchant.service_user_email'], {}), '(merchant.service_user_email)\n', (5062, 5091), False, 'from rogerthat.rpc import users\n'), ((6045, 6096), 'rogerthat.utils.service.create_service_identity_user', 'create_service_identity_user', (['customer.service_user'], {}), '(customer.service_user)\n', (6073, 6096), False, 'from rogerthat.utils.service import create_service_identity_user\n'), ((6113, 6169), 'google.appengine.ext.deferred.defer', 'deferred.defer', (['re_index_map_only', 'service_identity_user'], {}), '(re_index_map_only, service_identity_user)\n', (6127, 6169), False, 'from google.appengine.ext import ndb, deferred, db\n'), ((7528, 7562), 'solutions.common.integrations.cirklo.models.CirkloMerchant.create_key', 'CirkloMerchant.create_key', (['data.id'], {}), '(data.id)\n', (7553, 7562), False, 'from solutions.common.integrations.cirklo.models import CirkloCity, CirkloMerchant, SignupLanguageProperty, SignupMails, CirkloAppInfo\n'), ((7610, 7649), 'rogerthat.rpc.users.User', 'users.User', (['merchant.service_user_email'], {}), '(merchant.service_user_email)\n', (7620, 7649), False, 'from rogerthat.rpc import users\n'), ((11409, 11479), 'solutions.common.integrations.cirklo.models.SignupLanguageProperty', 'SignupLanguageProperty', ([], {'nl': 'data.signup_name_fr', 'fr': 'data.signup_name_fr'}), '(nl=data.signup_name_fr, fr=data.signup_name_fr)\n', (11431, 11479), False, 'from solutions.common.integrations.cirklo.models import CirkloCity, CirkloMerchant, SignupLanguageProperty, SignupMails, CirkloAppInfo\n'), ((13377, 13403), 'solutions.translate', 'translate', (['language', '"""Yes"""'], {}), "(language, 'Yes')\n", (13386, 13403), False, 'from solutions import translate\n'), ((13440, 13465), 'solutions.translate', 'translate', (['language', '"""No"""'], {}), "(language, 'No')\n", (13449, 13465), False, 'from solutions import translate\n'), ((8399, 8413), 'datetime.datetime.now', 'datetime.now', ([], {}), '()\n', (8411, 8413), False, 'from datetime import datetime\n'), ((8680, 8719), 'rogerthat.rpc.users.User', 'users.User', (['merchant.service_user_email'], {}), '(merchant.service_user_email)\n', (8690, 8719), False, 'from rogerthat.rpc import users\n')]