Exemplo n.º 1
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)
Exemplo n.º 2
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)
Exemplo n.º 3
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
Exemplo n.º 4
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)
Exemplo n.º 5
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))
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)
Exemplo n.º 7
0
    def trans():
        sln_cwl.pending = False
        sln_cwl.winners = winners
        sln_cwl.winners_info = json.dumps(
            serialize_complex_value(winners_info, ExtendedUserDetailsTO, True))
        sln_cwl.put()
        deferred.defer(_redeem_city_wide_lottery_visits,
                       service_user,
                       sln_cwl_lottery_key,
                       now(),
                       _transactional=True)

        to_emails = sln_settings.inbox_mail_forwarders
        if to_emails:
            solution_server_settings = get_solution_server_settings()
            subject = 'Winnaars gemeentelijke tombola'
            body = """Beste,
Volgende mensen hebben gewonnen met de tombola: %s


Met vriendelijke groeten,

Het Onze Stad App Team
""" % winner_text

            send_mail(solution_server_settings.shop_export_email, to_emails,
                      subject, body)
def _send_statistics_for_service_identity_user(si, admin_emails, default_language, settings, sisTO):
    stats_usage = list()
    for i in sisTO.menu_item_press:
        count = 0
        max_length = 8
        if len(i.data) < max_length:
            max_length = len(i.data)
        for d in range(1, max_length):
            count = count + i.data[d].count
        if count > 0:
            stats_usage.append({"name":i.name, "count":count})

    stats_usage.sort(cmp=_usage_compare)
    emails_to_send = [e.strip() for e in admin_emails.split(',') if e.strip()]
    variables = dict(service=si.name,
                     users={"today":sisTO.number_of_users,
                            "week":{"gained":sum(c.count for c in sisTO.users_gained[:8]),
                                    "lost":sum(c.count for c in sisTO.users_lost[:8])},
                            "month":{"gained":sum(c.count for c in sisTO.users_gained[:31]),
                                     "lost":sum(c.count for c in sisTO.users_lost[:31])}},
                     usage=stats_usage,
                     usage_length=len(stats_usage))
    body = render("service_identity_statistics_email", [default_language], variables)
    html = render("service_identity_statistics_email_html", [default_language], variables)
    subject = "Weekly statistics for %s" % si.name
    send_mail(settings.dashboardEmail, emails_to_send, subject, body, html=html)
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)
Exemplo n.º 10
0
def _send_denied_signup_email(city_customer, signup, lang, reason):
    subject = common_translate(city_customer.language,
                               SOLUTION_COMMON,
                               u'signup_request_denied_by_city',
                               city=city_customer.name)
    message = common_translate(city_customer.language,
                               SOLUTION_COMMON,
                               u'signup_request_denial_reason',
                               reason=reason)

    city_from = '%s <%s>' % (city_customer.name, city_customer.user_email)
    send_mail(city_from, signup.customer_email, subject, message)
Exemplo n.º 11
0
def reportObjectionableContent(request):
    report = Report(reported_by=users.get_current_user(),
                    reason=request.reason,
                    type=request.type,
                    object=json.loads(request.object))
    report.put()

    object_str = pprint.pformat(report.object)
    app = get_app_by_id(users.get_current_app_id())
    send_mail(app.dashboard_email_address, get_server_settings().supportEmail,
              u'User reports objectionable content',
              u'User: %s\nType: %s\nReason:%s\nContent:\n%s'
              % (report.reported_by.email(), request.type, request.reason, object_str))
 def get(self):
     expired_apps = [(app.app_id, time.ctime(app.apple_push_cert_valid_until))
                     for app in App.all() \
                         .filter('apple_push_cert_valid_until !=', None) \
                         .filter('apple_push_cert_valid_until <', now() + 60 * 86400) \
                         .order('apple_push_cert_valid_until')]
     if expired_apps:
         expired_apps_str = pprint.pformat(expired_apps)
         settings = get_server_settings()
         send_mail(settings.dashboardEmail,
                   settings.supportWorkers,
                   "There are APN certs that are about to expire",
                   "The following APN certs are about to expire:\n%s" % expired_apps_str)
         logging.error("The following APN certs are about to expire:\n%s" % expired_apps_str)
    def trans(owner_profile, ts, is_new_trial_service):
        if is_new_trial_service:
            deferred.defer(_configure_mobidick, ts.service, _transactional=True)

        deferred.defer(_friend_trial_service, user, ts.service, _transactional=True, _countdown=5)

        context = dict(name=owner_profile.name, email=ts.service.email(), password=ts.password)
        body = render("trial_service_signup", [DEFAULT_LANGUAGE], context)
        html = render("trial_service_signup_html", [DEFAULT_LANGUAGE], context)
        logging.info("Sending message to %s\n%s" % (ts.owner.email(), body))

        send_mail(server_settings.senderEmail, ts.owner.email(), "Rogerthat trial service", body, html=html)

        return TrialServiceTO.fromDBTrialServiceAccount(ts)
