def store_branding_zip(service_user, zip_, description, skip_meta_file=False):
    # type: (users.User, ZipFile, unicode) -> Branding
    try:
        pokes, html, branding_type, meta_properties = _parse_and_validate_branding_zip(zip_)

        # Remove previously added dimensions:
        html = re.sub("<meta\\s+property=\\\"rt:dimensions\\\"\\s+content=\\\"\\[\\d+,\\d+,\\d+,\\d+\\]\\\"\\s*/>", "",
                      html)

        try:
            zip_content = _create_new_zip(service_user, branding_type, zip_, html, skip_meta_file)
        except BadZipfile, e:
            raise BadBrandingZipException(reason=e.message)

        _validate_branding_size(zip_content, branding_type)

        hash_ = calculate_branding_hash(branding_type, zip_content)
        b = Branding.get_by_key_name(hash_)
        if b and b.user != service_user:
            raise BrandingAlreadyExistsException()

        def trans():
            return _create_branding(hash_, zip_content, description, service_user, branding_type, meta_properties,
                                    pokes)

        branding = run_in_transaction(trans, True)

        #
        # Try to add dimension information
        #

        if "</head>" in html:
            settings = get_server_settings()
            if not settings.brandingRendererUrl or branding_type in (Branding.TYPE_APP, Branding.TYPE_CORDOVA,):
                return branding

            if meta_properties.get('display-type', Branding.DISPLAY_TYPE_WEBVIEW) == Branding.DISPLAY_TYPE_NATIVE:
                return branding

            try:
                url = "%s?%s" % (settings.brandingRendererUrl, urllib.urlencode(
                    (('url', get_server_settings().baseUrl + '/branding/' + hash_ + '/branding.html?xrender=true'),)))
                response = urlfetch.fetch(url, deadline=10)
                if response.status_code != 200:
                    logging.error("Could not render branding to get dimensions.\n%s" % response.content)
                    return branding
            except:
                logging.warn("Failed to render dimensions")
                return branding

            html = html.replace("</head>",
                                '<meta property="rt:dimensions" content="' + response.content + '" /></head>')
        else:
            return branding

        zip_content = _create_new_zip(service_user, branding_type, zip_, html)
Ejemplo n.º 2
0
def signup(name, email, cont):
    if not name or not name.strip():
        return SIGNUP_INVALID_NAME

    if not email or not email.strip():
        return SIGNUP_INVALID_EMAIL

    if not EMAIL_REGEX.match(email):
        return SIGNUP_INVALID_EMAIL

    app = get_app_by_id(App.APP_ID_ROGERTHAT)  # TODO DEFAULT APP ID
    if app.user_regex:
        regexes = app.user_regex.splitlines()
        for regex in regexes:
            if re.match(regex, email):
                break
        else:
            return SIGNUP_INVALID_EMAIL_DOMAIN

    user = users.User(email)
    profile = get_service_or_user_profile(user)
    if profile and profile.passwordHash:
        return SIGNUP_ACCOUNT_EXISTS

    deactivated_profile = get_deactivated_user_profile(user)
    if deactivated_profile and deactivated_profile.passwordHash:
        return SIGNUP_ACCOUNT_EXISTS

    if not rate_signup_reset_password(user, os.environ.get('HTTP_X_FORWARDED_FOR', None)):
        return SIGNUP_RATE_LIMITED

    timestamp = now() + 5 * 24 * 3600
    data = dict(n=name, e=email, t=timestamp, a="registration", c=cont)
    data["d"] = calculate_secure_url_digest(data)
    data = encrypt(user, json.dumps(data))
    link = '%s/setpassword?%s' % (get_server_settings().baseUrl, urllib.urlencode((("email", email), ("data", base64.encodestring(data)),)))
    vars_ = dict(link=link, name=name, site=get_server_settings().baseUrl)
    body = render("signup", [DEFAULT_LANGUAGE], vars_)
    html = render("signup_html", [DEFAULT_LANGUAGE], vars_)
    logging.info("Sending message to %s\n%s" % (email, body))
    settings = get_server_settings()

    msg = MIMEMultipart('alternative')
    msg['Subject'] = "Rogerthat registration"
    msg['From'] = settings.senderEmail if app.type == App.APP_TYPE_ROGERTHAT else ("%s <%s>" % (app.name, app.dashboard_email_address))
    msg['To'] = email
    msg.attach(MIMEText(body.encode('utf-8'), 'plain', 'utf-8'))
    msg.attach(MIMEText(html.encode('utf-8'), 'html', 'utf-8'))
    send_mail_via_mime(settings.senderEmail, email, msg)

    return SIGNUP_SUCCESS
Ejemplo n.º 3
0
def signup(name, email, cont):
    if not name or not name.strip():
        return SIGNUP_INVALID_NAME

    if not email or not email.strip():
        return SIGNUP_INVALID_EMAIL

    if not EMAIL_REGEX.match(email):
        return SIGNUP_INVALID_EMAIL

    default_app = app.get_default_app()
    if default_app.user_regex:
        regexes = default_app.user_regex.splitlines()
        for regex in regexes:
            if re.match(regex, email):
                break
        else:
            return SIGNUP_INVALID_EMAIL_DOMAIN

    app_user_email = create_app_user_by_email(email).email()

    user = users.User(app_user_email)
    profile = get_service_or_user_profile(user)
    if profile and profile.passwordHash:
        return SIGNUP_ACCOUNT_EXISTS

    deactivated_profile = get_deactivated_user_profile(user)
    if deactivated_profile and deactivated_profile.passwordHash:
        return SIGNUP_ACCOUNT_EXISTS

    if not rate_signup_reset_password(user, os.environ.get('HTTP_X_FORWARDED_FOR', None)):
        return SIGNUP_RATE_LIMITED

    timestamp = now() + 5 * 24 * 3600
    data = dict(n=name, e=app_user_email, t=timestamp, a="registration", c=cont)
    data["d"] = calculate_secure_url_digest(data)
    data = encrypt(user, json.dumps(data))
    link = '%s/setpassword?%s' % (get_server_settings().baseUrl,
                                  urllib.urlencode((("email", app_user_email), ("data", base64.encodestring(data)),)))
    vars_ = dict(link=link, name=name, site=get_server_settings().baseUrl)
    body = render("signup", [DEFAULT_LANGUAGE], vars_)
    html = render("signup_html", [DEFAULT_LANGUAGE], vars_)
    logging.info("Sending message to %s\n%s" % (email, body))
    settings = get_server_settings()

    from_ = settings.senderEmail if default_app.type == App.APP_TYPE_ROGERTHAT else ("%s <%s>" % (default_app.name, default_app.dashboard_email_address))
    send_mail(from_, email, "Rogerthat registration", body, html=html)

    return SIGNUP_SUCCESS
Ejemplo n.º 4
0
 def get(self):
     from rogerthat.settings import get_server_settings
     key_name = self.request.get("key_name")
     template = QRTemplate.get_by_key_name(key_name)
     self.response.headers['Content-Type'] = "image/png"
     url = "%s/" % get_server_settings().baseUrl
     self.response.out.write(qrcode(url, template.blob, map(int, template.body_color), False))
Ejemplo n.º 5
0
    def get(self):
        customer_id = int(self.request.get("customer_id"))
        try:
            customer = Customer.get_by_id(customer_id)
        except CustomerNotFoundException:
            self.abort(404)

        current_user = users.get_current_user()
        current_customer = get_customer(current_user)
        sln_settings = get_solution_settings(current_user)
        if not sln_settings.can_edit_services(
                current_customer) or not current_customer.can_edit_service(
                    customer):
            logging.warn(
                'Service or user %s is trying to login to the dashboard of %s',
                current_user.email(), customer.name)
            self.abort(401)

        service_identity_user = create_service_identity_user(
            users.User(customer.service_email))
        current_session = users.get_current_session()
        new_secret, new_session = create_session(service_identity_user,
                                                 ignore_expiration=True)
        set_cookie(self.response,
                   get_server_settings().cookieSessionName, new_secret)
        new_session = switch_to_service_identity(new_session,
                                                 service_identity_user,
                                                 read_only=False,
                                                 shop=current_session.shop,
                                                 layout_only=True)
        new_session.parent_session_secret = current_session.secret
        new_session.put()
        self.redirect("/")
Ejemplo n.º 6
0
def reset_password(email, sender_name=u'Rogerthat', set_password_route=None):
    # XXX: Validating the email would be an improvement

    user = users.User(email)
    profile_info = get_profile_info(user)
    if not profile_info:
        return False

    if not rate_signup_reset_password(user, os.environ.get('HTTP_X_FORWARDED_FOR', None)):
        return False

    request = GenericRESTRequestHandler.getCurrentRequest()
    language = get_languages_from_request(request)[0]

    settings = get_server_settings()
    if not set_password_route:
        set_password_route = '/setpassword'
    action = localize(language, "password reset")
    name = profile_info.name
    url_params = get_reset_password_url_params(name, user, action)
    link = '%s%s?%s' % (settings.baseUrl, set_password_route, url_params)

    # The use of the variable "key" is to let it pass the unittests
    key = 'app_website_' + sender_name.lower()
    sender_website = localize(language, key)
    key = sender_name
    sender_name = localize(language, key)

    vars_ = dict(link=link, name=name, app_name=sender_name, app_website=sender_website, language=language)
    body = JINJA_ENVIRONMENT.get_template('generic/reset_password.tmpl').render(vars_)
    html = JINJA_ENVIRONMENT.get_template('generic/reset_password_html.tmpl').render(vars_)
    logging.info("Sending message to %s\n%s" % (email, body))
    subject = u' - '.join((sender_name, action))
    utils.send_mail(settings.senderEmail2ToBeReplaced, email, subject, body, html=html)
    return True
