Esempio n. 1
0
def index(category=None):
    Page.set_nav()
    pagination = request.args.get('page')
    try:
        pagination = int(pagination)
    except:
        pagination = 1
    related = request.args.get('related')
    related = Page.query.filter_by(slug=related).first()
    category = request.args.get('category')
    if category:
        category = Category.query.filter_by(name=category.lower()).first()
    products = Product.query.filter_by(active=True)
    if related:
        products = products.filter_by(linked_page_id=related.id)
    if category:
        products = products.filter_by(category_id=category.id)
    products = products.order_by('sort', 'name').paginate(page=pagination,
                                                          per_page=6)
    categories = Category.query.filter(Category.products.any()).all()
    page = Page.query.filter_by(slug='shop').first()
    if products and page:
        return render_template(
            f'shop/index.html',
            page=page,
            pagination=pagination,
            products=products,
            category=category,
            categories=categories,
        )
    page = Page.query.filter_by(slug='404-error').first()
    return render_template(f'page/{page.template}.html', page=page), 404
Esempio n. 2
0
def glossary(path):
    Page.set_nav()
    path = f"/{path}"
    page = Page.query.filter_by(path=path).first()
    definitions = {}
    for t in Definition.TYPE_CHOICES:
        definitions[t[1]] = []

    current_app.logger.debug(f'DEFINITIONS: {definitions}')
    if page:
        for d in Definition.query.filter_by(
                parent_id=page.id).order_by('name').all():
            definitions[d.type.title()] += [d]
        code = request.args['code'] if 'code' in request.args else None
        if page.published or page.check_view_code(code):
            return render_template(
                f'page/glossary.html',
                page=page,
                glossary=True,
                definitions=definitions,
                type_choices=Definition.TYPE_CHOICES,
                sorted=sorted,
                len=len,
            )
    current_app.logger.debug(f'DEFINITIONS: {definitions}')
    page = Page.query.filter_by(slug='404-error').first()
    return render_template(f'page/{page.template}.html', page=page), 404
Esempio n. 3
0
def product_data(slug):
    Page.set_nav()
    product = Product.query.filter_by(slug=slug, active=True).first()
    page = Page.query.filter_by(slug='shop').first()
    if product:
        current_app.logger.debug(product.__dict__)
        return jsonify(product.ghosted())
Esempio n. 4
0
def subscription(email, code):
    sub = Subscriber.query.filter_by(email=email).first()
    if sub and sub.check_update_code(code):
        Page.set_nav()
        form = SubscriptionForm()
        form.subscription.choices = Subscriber.SUBSCRIPTION_CHOICES
        choices = [c[0] for c in form.subscription.choices]
        for field in form:
            print(f"{field.name}: {field.data}")
        if form.validate_on_submit():
            print("Validated")
            current_app.logger.debug(form.subscription.data)
            if "all" in form.subscription.data:
                choices.remove("all")
                sub.subscription = "," + ",".join(choices) + ","
            else:
                sub.subscription = "," + ",".join(form.subscription.data) + ","
            current_app.logger.debug(sub.subscription)
            db.session.commit()
            current_app.logger.info(f'Subscription Updated!\n    {repr(sub)}')
            flash('Your subscription has been updated!', 'success')
            return redirect(
                url_for('page.subscription', email=email, code=code))
        form.subscription.data = sub.subscription[1:-1].split(',')
        return render_template(
            'update-subscription.html',
            form=form,
            subscriber=sub,
        )
    else:
        flash('Invalid code to update subscription!', 'danger')
        return redirect(url_for('page.home'))
Esempio n. 5
0
def view(slug):
    Page.set_nav()
    product = Product.query.filter_by(slug=slug, active=True).first()
    page = Page.query.filter_by(slug='shop').first()
    if product:
        if product.active or current_user.is_authenticated:
            related = Product.query.filter(
                Product.linked_page_id == product.linked_page_id,
                Product.id != product.id,
            ).order_by('sort', 'name').limit(4).all()
            page.title = f"Shop: {product.name}"
            price = product.price
            sale_text = ''
            if product.on_sale:
                price = product.sale_price if product.sale_price else product.price
                sale_text = "On Sale! "
            description = f'{sale_text}{product.description} Starting at {price}'
            return render_template(
                f'shop/view.html',
                page=page,
                product=product,
                related=related,
                banner=product.image,
                description=description,
            )
    page = Page.query.filter_by(slug='404-error').first()
    return render_template(f'page/{page.template}.html', page=page), 404