Exemplo n.º 14
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
Exemplo n.º 15
0
def feedback(user, type_, subject, message):
    email_subject = "Feedback - %s - %s" % (type_, subject)
    friend_count = len(get_friends_map(user).friends)
    message_count = get_messages_count(user)
    mobiles = get_user_active_mobiles(user)
    email = user.email()
    timestamp = now()
    profile_info = get_profile_info(user)

    d = dict(type_=type_, subject=subject, message=message, email=email,
             profile_info=profile_info, friend_count=friend_count, message_count=message_count,
             mobiles=mobiles, timestamp=timestamp)

    body = render("feedback", [DEFAULT_LANGUAGE], d)
    server_settings = get_server_settings()

    send_mail(server_settings.senderEmail, server_settings.supportEmail, email_subject, body)
    def send_mail(self, final_stats_per_app, min_time, max_time, interval, skip_messages_sent_by_js_mfr):
        min_date = datetime.datetime.utcfromtimestamp(min_time)
        max_date = datetime.datetime.utcfromtimestamp(max_time)
        min_date_str = min_date.strftime('%d %b %Y')
        max_date_str = max_date.strftime('%d %b %Y')

        with closing(StringIO()) as s:
            s.write('User stats from %s until %s with %s interval'
                    % (min_date_str, max_date_str,
                       ('%s month(s)' % interval.months) if interval.months else ('%s day(s)' % interval.days)))
            if skip_messages_sent_by_js_mfr:
                s.write(' (messages sent by JS_MFR are skipped)')
            s.write('.\nSee attached document for details per app.\n\nSummary:\n')

            # TOTAL STATS PER PERIOD
            result_length = get_interval_index(max_time, min_time, interval) + 1
            total_stats_list = [Stats(0, 0, 0, 0) for _ in xrange(result_length)]
            for stats_list in final_stats_per_app.itervalues():
                for i, stats in enumerate(total_stats_list):
                    total_stats_list[i] = _combine([total_stats_list[i], stats_list[i]])

            if DEBUG:
                logging.warn('ProcessOutputPipeline: total_stats_list = %s', total_stats_list)

            for i, stats in enumerate(total_stats_list):
                stats = Stats(*stats)
                s.write('''
%s
    total: %s
    received: %s/%s (%s of total)
    read: %s/%s (%s of received, %s of total)
    acked: %s/%s (%s of read, %s of received, %s of total)
''' % (get_period_str(i, min_date, max_date, interval), stats.total,
       stats.received, stats.total, percent(stats.received, stats.total),
       stats.read, stats.received, percent(stats.read, stats.received), percent(stats.read, stats.total),
       stats.acked, stats.read, percent(stats.acked, stats.read), percent(stats.acked, stats.received), percent(stats.acked, stats.total)))

            body = s.getvalue()

        server_settings = get_server_settings()
        mail_receivers = server_settings.supportWorkers
        subject = u'Rogerthat user stats: %s - %s' % (min_date_str, max_date_str)
        attachments = [self.create_xls_attachment(final_stats_per_app, total_stats_list, min_date, max_date, interval)]
        send_mail(server_settings.dashboardEmail, mail_receivers, subject, body, attachments=attachments)
