Example #1
0
def category(parent_id=ROOT_CAT_ID):
    ''' This will render the main page with the selected category defined by parent_id, and with the
    content that it contains  '''
    contents = get_images_for_category(parent_id)
    if request.is_xhr:
        categories = [c.jsond() for c in CategoryModel.all().filter('parent_id', parent_id).filter('visible', True)]
        categories = sorted(categories, key=lambda c: c.order)
        return jsonify(categories=categories, contents=contents)
    else:
        category = CategoryModel.get_by_id(parent_id)
        path = None
        if category:
            path = category.get_path_ids_to_root() + [category.key().id()]
        categories = []
        for cat in CategoryModel.all().filter('parent_id', ROOT_CAT_ID).filter('visible', True):
            if path is not None and cat.key().id() in path:
                init_sub_tree_along_path(cat, path)
            categories += [cat]
            pass
        pass
        categories = sorted(categories, key=lambda c: c.order)
        contents = sorted(contents, key=lambda c: c['order'])
        return render_template('category/index.html', categories=categories, contents=contents,
                               current_category=category)
    pass
Example #2
0
def category(parent_id=ROOT_CAT_ID):
    ''' This will render the main page with the selected category defined by parent_id, and with the
    content that it contains  '''
    contents = get_images_for_category(parent_id)
    if request.is_xhr:
        categories = [
            c.jsond() for c in CategoryModel.all().filter(
                'parent_id', parent_id).filter('visible', True)
        ]
        categories = sorted(categories, key=lambda c: c.order)
        return jsonify(categories=categories, contents=contents)
    else:
        category = CategoryModel.get_by_id(parent_id)
        path = None
        if category:
            path = category.get_path_ids_to_root() + [category.key().id()]
        categories = []
        for cat in CategoryModel.all().filter('parent_id', ROOT_CAT_ID).filter(
                'visible', True):
            if path is not None and cat.key().id() in path:
                init_sub_tree_along_path(cat, path)
            categories += [cat]
            pass
        pass
        categories = sorted(categories, key=lambda c: c.order)
        contents = sorted(contents, key=lambda c: c['order'])
        return render_template('category/index.html',
                               categories=categories,
                               contents=contents,
                               current_category=category)
    pass
Example #3
0
def admin_categories(parent_id=ROOT_CAT_ID):
    """List all categories"""
    form = CategoryForm(prefix='category')
    if form.validate_on_submit():
        if len(form.key_id.data) > 0 and long(form.key_id.data) > 0:
            category = CategoryModel.get_by_id(long(form.key_id.data)); 
        else:
            category = CategoryModel()
        v2m(form, category)
        try:
            category.put()
            category_id = category.key().id()
            flash(u'Category %s successfully saved.' % category_id, 'success')
            return redirect(url_for('admin_category', parent_id=category.parent_id))
        except CapabilityDisabledError:
            flash(u'App Engine Datastore is currently in read-only mode.', 'info')
            return redirect(url_for('admin_category', parent_id=category.parent_id))
        except CircularCategoryException as cce:
            flash(cce.message, 'error')
            return redirect(url_for('admin_category', parent_id=category.parent_id))
        pass
    elif form.is_submitted():
        parent_id = long(form.parent_id.data)
    (categories, category_path, all_categories) = CategoryModel.get_categories_info(parent_id)
    form.parent_id.data = category_path[-1].key().id()
    reset_category = CategoryModel()
    reset_category.parent_id = parent_id
    return render_template('category/admin_categories.html',
                           categories=categories, form=form,
                           category_path=category_path,
                           current_category=category_path[-1],
                           all_categories=all_categories,
                           reset_category=reset_category)
Example #4
0
def delete_category(category_id):
    """Delete an category object"""
    category = CategoryModel.get_by_id(category_id)
    parent_id = category.parent_id
    try:
        category.delete()
        flash(u'Category %s successfully deleted.' % category_id, 'success')
    except CapabilityDisabledError:
        flash(u'App Engine Datastore is currently in read-only mode.', 'info')
    return redirect(url_for('admin_category', parent_id=parent_id))
Example #5
0
def move_category(category_id, parent_id=ROOT_CAT_ID):
    cat = CategoryModel.get_by_id(category_id)
    cat.parent_id = parent_id
    try:
        cat.put()
    except CircularCategoryException as cce:
        flash(cce.message, 'error')
        return redirect(url_for('admin_category', parent_id=parent_id))
    return redirect(url_for('admin_category', parent_id=parent_id), 302);
    pass
Example #6
0
def delete_category(category_id):
    """Delete an category object"""
    category = CategoryModel.get_by_id(category_id)
    parent_id = category.parent_id
    try:
        category.delete()
        flash(u'Category %s successfully deleted.' % category_id, 'success')
    except CapabilityDisabledError:
        flash(u'App Engine Datastore is currently in read-only mode.', 'info')
    return redirect(url_for('admin_category', parent_id=parent_id))