Esempio n. 6
0
def subscribe():
    Page.set_nav()
    form = SubscribeForm()
    form.subscription.choices = Subscriber.SUBSCRIPTION_CHOICES
    for field in form:
        print(f"{field.name}: {field.data}")
    if form.validate_on_submit():
        print("Validated")
        sub = Subscriber(
            email=form.email.data,
            first_name=form.first_name.data,
            last_name=form.last_name.data,
        )
        current_app.logger.debug(form.subscription.choices)
        if "all" in form.subscription.data:
            choices = [c[0] for c in form.subscription.choices]
            current_app.logger.debug(choices)
            choices.remove("all")
            sub.subscription = "," + ",".join(choices) + ","
        else:
            sub.subscription = "," + ",".join(form.subscription.data) + ","
        current_app.logger.debug(sub.subscription)
        #raise Exception('pause')
        db.session.add(sub)
        db.session.commit()
        sub.welcome()
        current_app.logger.info(f'New Subscriber!\n    {repr(sub)}')
        flash('You have subscribed successfully!', 'success')
        return redirect(url_for('page.home'))
    form.subscription.data = [i[0] for i in Subscriber.SUBSCRIPTION_CHOICES]
    return render_template('subscribe.html', form=form)
Esempio n. 7
0
def search(tag=None, keyword=None):
    Page.set_nav()
    tags = Tag.query.filter(Tag.pages != None).order_by('name').all()
    form = SearchForm()
    results = None
    if keyword != None:
        form.keyword.data = keyword
    print(keyword)
    if form.validate_on_submit():
        print(keyword)
        keyword = form.keyword.data
    if form.errors:
        for error in form.errors:
            print(error)
    if tag:
        results = Page.query.filter(Page.tags.any(name=tag),
                                    Page.published == True).order_by(
                                        'sort', 'pub_date', 'title').all()
    if keyword:
        results = Page.query.filter(Page.body.ilike(f'%{keyword}%'),
                                    Page.published == True).order_by(
                                        'sort', 'pub_date', 'title').all()
    return render_template('page/search.html',
                           form=form,
                           keyword=keyword,
                           tag=tag,
                           tags=tags,
                           results=results,
                           page=Page.query.filter_by(slug='search').first())
Esempio n. 8
0
def add_page():
    form = AddPageForm()
    for field in form:
        print(f"{field.name}: {field.data}")
    form.parent_id.choices = [(0, '---')] + [(p.id, f"{p.title} ({p.path})")
                                             for p in Page.query.all()]
    form.user_id.choices = [(u.id, u.username) for u in User.query.all()]
    form.notify_group.choices = [
        ('', ''), ('all', 'All')
    ] + current_app.config['SUBSCRIPTION_GROUPS'] + [
        ('discord', 'Discord Only')
    ]
    if form.validate_on_submit():
        parentid = form.parent_id.data if form.parent_id.data else None
        page = Page(
            title=form.title.data,
            slug=form.slug.data,
            template=form.template.data,
            parent_id=parentid,
            cover=form.cover.data,
            banner=form.banner.data,
            body=form.body.data,
            notes=form.notes.data,
            summary=form.summary.data,
            author_note=form.author_note.data,
            author_note_location=form.author_note_location.data,
            sidebar=form.sidebar.data,
            tags=form.tags.data,
            user_id=current_user.id,
            notify_group=form.notify_group.data,
            published=form.published.data,
        )
        pdate = form.pub_date.data
        ptime = form.pub_time.data
        local_tz = form.timezone.data if form.timezone.data else current_user.timezone
        if pdate and ptime:
            page.set_local_pub_date(f"{pdate} {ptime}", local_tz)
        page.set_path()
        db.session.add(page)
        db.session.commit()
        if form.notify_subs.data:
            page.notify_subscribers(form.notify_group.data)
        flash("Page added successfully.", "success")
        log_new(page, 'added a page')
        Page.set_nav()
        return redirect(url_for('admin.edit_page', id=page.id))
    if form.errors:
        flash("<b>Error!</b> Please fix the errors below.", "danger")
    return render_template('admin/page-edit.html',
                           form=form,
                           tab='pages',
                           action='Add',
                           page=Page.query.filter_by(slug='admin').first())