Exemplo n.º 17
0
def _send_export_email(result_path, human_user_email, app_id, data_export_email, download_url):
    logging.info(download_url)
    app_user = create_app_user_by_email(human_user_email, app_id)
    user_profile, app = db.get([get_profile_key(app_user), App.create_key(app_id)])

    subject = localize(user_profile.language, "user_data_download_ready_summary")
    variables = dict(name=user_profile.name,
                     link=download_url,
                     app=app)
    body = render("data_download_ready_email", [user_profile.language], variables)
    html = render("data_download_ready_email_html", [user_profile.language], variables)
    server_settings = get_server_settings()

    email_receivers = [data_export_email]
    if app.is_default:
        email_sender = server_settings.senderEmail
    else:
        email_sender = ("%s <%s>" % (app.name, app.dashboard_email_address))

    send_mail(email_sender, email_receivers, subject, body, html=html)
    deferred.defer(_cleanup_export, result_path, _countdown=4 * DAY, _queue=DATA_EXPORT_QUEUE)
Exemplo n.º 18
0
def reset_password(email):
    # 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

    name = profile_info.name
    timestamp = now() + 5 * 24 * 3600
    data = dict(n=name, e=email, t=timestamp, a="reset password", c=None)
    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)
    body = render("reset_password", [DEFAULT_LANGUAGE], vars_)
    html = render("reset_password_html", [DEFAULT_LANGUAGE], vars_)
    logging.info("Sending message to %s\n%s" % (email, body))
    settings = get_server_settings()
    utils.send_mail(settings.senderEmail2ToBeReplaced, email, "Rogerthat password reset", body, html=html)
    return True
        def trans():
            db_puts = list()

            # Create registration entry.
            installation = Installation.get_by_key_name(install_id) if install_id else None
            app_user = create_app_user(users.User(email), app_id)
            registration = None
            if version == 2:
                registration = Registration.get_by_key_name(registration_id, parent_key(app_user))
                if registration and registration.request_id == request_id:
                    InstallationLog(parent=registration.installation, timestamp=now(), registration=registration,
                                    pin=registration.pin,
                                    description="Received a HTTP request retry for 'request pin'. Not sending a new mail.").put()
                    return

            rogerthat_profile = get_service_or_user_profile(users.User(email))
            if rogerthat_profile and isinstance(rogerthat_profile, ServiceProfile):
                # some guy tries to register a mobile on a service account ?!?
                variables = dict(email=email)
                body = render("somebody_tries_to_register_his_mobile_on_your_service_account_warning", [language], variables)
                html = render("somebody_tries_to_register_his_mobile_on_your_service_account_warning_html", [language], variables)
                logging.info("Sending message to %s\n%s" % (email, body))
                recipients = [email]

                for admin in get_service_admins_non_transactional(app_user):
                    recipients.append(admin.user_email)

                msg = MIMEMultipart('alternative')
                msg['Subject'] = "Warning, possibly somebody tries to hack your service account."
                msg['From'] = server_settings.senderEmail
                msg['To'] = ', '.join(recipients)
                msg.attach(MIMEText(body.encode('utf-8'), 'plain', 'utf-8'))
                msg.attach(MIMEText(html.encode('utf-8'), 'html', 'utf-8'))
                send_mail_via_mime(server_settings.senderEmail, recipients, msg)

                warning = InstallationLog(parent=installation, timestamp=now(),
                                     description="Warning somebody tries to register a mobile with the email address of service account %s" % email)
                db_puts.append(warning)
            else:
                profile = get_user_profile(app_user)
                if profile:
                    name = profile.name
                else:
                    deactivated_profile = get_deactivated_user_profile(app_user)
                    name = deactivated_profile.name if deactivated_profile else None
                if not registration:
                    registration = Registration(parent=parent_key(app_user), key_name=registration_id)
                registration.timestamp = registration_time
                registration.device_id = device_id
                server_settings = get_server_settings()
                for pin, static_email in chunks(server_settings.staticPinCodes, 2):
                    if email == static_email and len(pin) == 4:
                        registration.pin = int(pin)
                        pin_str = unicode(registration.pin).rjust(4, '0')
                        utils.send_mail(server_settings.dashboardEmail,
                                        server_settings.supportWorkers,
                                        pin_str,
                                        u'Configured pin code %s for %s' % (pin_str, app_user.email()))
                        break

                else:
                    registration.pin = random.randint(1000, 9999)
                registration.timesleft = 3
                registration.installation = installation
                registration.request_id = request_id
                registration.language = language
                db_puts.append(registration)

                i1 = InstallationLog(parent=registration.installation, timestamp=now(), registration=registration, pin=registration.pin,
                                     description="%s requested pin" % email)
                db_puts.append(i1)


                # Send email with pin.
                app = get_app_by_id(app_id)
                variables = dict(pin=registration.pin, name=name, app=app)
                body = render("activation_code_email", [language], variables)
                html = render("activation_code_email_html", [language], variables)

                logging.info("Sending message to %s\n%s" % (email, body))
                msg = MIMEMultipart('alternative')
                msg['Subject'] = localize(language, "%(app_name)s mobile registration", app_name=app.name)
                msg['From'] = server_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(server_settings.senderEmail, email, msg)

                i2 = InstallationLog(parent=registration.installation, timestamp=now(), registration=registration, pin=registration.pin,
                                     description="Sent email to %s with pin %s" % (email, registration.pin))
                db_puts.append(i2)

            db.put(db_puts)
