示例#1
0
def process_form(category_id=None):
    form = CategoryForm()
    cat_contr = CategoryController(current_user.id)

    if not form.validate():
        return render_template('edit_category.html', form=form)
    existing_cats = list(cat_contr.read(name=form.name.data))
    if existing_cats and category_id is None:
        flash(gettext("Couldn't add category: already exists."), "warning")
        return redirect(
            url_for('category.form', category_id=existing_cats[0].id))
    # Edit an existing category
    category_attr = {'name': form.name.data}

    if category_id is not None:
        cat_contr.update({'id': category_id}, category_attr)
        flash(
            gettext('Category %(cat_name)r successfully updated.',
                    cat_name=category_attr['name']), 'success')
        return redirect(url_for('category.form', category_id=category_id))

    # Create a new category
    new_category = cat_contr.create(**category_attr)

    flash(
        gettext('Category %(category_name)r successfully created.',
                category_name=new_category.name), 'success')

    return redirect(url_for('category.form', category_id=new_category.id))
示例#2
0
文件: category.py 项目: bzero/JARR
def process_form(category_id=None):
    form = CategoryForm()
    cat_contr = CategoryController(g.user.id)

    if not form.validate():
        return render_template('edit_category.html', form=form)
    existing_cats = list(cat_contr.read(name=form.name.data))
    if existing_cats and category_id is None:
        flash(gettext("Couldn't add category: already exists."), "warning")
        return redirect(url_for('category.form',
                                category_id=existing_cats[0].id))
    # Edit an existing category
    category_attr = {'name': form.name.data}

    if category_id is not None:
        cat_contr.update({'id': category_id}, category_attr)
        flash(gettext('Category %(cat_name)r successfully updated.',
                      cat_name=category_attr['name']), 'success')
        return redirect(url_for('category.form', category_id=category_id))

    # Create a new category
    new_category = cat_contr.create(**category_attr)

    flash(gettext('Category %(category_name)r successfully created.',
                  category_name=new_category.name), 'success')

    return redirect(url_for('category.form', category_id=new_category.id))
示例#3
0
 def test_feed_and_article_deletion(self):
     ccontr = CategoryController(2)
     cat = ccontr.read()[0].dump()
     ccontr.delete(cat['id'])
     self.assertEquals(0,
             ArticleController().read(category_id=cat['id']).count())
     self.assertEquals(0,
             FeedController().read(category_id=cat['id']).count())
示例#4
0
文件: user.py 项目: v-khdumi/JARR
def opml_import():
    if request.files.get('opmlfile', None) is None:
        flash(gettext('Got no file'), 'warning')
        return redirect(url_for('user.profile'))

    data = request.files.get('opmlfile', None)
    try:
        subscriptions = opml.from_string(data.read())
    except:
        flash(gettext("Couldn't parse file"), 'danger')
        return redirect(request.referrer)

    ccontr = CategoryController(current_user.id)
    fcontr = FeedController(current_user.id)
    created_count, existing_count, failed_count = 0, 0, 0
    categories = {cat.name: cat.id for cat in ccontr.read()}
    for line in subscriptions:
        try:
            link = line.xmlUrl
        except Exception:
            failed_count += 1
            continue

        # don't import twice
        if fcontr.read(link=link).count():
            existing_count += 1
            continue

        # handling categories
        cat_id = None
        category = getattr(line, 'category', None)
        if category:
            if category not in categories:
                new_category = ccontr.create(name=category)
                categories[new_category.name] = new_category.id
            cat_id = categories[category]

        fcontr.create(title=getattr(line, 'text', None),
                      category_id=cat_id,
                      description=getattr(line, 'description', None),
                      link=link,
                      site_link=getattr(line, 'htmlUrl', None))
        created_count += 1
    flash(
        gettext(
            "Created %(created)d feed ! (%(failed)d import failed, "
            "%(existing)d were already existing)",
            created=created_count,
            failed=failed_count,
            existing=existing_count), "info")
    return redirect(url_for('user.profile'))
示例#5
0
文件: user.py 项目: jaesivsm/JARR
def opml_import():
    if request.files.get('opmlfile', None) is None:
        flash(gettext('Got no file'), 'warning')
        return redirect(url_for('user.profile'))

    data = request.files.get('opmlfile', None)
    try:
        subscriptions = opml.from_string(data.read())
    except:
        flash(gettext("Couldn't parse file"), 'danger')
        return redirect(request.referrer)

    ccontr = CategoryController(current_user.id)
    fcontr = FeedController(current_user.id)
    created_count, existing_count, failed_count = 0, 0, 0
    categories = {cat.name: cat.id for cat in ccontr.read()}
    for line in subscriptions:
        try:
            link = line.xmlUrl
        except Exception:
            failed_count += 1
            continue

        # don't import twice
        if fcontr.read(link=link).count():
            existing_count += 1
            continue

        # handling categories
        cat_id = None
        category = getattr(line, 'category', None)
        if category:
            if category not in categories:
                new_category = ccontr.create(name=category)
                categories[new_category.name] = new_category.id
            cat_id = categories[category]

        fcontr.create(title=getattr(line, 'text', None), category_id=cat_id,
                      description=getattr(line, 'description', None),
                      link=link, site_link=getattr(line, 'htmlUrl', None))
        created_count += 1
    flash(gettext("Created %(created)d feed ! (%(failed)d import failed, "
                  "%(existing)d were already existing)",
                  created=created_count, failed=failed_count,
                  existing=existing_count), "info")
    return redirect(url_for('user.profile'))