def link_stripe_to_customer(customer_id_or_service_email, stripe_token, stripe_token_created, contact_id): solution_server_settings = get_solution_server_settings() stripe.api_key = solution_server_settings.stripe_secret_key if isinstance(customer_id_or_service_email, unicode): customer = Customer.get_by_service_email(customer_id_or_service_email) else: customer = Customer.get_by_id(customer_id_or_service_email) # Resellers and their customers should not be able to do this google_user = gusers.get_current_user() if not customer.team.legal_entity.is_mobicage: logging.error( "user %s tried to access function 'link_stripe_to_customer'", google_user.email()) raise NoPermissionException() if google_user: azzert(user_has_permissions_to_team(google_user, customer.team_id)) if customer.stripe_id: stripe_customer = stripe.Customer.retrieve(customer.stripe_id) card = stripe_customer.cards.create(card=stripe_token) stripe_customer.default_card = card.id stripe_customer.save() else: stripe_customer = stripe.Customer.create( card=stripe_token, description=u"%s %s -- %s, %s, %s, %s, %s, %s" % (customer.id, customer.vat, customer.name, customer.address1, customer.address2, customer.zip_code, customer.city, customer.country)) card = stripe_customer.cards.data[0] try_or_defer(store_stripe_link_to_datastore, customer.key(), stripe_customer.id, card.id, stripe_token_created, contact_id)
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)
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)
def get_facebook_app_info(domain): settings = get_solution_server_settings() apps = settings.facebook_apps for host, app_id, app_secret in chunks(apps, 3): if host == domain.lower().strip(): return app_id, app_secret
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)
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 get(self): result = self.request.get("result", "") settings = get_solution_server_settings() path = os.path.join(os.path.dirname(__file__), 'settings.html') self.response.out.write( template.render( path, dict(result=result, settings=model_to_yaml(settings))))
def get(self): email = self.request.get('email') if email.endswith('.'): email = email[:-1] data = self.request.get('data') if email and data: try: complete_customer_signup(email, data) except ExpiredUrlException: return self.return_error("link_expired", action='') except AlreadyUsedUrlException: return self.return_error("link_is_already_used", action='') except InvalidUrlException: return self.return_error('invalid_url') params = { 'email_verified': True, } else: apps = get_all_signup_enabled_apps() solution_server_settings = get_solution_server_settings() params = { 'apps': apps, 'recaptcha_site_key': solution_server_settings.recaptcha_site_key, 'email_verified': False, } params['signup_success'] = json.dumps(self.render('signup_success')) self.response.write(self.render('signup', **params))
def send_smart_email(email_id, to, add_recipients_to_list=True): """Send a smart email""" if DEBUG: logging.debug('Not sending out smart email %s to %s because DEBUG=True', email_id, to) return False solution_server_settings = get_solution_server_settings() credentials = base64.b64encode('%s:' % solution_server_settings.createsend_api_key) data = json.dumps({ 'To': to, "AddRecipientsToList": add_recipients_to_list }) headers = { 'Content-Type': 'application/json', 'Authorization': 'Basic %s' % credentials } try: response = urlfetch.fetch(SEND_URL % email_id, method=urlfetch.POST, payload=data, headers=headers) if response.status_code >= 400: raise Exception('%s: %s' % (response.status_code, response.content)) result = json.loads(response.content) if result and isinstance(result, list): result = result[0] if 'Status' in result and result['Status'] == "Accepted": return True return False except: logging.error('Cannot send smart email to %s', to, exc_info=True, _suppress=False) return False
def signup(user, service_name, service_description): solution_server_settings = get_solution_server_settings() azzert(users.get_current_user() == users.User( solution_server_settings.solution_trial_service_email)) user = users.User(user) from rogerthat.bizz.service.yourservicehere import signup as trial_signup return trial_signup(user, service_name, service_description, True)
def get(self): solution_server_settings = get_solution_server_settings() for module_name, service_user in chunks(solution_server_settings.solution_news_scrapers, 2): try: module = importlib.import_module("solutions.common.cron.news.%s" % module_name) getattr(module, 'check_for_news')(users.User(service_user)) except: pass
def _validate_request(handler): solution_server_settings = get_solution_server_settings() secret = handler.request.headers.get("X-BOB-SECRET") if not solution_server_settings.bob_api_secret: logging.error("bob_api_secret is not set yet") handler.abort(401) if secret != solution_server_settings.bob_api_secret: handler.abort(401)
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
def get(self): solution_server_settings = get_solution_server_settings() if not solution_server_settings.tropo_callback_token: logging.error("tropo_callback_token is not set yet") self.abort(401) if self.request.get( 'token') != solution_server_settings.tropo_callback_token: self.abort(401) prospect_id = self.request.get('prospect_id') if not prospect_id: logging.warn( "missing prospect_id in prospect callback invite result") self.abort(401) status = self.request.get('status') if not status: logging.warn("missing status in prospect callback invite result") self.abort(401) if status not in Prospect.INVITE_RESULT_STRINGS: logging.warn( "got unexpected status in prospect callback invite result %s", status) self.abort(401) prospect_interaction = db.get(prospect_id) if not prospect_interaction: logging.warn("could not find prospect with key %s", prospect_id) self.abort(401) prospect = prospect_interaction.prospect logging.info( "Process callback invite result for prospect with id '%s' and status '%s'", prospect.id, status) if status == Prospect.INVITE_RESULT_STRING_YES: prospect_interaction.code = prospect.invite_code = Prospect.INVITE_CODE_ANSWERED prospect_interaction.result = prospect.invite_result = Prospect.INVITE_RESULT_YES elif status == Prospect.INVITE_RESULT_STRING_NO: prospect_interaction.code = prospect.invite_code = Prospect.INVITE_CODE_ANSWERED prospect_interaction.result = prospect.invite_result = Prospect.INVITE_RESULT_NO elif status == Prospect.INVITE_RESULT_STRING_MAYBE: prospect_interaction.code = prospect.invite_code = Prospect.INVITE_CODE_ANSWERED prospect_interaction.result = prospect.invite_result = Prospect.INVITE_RESULT_MAYBE elif status == Prospect.INVITE_RESULT_STRING_NO_ANSWER: prospect_interaction.code = prospect.invite_code = Prospect.INVITE_CODE_NO_ANSWER elif status == Prospect.INVITE_RESULT_STRING_CALL_FAILURE: prospect_interaction.code = prospect.invite_code = Prospect.INVITE_CODE_CALL_FAILURE db.put([prospect, prospect_interaction]) self.response.out.write("Successfully processed invite result")
def post(self): sik = self.request.headers.get("X-Nuntiuz-Service-Key", None) solution_server_settings = get_solution_server_settings() if not sik or sik != solution_server_settings.shop_new_prospect_sik: logging.error("unauthorized access in callback handler: %s", sik) self.abort(401) return request = json.loads(self.request.body) # PERFORM CALL response = process_callback(request, sik) # WIRE RESULT self.response.headers['Content-Type'] = 'application/json-rpc' json.dump(response, self.response.out)
def post(self): settings_yaml = self.request.get("settings", "") try: settings = populate_model_by_yaml(get_solution_server_settings(), settings_yaml) 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 recaptcha_verify(recaptcha_response_token): """Verify the grecaptcha response token.""" solution_server_settings = get_solution_server_settings() params = { 'secret': solution_server_settings.recaptcha_secret_key, 'response': recaptcha_response_token } try: url = VERIFY_URL + '?' + urllib.urlencode(params) response = urlfetch.fetch(url, method=urlfetch.POST) result = json.loads(response.content) return result['success'] except: logging.error('cannot verify recaptcha response token', exc_info=True) return False
def create_prospect_from_customer(customer): azzert(customer.prospect_id is None and customer.service_email) contact = Contact.get_one(customer.key()) azzert(contact) si = get_default_service_identity(users.User(customer.service_email)) prospect = Prospect(key_name=str(uuid.uuid4()) + str(uuid.uuid4())) prospect.name = customer.name prospect.address = ', '.join(filter(None, [customer.address1 + ((' ' + customer.address2) if customer.address2 else ''), customer.zip_code, customer.city, OFFICIALLY_SUPPORTED_COUNTRIES.get(customer.country, customer.country)])) prospect.phone = contact.phone_number prospect.email = contact.email prospect.type = ['establishment'] if customer.organization_type == OrganizationType.EMERGENCY: prospect.type.append('health') prospect.customer_id = customer.id prospect.status = Prospect.STATUS_CUSTOMER prospect.app_id = si.app_id solution_server_settings = get_solution_server_settings() prospect.add_comment(u'Converted customer to prospect', users.User(solution_server_settings.shop_no_reply_email)) try: result = geo_code(prospect.address) except GeoCodeZeroResultsException: try: result = geo_code(' '.join(filter(None, [customer.zip_code, customer.city, OFFICIALLY_SUPPORTED_COUNTRIES.get(customer.country, customer.country)]))) except GeoCodeZeroResultsException: logging.warn('Could not geo_code customer: %s', db.to_dict(customer)) return prospect.geo_point = db.GeoPt(result['geometry']['location']['lat'], result['geometry']['location']['lng']) customer.prospect_id = prospect.id prospect.customer_id = customer.id logging.info('Creating prospect: %s', db.to_dict(prospect, dict(prospect_id=prospect.id))) put_and_invalidate_cache(customer, prospect) return prospect
def _get_client_flow(): if DEBUG: redirect_uri = "http://rt.dev:8080/common/events/google/oauth2callback" else: redirect_uri = "https://%s.appspot.com/common/events/google/oauth2callback" % APPENGINE_APP_ID redirect_uri = replace_url_with_forwarded_server(redirect_uri) solution_server_setting = get_solution_server_settings() flow = client.OAuth2WebServerFlow( client_id=solution_server_setting. solution_sync_calendar_events_client_id, client_secret=solution_server_setting. solution_sync_calendar_events_client_secret, scope='https://www.googleapis.com/auth/calendar.readonly email', redirect_uri=redirect_uri, approval_prompt='force') return flow
def service_auto_connect(service_user): from rogerthat.service.api.friends import invite as invite_api_call from rogerthat.bizz.friends import CanNotInviteFriendException solution_server_settings = get_solution_server_settings() with users.set_user(service_user): for invitee in solution_server_settings.solution_service_auto_connect_emails: try: invite_api_call(invitee, None, u"New Flex service created", DEFAULT_LANGUAGE, SERVICE_AUTOCONNECT_INVITE_TAG, None, App.APP_ID_ROGERTHAT) except CanNotInviteFriendException: logging.debug('%s is already connected with %s', invitee, service_user) except InvalidAppIdException: logging.debug('%s is not supported for %s', App.APP_ID_ROGERTHAT, service_user)
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>""")
def update_twitter_status(service_user, message, media_contents): """ Update twitter status. Args: service_user (users.User): users.User message (str) media_contents (list of str): every file/media content """ sln_settings = get_solution_settings(service_user) if not sln_settings.twitter_oauth_token: return False solution_server_settings = get_solution_server_settings() try: auth = OAuth(sln_settings.twitter_oauth_token, sln_settings.twitter_oauth_token_secret, \ solution_server_settings.twitter_app_key, solution_server_settings.twitter_app_secret) t = Twitter(auth=auth) media_ids = [] if media_contents: t_up = Twitter(domain='upload.twitter.com', auth=auth) for content in media_contents: try: media_id = t_up.media.upload( media=content)["media_id_string"] media_ids.append(media_id) except: logging.warn('upload media twitter failed', exc_info=True) message = message[:140] if media_ids: t.statuses.update(status=message, media_ids=",".join(media_ids)) else: t.statuses.update(status=message) return True except: logging.error('Update twitter status failed', exc_info=True) return False
def get_billing_credit_card_info(): service_user = users.get_current_user() try: customer = get_customer(service_user) if not customer: logging.error("Customer not found for %s", service_user) return None if not customer.stripe_id: logging.debug("No credit card coupled") return None solution_server_settings = get_solution_server_settings() stripe.api_key = solution_server_settings.stripe_secret_key stripe_customer = stripe.Customer.retrieve(customer.stripe_id) card = stripe_customer.cards.data[0] cc = CreditCardTO() cc.brand = card.brand cc.exp_month = card.exp_month cc.exp_year = card.exp_year cc.last4 = card.last4 return cc except BusinessException: return None
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)
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
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)
def post(self): # this url is used in the beacon configurator app to override the uuid, major and minor from rogerthat.pages.shortner import get_short_url_by_code url = self.request.POST.get("url", None) signature_client = self.request.POST.get("signature", None) if not (url and signature_client): logging.error("not all params given") self.abort(500) return signature_client = signature_client.upper() logging.info("validate beacon app url: %s and signature: %s", url, signature_client) solution_server_settings = get_solution_server_settings() signature_server = md5_hex( solution_server_settings.shop_beacons_app_secret % (url, url)).upper() logging.info("signature server: %s", signature_server) if not (url and signature_client == signature_server): logging.error("signature did not match") self.abort(500) return m = re.match("^(HTTP|http)(S|s)?://(.*)/(M|S)/(.*)$", url) if not m: logging.error("invalid url") self.abort(500) return _, _, _, _, code = m.groups() su = get_short_url_by_code(code) logging.info("validate short url: %s", su.full) if not su.full.startswith("/q/s/"): logging.error("short url does not start with /q/s") self.abort(500) return match = re.match("^/q/s/(.+)/(\\d+)$", su.full) if not match: logging.error("user_code not found in url") self.abort(500) return user_code = match.group(1) logging.info("validating user code: %s", user_code) sid = match.group(2) logging.info("validating sid: %s", sid) pp = ProfilePointer.get_by_key_name(user_code) if not pp: logging.error("ProfilePointer not found") self.abort(500) return sid = get_service_interaction_def(pp.user, int(sid)) if not sid: logging.error("sid not found") self.abort(500) return si = get_service_identity(sid.service_identity_user) if not si: logging.error("service_identity not found") self.abort(500) return def trans(): beacon = Beacon.all().ancestor(parent_key(si.service_user)).get() if beacon: return beacon.uuid, beacon.name app = App.get(App.create_key(si.app_id)) app.beacon_last_minor = app.beacon_last_minor + 1 name = "%s|%s" % (app.beacon_major, app.beacon_last_minor) logging.info("add_new_beacon: %s", name) if not add_new_beacon(app.beacon_uuid, name, u'Autoconnect', si.service_identity_user): raise Exception("Beacon already exists") app.put() return app.beacon_uuid, name xg_on = db.create_transaction_options(xg=True) beacon_uuid, beacon_name = db.run_in_transaction_options(xg_on, trans) major, minor = beacon_name.split("|") logging.info("Auto connecting beacon %s to service %s", beacon_name, si.service_identity_user) outfile = StringIO() s_ushort(outfile, int(major)) s_ushort(outfile, int(minor)) id_ = base64.b64encode(outfile.getvalue()) self.response.headers['Content-Type'] = 'text/json' self.response.write( json.dumps({ "uuid": beacon_uuid, "major": int(major), "minor": int(minor), "email": si.qualifiedIdentifier, "name": si.name, "id": id_ }))