Example #1
0
def create( args, helpers ):

    start = args.pop('start')

    creator_id = User.find_id_by_username( helpers.config['username'] )

    #Parse duration and date
    if 'duration' in args:
        args['duration'] = helpers.duration.parse( args['duration'] )
    if 'due_at' in args:
        args['due_at'] = helpers.date.parse( args['due_at'] )

    #Transform project name into project id
    found = Project.find_id_by_name( args.pop( 'project') )
    if len( found ) == 0:
        print "ERROR: project does not exist (project name not found)"
        sys.exit( 1 )
    elif len ( found ) > 1:
        print "ERROR: more than one project with the same name (try putting in a longer project name for searching)"
        sys.exit( 1 )

    args['project_id'] = found[0]

    #Transform usernames into user ids
    user_ids = [ creator_id[0] ]

    for username in args.pop( 'users', '' ):

        user_id = User.find_id_by_username( username )

        if len( user_id ) == 0:
            print "ERROR: no user found for username %s" % username
            sys.exit( 1 )
        elif len( user_id ) > 1:
            print "ERROR: more than one user found with username '%s' (try a longer username)"
            sys.exit( 1 )

        if not user_id:
            print "ERROR: user does not exist (username not found)"
            sys.exit( 1 )

        user_ids.append( user_id[0] )

    user_ids = list( set( user_ids ) )

    args['user_ids'] = user_ids

    task = Task.create( **args )

    if start:
        task.start()

    helpers.pprint( task, helpers.config['task']['format'] )