Beispiel #1
0
    def _import_categories(request, app):

        request.locale = locale
        headers = {
            'id': request.translate(_("ID")),
            'name': request.translate(_("Name")),
            'title': request.translate(_("Title")),
            'active': request.translate(_("Active"))
        }

        session = app.session()
        categories = CategoryCollection(session)

        if clear:
            click.secho("Deleting categories", fg='yellow')
            for category in categories.query():
                session.delete(category)

        csvfile = convert_xls_to_csv(
            file, sheet_name=request.translate(_("Categories"))
        )
        csv = CSVFile(csvfile, expected_headers=headers.values())
        lines = list(csv.lines)
        columns = {
            key: csv.as_valid_identifier(value)
            for key, value in headers.items()
        }

        count = 0
        for line in lines:
            count += 1
            id_ = int(getattr(line, columns['id']))
            name = getattr(line, columns['name'])
            title = getattr(line, columns['title'])
            active = bool(int(getattr(line, columns['active'])))
            categories.add_root(
                id=id_,
                name=name,
                title=title,
                active=active,
                order=count
            )

        click.secho(f"{count} categorie(s) imported", fg='green')

        if dry_run:
            transaction.abort()
            click.secho("Aborting transaction", fg='yellow')
Beispiel #2
0
def test_notice_collection_used_categories(session):
    collection = GazetteNoticeCollection(session)

    categories = CategoryCollection(session)
    a = categories.add_root(name='1', title='A')
    b = categories.add_root(name='2', title='B')
    c = categories.add_root(name='3', title='C')
    for category, count in (('1', 2), ('2', 4), ('3', 1)):
        for x in range(count):
            collection.add(title='',
                           text='',
                           organization_id=None,
                           category_id=category,
                           issues=['2017-{}'.format(y) for y in range(x)],
                           user=None)

    assert collection.used_categories == [a, b, c]
Beispiel #3
0
def delete_category(self, request, form):
    """ Delete a category.

    Only unused categorys may be deleted.

    """
    layout = Layout(self, request)
    session = request.session

    if self.in_use:
        request.message(
            _("Only unused categorys may be deleted."),
            'alert'
        )
        return {
            'layout': layout,
            'title': self.title,
            'subtitle': _("Delete Category"),
            'show_form': False
        }

    if form.submitted(request):
        collection = CategoryCollection(session)
        collection.delete(self)
        request.message(_("Category deleted."), 'success')
        return redirect(layout.manage_categories_link)

    return {
        'message': _(
            'Do you really want to delete "${item}"?',
            mapping={'item': self.title}
        ),
        'layout': layout,
        'form': form,
        'title': self.title,
        'subtitle': _("Delete Category"),
        'button_text': _("Delete Category"),
        'button_class': 'alert',
        'cancel': layout.manage_categories_link
    }
Beispiel #4
0
def test_category_form(session):
    request = DummyRequest(session)

    # Test apply / update
    # ... unused
    categories = CategoryCollection(session)
    category = categories.add_root(name='1', title='ABC', active=True)

    form = CategoryForm()
    form.request = request
    form.apply_model(category)
    assert form.title.data == 'ABC'
    assert form.active.data is True
    assert form.name.data == '1'

    form.title.data = 'DEF'
    form.active.data = False
    form.name.data = '3'
    form.update_model(category)
    assert category.title == 'DEF'
    assert category.active is False
    assert category.name == '3'

    # ... used
    category = categories.add_root(name='2', title='XYZ', active=True)
    GazetteNoticeCollection(session).add('title', 'text', '', '2', None, [])

    form.apply_model(category)
    assert form.name.render_kw == {'readonly': True}

    # Test validation
    # ... empty values
    form = CategoryForm()
    form.request = request
    assert not form.validate()

    # ... new model
    form = CategoryForm(DummyPostData({'title': 'title', 'name': '2'}))
    form.request = request
    assert not form.validate()
    assert 'This value already exists.' in form.errors['name']

    form = CategoryForm(DummyPostData({'title': 'title', 'name': '5'}))
    form.request = request
    assert form.validate()

    # ... existing model
    form = CategoryForm(DummyPostData({'title': 'title', 'name': '3'}))
    form.model = categories.query().filter_by(name='2').one()
    form.request = request
    assert not form.validate()
    assert 'This value already exists.' in form.errors['name']
    assert 'This value is in use.' in form.errors['name']

    form = CategoryForm(DummyPostData({'title': 'title', 'name': '5'}))
    form.model = categories.query().filter_by(name='3').one()
    form.request = request
    assert form.validate()
