Ejemplo n.º 1
0
def coupons(page):
    search_form = SearchForm()
    bulk_form = BulkDeleteForm()
    
    sort_by = Coupon.sort_by(request.args.get('sort', 'created_on'),
                             request.args.get('direction', 'desc'))
    
    #order_values = f'{sort_by[0], sort_by[1]}'
    order_values = '{0} {1}'.format(sort_by[0], sort_by[1])
    
    #q = request.args.get('q', text(''))

    #if q == '':
    #    q = text('')
    #print("@@ASDAS", q)

    paginated_coupons = Coupon.query \
        .filter(Coupon.search(request.args.get('q', text(''))))\
            .order_by(text(order_values))\
                .paginate(page, 50, True)
    
    print("@@@@", paginated_coupons)
    print("$$$$$$$$$$$$$$$$",dir(request.endpoint))
    return render_template('admin/coupon/index.html',
                            form=search_form, bulk_form=bulk_form,
                            coupons=paginated_coupons)
Ejemplo n.º 2
0
    def test_coupon_should_not_get_invalidated(self, session, coupons):
        """ Coupons that haven't expired should remain valid. """
        may_29_2015 = datetime.datetime(2015, 5, 29, 0, 0, 0)
        may_29_2015 = pytz.utc.localize(may_29_2015)

        june_29_2015 = datetime.datetime(2015, 6, 29, 0, 0, 0)
        june_29_2015 = pytz.utc.localize(june_29_2015)

        Coupon.expire_old_coupons(may_29_2015)

        coupon = Coupon.query.filter(Coupon.redeem_by == june_29_2015)
        assert coupon.first().valid is True
Ejemplo n.º 3
0
    def test_coupon_should_get_invalidated(self, session, coupons):
        """ Coupons that are not redeemable should expire. """
        may_29_2015 = datetime.datetime(2015, 5, 29, 0, 0, 0)
        may_29_2015 = pytz.utc.localize(may_29_2015)

        june_29_2015 = datetime.datetime(2015, 6, 29, 0, 0, 0)
        june_29_2015 = pytz.utc.localize(june_29_2015)

        Coupon.expire_old_coupons(june_29_2015)

        coupon = Coupon.query.filter(Coupon.redeem_by == may_29_2015)
        assert coupon.first().valid is False
Ejemplo n.º 4
0
def coupon_bulk_delete():
    form = BulkDeleteForm()

    if form.validate_on_submit():
        ids = Coupon.get_bulk_action_ids(request.form.get('scope'),
                                         request.form.getlist('bulk_id'),
                                         query=request.args.get('q', ''))

        Coupon.bulk_delete(ids)

        flash('{0} coupon(s) will be deleted.'.format(len(ids)), 'succes')
    else:
        flash('No coupon were deleted, something went wrong', 'error')
    return redirect(url_for('admin.coupon'))
Ejemplo n.º 5
0
def coupons(db):
    """
    Create coupon fixtures.

    :param db: Pytest fixture
    :return: SQLAlchemy database session
    """
    db.session.query(Coupon).delete()

    may_29_2015 = datetime.datetime(2015, 5, 29, 0, 0, 0)
    may_29_2015 = pytz.utc.localize(may_29_2015)

    june_29_2015 = datetime.datetime(2015, 6, 29)
    june_29_2015 = pytz.utc.localize(june_29_2015)

    coupons = [
        {
            'amount_off': 1,
            'redeem_by': may_29_2015
        },
        {
            'amount_off': 1,
            'redeem_by': june_29_2015
        },
        {
            'amount_off': 1
        }
    ]

    for coupon in coupons:
        db.session.add(Coupon(**coupon))

    db.session.commit()

    return db
Ejemplo n.º 6
0
def expire_old_coupons():
    """
    Invalidate coupons that are past their redeem date.

    :return: Result of updating the records
    """
    return Coupon.expire_old_coupons()
Ejemplo n.º 7
0
def delete_coupons(ids):
    """
    Delete coupons both on the payment gateway and locally.
    :param ids: List of ids to be deleted
    :type ids: list
    :return: int
    """
    return Coupon.bulk_delete(ids)
Ejemplo n.º 8
0
def coupons(page):
    search_form = SearchForm()
    bulk_form = BulkDeleteForm()

    sort_by = Coupon.sort_by(request.args.get('sort', 'created_on'),
                             request.args.get('direction', 'desc'))
    order_values = '{0} {1}'.format(sort_by[0], sort_by[1])

    paginated_coupons = Coupon.query \
        .filter(Coupon.search(request.args.get('q', text('')))) \
        .order_by(text(order_values)) \
        .paginate(page, 50, True)

    return render_template('admin/coupon/index.html',
                           form=search_form,
                           bulk_form=bulk_form,
                           coupons=paginated_coupons)