Ejemplo n.º 7
0
def _finishup_mobile_registration_step2(mobile_key, invitor_code, invitor_secret, ipaddress, majorVersion,
                                        minorVersion):
    mobile = db.get(mobile_key)
    mobile_user = mobile.user
    server_settings = get_server_settings()

    def trans():  # Operates on 2 entity groups
        hookup_with_default_services.schedule(mobile_user, ipaddress)
        deferred.defer(sync_payment_database, mobile_user, _transactional=True)

        if invitor_code and invitor_secret:
            pp = ProfilePointer.get(invitor_code)
            if not pp:
                logging.error("User with userCode %s not found!" % invitor_code)
            else:
                deferred.defer(ack_invitation_by_invitation_secret, mobile_user, pp.user, invitor_secret,
                               _transactional=True, _countdown=10)

        elif invitor_code:
            for ysaaa_hash, static_email in chunks(server_settings.ysaaaMapping, 2):
                if invitor_code == ysaaa_hash:
                    service_user = users.User(static_email)
                    makeFriends(service_user, mobile_user, original_invitee=None, servicetag=None, origin=ORIGIN_YSAAA)
                    break
            else:
                azzert(False, u"ysaaa registration received but not found mapping")

        for _, static_email in chunks(server_settings.staticPinCodes, 2):
            if mobile_user.email() == static_email:
                break
        else:
            deferred.defer(send_messages_after_registration, mobile_key, _transactional=True)

    xg_on = db.create_transaction_options(xg=True)
    db.run_in_transaction_options(xg_on, trans)
Ejemplo n.º 8
0
    def get(self):
        settings = get_server_settings()
        secret = self.request.headers.get("X-Nuntiuz-Secret", None)
        if secret != settings.jabberSecret:
            logging.error("Received unauthenticated apple certificate request, ignoring ...")
            return
        app_id = self.request.get("id")
        if not app_id:
            return
        app = get_app_by_id(app_id)
        if not app:
            return

        if app.apple_push_cert_valid_until < now() + 30 * DAY:
            send_mail(settings.dashboardEmail,
                      settings.supportWorkers,
                      "The APN cert of %s is about to expire" % app_id,
                      "The APN cert of %s is valid until %s GMT" % (app_id, time.ctime(app.apple_push_cert_valid_until)))
        if app.apple_push_cert_valid_until < now() + 15 * DAY:
            logging.error("The APN cert of %s is valid until %s GMT" % (app_id, time.ctime(app.apple_push_cert_valid_until)))

        result = json.dumps(dict(cert=app.apple_push_cert, key=app.apple_push_key, valid_until=app.apple_push_cert_valid_until))
        self.response.headers['Content-Type'] = 'application/binary'
        _, data = encrypt_for_jabber_cloud(secret, result)
        self.response.write(data)
Ejemplo n.º 9
0
    def post(self):
        settings_yaml = self.request.get("settings", "")

        try:
            settings = populate_model_by_yaml(get_server_settings(), settings_yaml)
            # validate jabberEndPoints
            for jabber_endpoint in settings.jabberEndPoints:
                parts = jabber_endpoint.split(":")
                bizz_check(len(parts) == 2, 'Invalid jabber endpoint: %s' % jabber_endpoint)
                ip = [int(p) for p in parts[0].split('.')]
                bizz_check(len(ip) == 4, 'Invalid jabber endpoint IP: %s' % ip)

            # validate srvEndPoints
            for srv_record in settings.srvEndPoints:
                parts = srv_record.split(":")
                bizz_check(len(parts) == 3, 'Invalid SRV record: %s' % srv_record)
                ip = [int(p) for p in parts[0].split('.')]
                bizz_check(len(ip) == 4, "Invalid IP '%s' in SRV record: %s" % (ip, srv_record))

            # sort srvEndPoints by priority
            settings.srvEndPoints = sorted(settings.srvEndPoints, key=lambda x: x.split(':')[2])

            if settings.dkimPrivateKey:
                # dkimPrivateKey dos2unix
                settings.dkimPrivateKey = settings.dkimPrivateKey.replace("\r", "")

            settings.put()

            result = "Settings saved successfully!"
        except Exception, e:
            if not isinstance(e, BusinessException):
                logging.exception('Error happened while updating setting')
            result = 'ERROR: %s' % e.message
def get_pending_payment_details(app_user, transaction_id):
    ppr = PaymentPendingReceive.create_key(transaction_id).get()
    user_profile = get_user_profile(ppr.app_user)
    payment_provider = _validate_transaction_call(ppr, app_user, validate_started=False)
    provider_module = get_api_module(ppr.provider_id)

    status = PaymentPendingReceive.STATUS_SCANNED
    payment_user = get_payment_user(app_user)
    enabled = payment_user is not None and payment_user.has_provider(payment_provider.id)
    base_url = get_server_settings().baseUrl
    provider = AppPaymentProviderTO.from_model(base_url, payment_provider, enabled, app_user)

    assets = []
    if payment_user and payment_user.has_provider(ppr.provider_id):
        assets = provider_module.get_payment_assets(app_user, ppr.currency)
    receiver = UserDetailsTO.fromUserProfile(user_profile)
    receiver_asset = provider_module.get_payment_asset(ppr.app_user, ppr.asset_id)

    ppr.status = status
    ppr.pay_user = app_user
    if ppr.precision is None:
        ppr.precision = 2

    ppr.put()

    deferred.defer(send_update_payment_status_request, ppr)
    return PendingPaymentDetailsTO(status, transaction_id, provider, assets, receiver, receiver_asset, ppr.currency,
                                   ppr.amount, ppr.memo, ppr.timestamp, ppr.precision)
def job():
    settings = get_server_settings()
    customers = list(Customer.all().fetch(10))
    customers_per_app = defaultdict(list)
    for customer in customers:
        customers_per_app[customer.default_app_id].append(customer)
    book = xlwt.Workbook(encoding='utf-8')
    for app_id in customers_per_app:
        if not app_id:
            continue
        sheet = book.add_sheet(app_id)
        row = 0
        sheet.write(row, 0, 'Customer name')
        for customer in customers_per_app[app_id]:
            row += 1
            url = '%s/internal/shop/login_as?customer_id=%d' % (settings.baseUrl, customer.id)
            sheet.write(row, 0, xlwt.Formula('HYPERLINK("%s";"%s")' % (url, customer.name.replace('"', ''))))

    excel = StringIO()
    book.save(excel)
    excel_string = excel.getvalue()

    current_date = format_datetime(datetime.datetime.now(), locale=DEFAULT_LANGUAGE)
    to_emails = [u'*****@*****.**', u'*****@*****.**', u'*****@*****.**']
    
    attachments = []
    attachments.append(('Customers %s.xls' % current_date,
                        base64.b64encode(excel_string)))

    subject = 'List of all customers'
    message = 'See attachment.'
    send_mail('*****@*****.**', to_emails, subject, message, attachments=attachments)
def report_on_site_payments():
    one_month_ago = datetime.datetime.today() - relativedelta(months=1)
    min_date = int(time.mktime(one_month_ago.timetuple()))

    invoices = Invoice.all().filter('payment_type',
                                    Invoice.PAYMENT_ON_SITE).filter(
                                        'date >=', min_date)
    charges = Charge.get((i.charge_key for i in invoices))
    customers = Customer.get((i.order_key.parent() for i in invoices))

    l = [
        '[%(date_str)s][%(customer)s][%(manager)s][%(amount).02f][%(charge_number)s]'
        % dict(customer=c.name,
               manager=i.operator,
               amount=i.amount / 100.0,
               date=i.date,
               date_str=time.ctime(i.date),
               charge_number=charge.charge_number)
        for (i, charge, c) in sorted(zip(invoices, charges, customers),
                                     key=lambda t: t[0].date)
    ]
    body = "\n".join(l) or u"There were no on site payments for the last month"
    logging.info(body)

    server_settings = get_server_settings()
    solution_server_settings = get_solution_server_settings()
    subject = u'On site payments for the last month'
    send_mail(server_settings.dashboardEmail,
              solution_server_settings.shop_payment_admin_emails, subject,
              body)
Ejemplo n.º 13
0
    def get(self, app_id):
        if not app_id:
            self.error(404)
            return

        language = get_languages_from_header(self.request.headers.get('Accept-Language', None))
        logging.info("Getting info about app with id %s", app_id)
        app = get_app_by_id(app_id.lower().strip())
        if not app or not app.is_in_appstores():
            # Unknown app ===> Show sorry page
            logging.info("Unknown app ===> Show sorry page ")
            context = {'app_id': app_id}
            self.response.out.write(render('sorry_app_unknown', language, context, 'web'))
            return
        user_agent = self.request.environ.get('HTTP_USER_AGENT')
        if user_agent:
            supported_platform = get_platform_by_user_agent(user_agent)
            if supported_platform == "android":
                self.redirect(str(app.android_market_web_uri))
                return
            if supported_platform == "ios":
                self.redirect(str(app.ios_appstore_web_uri))
                return
        logging.info("Unsupported platform ===> Show sorry page with install instructions for android & iphone")
        context = {'payload': urllib.urlencode([("chl", "%s/A/%s" % (get_server_settings().baseUrl, app_id))]),
                   'app_name': app.name}
        self.response.out.write(render('sorry_app_not_supported', language, context, 'web'))
