Exemple #1
0
 def get_data(self):
     user_id = self.enki_user.key.id()
     licences_to_activate = []
     # licences purchased by the user, available to activate or give. The user can only activate one licence per product. Licences activated by another user don't appear.
     licences_to_give = []
     # licences purchased by the user, available to give only as the user already activated a licence for the same product.
     licences_activated = []
     # licences activated byt the user (user purchased or received as gift).
     list_purchased = EnkiModelProductKey.fetch_by_purchaser(user_id)
     if list_purchased:
         for i, item in enumerate(list_purchased):
             item_licence_key = EnkiModelProductKey.insert_dashes_5_10(
                 item.licence_key)
             product_already_owned = EnkiModelProductKey.exist_product_by_activator(
                 user_id, item.product_name)
             if item.activated_by_user == -1:
                 if not product_already_owned:
                     licences_to_activate.append([
                         item.product_name, item_licence_key,
                         settings.product_displayname[item.product_name]
                     ])
                 else:
                     licences_to_give.append([
                         item.product_name, item_licence_key,
                         settings.product_displayname[item.product_name]
                     ])
     list_activated = EnkiModelProductKey.fetch_by_activator(user_id)
     if list_activated:
         for i, item in enumerate(list_activated):
             item_licence_key = EnkiModelProductKey.insert_dashes_5_10(
                 item.licence_key)
             licences_activated.append([
                 item.product_name, item_licence_key,
                 settings.product_displayname[item.product_name]
             ])
     error = self.session.pop('error_library', None)
     licence_key_value = self.session.pop('error_library_licence', None)
     data = [
         error, licences_to_activate, licences_to_give, licences_activated,
         licence_key_value
     ]
     return data
Exemple #2
0
    def post(self):
        self.check_CSRF()
        product = xstr(self.request.get('product'))
        quantity = xint(self.request.get('quantity'))
        order_type = xstr(self.request.get('order_type'))
        purchaser_email = xstr(self.request.get('purchaser_email'))
        purchaser_email = purchaser_email if purchaser_email else None
        purchaser_user_id = xint(self.request.get('purchaser_user_id'))
        purchaser_user_id = purchaser_user_id if purchaser_user_id != 0 else None
        info = xstr(self.request.get('info'))
        info = info if info else None

        order_id = webapp2_extras.security.generate_random_string(
            length=10, pool=webapp2_extras.security.DIGITS)
        licence_keys = EnkiModelProductKey.generate_licence_keys(quantity)
        licence_keys = licence_keys.replace('-', '').split()
        for licence_key in licence_keys:
            item = EnkiModelProductKey(licence_key=licence_key,
                                       order_id=order_id,
                                       product_name=product,
                                       shop_name='Generator',
                                       quantity=quantity,
                                       order_type=order_type,
                                       purchaser_email=purchaser_email,
                                       purchaser_user_id=purchaser_user_id,
                                       info=info)
            item.put()
        licence_key_display = []
        for item in licence_keys:
            item_dash = EnkiModelProductKey.insert_dashes_5_10(item)
            licence_key_display.append(item_dash)
        self.add_infomessage(
            MSG.INFORMATION(), '<h3>Licence keys generated</h3>' + '<ul>' +
            '<li>product_name = <em>' + product + '</em></li>' +
            '<li>order_type = <em>' + order_type + '</em></li>' +
            '<li>order_id = <em>' + order_id + '</em></li>' +
            '<li>quantity = <em>' + xstr(quantity) + '</em></li>' +
            '<li>Recipient mail (purchaser_email) = <em>' +
            (purchaser_email if purchaser_email else 'none') + '</em></li>' +
            '<li>Recipient user id (purchaser_user_id) = <em>' +
            (xstr(purchaser_user_id) if purchaser_user_id else 'none') +
            '</em></li>' + '<li>info = <em>' + (info if info else 'none') +
            '</em></li>' + '<li>licence_key(s) = <br><em>' +
            '<br>'.join(licence_key_display) + '</em></li>' + '</ul>')
        self.redirect('licencesgeneratefree')
