Пример #1
0
def delete_need(args):
    """Deletes needs with given primary key or with summary matching given
    query. If the query matches more than one item, the operation is cancelled.
    """
    for func in flatten_nargs_plus, fix_unicode:
        func(args, 'query', 'project')
    if args.primary_key:
        yield('Deleting need with primary key {0}'.format(args.primary_key))
        db = app.get_feature('document_storage').default_db
        need = db.get(Need, args.primary_key)
        if confirm(u'Delete need {summary}'.format(**need)):
            if not args.dry_run:
                remove_need(need, dry_run=args.dry_run)
        else:
            yield('Operation cancelled.')
    elif args.query or args.project:
        try:
            needs = ensure_results(find_needs, project=args.project,
                                   query=args.query)
        except NotFound as e:
            raise CommandError('Cannot delete items: {0}'.format(e))
        yield('Matching needs:')
        for need in needs:
            yield(u'- {summary}'.format(**need))
            yield('  primary key: {0}'.format(need.pk))
        if confirm('Delete these items'):
            if not args.dry_run:
                for need in needs:
                    yield('Dropping {summary}…'.format(**need))
                    remove_need(need, dry_run=args.dry_run)
        else:
            yield('Operation cancelled.')
    else:
        yield('Please specify either --primary-key or --query/--project.')

    if args.dry_run:
        yield('Simulation: nothing was actually changed in the database.')
Пример #2
0
def open_urls(args):
    "Gathers URLs for matching actors and opens them is a web browser."
    actors = find_actors(args.actor)
    def _contacts():
        for a in actors:
            for c in a.contacts.where(kind='url'):
                yield c
    contacts = list(_contacts())
    yield('Found:\n')
    for c in contacts:
        yield(u'  {url} {actor}\n'.format(
            url=c.value, actor=Style.DIM+unicode(c.actor)+Style.NORMAL))
    if confirm('Open these URLs', default=True):
        for c in contacts:
            webbrowser.open_new_tab(c.value)
        yield('URLs were open in new tabs.')
    else:
        yield('\nCancelled.')
Пример #3
0
def move_contact(args):
    "Moves matching contacts to given actor"
    fix_unicode(args, 'from_actor', 'query', 'type', 'new_actor')
    new_actor = None
    try:
        new_actor = get_single(find_actors, args.new_actor)
    except MultipleMatches as e:
        raise CommandError(e)
    except NotFound as e:
        raise CommandError(u'No actor matches "{0}".'.format(args.actor))

    assert args.from_actor or args.query, 'specify actor or auery'

    db = default_storage()
    contacts = Contact.objects(db).where_not(actor=new_actor.pk)
    if args.type:
        contacts = contacts.where(type__in=args.type)
    if args.query:
        contacts = contacts.where(value__matches_caseless=args.query)
    if args.from_actor:
        try:
            from_actor = get_single(find_actors, args.from_actor)
        except MultipleMatches as e:
            raise CommandError(e)
        except NotFound(e):
            raise CommandError('Bad --from-actor: no match for "{0}"'.format(
                args.from_actor))
        contacts = contacts.where(actor=from_actor.pk)

    if not len(contacts):
        raise CommandError('No suitable contacts were found.')

    yield('About to move these contacts:\n')
    for c in contacts:
        yield(u'- {actor}: {kind} {v}'.format(v=bright(c.value), **c))
    yield('')

    msg = u'Move these contacts to {0}'.format(bright(new_actor))
    if confirm(msg, default=True):
        for c in contacts:
            c.actor = new_actor
            c.save(db)
    else:
        yield('\nCancelled.')
Пример #4
0
def mark_need(args):
    "Marks matching needs as satisfied."
    for func in flatten_nargs_plus, fix_unicode:
        func(args, 'query', 'project')
    assert not (args.important and args.unimportant)
    assert not (args.satisfied and args.unsatisfied)
    assert any([args.satisfied, args.unsatisfied,
                args.important, args.unimportant]), 'A flag must be chosen'
    if args.primary_key:
        assert not (args.query or args.project), (
            '--primary-key cannot be combined with --query/--project')
        db = app.get_feature('document_storage').default_db
        needs = [db.get(Need, args.primary_key)]
    else:
        needs = ensure_results(find_needs, project=args.project, query=args.query)
    for need in needs:
        satisfied = 'satisfied' if need.is_satisfied else 'unsatisfied'
        important = 'important' if not need.is_discarded else 'unimportant'
        yield(u'- {summary} ({satisfied}, {important})'.format(
            satisfied=satisfied, important=important, **need))
    yield('')
    if confirm('Apply changes to these items', default=True, skip=args.yes):
        if args.dry_run:
            yield('Simulation: nothing was actually changed in the database.')
        else:
            for need in needs:
                yield(u'Marking "{summary}"...'.format(**need))
                if args.satisfied and not need.is_satisfied:
                    yield(u'  + unsatisfied → satisfied')
                    need.is_satisfied = True
                if args.unsatisfied and need.is_satisfied:
                    yield(u'  + satisfied → unsatisfied')
                    need.is_satisfied = False
                if args.important and need.is_discarded:
                    need.is_discarded = False
                    yield(u'  + discarded → important')
                if args.unimportant and not need.is_discarded:
                    yield(u'  + important → discarded')
                    need.is_discarded = True
                need.save()
            yield('Changes have been applied.')
    else:
        yield('Operation cancelled.')