def init_env():
    from google.appengine.api import datastore_file_stub, apiproxy_stub_map, user_service_stub, mail_stub, urlfetch_stub
    from google.appengine.api.app_identity import app_identity_stub
    from google.appengine.api.blobstore import blobstore_stub, file_blob_storage
    from google.appengine.api.images import images_stub
    from google.appengine.api.memcache import memcache_stub
    from google.appengine.api.taskqueue import taskqueue_stub
    try:

        if not apiproxy_stub_map.apiproxy.GetStub('user'):
            apiproxy_stub_map.apiproxy.RegisterStub('user', user_service_stub.UserServiceStub())
            apiproxy_stub_map.apiproxy.RegisterStub('datastore_v3',
                                                    datastore_file_stub.DatastoreFileStub('mobicagecloud', '/dev/null',
                                                                                          '/dev/null'))
            apiproxy_stub_map.apiproxy.RegisterStub('mail', mail_stub.MailServiceStub())
            apiproxy_stub_map.apiproxy.RegisterStub('blobstore', blobstore_stub.BlobstoreServiceStub(
                file_blob_storage.FileBlobStorage('/dev/null', 'mobicagecloud')))
            apiproxy_stub_map.apiproxy.RegisterStub('memcache', memcache_stub.MemcacheServiceStub())
            apiproxy_stub_map.apiproxy.RegisterStub('images', images_stub.ImagesServiceStub())
            apiproxy_stub_map.apiproxy.RegisterStub('urlfetch', urlfetch_stub.URLFetchServiceStub())
            apiproxy_stub_map.apiproxy.RegisterStub('taskqueue', taskqueue_stub.TaskQueueServiceStub())
            apiproxy_stub_map.apiproxy.RegisterStub('app_identity_service', app_identity_stub.AppIdentityServiceStub())

    except Exception as e:
        print e
        raise

    from rogerthat.settings import get_server_settings

    settings = get_server_settings()
    settings.jabberDomain = "localhost"
Ejemplo n.º 15
0
def start_admin_debugging(app_user, timeout):
    mobiles = list(get_user_active_mobiles(app_user))
    azzert(len(mobiles) == 1)

    m = mobiles[0]
    if m.type in (Mobile.TYPE_IPHONE_HTTP_APNS_KICK,
                  Mobile.TYPE_IPHONE_HTTP_XMPP_KICK,
                  Mobile.TYPE_WINDOWS_PHONE) \
              or (m.is_android and m.pushId):
        # No XMPP account needs to be created
        settings = get_server_settings()
        jid = base64.b64encode(encrypt_value(md5(settings.secret), users.get_current_user().email().encode('utf8')))
        password = None
        type_ = CurrentlyForwardingLogs.TYPE_GAE_CHANNEL_API
    else:
        account = generate_account(u'dbg_%s' % uuid.uuid4().get_hex())
        if not APPSCALE:
            create_jabber_account(account, None)
        jid = account.account
        password = account.password
        type_ = CurrentlyForwardingLogs.TYPE_XMPP

    def trans():
        debug_request = StartDebuggingRequest(key=StartDebuggingRequest.create_key(app_user, jid),
                                              timestamp=now())
        db.put_async(debug_request)
        deferred.defer(stop_debugging, app_user, jid, debug_request=debug_request, notify_user=False,
                       _countdown=timeout * 60, _transactional=True, _queue=SCHEDULED_QUEUE)
        return start_log_forwarding(app_user, jid, xmpp_target_password=password, type_=type_)

    xg_on = db.create_transaction_options(xg=True)
    return db.run_in_transaction_options(xg_on, trans)
Ejemplo n.º 16
0
def _finishup_mobile_registration(mobile, invitor_code, invitor_secret, ipaddress, ms_key):
    mobile_user = mobile.user
    app_settings = get_app_settings(get_app_id_from_app_user(mobile_user))
    user_profile = get_user_profile(mobile_user)
    server_settings = get_server_settings()

    def trans():  # Operates on 2 entity groups
        email = get_human_user_from_app_user(mobile_user).email()
        for _, static_email in chunks(server_settings.staticPinCodes, 2):
            if email == static_email:
                break
        else:
            deferred.defer(send_welcome_message, mobile_user, _transactional=True, _countdown=5)

        mobile_settings = db.get(ms_key)
        request = UpdateSettingsRequestTO()
        request.settings = SettingsTO.fromDBSettings(app_settings, user_profile, mobile_settings)
        updateSettings(update_settings_response_handler, logError, mobile_user, request=request)

        deferred.defer(_finishup_mobile_registration_step2, mobile.key(), invitor_code, invitor_secret, ipaddress,
                       mobile_settings.majorVersion, mobile_settings.minorVersion, _transactional=True,
                       _queue=FAST_QUEUE)

    xg_on = db.create_transaction_options(xg=True)
    db.run_in_transaction_options(xg_on, trans)
Ejemplo n.º 17
0
def _generate_unassigned_qr_codes_excel_for_app(app_id, amount, user_email):
    logging.info('Generating %d qr code urls and saving to excel sheet' %
                 amount)
    qr_codes = generate_unassigned_short_urls(app_id, amount)

    solution_server_settings = get_solution_server_settings()

    book = xlwt.Workbook(encoding="utf-8")

    qr_sheet = book.add_sheet('qrcodes')
    base_url = get_server_settings().baseUrl
    for i, short_url in enumerate(qr_codes):
        qr_sheet.write(i, 0, short_url.qr_code_content_with_base_url(base_url))
    excel_file = StringIO()
    book.save(excel_file)
    current_date = format_date(datetime.date.today(), locale=DEFAULT_LANGUAGE)

    subject = 'Generated QR code links (%d) for %s' % (amount, str(app_id))
    from_email = solution_server_settings.shop_export_email
    to_emails = [user_email]
    body_text = 'See attachment for the requested links to the QR codes.'
    attachments = []
    attachments.append(('%s generated QR codes(%d) %s.xls' %
                        (str(app_id), amount, current_date),
                        base64.b64encode(excel_file.getvalue())))
    send_mail(from_email,
              to_emails,
              subject,
              body_text,
              attachments=attachments)
Ejemplo n.º 18
0
 def get(self):
     service_user = users.get_current_user()
     session_ = users.get_current_session()
     service_identity = session_.service_identity
     slide_id = self.request.get('i')
     if not slide_id:
         self.redirect("/ourcityapp")
         return
     slide_id = long(slide_id)
     if is_default_service_identity(service_identity):
         service_identity_user = service_user
     else:
         service_identity_user = create_service_identity_user(service_user, service_identity)
     def trans():
         slide = SolutionLoyaltySlide.get_by_id(slide_id, parent=parent_key_unsafe(service_identity_user, SOLUTION_COMMON))
         return slide
     slide = db.run_in_transaction(trans)
     if not slide:
         self.redirect("/ourcityapp")
         return
     server_settings = get_server_settings()
     jinja_template = JINJA_ENVIRONMENT.get_template('loyalty_preview.html')
     self.response.out.write(jinja_template.render({'slide_id': slide_id,
                                                    'full_url': slide.slide_url(),
                                                    'overlay_url': '%s/common/loyalty/slide/overlay' % (server_settings.baseUrl)}))
Ejemplo n.º 19
0
def _generate_unassigned_qr_codes_svgs_for_app(app_id, amount, user_email):
    logging.info('Generating %d qr code urls and rendering them as svg' %
                 amount)
    qr_codes = generate_unassigned_short_urls(app_id, amount)
    base_url = get_server_settings().baseUrl
    stream = StringIO()
    with zipfile.ZipFile(stream, 'w', zipfile.ZIP_DEFLATED) as zip_file:
        for i, short_url in enumerate(qr_codes):
            url = short_url.qr_code_content_with_base_url(base_url)
            qr_content = generate_qr_code(url, None, '000000', None, svg=True)
            zip_file.writestr('qr-%s.svg' % i, qr_content)
    stream.seek(0)
    zip_content = stream.getvalue()
    if DEBUG:
        from google.appengine.tools.devappserver2.python.stubs import FakeFile
        FakeFile.ALLOWED_MODES = frozenset(['a', 'r', 'w', 'rb', 'U', 'rU'])
        with open('qrcodes.zip', 'w') as f:
            f.write(zip_content)
    else:
        solution_server_settings = get_solution_server_settings()
        current_date = format_date(datetime.date.today(),
                                   locale=DEFAULT_LANGUAGE)
        subject = 'Generated QR code links (%d) for %s' % (amount, str(app_id))
        from_email = solution_server_settings.shop_export_email
        to_emails = [user_email]
        body_text = 'See attachment for the QR codes in SVG format.'
        attachments = []
        attachments.append(('%s generated QR codes(%d) %s.xls' %
                            (str(app_id), amount, current_date),
                            base64.b64encode(zip_content)))
        send_mail(from_email,
                  to_emails,
                  subject,
                  body_text,
                  attachments=attachments)
Ejemplo n.º 20
0
    def get(self):
        queries = {}
        for ns in NewsStream.query():
            ng_count = NewsGroup.list_by_app_id(ns.app_id).count(None)
            if ng_count == 0:
                continue

            ids = [i for i in xrange(1, 21)]
            ids.append(999)

            queries[ns.app_id] = [NewsSettingsService.list_setup_needed(ns.app_id, i).count_async(None) for i in ids]

        data = []
        for app_id in queries:
            setup_count = 0
            for qry in queries[app_id]:
                setup_count += qry.get_result()

            if setup_count > 0:
                data.append({'app_id': app_id, 'count': setup_count})

        if not data:
            return

        r = ''
        for d in sorted(data, key=lambda x:-x['count']):
            r += '%s: %s services\n' % (d['app_id'], d['count'])

        server_settings = get_server_settings()
        send_mail(server_settings.dashboardEmail,
                  server_settings.news_admin_emails,
                  'News group setup required',
                  'Setup services here: %s/mobiadmin/google/news\nThe following apps have unconnected news services:\n\n%s' % (server_settings.baseUrl, r))
Ejemplo n.º 21
0
def get_postback_url():
    if DEBUG:
        return "%s/api/1/callback" % get_server_settings().baseUrl
    elif APPSCALE:
        return "https://%s/api/1/callback" % os.environ['SERVER_NAME']
    else:
        return "https://%s.appspot.com/api/1/callback" % APPENGINE_APP_ID
 def _validate_hash(self, hash_to_be_validated, user_email, message):
     hash_ = hashlib.sha256()
     hash_.update(user_email)
     hash_.update(message)
     hash_.update(get_server_settings().jabberSecret)
     hash_ = hash_.hexdigest()
     return hash_to_be_validated == hash_