Exemple #3
0
    def post_reauthenticated(self, params):
        licence_key_preset = params.get('licence_key_preset')
        licence_key_manual = params.get('licence_key_manual')
        user_id = self.enki_user.key.id()
        if EnkiModelBackoffTimer.get(str(user_id), True) == 0:
            licence_key_preset = licence_key_preset.strip()[:(
                EnkiModelProductKey.LICENCE_KEY_DASHES_LENGTH + 4
            )] if licence_key_preset else ''  # 4 allows for some leading and trailing characters
            licence_key_manual = licence_key_manual.strip()[:(
                EnkiModelProductKey.LICENCE_KEY_DASHES_LENGTH +
                4)] if licence_key_manual else ''
            licence_key = licence_key_manual
            is_manual = True
            if licence_key_preset and not licence_key_manual:
                licence_key = licence_key_preset
                is_manual = False
            if licence_key:
                if len(licence_key) < EnkiModelProductKey.LICENCE_KEY_LENGTH:
                    self.session['error_library'] = MSG.LICENCE_TOO_SHORT()
                    self.session['error_library_licence'] = licence_key
                elif len(licence_key) <= (
                        EnkiModelProductKey.LICENCE_KEY_DASHES_LENGTH):
                    licence_key_reduced = re.sub(
                        r'[^\w]', '',
                        licence_key)[:EnkiModelProductKey.LICENCE_KEY_LENGTH]
                    item = EnkiModelProductKey.get_by_licence_key(
                        licence_key_reduced)
                    if not item:
                        if is_manual:
                            self.session[
                                'error_library'] = MSG.LICENCE_INVALID()
                            self.session['error_library_licence'] = licence_key
                    elif item:
                        licence_key_formatted = EnkiModelProductKey.insert_dashes_5_10(
                            licence_key_reduced)
                        if item.activated_by_user == -1:
                            # the licence key is not activated.
                            if EnkiModelProductKey.exist_product_by_activator(
                                    user_id, item.product_name):
                                # the user has already activated a key for the same product
                                if is_manual:
                                    self.session[
                                        'error_library'] = MSG.LICENCE_ALREADY_ACTIVATED_GIVE(
                                            settings.product_displayname[
                                                item.product_name])
                                    self.session[
                                        'error_library_licence'] = licence_key_formatted
                            else:
                                # activate the licence
                                item.activated_by_user = user_id
                                item.put()
                                EnkiModelBackoffTimer.remove(str(user_id))
                                self.add_infomessage(
                                    MSG.SUCCESS(),
                                    MSG.PRODUCT_LICENCE_ACTIVATED(
                                        settings.product_displayname[
                                            item.product_name],
                                        licence_key_formatted))
                        elif item.activated_by_user == user_id:
                            # the user has already activated this specific key
                            if is_manual:
                                self.session[
                                    'error_library'] = MSG.PRODUCT_ALREADY_ACTIVATED(
                                        settings.product_displayname[
                                            item.product_name])
                                self.session[
                                    'error_library_licence'] = licence_key_formatted
                            else:
                                self.add_infomessage(
                                    MSG.INFORMATION(),
                                    MSG.PRODUCT_ALREADY_ACTIVATED(
                                        settings.product_displayname[
                                            item.product_name]))
                        else:
                            # another user has activated the key
                            if is_manual:
                                self.session[
                                    'error_library'] = MSG.LICENCE_ANOTHER_USER_ACTIVATED(
                                        settings.product_displayname[
                                            item.product_name],
                                        licence_key_formatted)
                                self.session[
                                    'error_library_licence'] = licence_key_formatted
                            else:
                                self.add_infomessage(
                                    MSG.INFORMATION(),
                                    MSG.LICENCE_ANOTHER_USER_ACTIVATED(
                                        settings.product_displayname[
                                            item.product_name],
                                        licence_key_formatted))
                else:
                    self.session['error_library'] = MSG.LICENCE_TOO_LONG()
                    self.session['error_library_licence'] = licence_key
            elif is_manual:
                self.session['error_library'] = MSG.LICENCE_MISSING()
        else:
            backoff_timer = EnkiModelBackoffTimer.get(str(user_id))
            if backoff_timer != 0:
                self.session['error_library'] = MSG.TIMEOUT(
                    enki.libutil.format_timedelta(backoff_timer))

        self.render_tmpl('library.html',
                         active_menu='profile',
                         data=self.get_data())
