Exemple #1
0
def new_order_notification(notification_dict):
    google_order_number = int(notification_dict['google-order-number'])
    purchase = models.Purchase.all().filter("google_order_number =", google_order_number).get()
    cart = models.Cart.get(notification_dict['cart-key'])
    email = notification_dict['email']
    if cart is not None:
        cart.status = 'Order Received'
        cart.put()
    if not purchase:
        purchase_key = models.generate_purchase_key()
        purchase_id = int(purchase_key.split('-')[0]) #get the id from the beginning of the key
        purchase = models.Purchase(
            key_name=purchase_key,
            purchase_email=email,
            purchase_id=purchase_id,
            user=cart.user,
            google_order_number=google_order_number,
            )
    if cart is None: #cart wasn't found - unlikely
        purchase.errors = 'cart not found'

    purchase.item = str(notification_dict['merchant-item-id'])
    purchase.quantity = int(notification_dict['quantity'])
    purchase.put()
    return True
def amount_notification(notification_dict):
    "Charge the purchase and give the user access to the files."
    purchase = models.Purchase.all().filter("google_order_number =", int(notification_dict['google-order-number'])).get()
    errors = [purchase.errors]
    if purchase:#add charge amount and expiration date, charge date
        purchase.purchase_charged(notification_dict['total-charge-amount'])
    else: #orphan amount notification - just in case
        errors.append('no previous notification for this order') 
        purchase_key = models.generate_purchase_key()
        purchase_id = int(purchase_key.split('-')[0])
        purchase = models.Purchase(key_name = purchase_key, errors = errors, purchase_id = purchase_id, google_order_number = int(notification_dict['google-order-number']))
        purchase.purchase_charged(notification_dict['total-charge-amount'])
    user = purchase.user #change user purchase access attribute
    session = None
    if user: #Google user associated with purchase
        user.purchase_access = True
        user.put()
        session = models.Session.all().filter('user ='******'google-order-number'])
        session = models.Session.all().filter('google_order_number =', google_order_number).get()
        if session is not None:
            session.load_values() #need to load values in order to update them
            session.add_purchase(purchase)
            session.put()
    emails.mail_user_purchase(purchase.purchase_email, str(purchase.key().name()))
    if errors:
        purchase.errors = models.build_string_from_list(errors, '\n')
        purchase.put()
    return True
def new_order_notification(notification_dict):
    "Create Purchase and PurchaseItems, but don't give access until the purchase is charged/amount notification received."
    #make sure this notification hasn't already been processed
    purchase = models.Purchase.all().filter(
        "google_order_number =",
        int(notification_dict['google-order-number'])).get()
    session = models.Session.get_by_key_name(
        notification_dict['session-key-name'])
    email = notification_dict['email']
    if session is not None:  #session dict will be empty, so we need this test
        session.load_values()
        if 'cart' in session:
            del session['cart']  #remove the purchased cart
            session['number_cart_items'] = 0
        if session.has_user(): user = session.user
        else: user = None
        session.google_order_number = int(
            notification_dict['google-order-number'])
        session.put()
    else:
        user = None
    errors = ''
    if not user: user = models.User().all().filter('email =', email).get()
    if not purchase:
        purchase_key = models.generate_purchase_key()
        purchase_id = int(purchase_key.split('-')
                          [0])  #get the id from the beginning of the key
        purchase = models.Purchase(
            key_name=purchase_key,
            purchase_email=email,  #give the purchase the shipping email address
            purchase_id=purchase_id,
            user=user,
            google_order_number=int(notification_dict['google-order-number']),
        )
    if session is None:  #session wasn't found - unlikely
        errors = 'session not found'
    if errors:
        purchase.errors = errors
    purchase.put(
    )  #need to add purchase to the datastore before we can add purchase items
    #get all the items from the notification_dict and add to the purchase
    item_ids = get_list_from_value(notification_dict['merchant-item-id'])
    quantities = get_list_from_value(notification_dict['quantity'])
    item_dict = dict(zip(item_ids, quantities))
    purchase.add_purchase_items(item_dict, user)
    return True