Ejemplo n.º 23
0
def _send_mail_via_mime(from_, to, mime):
    import smtplib
    from rogerthat.settings import get_server_settings
    settings = get_server_settings()

    mime_string = mime.as_string()
    logging.info("mime_string type: %s", type(mime_string))
    if DEBUG:
        logging.warn("Not sending real email via mime\n%s", mime_string)
        for part in mime.walk():
            logging.info("part.get_content_type(): %s", part.get_content_type())
            if part.get_content_type() == 'text/plain':
                logging.info(base64.b64decode(part.get_payload()))
        return
    if settings.dkimPrivateKey:
        if from_ == settings.dashboardEmail or "<%s>" % settings.dashboardEmail in from_:
            logging.info("Adding dkim signature")
            try:
                import dkim
                signature = dkim.sign(mime_string, 'dashboard.email', settings.dashboardEmail.split('@')[1], settings.dkimPrivateKey, include_headers=['To', 'From', 'Subject'])
                logging.info("signature type: %s", type(signature))
                mime_string = signature.encode('utf-8') + mime_string
            except:
                logging.exception("Could not create dkim signature!")
        else:
            logging.info("Skipping dkim signature because '%s' != '%s'", from_, settings.dashboardEmail)

    mailserver = smtplib.SMTP_SSL(settings.smtpserverHost, int(settings.smtpserverPort))
    mailserver.ehlo()
    mailserver.login(settings.smtpserverLogin, settings.smtpserverPassword)
    mailserver.sendmail(from_, to, mime_string)
    mailserver.quit()
Ejemplo n.º 24
0
def _execute_flow(service_identity_user, message_flow_design, message_flow_run_record, members, message_parent_key,
                  context=None, resultKey=None, force_language=None, broadcast_type=None, broadcast_guid=None, tag=None):
    logging.info("Executing message flow for %s with force_language %s" %
                 (service_identity_user.email(), force_language))

    xml_doc = _create_message_flow_run_xml_doc(service_identity_user, message_flow_design, message_flow_run_record,
                                               members, force_language)
    logging.info(xml_doc.toxml())
    xml = xml_doc.toxml("utf-8")

    settings = get_server_settings()

    headers = {
        'X-Nuntiuz-Service-Identifier-Key': get_mfr_sik(service_identity_user).sik,
        'X-Nuntiuz-Service-Identity': base64.b64encode(get_identity_from_service_identity_user(service_identity_user)),
        'X-Nuntiuz-Service-API-Key': get_mfr_api_key(service_identity_user).key().name(),
        'X-Nuntiuz-Shared-Secret': settings.secret,
        'X-Nuntiuz-MessageFlowRunId': message_flow_run_record.messageFlowRunId,
        'X-Nuntiuz-MessageParentKey': message_parent_key if message_parent_key else "",
        'X-Nuntiuz-Context': context if context else "",
        'X-Nuntiuz-ResultKey': resultKey,
        'Content-type': 'text/xml'
    }

    if broadcast_guid:
        headers['X-Nuntiuz-BroadcastGuid'] = broadcast_guid
    if tag:
        headers['X-Nuntiuz-Tag'] = tag.encode('utf')

    logging.debug('Executing flow with run id %s for members %s', message_flow_run_record.messageFlowRunId, members)
    result = urlfetch.fetch(
        "%s/api" % settings.messageFlowRunnerAddress, xml, "POST", headers, False, False, deadline=10 * 60)
    if result.status_code != 200:
        error_message = "MFR returned status code %d" % result.status_code
        if result.status_code == 400:
            logging.error(error_message, _suppress=False)
            raise deferred.PermanentTaskFailure(error_message)
        raise Exception(error_message)

    logging.debug('MFR response: %s', result.content)
    if len(members) == 1 and result.content:
        try:
            flow_start_result = parse_complex_value(FlowStartResultCallbackResultTO, json.loads(result.content), False)
            if isinstance(flow_start_result.value, MessageCallbackResultTypeTO):
                sendMessage(service_identity_user, [UserMemberTO(members[0], flow_start_result.value.alert_flags)],
                            flow_start_result.value.flags, 0, message_parent_key, flow_start_result.value.message,
                            flow_start_result.value.answers, None, flow_start_result.value.branding,
                            flow_start_result.value.tag, flow_start_result.value.dismiss_button_ui_flags, context,
                            key=resultKey, is_mfr=True, broadcast_type=broadcast_type, broadcast_guid=broadcast_guid,
                            attachments=flow_start_result.value.attachments,
                            step_id=None if flow_start_result.value.step_id is MISSING else flow_start_result.value.step_id)
            elif isinstance(flow_start_result.value, FormCallbackResultTypeTO):
                sendForm(service_identity_user, message_parent_key, members[0], flow_start_result.value.message,
                         flow_start_result.value.form, flow_start_result.value.flags, flow_start_result.value.branding,
                         flow_start_result.value.tag, flow_start_result.value.alert_flags, context, key=resultKey,
                         is_mfr=True, broadcast_type=broadcast_type, attachments=flow_start_result.value.attachments,
                         broadcast_guid=broadcast_guid,
                         step_id=None if flow_start_result.value.step_id is MISSING else flow_start_result.value.step_id)
        except:
            logging.exception("Failed to parse result from message flow runner.")
Ejemplo n.º 25
0
    def _handle_service_interaction_based_invitation(self, entry_point, code, language, su, user_agent, version):
        match = re.match("^/q/s/(.+)/(\\d+)$", su.full)
        if not match:
            self.response.out.write(_TMPL % su.full)
        else:
            user_code = match.group(1)

            service_profile = get_service_profile_via_user_code(user_code)

            if not service_profile:  # Profile of usercode not found. Sorry we can't help you :(
                logging.info("Profile of usercode not found. Sorry we can't help you :(")
                self.response.out.write(_TMPL % "/")
                return

            service_user = service_profile.user

            if entry_point == "S":  # It's a QR-Code
                logging.info("It's a QR-Code")
                sid = int(match.group(2))
                service_interaction_def = get_service_interaction_def(service_user, int(sid))
                service_identity_user = create_service_identity_user(service_user, service_interaction_def.service_identity)
                service_identity = get_service_identity(service_identity_user)
                self._handle_after_qr_code_scan(user_agent, service_identity, language, sid)
                return

            if entry_point == "M":
                logging.info("It's a Message")
            else:
                logging.warn("Expected entry_point 'M' but got '%s'." % entry_point)

            # Get service identity by sid
            sid = int(match.group(2))
            service_interaction_def = get_service_interaction_def(service_user, int(sid))
            service_identity_user = create_service_identity_user(service_user, service_interaction_def.service_identity)
            service_identity = get_service_identity(service_identity_user)
            supported_platform = determine_if_platform_supports_rogerthat_by_user_agent(user_agent)
            if (version and version == "web") or not supported_platform:
                logging.info("Unsupported platform ===> Show sorry page with install instructions for android & iphone")

                variables = {'profile': service_identity,
                             'payload': urllib.urlencode([("chl", "%s/S/%s" % (get_server_settings().baseUrl, code))]),
                             'app_name': get_app_name_by_id(service_identity.app_id)}
                # Unsupported platform ===> Show sorry page with install instructions for android & iphone
                self.response.out.write(render('sorry_step2', language, variables, 'web'))
            else:
                fallback_url = "%s/%s/%s?v=web" % (get_server_settings().baseUrl, entry_point, code)
                redirect_to_app(self, user_agent.lower(), service_identity.app_id, su.full, fallback_url)
Ejemplo n.º 26
0
def add_new_beacon_discovery(app_user, beacon_uuid, beacon_name):
    '''A human user has discovered a beacon'''
    beacon_uuid = beacon_uuid.lower()
    beacon = get_beacon(beacon_uuid, beacon_name)
    if not beacon:
        logging.warn("Beacon not found: {uuid: %s, name: %s}", beacon_uuid, beacon_name)
        return None, None

    keyy = BeaconDiscovery.create_key(app_user, beacon.service_identity_user)
    beaconDiscovery = BeaconDiscovery.get(keyy)
    if beaconDiscovery:
        return beacon.service_identity_user, beacon.tag

    BeaconDiscovery(key=keyy, creation_time=now()).put()

    si = get_service_identity(beacon.service_identity_user)
    if not si:
        logging.error("Service identity not found for service_identity_user: %s" % beacon.service_identity_user)
    elif not si.beacon_auto_invite:
        logging.info("Beacon detected but %s does not allow auto invite {user %s, uuid: %s, name: %s}",
                     beacon.service_identity_user, app_user, beacon_uuid, beacon_name)
    elif get_app_id_from_app_user(app_user) not in si.appIds:
        logging.info("Beacon detected but %s does not contain app_id of user %s: {uuid: %s, name: %s}",
                     beacon.service_identity_user, app_user, beacon_uuid, beacon_name)
        return None, None
    elif get_friend_serviceidentity_connection(app_user, beacon.service_identity_user):
        logging.info("Beacon detected but %s and %s were already friends: {uuid: %s, name: %s}",
                     app_user, beacon.service_identity_user, beacon_uuid, beacon_name)
    else:
        # Fake a deleted connection between service and human user to be able to show the service's avatar on the phone
        friend_map = get_friends_map(app_user)
        friend_map.generation += 1
        friend_map.version += 1
        friend_map.put()

        friend_detail = FriendDetails().addNew(remove_slash_default(beacon.service_identity_user),
                                               si.name, si.avatarId, type_=FriendDetail.TYPE_SERVICE)
        friend_detail.relationVersion = -1

        from rogerthat.bizz.job.update_friends import _do_update_friend_request
        _do_update_friend_request(app_user, friend_detail, UpdateFriendRequestTO.STATUS_ADD, friend_map,
                                  extra_conversion_kwargs=dict(existence=FriendTO.FRIEND_EXISTENCE_DELETED,
                                                               includeServiceDetails=False))

        user_profile = get_user_profile(app_user)
        app_name = get_app_name_by_id(user_profile.app_id)
        m = localize(user_profile.language, "_automatic_detection_invitation", app_name=app_name, service_name=si.name)
        _send_invitation_message_from_service_to_user(app_user, beacon.service_identity_user, m,
                                                      si.descriptionBranding, user_profile.language,
                                                      ORIGIN_BEACON_DETECTED, BEACON_DISCOVERY)

        message = "Beacon detected at %s (%s) with tag: %s" % (si.name.encode('utf8'),
                                                               remove_slash_default(si.service_identity_user).email().encode('utf8'),
                                                               beacon.tag.encode('utf8'))

        server_settings = get_server_settings()
        xmpp.send_message(server_settings.xmppInfoMembers, message, message_type=xmpp.MESSAGE_TYPE_CHAT)

    return beacon.service_identity_user, beacon.tag