Esempio n. 9
0
def home():
    comment_form = AuthenticatedCommentForm(
    ) if current_user.is_authenticated else CommentForm()
    comment_form.subscribe.data = False if current_user.is_authenticated else True
    Page.set_nav()
    page = Page.query.filter_by(slug='home', published=True).first()
    if page:
        comment_form.page_id.data = page.id
        return render_template(f'page/{page.template}.html', page=page)
    return render_template('home.html',
                           page='page',
                           comment_form=comment_form,
                           js='comments.js')
Esempio n. 10
0
def index():
    Page.set_nav()
    products = Product.query.filter_by(active=True).order_by('sort',
                                                             'name').all()
    page = Page.query.filter_by(slug='shop').first()
    if products and page:
        return render_template(
            f'shop/index.html',
            page=page,
            products=products,
        )
    page = Page.query.filter_by(slug='404-error').first()
    return render_template(f'page/{page.template}.html', page=page), 404
Esempio n. 11
0
def download(obj_id):
    days_until_expired = 7
    Page.set_nav()
    product = Product.query.filter_by(id=obj_id, active=True).first()
    access_code = request.args.get('code')
    if product.verify_download(access_code, days=days_until_expired):
        return send_from_directory(current_app.config['PRODUCT_DIR'],
                                   product.download_path,
                                   as_attachment=True)
    flash(
        f'The access code for <b>{product.name}</b> is either invalid or expired. Download links expire after {days_until_expired} days. If there was a problem with your purchase, please contact <a href="mailto:{current_app.config.get("ADMINS")[0]}">{current_app.config.get("ADMINS")[0]}</a>.'
    )
    return redirect(url_for('shop.index'))
Esempio n. 12
0
def pages(unpub=None):
    page = Page.query.filter_by(slug='admin').first()
    Page.set_nav()
    if unpub:
        pages = Page.query.filter_by(published=False).order_by(
            'dir_path', 'sort', 'title')
    else:
        pages = Page.query.filter_by(published=True).order_by(
            'dir_path', 'sort', 'title')
    return render_template(
        'admin/pages.html',
        tab='pages',
        pages=pages,
        unpub=unpub,
        page=page,
    )
Esempio n. 13
0
def index(path):
    Page.set_nav()
    current_app.logger.debug(request.host_url)
    current_app.logger.debug(request.host.lower())
    if request.host.lower() == "sprig.houstonhare.com":
        path = f"/stories/sprig/{path}"
    else:
        path = f"/{path}"
    page = Page.query.filter_by(path=path).first()
    print(f"path: {path}")
    print(f"page: {page}")
    if page:
        code = request.args['code'] if 'code' in request.args else None
        if page.published or page.check_view_code(code):
            return render_template(f'page/{page.template}.html', page=page)
    page = Page.query.filter_by(slug='404-error').first()
    return render_template(f'page/{page.template}.html', page=page), 404
Esempio n. 14
0
def index(path):
    comment_form = AuthenticatedCommentForm(
    ) if current_user.is_authenticated else CommentForm()
    comment_form.subscribe.data = False if current_user.is_authenticated else True
    Page.set_nav()
    current_app.logger.debug(request.host_url)
    current_app.logger.debug(request.host.lower())
    if request.host.lower() == "sprig.houstonhare.com":
        path = f"/stories/sprig/{path}"
    else:
        path = f"/{path}"
    page = Page.query.filter_by(path=path).first()
    print(f"path: {path}")
    print(f"page: {page}")
    if page:
        comment_form.page_id.data = page.id
        code = request.args['code'] if 'code' in request.args else None
        if page.published or page.check_view_code(code):
            return render_template(f'page/{page.template}.html',
                                   page=page,
                                   comment_form=comment_form,
                                   js='comments.js')
    page = Page.query.filter_by(slug='404-error').first()
    return render_template(f'page/{page.template}.html', page=page), 404