Beispiel #5
0
def test_unused_column_key_value_validator(session):
    # new item
    form = DummyForm(session)
    field = DummyField('name', 'XXX')
    validator = UnusedColumnKeyValue(OfficialNotice._categories)
    validator(form, field)

    # used value
    OfficialNoticeCollection(session).add('title', 'text', categories=['1'])
    form.model = CategoryCollection(session).add_root(title='XXX')
    assert form.model.name == '1'
    with raises(ValidationError) as excinfo:
        validator(form, field)
    assert str(excinfo.value) == 'This value is in use.'
Beispiel #6
0
def test_notice_collection_for_categories(session):
    collection = GazetteNoticeCollection(session)

    categories = CategoryCollection(session)
    categories.add_root(name='1', title='A')
    categories.add_root(name='2', title='B')
    categories.add_root(name='3', title='C')
    for category, count in (('1', 2), ('2', 4), ('3', 1)):
        for x in range(count):
            collection.add(title='',
                           text='',
                           organization_id=None,
                           category_id=category,
                           issues=['2017-{}'.format(y) for y in range(x)],
                           user=None)

    assert collection.for_categories(['1']).query().count() == 2
    assert collection.for_categories(['2']).query().count() == 4
    assert collection.for_categories(['3']).query().count() == 1
    assert collection.for_categories(['1', '3']).query().count() == 3
Beispiel #7
0
def test_notice_collection_count_by_category(session):
    collection = GazetteNoticeCollection(session)
    assert collection.count_by_category() == []

    categories = CategoryCollection(session)
    categories.add_root(name='1', title='A')
    categories.add_root(name='2', title='B')
    categories.add_root(name='3', title='C')
    for category, count in (('1', 2), ('2', 4), ('3', 1)):
        for x in range(count):
            collection.add(title='',
                           text='',
                           organization_id=None,
                           category_id=category,
                           issues=['2017-{}'.format(y) for y in range(x)],
                           user=None)
    assert collection.count_by_category() == [('A', 1), ('B', 6)]

    assert collection.count_by_category() == \
        collection.for_state('drafted').count_by_category()

    collection.issues = ['2017-0', '2017-2']
    assert collection.count_by_category() == [('A', 1), ('B', 4)]
Beispiel #8
0
def get_category(app, id):
    return CategoryCollection(app.session()).by_id(id)
Beispiel #9
0
def get_categories(app):
    return CategoryCollection(app.session())
Beispiel #10
0
def test_category_collection(session):
    collection = CategoryCollection(session)

    collection.add_root(title='First', active=True)
    collection.add_root(title='Second', active=False)
    collection.add_root(title='Third')
    collection.add_root(title='Fourth', active=True)

    categories = collection.query().all()
    assert categories[0].title == 'First'
    assert categories[0].name == '1'

    assert categories[1].title == 'Fourth'
    assert categories[1].name == '4'

    assert categories[2].title == 'Second'
    assert categories[2].name == '2'

    assert categories[3].title == 'Third'
    assert categories[3].name == '3'
Beispiel #11
0
 def manage_categories_link(self):
     return self.request.link(CategoryCollection(self.session))
Beispiel #12
0
def create_categories(session):
    categories = CategoryCollection(session)
    categories.add_root(name='10', title='Complaints', active=False)
    categories.add_root(name='11', title='Education', active=True)
    categories.add_root(name='12', title='Submissions', active=True)
    categories.add_root(name='13', title='Commercial Register', active=True)
    categories.add_root(name='14', title='Elections', active=True)
    return categories.query().all()