Ejemplo n.º 27
0
 def get(self):
     server_settings = get_server_settings()
     cookie = self.request.cookies.get(server_settings.cookieQRScanName)
     if cookie:
         scan_url = parse_cookie(cookie)
         self.redirect(scan_url)
     else:
         self.redirect("rogerthat://i/qr?success=false")
Ejemplo n.º 28
0
def get(news_id, service_identity=None, include_statistics=False):
    t = time.time()
    service_user = users.get_current_user()
    service_identity_user = get_and_validate_service_identity_user(service_user, service_identity)
    news_item = news.get_news(service_identity_user, news_id)
    logging.info('Fetching news item took %s', time.time() - t)
    statistics = get_news_items_statistics([news_item], True).get(news_id) if include_statistics else None
    return NewsItemTO.from_model(news_item, get_server_settings().baseUrl, statistics)
Ejemplo n.º 29
0
def api_create_payment_provider(data):
    """
    Args:
        data (CreatePaymentProviderTO)
    """
    payment_provider = create_payment_provider(data)
    base_url = get_server_settings().baseUrl
    return PaymentProviderTO.from_model(base_url, payment_provider)
Ejemplo n.º 30
0
 def slide_url(self):
     from rogerthat.settings import get_server_settings
     server_settings = get_server_settings()
     if self.gcs_filename:
         return get_serving_url(self.gcs_filename)
     return unicode("%s/unauthenticated/loyalty/slide?%s" %
                    (server_settings.baseUrl,
                     urllib.urlencode(dict(slide_key=self.item.key()))))
Ejemplo n.º 31
0
def get_serving_url(filename):
    """
    Args:
        filename (unicode)
    """
    if DEBUG:
        return '%s/_ah/gcs%s' % (get_server_settings().baseUrl, filename)
    return 'https://storage.googleapis.com%s' % filename
Ejemplo n.º 32
0
    def post(self):
        data = json.loads(self.request.body)
        jid = data['jid']
        message = data['message']

        settings = get_server_settings()
        app_user_email = decrypt_value(md5(settings.secret), base64.b64decode(jid))
        forward_log(users.User(app_user_email), message)
Ejemplo n.º 33
0
    def post(self):
        settings = get_server_settings()
        secret = self.request.headers.get("X-Nuntiuz-Secret", None)
        if secret != settings.jabberSecret:
            logging.error(u"Received unauthenticated callback response, ignoring ...")
            return
        sik = self.request.headers.get("X-Nuntiuz-Service-Key", None)
        if not sik:
            logging.error(u"Received invalid Callback response without Service Identifier Key")
            return
        sik = get_sik(sik)
        if not sik:
            logging.error("Received invalid Callback response with unknown Service Identifier Key:\nsik: %s\nbody:\n%s" % (
                self.request.headers.get("X-Nuntiuz-Service-Key", None), self.request.body))
            return
        users.set_user(sik.user)

        raw_result = self.request.body
        try:
            from google.appengine.api.memcache import get  # @UnresolvedImport
            if get(sik.user.email() + "_interactive_logs"):
                content_type = self.request.headers.get('content-type', 'unknown')
                status = self.request.headers.get('X-Nuntiuz-Service-Status', 'unknown')
                if status == "600":
                    status = "unknown"
                result_url = self.request.headers.get('X-Nuntiuz-Service-Result-Url', 'unknown')
                channel.send_message(sik.user, "rogerthat.service.interactive_logs", content_type=content_type,
                                     status=status, result_url=result_url, body=raw_result.decode('utf-8', 'replace'))
        except:
            logging.exception("Error during interactive logging.")

        try:
            result = json.loads(raw_result)
        except Exception:
            raw_result_unicode = raw_result.decode('utf-8', 'replace')
            logging.warning(u"Could not parse request body as JSON!\n" + raw_result_unicode)
            error_code = ERROR_CODE_INVALID_JSON
            error_message = u"The JSON_RPC response could not be parsed as a valid JSON."

            log_service_activity(sik.user, str(time.time()), ServiceLog.TYPE_CALLBACK, ServiceLog.STATUS_ERROR,
                                 None, None, raw_result_unicode, error_code, error_message)
            return
        raw_result_unicode = json.dumps(privatize(deepcopy(result)), ensure_ascii=False)
        logging.info(u"Incoming call back response:\n" + raw_result_unicode)

        if not result:
            error_code = ERROR_CODE_INVALID_JSON
            error_message = u"The JSON_RPC response could not be parsed as a valid json."
            log_service_activity(sik.user, None, ServiceLog.TYPE_CALLBACK, ServiceLog.STATUS_ERROR, None, None,
                                 raw_result_unicode, error_code, error_message)
            return

        from rogerthat.dal import parent_key
        service_api_callback = ServiceAPICallback.get_by_key_name(result["id"], parent=parent_key(sik.user))
        if not service_api_callback:
            logging.warning(u"Service api call back response record not found !")
            return
        _process_callback_result(sik, result, raw_result_unicode, service_api_callback, True)
Ejemplo n.º 34
0
def submit_service_api_callback(profile, service_api_callback, effective_kwargs=None, function=None, synchronous=False,
                                call_dict=None, response_type=None):
    if service_api_callback.is_solution and not service_api_callback.targetMFR:
        result = _send_to_solution_context(service_api_callback, profile, effective_kwargs, function, synchronous,
                                           call_dict, response_type)
        if synchronous:
            return result
        return True, None

    from rogerthat.bizz.service import get_mfr_sik
    settings = get_server_settings()
    mfr_uri = "%s/callback_api" % settings.messageFlowRunnerAddress
    mobidick_uri = "%s/callback_api" % settings.mobidickAddress
    if service_api_callback.targetMFR:
        sik = get_mfr_sik(profile.user).sik
        uri = mfr_uri
    else:
        if profile.callBackURI == "mobidick":
            uri = mobidick_uri
        else:
            uri = profile.callBackURI
        sik = profile.sik

        callback_name = call_dict.get("callback_name") if call_dict else None
        tag = call_dict.get("params", dict()).get("tag") if call_dict else None
        logging.debug("callback_name: '%s' and tag: '%s'", callback_name, tag)
        if callback_name:
            scc = ServiceCallBackConfiguration.get(
                ServiceCallBackConfiguration.create_key(callback_name, profile.service_user))
            if scc:
                uri = scc.callBackURI
        elif tag:
            try:
                from rogerthat.dal.service import get_regex_callback_configurations_cached
                regex_callbacks = []
                for scc in get_regex_callback_configurations_cached(profile.service_user):
                    if re.match(scc.regex, tag):
                        regex_callbacks.append(scc)
                        logging.debug("Matched callback (%s) with regex '%s' to tag '%s'", scc.name, scc.regex, tag)
                    else:
                        logging.debug(
                            "Could NOT match callback (%s) with regex '%s' to tag '%s'", scc.name, scc.regex, tag)
                if regex_callbacks:
                    regex_callback = random.choice(regex_callbacks)
                    uri = regex_callback.callBackURI
            except:
                logging.warn("Failed to submit to regex callback with tag '%s'" % tag, exc_info=True)

    logging.info("Sending callback to %s, identifying through sik %s @ %s:\n%s"
                 % (profile.user, sik, uri, service_api_callback.call))
    if not (uri and sik):
        logging.error("Not enough information present to send the call!")
        return False, "Not enough information present to send the call!"

    result = send_service_api_callback(service_api_callback, profile.sik, uri, synchronous)
    if synchronous:
        return result
    return True, None
Ejemplo n.º 35
0
    def get(self):
        if self.request.headers.get(
                'X-Rogerthat-Secret') != get_server_settings().secret:
            self.abort(401)

        products = export_products()

        self.response.headers['Content-Type'] = 'text/json'
        self.response.write(json.dumps(products))
def id_to_email(id_):
    from rogerthat.settings import get_server_settings
    from rogerthat.utils.crypto import sha256, decrypt_value
    from mcfw.serialization import ds_str

    settings = get_server_settings()
    stream = StringIO(base64.decodestring(id_))
    tmp_secret = ds_str(stream)
    return decrypt_value(sha256("%s%s" % (tmp_secret, settings.secret.encode("utf-8"))), ds_str(stream)).decode('utf-8')
Ejemplo n.º 37
0
 def get(self):
     user = users.get_current_user()
     template_id = self.request.get("template_id")
     logging.info(template_id)
     template = QRTemplate.get_by_id(int(template_id[2:], 16), parent_key(user))
     self.response.headers['Content-Type'] = "image/png"
     logging.info(map(int, template.body_color))
     url = "%s/" % get_server_settings().baseUrl
     self.response.out.write(qrcode(url, template.blob, map(int, template.body_color), True))
Ejemplo n.º 38
0
    def get(self):
        settings = get_server_settings()
        secret = self.request.headers.get("X-Nuntiuz-Secret", None)
        if secret != settings.jabberSecret:
            logging.error("Received unauthenticated apple feedback response, ignoring ...")
            return

        device = self.request.get('device')
        obsolete_apple_push_device_token(device)
