Пример #1
0
def seed_catagories(type_list, user_list):
    catagory_list = [
        Catagory(owner=user_list[0].id , typeId=type_list[0].id, name="Fiction"),
        Catagory(owner=user_list[0].id ,typeId=type_list[1].id, name="Sci-Fi"),
        Catagory(owner=user_list[0].id ,typeId=type_list[2].id, name="Alternative")
    ]
    

    db.session.add_all(catagory_list)

    db.session.commit()
    return catagory_list
def test_form_has_all_catagories_in_form(test_app, logged_in,
                                         dummy_catagories):
    catatgories = [c.name for c in Catagory.fetch_all()]
    res = test_app.get(url_for('create_item_page'))
    form = res.html.find('form', class_='item-form')
    options = [o['value'] for o in form.find_all('option')]
    assert options == catatgories
Пример #3
0
def update_item(item_name, catagory_name):
    """Post endpoint to update an item. If form is invalid will return create
    item page with errors displayed, otherwise update item and redirect to
    item page.
    """
    try:
        item = Item.fetch_by_name_and_catagory_name(item_name, catagory_name)
    except NoResultFound:
        abort(404)
    errors = form_errors(request.form)
    new_item_name = request.form.get('name')
    new_catagory_name = request.form.get('catagory')
    new_description = request.form.get('description')
    if errors:
        values = {
            'name': new_item_name,
            'catagory': new_catagory_name,
            'description': new_description
        }
        catagories = [c.name for c in Catagory.fetch_all()]
        return render_template(
            'add_item.html',
            catagories=catagories,
            values=values,
            errors=errors
        )
    item.update(
        name=new_item_name,
        catagory_name=new_catagory_name,
        description=new_description
    )
    return redirect(url_for(
        'read_item', item_name=new_item_name, catagory_name=new_catagory_name
    ))
Пример #4
0
def update_item_page(item_name, catagory_name):
    """Endpoint to display update item page."""
    item = Item.fetch_by_name_and_catagory_name(item_name, catagory_name)
    catagories = [c.name for c in Catagory.fetch_all()]
    return render_template(
        'edit_item.html',
        catagories=catagories,
        values={
            'name': item.name,
            'catagory': item.catagory_name,
            'description': item.description
        },
    )
Пример #5
0
def form_errors(form):
    """Return dict containing form validation errors for create / update item.
    """
    errors = {}
    max_name_length = Item.name.property.columns[0].type.length
    if not form.get('name', None):
        errors['name'] = 'Please enter a name.'
    elif len(form['name']) > max_name_length:
        errors['name'] = (
            'Name must be less than %s characters.' % max_name_length
        )
    if not Catagory.exists(form.get('catagory', None)):
        errors['catagory'] = 'Not a valid catagory.'
    if not form.get('description', None):
        errors['description'] = 'Please enter a description.'
    return errors
Пример #6
0
def write_post_api():
    title = request.json.get('title')
    content = request.json.get('content')
    catagory = request.json.get('catagory')
    cover_url = request.json.get('url')
    if not (title and content and catagory and cover_url):
        raise BadRequestError("The request body is not present")
    post_id = int(time.time())
    headers = {
        'Content-Type': 'text/plain'
    }
    body_html = requests.post("https://api.github.com/markdown/raw", content.encode('utf-8'), headers=headers).text
    new_post = Post(title=title, body=content, post_id=post_id, cover_url=cover_url, body_html=body_html)
    db.session.add(new_post)
    db.session.commit()
    # 将文章的标签存入Catagory模型的item字段中:
    for each in catagory:
        new_item = Catagory(item=each, post_id=post_id)
        db.session.add(new_item)
    db.session.commit()
    return new_post.return_dict()
Пример #7
0
def create_item():
    """Post endpoint to create an item. If form is invalid will return create
    item page with errors displayed, otherwise create item and redirect to
    item page.
    """
    name = request.form['name']
    catagory = request.form['catagory']
    description = request.form['description']
    errors = form_errors(request.form)
    if errors:
        catagories = [c.name for c in Catagory.fetch_all()]
        values = {
            'name': name, 'catagory': catagory, 'description': description
        }
        return render_template(
            'add_item.html',
            catagories=catagories,
            values=values,
            errors=errors
        )
    Item.create(name, catagory_name=catagory, description=description)
    return redirect(url_for(
        'read_item', catagory_name=catagory, item_name=name
    ))
Пример #8
0
def create_item_page():
    """Endpoint to display create item page."""
    catagories = [c.name for c in Catagory.fetch_all()]
    return render_template('add_item.html', catagories=catagories, values={})