Exemple #4
0
def amount_notification(notification_dict):
    "Charge the purchase and give the user access to the files."

    # Get the purchase object built from the new order notification
    purchase = models.Purchase.all().filter("google_order_number =",
                                            int(notification_dict['google-order-number'])).get()

    cart = models.Cart.get(notification_dict['cart-key'])

    if purchase:
        if purchase.processed: # this is a duplicate notification
            return True

        purchase.total_charge_amount = float(notification_dict['total-charge-amount'])
        purchase.charge_date = datetime.utcnow()
        if cart:
            purchase.number_of_days = cart.number_of_days
        purchase.put()
    else: #orphan amount notification - just in case
        purchase_key = models.generate_purchase_key()
        purchase_id = int(purchase_key.split('-')[0])

        purchase = models.Purchase(
            key_name=purchase_key,
            errors='no previous notification for this order',
            purchase_id=purchase_id,
            google_order_number=int(notification_dict['google-order-number']),
            total_charge_amount=float(notification_dict['total-charge-amount']),
            charge_date=datetime.utcnow()
        )
        purchase.put()
        return True

    account = models.Account.all().filter("user", cart.user).get()
    if account:
        account.expiration_date = account.expiration_date + timedelta(days=+cart.number_of_days)
        account.trial = False
        account.put()
        purchase.processed = True
        purchase.put()

    if cart:
        cart.delete()

    return True
def amount_notification(notification_dict):
    "Charge the purchase and give the user access to the files."
    purchase = models.Purchase.all().filter(
        "google_order_number =",
        int(notification_dict['google-order-number'])).get()
    errors = [purchase.errors]
    if purchase:  #add charge amount and expiration date, charge date
        purchase.purchase_charged(notification_dict['total-charge-amount'])
    else:  #orphan amount notification - just in case
        errors.append('no previous notification for this order')
        purchase_key = models.generate_purchase_key()
        purchase_id = int(purchase_key.split('-')[0])
        purchase = models.Purchase(
            key_name=purchase_key,
            errors=errors,
            purchase_id=purchase_id,
            google_order_number=int(notification_dict['google-order-number']))
        purchase.purchase_charged(notification_dict['total-charge-amount'])
    user = purchase.user  #change user purchase access attribute
    session = None
    if user:  #Google user associated with purchase
        user.purchase_access = True
        user.put()
        session = models.Session.all().filter(
            'user ='******'google-order-number'])
        session = models.Session.all().filter('google_order_number =',
                                              google_order_number).get()
        if session is not None:
            session.load_values()  #need to load values in order to update them
            session.add_purchase(purchase)
            session.put()
    emails.mail_user_purchase(purchase.purchase_email,
                              str(purchase.key().name()))
    if errors:
        purchase.errors = models.build_string_from_list(errors, '\n')
        purchase.put()
    return True