Example #7
0
def move_category(category_id, parent_id=ROOT_CAT_ID):
    cat = CategoryModel.get_by_id(category_id)
    cat.parent_id = parent_id
    try:
        cat.put()
    except CircularCategoryException as cce:
        flash(cce.message, 'error')
        return redirect(url_for('admin_category', parent_id=parent_id))
    return redirect(url_for('admin_category', parent_id=parent_id), 302)
    pass
Example #8
0
def admin_images(parent_id= -1):
    """
        List all images from within a category
    """
    category_id = parent_id
    form = ImageForm(prefix='image')
    if form.validate_on_submit():
        is_new = False
        if len(form.key_id.data) > 0 and long(form.key_id.data) > 0:
            image = ImageModel.get_by_id(long(form.key_id.data))
        else:
            image = ImageModel()
            is_new = True
        v2m(form, image)
        category_id = long(form.category_id.data)
        if type(form.image.data) == FileStorage and (is_new or form.update_image.data):
            blob_key = form.image.data.mimetype_params['blob-key']
            #create a small
            (image.image_thumb_blob_key, image.width, image.height) = create_thumbnail(blob_key, 50, 50)
            #and a slightly larger version 
            (image.image_blob_key, image.width, image.height) = create_thumbnail(blob_key)
            #delete the uploaded (possibly 'uge file)
            blobstore.delete(blob_key)
        elif not is_new and form.update_image.data: # we set to update with an empty image so we delete the one in the db
            image.delete_images()
            image.image_blob_key = ''
            image.image_thumb_blob_key = ''
            image.width = 0
            image.height = 0
        try:
            image.put()
            image_id = image.key().id()
            flash(u'Image %s successfully saved.' % image_id, 'success')
            return redirect(url_for('admin_images_in_category', parent_id=category_id))
        except CapabilityDisabledError:
            image.delete_images()
            flash(u'App Engine Datastore is currently in read-only mode.', 'info')
            return redirect(url_for('admin_images_in_category', parent_id=category_id))
        pass
    elif form.is_submitted():
        category_id = long(form.category_id.data)  
    images = sorted(ImageModel.all().filter('category_id', category_id), key=lambda i: i.order)
    (categories, category_path, all_categories) = CategoryModel.get_categories_info(category_id)
    form.category_id.data = category_path[-1].key().id()
    return render_template('image/admin_images.html', images=images,
                           form=form,
                           categories=categories,
                           category_path=category_path,
                           current_category=category_path[-1],
                           all_categories=all_categories,
                           reset_image=ImageModel(category_id=category_id),
                           upload_url=blobstore.create_upload_url(url_for('admin_images')))
Example #9
0
def admin_categories(parent_id=ROOT_CAT_ID):
    """List all categories"""
    form = CategoryForm(prefix='category')
    if form.validate_on_submit():
        if len(form.key_id.data) > 0 and long(form.key_id.data) > 0:
            category = CategoryModel.get_by_id(long(form.key_id.data))
        else:
            category = CategoryModel()
        v2m(form, category)
        try:
            category.put()
            category_id = category.key().id()
            flash(u'Category %s successfully saved.' % category_id, 'success')
            return redirect(
                url_for('admin_category', parent_id=category.parent_id))
        except CapabilityDisabledError:
            flash(u'App Engine Datastore is currently in read-only mode.',
                  'info')
            return redirect(
                url_for('admin_category', parent_id=category.parent_id))
        except CircularCategoryException as cce:
            flash(cce.message, 'error')
            return redirect(
                url_for('admin_category', parent_id=category.parent_id))
        pass
    elif form.is_submitted():
        parent_id = long(form.parent_id.data)
    (categories, category_path,
     all_categories) = CategoryModel.get_categories_info(parent_id)
    form.parent_id.data = category_path[-1].key().id()
    reset_category = CategoryModel()
    reset_category.parent_id = parent_id
    return render_template('category/admin_categories.html',
                           categories=categories,
                           form=form,
                           category_path=category_path,
                           current_category=category_path[-1],
                           all_categories=all_categories,
                           reset_category=reset_category)
Example #10
0
def init_sub_tree_along_path(category, path, visible_only=True):
    '''
        Initializes the subcategories for the category recursively, if the category is in the path
    '''
    category.subcategories = []
    query_set = CategoryModel.all().filter('parent_id', category.key().id())
    if visible_only:
        query_set.filter('visible', True)
    for cat in query_set:
        if cat.key().id() in path:
            init_sub_tree_along_path(cat, path)
            pass
        category.subcategories += [cat] 
    pass
Example #11
0
def init_sub_tree_along_path(category, path, visible_only=True):
    '''
        Initializes the subcategories for the category recursively, if the category is in the path
    '''
    category.subcategories = []
    query_set = CategoryModel.all().filter('parent_id', category.key().id())
    if visible_only:
        query_set.filter('visible', True)
    for cat in query_set:
        if cat.key().id() in path:
            init_sub_tree_along_path(cat, path)
            pass
        category.subcategories += [cat]
    pass