Ejemplo n.º 39
0
def get_twitter_auth_url(service_user):
    server_settings = get_server_settings()
    solution_server_settings = get_solution_server_settings()
    app_key = str(solution_server_settings.twitter_app_key)
    app_secret = str(solution_server_settings.twitter_app_secret)
    client = oauth.TwitterClient(
        app_key, app_secret, CALLBACK_URL %
        (server_settings.baseUrl, urlencode({"s": service_user.email()})))
    auth_url = client.get_authorization_url()
    return auth_url
Ejemplo n.º 40
0
def reset():
    db.delete(RogerthatBackendErrors.get_key())
    db.delete(list(get_monitored_services_in_trouble_qry()))
    server_settings = get_server_settings()
    send_to = server_settings.supportWorkers
    send_to.append(server_settings.dashboardEmail)
    send_message(map(users.User, send_to),
                 'shop.monitoring.reset',
                 skip_dashboard_user=False)
    return RETURNSTATUS_TO_SUCCESS
Ejemplo n.º 41
0
    def get(self):
        if self.request.headers.get(
                'X-Rogerthat-Secret') != get_server_settings().secret:
            self.abort(401)

        year = int(self.request.GET['year'])
        month = int(self.request.GET['month'])

        invoices = export_invoices(year, month)

        self.response.headers['Content-Type'] = 'text/json'
        self.response.write(json.dumps(invoices))
Ejemplo n.º 42
0
def find_prospects(app_id,
                   postal_codes,
                   sw_lat,
                   sw_lon,
                   ne_lat,
                   ne_lon,
                   city_name,
                   check_phone_number,
                   radius=200):
    logging.info(
        'Finding prospects for %s',
        dict(app_id=app_id,
             postal_codes=postal_codes,
             south_west=(sw_lat, sw_lon),
             north_east=(ne_lat, ne_lon),
             check_phone_number=check_phone_number,
             city_name=city_name))

    google_maps_key = get_server_settings().googleMapsKey
    shop_app_key = ShopApp.create_key(app_id)

    def trans():
        app = get_app_by_id(app_id)
        azzert(app)

        shop_app = db.get(shop_app_key)
        if not shop_app:
            shop_app = ShopApp(key=shop_app_key)

        shop_app.name = app.name
        shop_app.searched_south_west_bounds.append(db.GeoPt(sw_lat, sw_lon))
        shop_app.searched_north_east_bounds.append(db.GeoPt(ne_lat, ne_lon))
        shop_app.postal_codes = postal_codes
        shop_app.put()

        deferred.defer(_start_building_grid,
                       google_maps_key,
                       app_id,
                       postal_codes,
                       radius,
                       sw_lat,
                       sw_lon,
                       ne_lat,
                       ne_lon,
                       city_name,
                       check_phone_number,
                       _transactional=True,
                       _queue=HIGH_LOAD_WORKER_QUEUE)

    xg_on = db.create_transaction_options(xg=True)
    db.run_in_transaction_options(xg_on, trans)
Ejemplo n.º 43
0
    def get(self):
        service_user_email = self.request.get("s")
        service_user = users.User(service_user_email)
        denied = self.request.get("denied")
        if denied:
            self.response.out.write("""<html>
    <body><h1>Twitter login cancelled, you can close this window now.</h1></body>
    <script>self.close();</script>
</html>""")
        else:
            oauth_token = self.request.get("oauth_token")
            oauth_verifier = self.request.get("oauth_verifier")

            logging.info("service_user_email: %s", service_user_email)
            logging.info("oauth_token: %s", oauth_token)
            logging.info("oauth_verifier: %s", oauth_verifier)

            key = AuthToken.key_name(oauth.TWITTER, oauth_token)
            auth_model = AuthToken.get_by_key_name(key)
            if auth_model:
                server_settings = get_server_settings()
                solution_server_settings = get_solution_server_settings()
                client = oauth.TwitterClient(
                    solution_server_settings.twitter_app_key,
                    solution_server_settings.twitter_app_secret,
                    CALLBACK_URL % (server_settings.baseUrl,
                                    urlencode({"s": service_user.email()})))

                r = client.get_user_info(oauth_token, oauth_verifier, True)

                sln_settings = get_solution_settings(service_user)
                if sln_settings:
                    sln_settings.twitter_oauth_token = r["token"]
                    sln_settings.twitter_oauth_token_secret = r["secret"]
                    sln_settings.twitter_username = r["username"]
                    put_and_invalidate_cache(sln_settings)
                    send_message(service_user,
                                 u"solutions.common.twitter.updated",
                                 username=r["username"])

            self.response.out.write("""<html>
    <body><h1>witter login success, you can close this window now.</h1></body>
    <script>self.close();</script>
</html>""")
Ejemplo n.º 44
0
def export_menu_to_excel(service_user, sln_settings=None):
    """Exports the menu to excel file

    Args:
        service_user: users.User

    Returns:
        Excel file content
    """
    if not sln_settings:
        sln_settings = get_solution_settings(service_user)
    language = sln_settings.main_language
    translate = partial(common_translate, language, SOLUTION_COMMON)

    menu = get_restaurant_menu(service_user, sln_settings.solution)
    workbook = xlwt.Workbook(encoding="utf-8")
    sheet = workbook.add_sheet(translate('menu'))
    settings = get_server_settings()

    row = 0
    for i, head in enumerate(MENU_HEADER):
        sheet.write(row, i, translate(head).capitalize())

    for cat in menu.categories:
        for item_idx, item in enumerate(cat.items):
            row += 1
            if item_idx == 0:
                sheet.write(row, 0, cat.name)
            sheet.write(row, 1, item.name)
            sheet.write(row, 2, item.description)
            sheet.write(row, 3, translate(UNITS[item.unit]))
            sheet.write(row, 4, item.price / 100.0)
            if item.image_id:
                sheet.write(row, 5, get_item_image_url(item.image_id,
                                                       settings))

    excel_file = StringIO()
    workbook.save(excel_file)
    return excel_file.getvalue()
Ejemplo n.º 45
0
def init_env():
    try:

        if not apiproxy_stub_map.apiproxy.GetStub('user'):
            apiproxy_stub_map.apiproxy.RegisterStub(
                'user', user_service_stub.UserServiceStub())
            apiproxy_stub_map.apiproxy.RegisterStub(
                'datastore_v3',
                datastore_file_stub.DatastoreFileStub('mobicagecloud',
                                                      '/dev/null',
                                                      '/dev/null'))
            apiproxy_stub_map.apiproxy.RegisterStub(
                'mail', mail_stub.MailServiceStub())
            apiproxy_stub_map.apiproxy.RegisterStub(
                'blobstore',
                blobstore_stub.BlobstoreServiceStub(
                    file_blob_storage.FileBlobStorage('/dev/null',
                                                      'mobicagecloud')))
            apiproxy_stub_map.apiproxy.RegisterStub(
                'memcache', memcache_stub.MemcacheServiceStub())
            apiproxy_stub_map.apiproxy.RegisterStub(
                'images', images_stub.ImagesServiceStub())
            apiproxy_stub_map.apiproxy.RegisterStub(
                'urlfetch', urlfetch_stub.URLFetchServiceStub())
            apiproxy_stub_map.apiproxy.RegisterStub(
                'taskqueue', taskqueue_stub.TaskQueueServiceStub())
            apiproxy_stub_map.apiproxy.RegisterStub(
                'app_identity_service',
                app_identity_stub.AppIdentityServiceStub())

    except Exception as e:
        print e
        raise

    from rogerthat.settings import get_server_settings

    settings = get_server_settings()
    settings.jabberDomain = "localhost"
Ejemplo n.º 46
0
 def get(self, app_id):
     path = os.path.join(os.path.dirname(__file__), 'html',
                         'customer_map.html')
     settings = get_server_settings()
     lang = get_languages_from_request(self.request)[0]
     translations = {
         'merchants':
         shop_translate(lang, 'merchants'),
         'merchants_with_terminal':
         shop_translate(lang, 'merchants_with_terminal'),
         'community_services':
         shop_translate(lang, 'community_services'),
         'care':
         shop_translate(lang, 'care'),
         'associations':
         shop_translate(lang, 'associations'),
     }
     params = {
         'maps_key': settings.googleMapsKey,
         'app_id': app_id,
         'translations': json.dumps(translations)
     }
     self.response.out.write(template.render(path, params))
Ejemplo n.º 47
0
def get_item_image_url(image_id, settings=None):
    if not settings:
        settings = get_server_settings()
    return u"%s/solutions/common/public/menu/image/%s" % (settings.baseUrl,
                                                          image_id)
