def get(self):
     if not self.is_logged_in():
         self.redirect(LOGIN_PAGE_URL)
     else:
         from models import SubscriptionPeriod
         products = Product.get_all()
         subscription_periods = SubscriptionPeriod.get_all()
         self.render('select_products.html',
             products=products,
             subscription_periods=subscription_periods)
 def get(self):
     invoice_key = self.session.get('activation-invoice-key')
     invoice = db.get(db.Key(invoice_key))
     
     subscription_data = self.session['subscription-data']
     subscription_period = SubscriptionPeriod.get_by_period(subscription_data.get('period'))
     
     logging.info([(order.customer.first_name, unicode(order.subscription)) for order in invoice.orders])
     self.render('activate_overview.html', 
         subscription_period=subscription_period,
         invoice=invoice,
         return_url='%sactivate/complete' % (configuration.ROOT_URL,)
         )
    def post(self):
        # Get customer and create an invoice.
        customer = Customer.get_by_key_name(self.get_current_username())
        invoice = Invoice(customer=customer)
        invoice.put()

        # Now start processing subscription data.
        subscription_data = self.session['subscription-data']
        products = db.get([db.Key(key) for key in subscription_data.get('products')])
        period = subscription_data.get('period')
        subscription_period = SubscriptionPeriod.get_by_period(period)

        units = [product for product in products if 'product_keys' not in product.properties()]
        baskets = [product for product in products if 'product_keys' in product.properties()]

        activation_credentials_list = []
        orders = []
        for unit in units:
            subscription = Subscription.get_by_product_and_period(unit, period)

            order = Order(customer=customer, invoice=invoice, subscription=subscription)
            order.subscription_price = subscription.price
            order.subscription_general_sales_tax = subscription.general_sales_tax
            order.subscription_period_in_months = subscription.period_in_months
            order.subscription_free_period_in_months = subscription_period.free_period_in_months
            order.price = subscription.price + subscription.general_sales_tax
            order.subscription_currency = subscription.currency
            orders.append(order)
            order.put()

            unit_id = unit.key().id()
            activation_credentials = ActivationCredentials()
            activation_credentials.serial_number = self.get_argument('u_%d_serial_number' % unit_id)
            activation_credentials.machine_id = self.get_argument('u_%d_machine_id' % unit_id)
            activation_credentials.order = order
            activation_credentials.product = unit
            activation_credentials.customer = customer
            logging.info(activation_credentials)
            activation_credentials_list.append(activation_credentials)

        for basket in baskets:
            subscription = Subscription.get_by_product_and_period(basket, period)

            order = Order(customer=customer, invoice=invoice, subscription=subscription)
            order.subscription_price = subscription.price
            order.subscription_general_sales_tax = subscription.general_sales_tax
            order.subscription_period_in_months = subscription.period_in_months
            order.subscription_free_period_in_months = subscription_period.free_period_in_months
            order.price = subscription.price + subscription.general_sales_tax
            order.subscription_currency = subscription.currency
            orders.append(order)
            order.put()

            for unit in basket.products:
                basket_id = basket.key().id()
                unit_id = unit.key().id()

                activation_credentials = ActivationCredentials()
                activation_credentials.serial_number = self.get_argument('b_%d_u_%d_serial_number' % (basket_id, unit_id,))
                activation_credentials.machine_id = self.get_argument('b_%d_u_%d_machine_id' % (basket_id, unit_id,))
                activation_credentials.order = order
                activation_credentials.product = unit
                activation_credentials.customer = customer
                logging.info(activation_credentials)
                activation_credentials_list.append(activation_credentials)

        db.put(activation_credentials_list)
        
        invoice.total_price = sum([order.price for order in orders])
        invoice.currency = orders[0].subscription_currency
        invoice.put()
        
        self.session['activation-invoice-key'] = str(invoice.key())
        self.redirect('/activate/overview')