def _send_statistics_for_service_user(si, admin_emails, default_language, settings):
    service_identities = list(get_service_identities(si.service_user))
    book = xlwt.Workbook(encoding="utf-8")
    sheet = book.add_sheet("Statistics")
    wrap_alignment = xlwt.Alignment()
    wrap_alignment.wrap = xlwt.Alignment.WRAP_AT_RIGHT
    wrap_alignment.horz = xlwt.Alignment.HORZ_LEFT
    wrap_alignment.vert = xlwt.Alignment.VERT_CENTER
    red_font = xlwt.Font()
    red_font.colour_index = 10
    pattern_white = xlwt.Pattern()
    pattern_white.pattern = xlwt.Pattern.SOLID_PATTERN
    pattern_white.pattern_fore_colour = 1
    pattern_dark = xlwt.Pattern()
    pattern_dark.pattern = xlwt.Pattern.SOLID_PATTERN
    pattern_dark.pattern_fore_colour = 41
    regular_style1 = xlwt.XFStyle()
    regular_style1.alignment = wrap_alignment
    regular_style1.pattern = pattern_white
    red_style2 = xlwt.XFStyle()
    red_style2.alignment = wrap_alignment
    red_style2.pattern = pattern_dark
    red_style2.font = red_font
    START_USERS_DETAIL = 15
    START_USAGE_DETAIL = 26
    sheet.col(0).width = 5000
    sheet.col(1).width = 7000
    sheet.write(1, 0, "Summary users", red_style2)

    if len(service_identities) >= 2:
        col = 0
        sheet.write(3, 1, "Total")
        sheet.write(START_USERS_DETAIL - 1, 1, "Total", regular_style1)
    else:
        col = 1
    # SET summary users names
    sheet.write(4, col, "Today")
    sheet.write(5, col, "Last 7 days:")
    sheet.write(6, col, " - gained")
    sheet.write(7, col, " - lost")
    sheet.write(8, col, "Last 30 days:")
    sheet.write(9, col, " - gained")
    sheet.write(10, col, " - lost")
    row = START_USERS_DETAIL
    sheet.write(START_USERS_DETAIL - 3, 0, "Detail users", red_style2)
    sheet.write(START_USERS_DETAIL - 1, col, "Week", regular_style1)

    now_ = datetime.datetime.utcnow()
    now_date_today = datetime.date(now_.year, now_.month, now_.day).today()
    # SET detail users dates
    for i in range(1, 8):
        day_stats = now_date_today - datetime.timedelta(i * 7)
        dt = datetime.date(day_stats.year, day_stats.month, day_stats.day)
        sheet.write(row, col, dt.isocalendar()[1], regular_style1)
        row += 1

    # SET detail usage dates
    row = START_USAGE_DETAIL
    sheet.write(START_USAGE_DETAIL - 3, 0, "Detail usage", red_style2)
    sheet.write(START_USAGE_DETAIL - 1, 0, "Week", regular_style1)
    sheet.write(START_USAGE_DETAIL - 1, 1, "Menu item label", regular_style1)
    service_menu_items = get_service_menu_items(si.service_user)
    smi_labels = sorted([smi.label for smi in service_menu_items], key=lambda x: x.lower())
    for i in range(1, 8):
        day_stats = now_date_today - datetime.timedelta(i * 7)
        dt = datetime.date(day_stats.year, day_stats.month, day_stats.day)
        for label in smi_labels:
            sheet.write(row, 0, dt.isocalendar()[1], regular_style1)
            sheet.write(row, 1, label, regular_style1)
            row += 1

        row += 1

    col = 2
    summary_users_total = dict()

    summary_users_total["number_of_users"] = 0
    summary_users_total["w_user_gained"] = 0
    summary_users_total["w_user_lost"] = 0
    summary_users_total["m_user_gained"] = 0
    summary_users_total["m_user_lost"] = 0


    detail_users_total = [0, 0, 0, 0, 0, 0, 0]

    for service_identity in service_identities:
        ss = get_identity_statistics(service_identity.user)
        sis = get_statistics_list(ss)
        if len(sis["users_gained"]) >= 8:
            # Service has statistics for longer than 7 days
            # SET summary users data
            sheet.write(2, col, service_identity.name, regular_style1)
            sheet.write(3, col, service_identity.identifier, regular_style1)
            sheet.write(4, col, ss.number_of_users)
            sheet.write(6, col, sum(sis["users_gained"][:8]))
            sheet.write(7, col, sum(sis["users_lost"][:8]))
            sheet.write(9, col, sum(sis["users_gained"][:31]))
            sheet.write(10, col, sum(sis["users_lost"][:31]))

            summary_users_total["number_of_users"] += ss.number_of_users
            summary_users_total["w_user_gained"] += sum(sis["users_gained"][:8])
            summary_users_total["w_user_lost"] += sum(sis["users_lost"][:8])
            summary_users_total["m_user_gained"] += sum(sis["users_gained"][:31])
            summary_users_total["m_user_lost"] += sum(sis["users_lost"][:31])

            # SET detail users data
            row = START_USERS_DETAIL
            total_users = ss.number_of_users
            sheet.write(START_USERS_DETAIL - 2, col, service_identity.name, regular_style1)
            sheet.write(START_USERS_DETAIL - 1, col, service_identity.identifier, regular_style1)
            for i in range(1, 8):  # last 7 weeks
                weekly_total_users = total_users - sum(sis["users_gained"][:7 * i]) + sum(sis["users_lost"][:7 * i])
                sheet.write(row, col, weekly_total_users)
                detail_users_total[i - 1] += weekly_total_users
                row += 1

            # SET detail usage data
            row = START_USAGE_DETAIL
            sheet.write(START_USAGE_DETAIL - 2, col, service_identity.name, regular_style1)
            sheet.write(START_USAGE_DETAIL - 1, col, service_identity.identifier, regular_style1)
            for i in range(1, 8):  # last 7 weeks
                for label in smi_labels:
                    if label in sis["labels"]:
                        sheet.write(row, col, sum(sis[label][7 * (i - 1):7 * i]))
                    else:
                        sheet.write(row, col, 0)
                    row += 1
                row += 1
            col += 1

    if len(service_identities) >= 2:
        sheet.write(4, 1, summary_users_total["number_of_users"])
        sheet.write(6, 1, summary_users_total["w_user_gained"])
        sheet.write(7, 1, summary_users_total["w_user_lost"])
        sheet.write(9, 1, summary_users_total["m_user_gained"])
        sheet.write(10, 1, summary_users_total["m_user_lost"])

        row = START_USERS_DETAIL
        for i in range(7):
            sheet.write(row, 1, detail_users_total[i])
            row += 1

    if col > 2:
        output = StringIO()
        book.save(output)
        output.seek(0)
        emails_to_send = [e.strip() for e in admin_emails.split(',') if e.strip()]
        variables = dict(service=si.name)
        body = render("service_statistics_email", [default_language], variables)
        subject = "Weekly statistics for your identities of %s" % si.name
        
        attachments = []
        attachments.append(('filename=stats_%s_w_%s.xls' % (now_date_today.year, now_date_today.isocalendar()[1]),
                            base64.b64encode(output.getvalue())))
        
        send_mail(settings.dashboardEmail, emails_to_send, subject, body, attachments=attachments)