Ejemplo n.º 48
0
def create_reseller_invoice_for_legal_entity(legal_entity,
                                             start_date,
                                             end_date,
                                             do_send_email=True):
    """
    Args:
        legal_entity (LegalEntity) 
        start_date (long)
        end_date (long)
        do_send_email (bool)
    """
    if legal_entity.is_mobicage:
        # To avoid a composite index we don't filter on is_mobicage
        return
    solution_server_settings = get_solution_server_settings()
    from_email = solution_server_settings.shop_no_reply_email
    to_emails = solution_server_settings.shop_payment_admin_emails
    mobicage_legal_entity = get_mobicage_legal_entity()
    logging.info(
        'Exporting reseller invoices for legal entity %s(id %d) from %s(%s) to %s(%s)',
        legal_entity.name, legal_entity.id, start_date, time.ctime(start_date),
        end_date, time.ctime(end_date))
    invoices = list(Invoice.all().filter(
        'legal_entity_id',
        legal_entity.id).filter('paid_timestamp >', start_date).filter(
            'paid_timestamp <', end_date).filter('paid', True).filter(
                'payment_type IN',
                (Invoice.PAYMENT_MANUAL, Invoice.PAYMENT_MANUAL_AFTER)))
    start_time = time.strftime('%m/%d/%Y', time.gmtime(int(start_date)))
    end_time = time.strftime('%m/%d/%Y', time.gmtime(int(end_date)))
    if not invoices:
        message = 'No new invoices for reseller %s for period %s - %s' % (
            legal_entity.name, start_time, end_time)
        logging.info(message)
        if do_send_email:
            send_mail(from_email, to_emails, message, message)
        return
    items_per_customer = {}
    customers_to_get = set()
    products = {
        p.code: p
        for p in Product.list_by_legal_entity(legal_entity.id)
    }
    for invoice in invoices:
        # get all subscription order items
        order_items = list(OrderItem.list_by_order(invoice.order_key))
        for item in reversed(order_items):
            product = products[item.product_code]
            # We're only interested in subscription items
            if product.is_subscription or product.is_subscription_extension or product.is_subscription_discount:
                if invoice.customer_id not in items_per_customer:
                    items_per_customer[invoice.customer_id] = []
                    customers_to_get.add(
                        Customer.create_key(invoice.customer_id))
                items_per_customer[invoice.customer_id].append(item)
            else:
                order_items.remove(item)
    if not customers_to_get:
        message = 'No new invoices containing subscriptions for reseller %s for period %s - %s' % (
            legal_entity.name, start_time, end_time)
        logging.info(message)
        if do_send_email:
            send_mail(from_email, to_emails, message, message)
        return
    customers = {c.id: c for c in db.get(customers_to_get)}
    product_totals = {}
    for customer_id in items_per_customer:
        items = items_per_customer[customer_id]
        for item in items:
            if item.product_code not in product_totals:
                product_totals[item.product_code] = {
                    'count': 0,
                    'price': int(item.price * legal_entity.revenue_percent)
                }
            product_totals[item.product_code]['count'] += item.count
    total_amount = 0
    for product in product_totals:
        p = product_totals[product]
        price = p['count'] * p['price']
        p['total_price'] = format_currency(
            price / 100.,
            legal_entity.currency_code,
            locale=mobicage_legal_entity.country_code)
        total_amount += price
    total_amount_formatted = format_currency(
        total_amount / 100.,
        legal_entity.currency_code,
        locale=mobicage_legal_entity.country_code)
    vat_amount = total_amount / mobicage_legal_entity.vat_percent if mobicage_legal_entity.country_code == legal_entity.country_code else 0
    vat_amount_formatted = format_currency(
        vat_amount / 100.,
        legal_entity.currency_code,
        locale=mobicage_legal_entity.country_code)
    from_date = format_datetime(datetime.utcfromtimestamp(start_date),
                                locale=SHOP_DEFAULT_LANGUAGE,
                                format='dd/MM/yyyy HH:mm')
    until_date = format_datetime(datetime.utcfromtimestamp(end_date),
                                 locale=SHOP_DEFAULT_LANGUAGE,
                                 format='dd/MM/yyyy HH:mm')

    solution_server_settings = get_solution_server_settings()
    template_variables = {
        'products':
        products,
        'customers':
        customers,
        'invoices':
        invoices,
        'items_per_customer':
        items_per_customer,
        'product_totals':
        product_totals.items(),
        'mobicage_legal_entity':
        mobicage_legal_entity,
        'legal_entity':
        legal_entity,
        'language':
        SHOP_DEFAULT_LANGUAGE,
        'from_date':
        from_date,
        'until_date':
        until_date,
        'revenue_percent':
        legal_entity.revenue_percent,
        'vat_amount_formatted':
        vat_amount_formatted,
        'total_amount_formatted':
        total_amount_formatted,
        'logo_path':
        '../html/img/osa_white_en_250.jpg',
        'tos_link':
        '<a href="%s">%s</a>' %
        (solution_server_settings.shop_privacy_policy_url,
         solution_server_settings.shop_privacy_policy_url)
    }
    source_html = SHOP_JINJA_ENVIRONMENT.get_template(
        'invoice/reseller_invoice.html').render(template_variables)
    output_stream = StringIO()
    pisa.CreatePDF(src=source_html,
                   dest=output_stream,
                   path='%s/invoice' % SHOP_TEMPLATES_FOLDER)
    invoice_pdf_contents = output_stream.getvalue()
    output_stream.close()
    # Create an order, order items, charge and invoice.
    _now = now()
    customer = legal_entity.get_or_create_customer()
    mobicage_team = RegioManagerTeam.get_mobicage()

    def trans():
        to_put = list()
        order_number = OrderNumber.next(mobicage_legal_entity)
        order_key = db.Key.from_path(Order.kind(),
                                     order_number,
                                     parent=customer.key())
        order = Order(key=order_key)
        order.contact_id = legal_entity.contact_id
        order.date = _now
        order.vat_pct = mobicage_legal_entity.vat_percent if legal_entity.country_code == mobicage_legal_entity.country_code else 0
        order.amount = int(round(total_amount))
        order.vat = int(round(vat_amount))
        order.total_amount = int(round(total_amount + vat_amount))
        order.is_subscription_order = False
        order.is_subscription_extension_order = False
        order.team_id = mobicage_team.id
        order.manager = customer.manager
        order.status = Order.STATUS_SIGNED
        to_put.append(order)

        for i, (product_code, item) in enumerate(product_totals.iteritems()):
            order_item = OrderItem(parent=order_key)
            order_item.number = i + 1
            order_item.comment = products[product_code].default_comment(
                SHOP_DEFAULT_LANGUAGE)
            order_item.product_code = product_code
            order_item.count = item['count']
            order_item.price = item['price']
            to_put.append(order_item)

        charge_key = Charge.create_key(allocate_id(Charge), order_number,
                                       customer.id)
        charge = Charge(key=charge_key)
        charge.date = _now
        charge.type = Charge.TYPE_ORDER_DELIVERY
        charge.amount = order.amount
        charge.vat_pct = order.vat_pct
        charge.vat = order.vat
        charge.total_amount = order.total_amount
        charge.manager = order.manager
        charge.team_id = order.team_id
        charge.charge_number = ChargeNumber.next(mobicage_legal_entity)
        charge.currency_code = legal_entity.currency_code
        to_put.append(charge)

        invoice_number = InvoiceNumber.next(mobicage_legal_entity)
        invoice = Invoice(key_name=invoice_number,
                          parent=charge,
                          amount=charge.amount,
                          vat_pct=charge.vat_pct,
                          vat=charge.vat,
                          total_amount=charge.total_amount,
                          currency_code=legal_entity.currency_code,
                          date=_now,
                          payment_type=Invoice.PAYMENT_MANUAL_AFTER,
                          operator=charge.manager,
                          paid=False,
                          legal_entity_id=mobicage_legal_entity.id,
                          pdf=invoice_pdf_contents)
        charge.invoice_number = invoice_number
        to_put.append(invoice)
        put_and_invalidate_cache(*to_put)
        return order, charge, invoice

    order, charge, invoice = run_in_xg_transaction(trans)

    if do_send_email:
        serving_url = '%s/internal/shop/invoice/pdf?customer_id=%d&order_number=%s&charge_id=%d&invoice_number=%s' % (
            get_server_settings().baseUrl, customer.id, order.order_number,
            charge.id, invoice.invoice_number)
        subject = 'New reseller invoice for %s, %s - %s' % (
            legal_entity.name, start_time, end_time)
        body_text = 'A new invoice is available for reseller %s for period %s to %s here: %s' % (
            legal_entity.name, start_time, end_time, serving_url)

        send_mail(from_email, to_emails, subject, body_text)
Ejemplo n.º 49
0
def solution_add_to_calender_event(service_user, email, method, params, tag,
                                   service_identity, user_details):
    rogerthat_settings = get_server_settings()
    settings = get_solution_settings(service_user)
    service_name = settings.name

    app = get_app_by_id(user_details[0].app_id)

    jsondata = json.loads(params)
    emailSubject = "Event:  %s" % jsondata['eventTitle']
    if jsondata['eventPlace']:
        eventPlace = "Place: %s " % jsondata['eventPlace']
    else:
        eventPlace = ''
    emailBody = u"Title: %s \nDate: %s \nDescription: %s \n%s \n" % (
        jsondata['eventTitle'], jsondata['eventDate'],
        jsondata['eventDescription'], eventPlace)

    cal = Calendar()
    cal.add('prodid', '-//My calendar product//mxm.dk//')
    cal.add('version', '2.0')
    cal.add('method', 'REQUEST')
    event = ICalenderEvent()
    event.add('summary', jsondata['eventTitle'])
    event.add('description', jsondata['eventDescription'])
    event.add('location', jsondata['eventPlace'])
    startDate = datetime.utcfromtimestamp(int(jsondata['eventStart']))

    try:
        endDate = datetime.utcfromtimestamp(int(jsondata['eventEnd']))
    except TypeError:
        endDate = None

    nowDate = datetime.utcfromtimestamp(time.time())
    dtstart = datetime(startDate.year,
                       startDate.month,
                       startDate.day,
                       startDate.hour,
                       startDate.minute,
                       startDate.second,
                       tzinfo=pytz.utc)
    dtstamp = datetime(nowDate.year,
                       nowDate.month,
                       nowDate.day,
                       nowDate.hour,
                       nowDate.minute,
                       nowDate.second,
                       tzinfo=pytz.utc)
    event.add('dtstart', dtstart)
    if endDate:
        dtend = datetime(endDate.year,
                         endDate.month,
                         endDate.day,
                         endDate.hour,
                         endDate.minute,
                         endDate.second,
                         tzinfo=pytz.utc)
        event.add('dtend', dtend)
    event.add('dtstamp', dtstamp)

    event.add('uid',
              "%s %s" % (app.dashboard_email_address, jsondata['eventId']))

    organizer = vCalAddress('MAILTO:%s' % app.dashboard_email_address)
    organizer.params['cn'] = vText(service_name)
    event.add('organizer', organizer)

    cal.add_component(event)
    icall = cal.to_ical()

    attachments = []
    attachments.append(("event.ics", base64.b64encode(icall)))

    from_ = rogerthat_settings.senderEmail if app.type == App.APP_TYPE_ROGERTHAT else (
        "%s <%s>" % (app.name, app.dashboard_email_address))
    send_mail(from_, email, emailSubject, emailBody, attachments=attachments)

    r = SendApiCallCallbackResultTO()
    r.result = u"successfully reminded"
    r.error = None
    return r