def new_order_notification(notification_dict):
    "Create Purchase and PurchaseItems, but don't give access until the purchase is charged/amount notification received."
    #make sure this notification hasn't already been processed
    purchase = models.Purchase.all().filter("google_order_number =", int(notification_dict['google-order-number'])).get()
    session = models.Session.get_by_key_name(notification_dict['session-key-name'])
    email = notification_dict['email']
    if session is not None: #session dict will be empty, so we need this test
        session.load_values()
        if 'cart' in session: 
            del session['cart'] #remove the purchased cart
            session['number_cart_items'] = 0 
        if session.has_user(): user = session.user
        else: user = None
        session.google_order_number = int(notification_dict['google-order-number'])
        session.put()
    else: user = None
    errors = ''
    if not user: user = models.User().all().filter('email =', email).get()
    if not purchase:
        purchase_key = models.generate_purchase_key()
        purchase_id = int(purchase_key.split('-')[0]) #get the id from the beginning of the key
        purchase = models.Purchase(
                                   key_name = purchase_key,
                                   purchase_email = email, #give the purchase the shipping email address
                                   purchase_id = purchase_id,
                                   user = user,
                                   google_order_number = int(notification_dict['google-order-number']),
                                   )
    if session is None: #session wasn't found - unlikely
        errors = 'session not found'
    if errors:
        purchase.errors = errors
    purchase.put() #need to add purchase to the datastore before we can add purchase items
    #get all the items from the notification_dict and add to the purchase
    item_ids = get_list_from_value(notification_dict['merchant-item-id'])
    quantities = get_list_from_value(notification_dict['quantity'])
    item_dict = dict(zip(item_ids, quantities))
    purchase.add_purchase_items(item_dict, user)
    return True
    def post(self):
        if self.template_values['admin']:
            action = self.request.get('action')
            model = self.request.get('item')
            id = self.request.get(
                'id')  #entity id for purchases and title for pages
            post_url = self.build_post_url(action, model, id)
            if action == 'new':
                entity = False
                page_title = 'New ' + model.capitalize()
                data = self.model_form_dict[model](data=self.request.POST)
            else:
                entity = self.get_entity(model, id)
                if not entity:
                    self.generate_error(404)
                    return
                if model == 'product':
                    old_tag_list = entity.tags  #keep copy of old tags
                    original_product_status = entity.active  #keep copy of original active status
                    original_title = entity.title
                if not action:
                    page_title = 'Edit'
                    data = self.model_form_dict[model](data=self.request.POST,
                                                       instance=entity)
                elif action == 'delete':
                    redirect_url = '/admin/home?item=%ss' % (model)
                    if model == 'product':  #make sure entity is not purchased before deleting
                        is_entity_purchased = entity.is_entity_purchased()
                        if is_entity_purchased:
                            redirect_url = '/edit?action=delete&item=product&id=%s' % str(
                                entity.key().id())
                            self.redirect(redirect_url +
                                          '&errors=product_purchased')
                            return
                        else:
                            models.update_popular_products()
                    entity.delete()
                    self.redirect(redirect_url)
                    return
                else:
                    self.generate_error(400)
                    return

            data_valid = data.is_valid()
            if data_valid:  #check the to make sure the data is valid
                entity = data.save(commit=False)
                if model == 'product':
                    product_tags = self.request.POST.get('tags')
                    new_tag_list = models.Tag.clean_tags(product_tags, ',')
                    entity.tags = new_tag_list
                elif model == 'page':  # for pages slugify the title for the url
                    entity.url = '%s' % (models.slugify(entity.title))
                elif model == 'purchase':  #for purchases
                    product_id_add_list = self.request.get_all('product_ids')
                    email = entity.purchase_email
                    user = models.User().all().filter('email =', email).get()
                    if user:
                        user.delete_sessions(
                        )  #delete current user session, so purchases get updated
                        entity.user = user
                    if action == "new":
                        key_name = models.generate_purchase_key()
                        entity.purchase_id = int(key_name.split('-')[0])
                        entity = models.Purchase(
                            key_name=key_name,
                            **dict([(prop, getattr(entity, prop))
                                    for prop in models.Purchase.properties()]))
                entity.put()
                if model == 'product':
                    need_update_popular_products = False
                    try:
                        entity.index()
                    except:
                        print 'indexing failed - at least one tag is required'
                    if action == 'new':
                        old_tag_list = []  #no tags for new product
                        need_update_popular_products = True
                    else:
                        if not entity.active == original_product_status or not entity.title == original_title:
                            need_update_popular_products = True
                    if not new_tag_list == old_tag_list:
                        models.Tag.update_tags(new_tag_list, old_tag_list,
                                               entity)
                        need_update_popular_products = True
                    if need_update_popular_products:
                        models.update_popular_products()
                elif model == 'purchase':
                    if product_id_add_list:
                        entity.admin_add_purchase_items(
                            entity.charge_date, product_id_add_list, user,
                            entity.total_charge_amount)
                self.redirect(self.build_redirect_url(entity, model))

            else:
                template = 'templates/edit.html'
                self.generate_edit_form(data,
                                        model,
                                        page_title,
                                        post_url,
                                        template,
                                        entity=entity)