Exemplo n.º 21
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)
Exemplo n.º 22
0
def generate_prospect_export_excel(prospect_ids, do_send_email=True, recipients=None):
    if not prospect_ids:
        raise BusinessException('No prospects to export')
    azzert(not do_send_email or recipients)
    bold_style = xlwt.XFStyle()
    bold_style.font.bold = True
    column_name, column_address, column_city, column_phone, column_status, column_type, column_categories, column_comments = range(8)

    book = xlwt.Workbook(encoding="utf-8")
    sheet = book.add_sheet('Prospects')
    prospects = Prospect.get(map(Prospect.create_key, prospect_ids))
    app_id = None

    sheet.write(0, column_name, 'Name', bold_style)
    sheet.write(0, column_address, 'Address', bold_style)
    sheet.write(0, column_city, 'City', bold_style)
    sheet.write(0, column_phone, 'Phone', bold_style)
    sheet.write(0, column_status, 'Status', bold_style)
    sheet.write(0, column_type, 'Type', bold_style)
    sheet.write(0, column_categories, 'Category', bold_style)
    sheet.write(0, column_comments, 'Comments', bold_style)
    for i, prospect in enumerate(prospects):
        row = i + 1
        comments_str = '\n'.join(['* %s' % comment.text for comment in prospect.comments])
        sheet.write(row, column_name, prospect.name)
        formatted_address = format_address(prospect.address)
        sheet.write(row, column_address, formatted_address[0])
        sheet.write(row, column_city, formatted_address[1])
        sheet.write(row, column_phone, prospect.phone)
        sheet.write(row, column_status, Prospect.STATUS_TYPES[prospect.status])
        sheet.write(row, column_type, ', '.join(prospect.type))
        sheet.write(row, column_categories, ', '.join(prospect.categories))
        sheet.write(row, column_comments, comments_str)
        sheet.col(column_name).width = 5000
        sheet.col(column_address).width = 5000
        sheet.col(column_phone).width = 5000
        sheet.col(column_status).width = 5000
        sheet.col(column_type).width = 10000
        sheet.col(column_categories).width = 10000
        sheet.col(column_comments).width = 20000
        if not app_id:
            app_id = prospect.app_id
    excel = StringIO()
    book.save(excel)
    excel_string = excel.getvalue()

    if do_send_email:
        app = get_app_by_id(app_id)
        solution_server_settings = get_solution_server_settings()
        current_date = format_datetime(datetime.datetime.now(), locale=DEFAULT_LANGUAGE)
        subject = 'Exported prospects of %s %s' % (app.name, current_date)
        from_email = solution_server_settings.shop_export_email
        to_emails = recipients
        body_text = 'See attachment for the exported prospects'
        
        attachments = []
        attachments.append(('Prospects %s %s.xls' % (app.name, current_date),
                            base64.b64encode(excel_string)))
        
        send_mail(from_email, to_emails, subject, body_text, attachments=attachments)
    return excel_string
