예제 #1
0
def update(options, args):
    if len(options.objects) == 1:
        if len(args) == 2:
            to_update = b.get(
                options.user_id,
                options.workspace_id,
                options.objects[0],
                filter_by={'id': int(args[0])}
            )[0]
            update = None
            try:
                update = json.loads(args[1]) # FIXME: is there something in simplejson that lets me just check whether a string is valid JSON?
            except json.JSONDecodeError as e:
                value = args[1]
                if isinstance(value, str):
                    value = unicode(value)
                b.set_attributes(options.user_id, options.workspace_id, to_update, {
                    to_update['primary_descriptor']: value
                })
            else:
                for prop, value in (json.loads(args[1])).iteritems():
                    if isinstance(value, str):
                        value = unicode(value)
                    b.set_attributes(options.user_id, options.workspace_id,
                        to_update, {prop: value})
            print 'Updated:', format(to_update, options)
            b.commit()
        else:
            raise Exception('Two and only two arguments in update')
    else:
        raise Exception('One and only one object in update')
예제 #2
0
def lnk(options, args):
    if len(options.objects) == 2:
        if len(args) == 2:
            obj = b.get(options.user_id, options.workspace_id,
                options.objects[0], filter_by={'id': int(args[0])})[0]
            related_obj = b.get(
                options.user_id, options.workspace_id,
                options.objects[1], filter_by={'id': int(args[1])}
            )[0]
            if options.unlink:
                b.unlink(options.user_id, options.workspace_id, obj,
                    related_obj, options.relationship)
            else:
                b.link(options.user_id, options.workspace_id, obj,
                    related_obj, options.relationship)
            b.commit()
        else:
            raise Exception('Two and only two arguments in link')
    else:
        raise Exception('Two and only two objects in link')
예제 #3
0
def complete(options, args):
    if len(options.objects) == 1:
        if len(args) == 1:
            to_complete = b.get(
                options.user_id, options.workspace_id, options.objects[0],
                filter_by={'id': int(args[0])}
            )[0]
            result = b.set_completed(options.user_id, options.workspace_id, to_complete)
            if result is not None:
                print 'Completed:', format(to_complete, options)
                b.commit()
        else:
            raise Exception('One and only one argument in complete')
    else:
        raise Exception('One and only one object in complete')
예제 #4
0
def read(options, args):
    objs = None
    filter_by = {'id': args[0]} if not options.head and args is not None and len(args) == 1 else None
    objs = b.get(options.user_id, options.workspace_id, options.objects[0], options.all, filter_by)
    if options.head:
        objs = objs[:int(args[0])]
    if len(options.objects) == 1:
        print '{0}s:'.format(options.objects[0]), format(objs, options)
    else:
        for obj in objs:
            print '{0}:'.format(options.objects[0]), format(obj, options)
            related_attribute_name = b.relationship_name(obj['type'], options.objects[1], options.relationship)
            print '{0}'.format(links_to_language[related_attribute_name])
            related_objs = obj[related_attribute_name]
            print '\t{0}s:'.format(options.objects[1])
            for related_obj in related_objs:
                if not 'state' in related_obj or options.all or related_obj['state'] != 'completed':
                    print '\t', format(related_obj, options)
예제 #5
0
def delete(options, args):
    if len(options.objects) == 1:
        if len(args) == 1:
            to_delete = b.get(
                options.user_id, options.workspace_id, options.objects[0],
                filter_by={'id': int(args[0])}
            )
            if len(to_delete) > 0:
                deleted = b.delete_from_db(options.user_id, options.workspace_id, to_delete[0])
                print 'Deleted:', format(deleted, options)
                b.commit()
            else:
                raise Exception('Todo with id {0} not found.'.format(
                    args[0]
                ))
        else:
            raise Exception('One and only one argument in delete')
    else:
        raise Exception('One and only one object in delete')