Ejemplo n.º 50
0
    def trans():
        old_order, team = db.get(
            (old_order_key, RegioManagerTeam.create_key(customer.team_id)))

        if not old_order:
            return BoolReturnStatusTO.create(
                False,
                translate(
                    get_solution_settings(service_user).main_language,
                    SOLUTION_COMMON, u'cart_empty'), False)

        # Duplicate the order
        to_put = list()
        to_delete = list()
        properties = copy_model_properties(old_order)
        properties['status'] = Order.STATUS_SIGNED
        properties['date_signed'] = now()
        new_order_key = Order.create_key(
            customer.id, OrderNumber.next(team.legal_entity_key))
        new_order = Order(key=new_order_key, **properties)
        new_order.team_id = team.id
        to_delete.append(old_order)

        # duplicate all of the order items
        old_order_items = OrderItem.list_by_order(old_order_key)
        all_products = db.get([
            Product.create_key(item.product_code) for item in old_order_items
        ])
        is_subscription_extension_order = False
        for product in all_products:
            if product.is_subscription_extension:
                is_subscription_extension_order = True
                break
        new_order.is_subscription_extension_order = is_subscription_extension_order
        if is_subscription_extension_order:
            subscription_order = Order.get_by_order_number(
                customer.id, customer.subscription_order_number)
            new_order.next_charge_date = subscription_order.next_charge_date
        added_apps = list()
        should_create_shoptask = False
        for old_item in old_order_items:
            properties = copy_model_properties(old_item)
            new_item = OrderItem(parent=new_order_key, **properties)
            to_put.append(new_item)
            to_delete.append(old_item)
            if hasattr(old_item, 'app_id'):
                added_apps.append(old_item.app_id)
            else:
                should_create_shoptask = True
        to_put.append(new_order)
        db.put(to_put)
        db.delete(to_delete)

        deferred.defer(generate_and_put_order_pdf_and_send_mail,
                       customer,
                       new_order_key,
                       service_user,
                       _transactional=True)

        # No need for signing here, immediately create a charge.
        azzert(new_order.total_amount > 0)
        charge = Charge(parent=new_order_key)
        charge.date = now()
        charge.type = Charge.TYPE_ORDER_DELIVERY
        charge.amount = new_order.amount
        charge.vat_pct = new_order.vat_pct
        charge.vat = new_order.vat
        charge.total_amount = new_order.total_amount
        charge.manager = new_order.manager
        charge.team_id = new_order.team_id
        charge.status = Charge.STATUS_PENDING
        charge.date_executed = now()
        charge.currency_code = team.legal_entity.currency_code
        charge.put()

        # Update the regiomanager statistics so these kind of orders show up in the monthly statistics
        deferred.defer(update_regiomanager_statistic,
                       gained_value=new_order.amount / 100,
                       manager=new_order.manager,
                       _transactional=True)

        # Update the customer service
        si = get_default_service_identity(users.User(customer.service_email))
        si.appIds.extend(added_apps)
        si.put()
        deferred.defer(re_index, si.user, _transactional=True)

        # Update the customer object so the newly added apps are added.
        customer.app_ids.extend(added_apps)
        customer.extra_apps_count += len(added_apps)
        customer.put()

        get_payed(customer.id, new_order, charge)
        # charge the credit card
        channel.send_message(service_user, 'common.billing.orders.update')
        channel_data = {
            'customer': customer.name,
            'no_manager': True,
            'amount': charge.amount / 100,
            'currency': charge.currency
        }
        server_settings = get_server_settings()
        send_to = server_settings.supportWorkers
        send_to.append(MC_DASHBOARD.email())
        channel.send_message(map(users.User, send_to),
                             'shop.monitoring.signed_order',
                             info=channel_data)
        if should_create_shoptask:
            prospect_id = customer.prospect_id
            if prospect_id is None:
                prospect = create_prospect_from_customer(customer)
                prospect_id = prospect.id
            deferred.defer(create_task_for_order,
                           customer.team_id,
                           prospect_id,
                           new_order.order_number,
                           _transactional=True)
        return BoolReturnStatusTO.create(True, None)
Ejemplo n.º 51
0
def create_and_pay_news_order(service_user, news_item_id, order_items_to):
    """
    Creates an order, orderitems, charge and executes the charge. Should be executed in a transaction.
    Args:
        service_user (users.User)
        news_item_id (long)
        order_items_to (ist of OrderItemTO)

    Raises:
        NoCreditCardException
        ProductNotFoundException
    """
    @db.non_transactional
    def _get_customer():
        return get_customer(service_user)

    @db.non_transactional
    def _get_contact():
        return Contact.get_one(customer)

    customer = _get_customer()
    azzert(customer)
    contact = _get_contact()
    azzert(contact)
    if not customer.stripe_valid:
        raise NoCreditCardException(customer)
    extra_city_product_key = Product.create_key(Product.PRODUCT_EXTRA_CITY)
    news_product_key = Product.create_key(Product.PRODUCT_NEWS_PROMOTION)
    rmt_key = RegioManagerTeam.create_key(customer.team_id)
    extra_city_product, news_promotion_product, team = db.get(
        (extra_city_product_key, news_product_key, rmt_key))
    azzert(extra_city_product)
    azzert(news_promotion_product)
    azzert(team)
    new_order_key = Order.create_key(customer.id,
                                     OrderNumber.next(team.legal_entity_key))
    vat_pct = get_vat_pct(customer, team)

    total_amount = 0
    added_app_ids = []
    for order_item in order_items_to:
        if order_item.product == Product.PRODUCT_EXTRA_CITY:
            total_amount += extra_city_product.price * order_item.count
            added_app_ids.append(order_item.app_id)
            order_item.price = extra_city_product.price
        elif order_item.product == Product.PRODUCT_NEWS_PROMOTION:
            total_amount += news_promotion_product.price * order_item.count
            order_item.price = news_promotion_product.price
        else:
            raise BusinessException('Invalid product \'%s\'' %
                                    order_item.product)
    si = get_default_service_identity(users.User(customer.service_email))
    if added_app_ids:
        keys = [App.create_key(app_id) for app_id in added_app_ids]
        apps = db.get(keys)
        for app_id, app in zip(added_app_ids, apps):
            if not app:
                raise AppNotFoundException(app_id)
            if app_id in si.appIds:
                raise BusinessException('Customer %s already has app_id %s' %
                                        (customer.id, app_id))

    vat = int(round(vat_pct * total_amount / 100))
    total_amount_vat_incl = int(round(total_amount + vat))
    now_ = now()
    to_put = []
    order = Order(key=new_order_key,
                  date=now_,
                  amount=total_amount,
                  vat_pct=vat_pct,
                  vat=vat,
                  total_amount=total_amount_vat_incl,
                  contact_id=contact.id,
                  status=Order.STATUS_SIGNED,
                  is_subscription_order=False,
                  is_subscription_extension_order=False,
                  date_signed=now_,
                  manager=STORE_MANAGER,
                  team_id=team.id)
    to_put.append(order)
    azzert(order.total_amount >= 0)

    for item in order_items_to:
        order_item = OrderItem(parent=new_order_key,
                               number=item.number,
                               product_code=item.product,
                               count=item.count,
                               comment=item.comment,
                               price=item.price)
        order_item.app_id = item.app_id
        if order_item.product_code == Product.PRODUCT_NEWS_PROMOTION:
            order_item.news_item_id = news_item_id
        to_put.append(order_item)

    db.put(to_put)

    # Not sure if this is necessary
    deferred.defer(generate_and_put_order_pdf_and_send_mail,
                   customer,
                   new_order_key,
                   service_user,
                   _transactional=True)

    # No need for signing here, immediately create a charge.
    to_put = []
    charge = Charge(parent=new_order_key)
    charge.date = now()
    charge.type = Charge.TYPE_ORDER_DELIVERY
    charge.amount = order.amount
    charge.vat_pct = order.vat_pct
    charge.vat = order.vat
    charge.total_amount = order.total_amount
    charge.manager = order.manager
    charge.team_id = order.team_id
    charge.status = Charge.STATUS_PENDING
    charge.date_executed = now()
    charge.currency_code = team.legal_entity.currency_code
    to_put.append(charge)

    # Update the regiomanager statistics so these kind of orders show up in the monthly statistics
    deferred.defer(update_regiomanager_statistic,
                   gained_value=order.amount / 100,
                   manager=order.manager,
                   _transactional=True)

    # Update the customer service
    si.appIds.extend(added_app_ids)
    to_put.append(si)

    # Update the customer object so the newly added apps are added.
    customer.app_ids.extend(added_app_ids)
    customer.extra_apps_count += len(added_app_ids)
    to_put.append(customer)
    db.put(to_put)
    deferred.defer(re_index, si.user)

    # charge the credit card
    if charge.total_amount > 0:
        get_payed(customer.id, order, charge)
        server_settings = get_server_settings()
        send_to = server_settings.supportWorkers
        send_to.append(MC_DASHBOARD.email())
        channel_data = {
            'customer': customer.name,
            'no_manager': True,
            'amount': charge.amount / 100,
            'currency': charge.currency
        }
        channel.send_message(map(users.User, send_to),
                             'shop.monitoring.signed_order',
                             info=channel_data)
    else:
        charge.status = Charge.STATUS_EXECUTED
        charge.date_executed = now()
        charge.put()
    channel.send_message(service_user, 'common.billing.orders.update')