Exemplo n.º 23
0
def create_voucher_statistics_for_city_service(service_user, language,
                                               first_day_of_last_month,
                                               first_day_of_current_month):
    customer = Customer.get_by_service_email(service_user.email())
    translate = partial(common_translate, language, SOLUTION_COMMON)
    if not customer:
        logging.error("failed to create voucher statistics customer not found")
        return
    sln_settings = get_solution_settings(service_user)

    users.set_user(service_user)
    try:
        si = system.get_identity()
    finally:
        users.clear_user()
    app_id = si.app_ids[0]
    ancestor_key = SolutionCityVoucher.create_parent_key(app_id)

    qry = SolutionCityVoucherTransaction.all().ancestor(ancestor_key)
    qry.filter("action =", SolutionCityVoucherTransaction.ACTION_REDEEMED)
    qry.filter("created >=", first_day_of_last_month)
    qry.filter("created <", first_day_of_current_month)

    transactions = []
    merchant_transactions = dict()
    merchants = dict()
    unique_vouchers = dict()
    for t in qry:
        t.dt = format_timestamp(t.created, sln_settings)
        t.voucher = t.parent()
        transactions.append(t)
        if t.service_user not in merchant_transactions:
            merchant_transactions[t.service_user] = {
                "value": 0,
                "transactions": []
            }
        merchant_transactions[t.service_user]["value"] += t.value
        merchant_transactions[t.service_user]["transactions"].append(t.key())
        unique_vouchers[t.voucher.key()] = t.voucher

    for merchant_service_user in merchant_transactions.keys():
        merchants[merchant_service_user] = get_solution_settings(
            merchant_service_user)

    qry = SolutionCityVoucher.all().ancestor(ancestor_key)
    qry.filter("activated =", True)
    qry.filter("redeemed = ", False)
    vouchers = []
    expired_vouchers = []
    for v in qry:
        v.dt = format_timestamp(v.created, sln_settings)
        if v.expired:
            if v.expiration_date >= first_day_of_last_month and \
               v.expiration_date < first_day_of_current_month:
                expired_vouchers.append(v)
        else:
            vouchers.append(v)

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

    # TAB 1
    sheet_transactions = book.add_sheet(translate("Transactions"))
    row = 0
    write_header(sheet_transactions, row, translate, "Date", "Voucher",
                 "Internal account", "Cost center", "merchant",
                 "Withdrawn value")

    for transaction in transactions:
        row += 1
        sheet_transactions.write(row, 0, transaction.dt)
        sheet_transactions.write(row, 1, transaction.voucher.uid)
        sheet_transactions.write(row, 2, transaction.voucher.internal_account)
        sheet_transactions.write(row, 3, transaction.voucher.cost_center)
        sheet_transactions.write(row, 4,
                                 merchants[transaction.service_user].name)
        sheet_transactions.write(row, 5, round(transaction.value / 100.0, 2))
    row += 2
    sheet_transactions.write(row, 0, translate("total"))
    sheet_transactions.write(row, 5, xlwt.Formula('SUM(F2:F%s)' % (row - 1)))

    # TAB 2
    sheet_merchants = book.add_sheet(translate("merchants"))
    row = 0
    sheet_merchants.write(row, 0, translate("merchant"))
    sheet_merchants.write(row, 1, translate("address"))
    sheet_merchants.write(row, 2, "IBAN")
    sheet_merchants.write(row, 3, "BIC")
    sheet_merchants.write(row, 4, translate("Total value to be paid"))
    for merchant_service_user in merchants.keys():
        merchant = merchants[merchant_service_user]
        row += 1
        sheet_merchants.write(row, 0, merchant.name)
        sheet_merchants.write(row, 1, merchant.address)
        sheet_merchants.write(row, 2, merchant.iban or u"")
        sheet_merchants.write(row, 3, merchant.bic or u"")
        sheet_merchants.write(
            row, 4,
            round(
                merchant_transactions[merchant_service_user]["value"] / 100.0,
                2))

    row += 2
    sheet_merchants.write(row, 0, translate("total"))
    sheet_merchants.write(row, 4, xlwt.Formula('SUM(E2:E%s)' % (row - 1)))

    # TAB 3
    sheet_vouchers = book.add_sheet(translate("Vouchers in circulation"))
    row = 0
    write_header(sheet_vouchers, row, translate, "Voucher", "Internal account",
                 "Cost center", "Date", "Remaining value")

    for voucher in vouchers:
        unique_vouchers[voucher.key()] = voucher
        row += 1
        sheet_vouchers.write(row, 0, voucher.uid)
        sheet_vouchers.write(row, 1, voucher.internal_account)
        sheet_vouchers.write(row, 2, voucher.cost_center)
        sheet_vouchers.write(row, 3, voucher.dt)
        value = voucher.value - voucher.redeemed_value
        sheet_vouchers.write(row, 4, round(value / 100.0, 2))

    row += 2
    sheet_vouchers.write(row, 0, translate("total"))
    sheet_vouchers.write(row, 2, xlwt.Formula('SUM(E2:E%s)' % (row - 1)))

    # TAB 4
    expired_vouchers_sheet = book.add_sheet(translate("expired"))
    row = 0
    write_header(expired_vouchers_sheet, row, translate, "Voucher",
                 "Internal account", "Cost center", "Date", "Expiration date",
                 "Remaining value")

    for voucher in expired_vouchers:
        row += 1
        expired_vouchers_sheet.write(row, 0, voucher.uid)
        expired_vouchers_sheet.write(row, 1, voucher.internal_account)
        expired_vouchers_sheet.write(row, 2, voucher.cost_center)
        expired_vouchers_sheet.write(
            row, 3, format_timestamp(voucher.created, sln_settings))
        expired_vouchers_sheet.write(
            row, 4,
            format_timestamp(voucher.expiration_date,
                             sln_settings,
                             format='yyyy-MM-dd'))
        value = voucher.value - voucher.redeemed_value
        expired_vouchers_sheet.write(row, 5, round(value / 100.0, 2))

    row += 2
    expired_vouchers_sheet.write(row, 0, translate("total"))
    expired_vouchers_sheet.write(row, 5,
                                 xlwt.Formula('SUM(F2:F%s)' % (row - 1)))

    # TAB 5
    sheet_voucher_details = book.add_sheet(translate("Voucher details"))
    row = 0
    for voucher in sorted(unique_vouchers.itervalues(),
                          key=lambda v: v.created):
        voucher_transactions = [h for h in voucher.load_transactions()]
        sheet_voucher_details.write(row, 0, translate("Voucher"))
        sheet_voucher_details.write(row, 1, voucher.uid)
        sheet_voucher_details.write(row, 2, translate("Remaining value"))
        sheet_voucher_details.write(
            row, 3,
            xlwt.Formula('SUM(D%s:D%s)' %
                         (row + 2, row + 1 + len(voucher_transactions))))

        row += 1
        sheet_voucher_details.write(row, 0, translate("Internal account"))
        sheet_voucher_details.write(row, 1, voucher.internal_account)
        sheet_voucher_details.write(row, 2, translate("Cost center"))
        sheet_voucher_details.write(row, 3, voucher.cost_center)

        for history in reversed(voucher_transactions):
            merchant_service_user = history.service_user or service_user
            if merchant_service_user not in merchants:
                merchants[merchant_service_user] = get_solution_settings(
                    merchant_service_user)

            row += 1
            dt = format_timestamp(history.created, sln_settings)

            sheet_voucher_details.write(row, 0, dt)
            sheet_voucher_details.write(row, 1,
                                        merchants[merchant_service_user].name)
            sheet_voucher_details.write(row, 2, history.action_str)
            if history.action == SolutionCityVoucherTransaction.ACTION_ACTIVATED or \
                    history.action == SolutionCityVoucherTransaction.ACTION_REDEEMED:
                sheet_voucher_details.write(row, 3,
                                            round(history.value / 100.0, 2))

        row += 2

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

    second_day_of_last_month = first_day_of_last_month + 86400
    d = datetime.fromtimestamp(second_day_of_last_month)

    sln_city_voucher_export_key = SolutionCityVoucherExport.create_key(
        app_id, d.year, d.month)
    sln_city_voucher_export = SolutionCityVoucherExport(
        key=sln_city_voucher_export_key)
    sln_city_voucher_export.xls = excel_string
    sln_city_voucher_export.year_month = d.year * 100 + d.month
    sln_city_voucher_export.put()

    for merchant_service_user in merchant_transactions.keys():
        deferred.defer(
            create_voucher_statistics_for_service,
            merchants[merchant_service_user], app_id, customer.language,
            merchant_transactions[merchant_service_user]["transactions"],
            d.year, d.month)

    to_emails = sln_settings.inbox_mail_forwarders
    if to_emails:
        solution_server_settings = get_solution_server_settings()
        attachments = []
        attachments.append(
            ('%s %s-%s.xls' % (translate('Vouchers'), d.year, d.month),
             base64.b64encode(excel_string)))
        subject = translate('Vouchers export')
        message = translate('see_attachment_for_vouchers_export')
        send_mail(solution_server_settings.shop_export_email,
                  to_emails,
                  subject,
                  message,
                  attachments=attachments)
Exemplo n.º 24
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