Ejemplo n.º 9
0
def coupons():
    """Fucntion to list all users in the database."""
    form_search = SearchForm()
    bulk_form = BulkDeleteForm()
    sort_by = Coupon.sort_by(request.args.get('sort', 'created_on'),
                             request.args.get('direction', 'desc'))
    order_values = '{0} {1}'.format(sort_by[0], sort_by[1])

    page = request.args.get('page', 1, type=int)
    paginated_users = Coupon.query \
        .filter(Coupon.search(request.args.get('q', ''))) \
        .order_by(text(order_values)) \
        .paginate( page = page , per_page =current_app.config['FLASKY_POSTS_PER_PAGE'] , error_out = False )

    return render_template('admin/coupon/index.html',
                           form=form_search,
                           coupons=paginated_users,
                           bulk_form=bulk_form)
Ejemplo n.º 10
0
    def test_random_coupon_code(self):
        """ Random coupon code is created. """
        from snakeeyes.blueprints.billing.tasks import expire_old_coupons

        expire_old_coupons.delay()

        random_coupon = Coupon.random_coupon_code()
        assert len(random_coupon) == 14
        assert random_coupon.isupper()
Ejemplo n.º 11
0
def delete_coupons(ids):
    """
    Delete coupons on the payment gateway and locally.

    :param ids: List of ids to be deleted.
    :return: Bulk delete coupons.
    """

    return Coupon.bulk_delete(ids)
Ejemplo n.º 12
0
def coupon_code():
    code = request.form.get('coupon_code')
    if code is None:
        return render_json(422, {'error': 'Coupon code cannot be processed.'})

    coupon = Coupon.find_by_code(code)
    if coupon is None:
        return render_json(404, {'error': 'Coupon code not found.'})

    return render_json(200, {'data': coupon.to_json()})
Ejemplo n.º 13
0
def coupons_new():
    coupon = Coupon()
    form = CouponForm(obj=coupon)

    if form.validate_on_submit():
        form.populate_obj(coupon)

        params = {
            'code': coupon.code,
            'duration': coupon.duration,
            'percent_off': coupon.percent_off,
            'amount_off': coupon.amount_off,
            'currency': coupon.currency,
            'redeem_by': coupon.redeem_by,
            'max_redemptions': coupon.max_redemptions,
            'duration_in_months': coupon.duration_in_months,
        }

        if Coupon.create(params):
            flash('Coupon has been created successfully.', 'success')
            return redirect(url_for('admin.coupons'))

    return render_template('admin/coupon/new.html', form=form, coupon=coupon)
Ejemplo n.º 14
0
def coupons_bulk_delete():
    form = BulkDeleteForm()

    if form.validate_on_submit():
        ids = Coupon.get_bulk_action_ids(request.form.get('scope'),
                                         request.form.getlist('bulk_ids'),
                                         query=request.args.get('q', None))

        # Prevent circular imports.
        from snakeeyes.blueprints.billing.tasks import delete_coupons

        delete_coupons.delay(ids)

        flash('{0} coupons(s) were scheduled to be deleted.'.format(len(ids)),
              'success')
    else:
        flash('No coupons were deleted, something went wrong.', 'error')

    return redirect(url_for('admin.coupons'))
Ejemplo n.º 15
0
def coupons_bulk_delete():
    form = BulkDeleteForm()

    if form.validate_on_submit():

        q = request.args.get('q', text(''))
        if q == '':
            q = text('')

        ids = Coupon.get_bulk_action_ids(request.form.get('scope'),
                                         request.form.getlist('bulk_ids'),
                                         query=q)

        from snakeeyes.blueprints.billing.tasks import delete_coupons

        delete_coupons.delay(ids)

        flash(f'{len(ids)} coupons were scheduled to be deleted.', 'success')

    else:
        flash('No coupons were deleted. Something went wrong.', 'error')

    return redirect(url_for('admin.coupons'))
Ejemplo n.º 16
0
    def test_coupon_without_redeem_by_should_be_valid(self, session, coupons):
        """ Coupons that do not expire should be valid. """
        Coupon.expire_old_coupons()

        coupon = Coupon.query.filter(Coupon.redeem_by.is_(None))
        assert coupon.first().valid is True
Ejemplo n.º 17
0
def expire_old_coupons():
    """
    Invalidate coupons that are expired.
    :return: Update records
    """
    return Coupon.expire_old_coupons()