Exemple #4
0
    def post(self):
        if not settings.SECRET_FASTSPRING or enki.libutil.is_debug(
        ) or settings.ENKI_EMULATE_STORE:
            self.check_CSRF()

            product = xstr(self.request.get('product'))
            quantity = xint(self.request.get('quantity'))
            purchase_price = xstr(self.request.get('purchase_price'))
            purchaser_email = xstr(self.request.get('purchaser_email'))
            order_type = xstr(self.request.get('order_type'))
            licence_keys = 'not generated'
            user_id = ''
            order_id = webapp2_extras.security.generate_random_string(
                length=10, pool=webapp2_extras.security.DIGITS)

            url = webapp2.uri_for('generatelicencefastspring', _full=True)
            form_fields = {
                'secret': 'pretendsecret',
                'quantity': str(quantity)
            }
            form_data = enki.libutil.urlencode(form_fields)
            result = urlfetch.fetch(url=url,
                                    payload=form_data,
                                    method=urlfetch.POST)
            if result.status_code == 200:
                licence_keys = result.content.replace('-', '')

            referrer = xstr(self.request.get('referrer'))
            token = EnkiModelTokenVerify.get_by_token_type(
                referrer, 'purchasebyuser')
            if token:
                user_id = token.user_id
            licence_key_display = []
            for item in licence_keys.split():
                item_dash = EnkiModelProductKey.insert_dashes_5_10(item)
                licence_key_display.append(item_dash)
            self.add_infomessage(
                MSG.INFORMATION(),
                '<h3>FastSpring Store Emulator - Step 1</h3>' +
                '<h4>Emulated purchase details</h4>' + '<ul>' +
                '<li>&lt;EnkiModelProductKey&gt; #{FastSpring variable} = <em>&lt;emulated value&gt;</em></li>'
                + '<li>product_name #{orderItem.productName} = <em>' +
                product + '</em></li>' + '<li>order_id #{order.id} = <em>' +
                order_id + '</em></li>' +
                '<li>quantity #{orderItem.quantity} = <em>' + xstr(quantity) +
                '</em></li>' +
                '<li>purchase_price #{orderItem.totalUSD} = <em>' +
                purchase_price + '</em></li>' +
                '<li>purchaser_email #{order.customer.email} = <em>' +
                purchaser_email + '</em></li>' +
                '<li>licence_key #{orderItem.fulfillment.licence.licences.list} = <br><em><b>'
                + '<br>'.join(licence_key_display) + '</b></em></li>' +
                '<li>shop_name = <em>Emulator</em></li>' +
                '<li>order_type = <em>' + order_type + '</em></li>' + '</ul>'
                '<h4>Internal data - generated if the purchaser was logged in when they bought the product</h4>'
                + '<ul>' +
                '<li>EnkiModelTokenVerify.user_id = purchaser user_id = <em>' +
                (xstr(user_id) if user_id else 'None') + '</em></li>' +
                '<li>EnkiModelTokenVerify.type purchasebyuser referrer #{order.referrer} = <em>'
                + (xstr(referrer) if referrer else 'None') + '</em></li>' +
                '</ul>')

            url = webapp2.uri_for('ordercompletefastspring', _full=True)
            form_fields = {
                'licence_key': licence_keys,
                'purchase_price': purchase_price,
                'order_id': order_id,
                'order_type': order_type,
                'product_name': product,
                'purchaser_email': purchaser_email,
                'shop_name': 'Emulator',
                'quantity': quantity,
                'referrer': referrer,
                'is_test': 'true'
            }

            form_data = enki.libutil.urlencode(form_fields)
            result = urlfetch.fetch(url=url,
                                    payload=form_data,
                                    method=urlfetch.POST)
            if result.status_code == 200:
                message_view_library = ''
                if self.is_logged_in():
                    message_view_library = '<p><a href="/profile" class="alert-link">View and activate licence keys</a>.</p>'
                self.add_infomessage(
                    MSG.INFORMATION(),
                    '<h3>FastSpring Store Emulator - Step 2</h3><p>Purchase records created<p>'
                    + message_view_library)
            else:
                self.add_infomessage(
                    MSG.WARNING(),
                    '<h3>FastSpring Store Emulator - Step 2 FAILED: Purchase records not created</h3>'
                )

            self.redirect_to_relevant_page()