コード例 #1
0
ファイル: views.py プロジェクト: muhilvarnan/django-sample
def index(request):
    nav = get_nav_list()
    return render(request, "blog/index.html", {
        "header": "Start Bootstrap",
        "nav": nav,
        "categories": get_categories()
    })
コード例 #2
0
ファイル: item.py プロジェクト: cstieg/Item-Catalog
def edit_item_handler(catalog_id, item_id):
    """Edits an item in a catalog
    Parameters:
    - catalog_id: integer id of catalog to which item belongs
    - item_id: integer id of item to edit
    
    Returns:
    - HTTP response
    """
    user = models.get_current_user()
    catalog_id = int(catalog_id)
    catalog_entity = models.Catalog.get_by_id(catalog_id)
    item_id = int(item_id)
    item_entity = models.get_item_by_id(catalog_id, item_id)

    # Check parameters
    if not catalog_entity:
        raise BadRequest('Could not find catalog with id %d' % catalog_id)
    if not (item_entity):
        raise BadRequest('Could not find item with id %d!' % item_id)
    if not catalog_entity.user_can_edit(user):
        raise Unauthorized

    if flask.request.method == 'GET':
        # Render edit item form
        categories = models.get_categories(catalog_id)
        return flask.render_template('edititem.html',
                                     user=user,
                                     catalog=catalog_entity,
                                     categories=categories,
                                     category_id=item_entity.category.id(),
                                     item=item_entity)

    elif flask.request.method == 'POST':
        # Handle item edit from form
        picture_obj = flask.request.files.get('picture')
        picture_url = uploadfile.save_file(picture_obj)

        try:
            price = float(flask.request.form.get('price'))
        except:
            price = 0.00

        # Update entity
        item_entity.name = flask.request.form.get('name')
        item_entity.description = flask.request.form.get('description')
        item_entity.price = price
        item_entity.picture = picture_url

        # Allow possibility of item without category
        category_id = flask.request.form.get('category_id')
        if category_id:
            category_id = int(category_id)
        category_entity = models.get_category_by_id(catalog_id, category_id)
        if category_entity:
            item_entity.category = category_entity.key

        item_entity.put()
        models.wait_for(item_entity)
        return flask.redirect('/catalog/%d' % catalog_entity.key.id())
コード例 #3
0
def categories():
    """
    Return list of categories

    :return string: JSON
    """
    cats = [item.serialize for item in get_categories()]
    return jsonify(cats), 200
コード例 #4
0
ファイル: markups.py プロジェクト: dkurchigin/example_tg_bot
def get_category_markups():
    category_markup = types.ReplyKeyboardMarkup(row_width=1)
    all_categories = get_categories()

    for one_category in all_categories:
        button = types.KeyboardButton(one_category.name)
        category_markup.add(button)

    return category_markup
コード例 #5
0
def catalog_view_handler(catalog_id):
    """Renders a catalog page
     Parameters:
    - catalog_id: integer id of catalog to be rendered
    
    Returns:
    - HTTP response
    """
    user = models.get_current_user()
    catalog_id = int(catalog_id)
    catalog = models.get_catalog_by_id(catalog_id)
    if not catalog:
        raise BadRequest('Could not find catalog with id %d!' % catalog_id)
    categories = models.get_categories(catalog_id)
    return flask.render_template('catalog.html',
                                 user=user,
                                 catalog=catalog,
                                 categories=categories)
コード例 #6
0
def add_category():
    """
    Add a new category

    :return string: JSON
    """

    # check user status
    if g.user.status != 'admin':
        return jsonify({'error': "You do not have permission to do that"}), 200

    # get and clean data
    new_category = clean(request.json.get('name'))

    # check category exist
    if check_category(new_category):
        create_category(new_category)

    # return list of categories
    cats = [item.serialize for item in get_categories()]
    return jsonify(cats), 200
コード例 #7
0
# create TeleBot instance
bot = telebot.TeleBot(config.token)
# Setup connection to the DB
conn = sqlite3.connect(config.db_source)

# Initialize base entities
users = {}
categories = ()
products = ()
baskets = ()
deliveries = ()
orders = ()

# fill categories from DB
categories = get_categories(conn)

# fill products from DB
products = get_products(conn)

# fill baskets from DB
baskets = get_baskets(conn)

# fill deliveries from DB
deliveries = get_deliveries(conn)

# fill orders from DB
orders = get_orders(conn)

conn.close()
コード例 #8
0
from werkzeug.utils import secure_filename
from oauth2client.client import flow_from_clientsecrets, FlowExchangeError
from bleach import clean
from httplib2 import Http
from flask import make_response
from requests import get as r_get
from json import dumps, loads
from re import findall

ALLOWED_EXTENSIONS = set(EXTENSIONS)
auth = HTTPBasicAuth()

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

brands = get_categories()
csrf_token = get_unique_str(32)


# TODO: Login required
def login_required(f):
    """
    Checking the user is logged in

    :param f:
    :return:
    """
    @wraps(f)
    def decorated_function(*args, **kwargs):
        if 'uid' in session:
            return f(*args, **kwargs)
コード例 #9
0
ファイル: item.py プロジェクト: cstieg/Item-Catalog
def add_item_handler(catalog_id):
    """Adds an item to a catalog
    Parameters:
    - catalog_id: integer id of catalog to which item belongs
    
    Returns:
    - HTTP response
    """
    user = models.get_current_user()
    catalog_id = int(catalog_id)
    catalog_entity = models.get_catalog_by_id(catalog_id)

    # Check parameters
    if not catalog_entity:
        raise BadRequest('Could not find catalog with id %d' % catalog_id)
    if not catalog_entity.user_can_edit(user):
        raise Unauthorized

    if flask.request.method == 'GET':
        # Render add item form
        # Category_id may be passed in if adding item from category edit page
        category_id = flask.request.args.get('category_id')
        if category_id:
            category_id = int(category_id)

        categories = models.get_categories(catalog_id)
        return flask.render_template('additem.html',
                                     user=user,
                                     catalog=catalog_entity,
                                     category_id=category_id,
                                     categories=categories,
                                     item=None)

    elif flask.request.method == 'POST':
        # Handle new item submission from form
        picture_obj = flask.request.files.get('picture')
        picture_url = uploadfile.save_file(picture_obj)

        try:
            price = float(flask.request.form.get('price'))
        except:
            price = 0.00

        new_item = models.Item(
            name=flask.request.form.get('name'),
            description=flask.request.form.get('description'),
            price=price,
            picture=picture_url,
            owner=user.key,
            catalog=catalog_entity.key)

        # Allow possibility of putting items without category
        category_id = flask.request.form.get('category_id')
        if category_id:
            category_id = int(category_id)
        category_entity = models.get_category_by_id(catalog_id, category_id)
        if category_entity:
            new_item.category = category_entity.key

        new_item.put()
        models.wait_for(new_item)
        return flask.redirect('/catalog/%d' % catalog_entity.key.id())