Esempio n. 15
0
def edit_page(id, ver_id=None):
    page = Page.query.filter_by(id=id).first()
    was_published = page.published
    print(f"ANCESTORS: {page.ancestors()}")
    for anc in page.ancestors():
        print(f"ANCESTOR: {anc}")
    form = AddPageForm()
    form.parent_id.choices = [(0, '---')] + [(p.id, f"{p.title} ({p.path})")
                                             for p in Page.query.all()]
    form.user_id.choices = [(u.id, u.username) for u in User.query.all()]
    form.notify_group.choices = [
        ('', ''), ('all', 'All')
    ] + Subscriber.SUBSCRIPTION_CHOICES + [('discord', 'Discord Only')]
    for field in form:
        print(f"{field.name}: {field.data}")
    if form.validate_on_submit():

        prev_parentid = page.parent_id if page.parent_id else None
        # Create version from current
        version = PageVersion(
            original_id=id,
            title=page.title,
            slug=page.slug,
            template=page.template,
            parent_id=prev_parentid,
            banner=page.banner,
            body=page.body,
            notes=page.notes,
            summary=page.summary,
            author_note=page.author_note,
            author_note_location=page.author_note_location,
            sidebar=page.sidebar,
            tags=page.tags,
            user_id=page.user_id,
            notify_group=page.notify_group,
            pub_date=page.pub_date,
            published=page.published,
            path=page.path,
            dir_path=page.dir_path,
        )
        db.session.add(version)

        # Update page
        log_orig = log_change(page)
        parentid = form.parent_id.data if form.parent_id.data else None
        page.title = form.title.data
        page.slug = form.slug.data
        page.template = form.template.data
        page.parent_id = parentid
        page.banner = form.banner.data
        page.body = form.body.data
        page.notes = form.notes.data
        page.summary = form.summary.data
        page.author_note = form.author_note.data
        page.author_note_location = form.author_note_location.data
        page.sidebar = form.sidebar.data
        page.tags = form.tags.data
        page.user_id = form.user_id.data
        page.notify_group = form.notify_group.data
        page.published = form.published.data
        page.edit_date = datetime.utcnow()

        pdate = form.pub_date.data
        ptime = form.pub_time.data
        local_tz = form.timezone.data if form.timezone.data else current_user.timezone
        if pdate and ptime:
            page.set_local_pub_date(f"{pdate} {ptime}", local_tz)
        else:
            page.pub_date = None
        page.set_path()
        log_change(log_orig, page, 'edited a page')
        db.session.commit()
        if form.notify_subs.data:
            current_app.logger.debug(form.notify_group.data)
            page.notify_subscribers(form.notify_group.data)
        flash("Page updated successfully.", "success")
        Page.set_nav()
        return redirect(url_for('admin.edit_page', id=id))
    if form.errors:
        flash("<b>Error!</b> Please fix the errors below.", "danger")
    versions = PageVersion.query.filter_by(original_id=id).order_by(
        desc('edit_date')).all()
    version = PageVersion.query.filter_by(
        id=ver_id).first() if ver_id else None
    if version:
        form.title.data = version.title
        form.slug.data = version.slug
        form.template.data = version.template
        form.parent_id.data = version.parent_id
        form.banner.data = version.banner
        form.body.data = version.body
        form.notes.data = version.notes
        form.summary.data = version.summary
        form.author_note.data = version.author_note
        form.author_note_location.data = version.author_note_location
        form.sidebar.data = version.sidebar
        form.tags.data = version.tags
        form.user_id.data = version.user_id
        form.notify_group.data = version.notify_group
        form.pub_date.data = version.local_pub_date(current_user.timezone)
        form.pub_time.data = version.local_pub_date(current_user.timezone)
        form.published.data = version.published
    else:
        form.title.data = page.title
        form.slug.data = page.slug
        form.template.data = page.template
        form.parent_id.data = page.parent_id
        form.banner.data = page.banner
        form.body.data = page.body
        form.notes.data = page.notes
        form.summary.data = page.summary
        form.author_note.data = page.author_note
        form.author_note_location.data = page.author_note_location
        form.sidebar.data = page.sidebar
        form.tags.data = page.tags
        form.user_id.data = page.user_id
        form.notify_group.data = page.notify_group
        form.pub_date.data = page.local_pub_date(current_user.timezone)
        form.pub_time.data = page.local_pub_date(current_user.timezone)
        form.published.data = page.published
    return render_template('admin/page-edit.html',
                           form=form,
                           tab='pages',
                           action='Edit',
                           edit_page=page,
                           versions=versions,
                           version=version,
                           page=Page.query.filter_by(slug='admin').first())
Esempio n. 16
0
def home():
    Page.set_nav()
    page = Page.query.filter_by(path='/home', published=True).first()
    if page:
        return render_template(f'page/{page.template}.html', page=page)
    return render_template('home.html', page='page')
Esempio n. 17
0
def latest(path):
    Page.set_nav()
    path = f"/{path}"
    page = Page.query.filter_by(path=path).first()
    return redirect(url_for('page.index', path=page.latest().path))
Esempio n. 18
0
def set_nav():
    Page.set_nav()