Exemple #1
0
    def route_save(self, path):
        cfg = get_project_config()
        data_file = get_data(cfg, path[1])
        if not data_file:
            return self.render_error(404)

        fn, data = data_file
        form_data = self.parse_form()
        reqs = get_video_requirements()

        for req in reqs:
            key = req['name']
            if key in form_data:
                value = None
                if req['type'] == 'IntegerField':
                    value = int(form_data[key].value)
                elif req['type'] == 'BooleanField':
                    value = bool(int(form_data[key].value))
                elif req['type'] in ('DateField', 'DateTimeField'):
                    # TODO: Verify the data format
                    value = form_data[key].value
                elif req['type'] in ('CharField', 'TextField'):
                    value = form_data[key].value.replace('\r\n', '\n')
                elif req['type'] == 'SlugField':
                    # TODO: Verify the data format. Maybe if there is
                    # no slug field, we create it from the title?
                    value = form_data[key].value
                elif req['type'] == 'TextArrayField':
                    # Split the field on carriage returns and drop any
                    # empty strings.
                    value = form_data[key].value
                    value = [
                        mem.strip() for mem in value.split('\n')
                        if mem.strip()
                    ]
                elif req['type'] in ('URLField'):
                    value = form_data[key].value
                else:
                    raise NotImplementedError

                data[key] = value
            else:
                if req['type'] in ('CharField', 'TextField'):
                    data[key] = ''
                elif req['type'] == 'TextArrayField':
                    data[key] = []

                # TODO: What to do about other fields? Set to default?

        # Category is a special case. If it's empty, we should remove
        # it so verify works well.
        if 'category' in data and not data['category'].strip():
            del data['category']

        save_json_file(cfg, fn, data)

        return self.redirect('/edit/{0}'.format(fn))
Exemple #2
0
    def route_save(self, path):
        cfg = get_project_config()
        data_file = get_data(cfg, path[1])
        if not data_file:
            return self.render_error(404)

        fn, data = data_file
        form_data = self.parse_form()
        reqs = get_video_requirements()

        for req in reqs:
            key = req['name']
            if key in form_data:
                value = None
                if req['type'] == 'IntegerField':
                    value = int(form_data[key].value)
                elif req['type'] == 'BooleanField':
                    value = bool(int(form_data[key].value))
                elif req['type'] in ('DateField', 'DateTimeField'):
                    # TODO: Verify the data format
                    value = form_data[key].value
                elif req['type'] in ('CharField', 'TextField'):
                    value = form_data[key].value.replace('\r\n', '\n')
                elif req['type'] == 'SlugField':
                    # TODO: Verify the data format. Maybe if there is
                    # no slug field, we create it from the title?
                    value = form_data[key].value
                elif req['type'] == 'TextArrayField':
                    # Split the field on carriage returns and drop any
                    # empty strings.
                    value = form_data[key].value
                    value = [mem.strip() for mem in value.split('\n')
                             if mem.strip()]
                elif req['type'] in ('URLField'):
                    value = form_data[key].value
                else:
                    raise NotImplementedError

                data[key] = value
            else:
                if req['type'] in ('CharField', 'TextField'):
                    data[key] = ''
                elif req['type'] == 'TextArrayField':
                    data[key] = []

                # TODO: What to do about other fields? Set to default?

        # Category is a special case. If it's empty, we should remove
        # it so verify works well.
        if 'category' in data and not data['category'].strip():
            del data['category']

        save_json_file(cfg, fn, data)

        return self.redirect('/edit/{0}'.format(fn))
Exemple #3
0
    def route_save(self, path):
        cfg = get_project_config()
        data_file = get_data(cfg, path[1])
        if not data_file:
            return self.render_error(404)

        fn, data = data_file
        form_data = self.parse_form()
        reqs = get_video_requirements()

        for req in reqs:
            key = req['name']
            if key in form_data:
                value = None
                if req['type'] == 'IntegerField':
                    value = int(form_data[key].value)
                elif req['type'] == 'BooleanField':
                    value = bool(int(form_data[key].value))
                elif req['type'] in ('DateField', 'DateTimeField'):
                    # TODO: Verify the data format
                    value = form_data[key].value
                elif req['type'] in ('CharField', 'TextField'):
                    value = form_data[key].value
                elif req['type'] == 'SlugField':
                    # TODO: Verify the data format. Maybe if there is
                    # no slug field, we create it from the title?
                    value = form_data[key].value
                elif req['type'] in ('TextArrayField'):
                    # Split the field on carriage returns and drop any
                    # empty strings.
                    value = [
                        mem.strip() for mem in form_data[key].value.split('\n')
                        if mem.strip()
                    ]

                data[key] = value
            else:
                if req['type'] in ('CharField', 'TextField'):
                    data[key] = ''

                # TODO: What to do about other fields? Set to default?

        save_json_file(cfg, fn, data)

        return self.redirect('/edit/{0}'.format(fn[5:]))
Exemple #4
0
    def route_save(self, path):
        cfg = get_project_config()
        data_file = get_data(cfg, path[1])
        if not data_file:
            return self.render_error(404)

        fn, data = data_file
        form_data = self.parse_form()
        reqs = get_video_requirements()

        for req in reqs:
            key = req['name']
            if key in form_data:
                value = None
                if req['type'] == 'IntegerField':
                    value = int(form_data[key].value)
                elif req['type'] == 'BooleanField':
                    value = bool(int(form_data[key].value))
                elif req['type'] in ('DateField', 'DateTimeField'):
                    # TODO: Verify the data format
                    value = form_data[key].value
                elif req['type'] in ('CharField', 'TextField'):
                    value = form_data[key].value
                elif req['type'] == 'SlugField':
                    # TODO: Verify the data format. Maybe if there is
                    # no slug field, we create it from the title?
                    value = form_data[key].value
                elif req['type'] in ('TextArrayField'):
                    # Split the field on carriage returns and drop any
                    # empty strings.
                    value = [mem.strip()
                             for mem in form_data[key].value.split('\n')
                             if mem.strip()]

                data[key] = value
            else:
                if req['type'] in ('CharField', 'TextField'):
                    data[key] = ''

                # TODO: What to do about other fields? Set to default?

        save_json_file(cfg, fn, data)

        return self.redirect('/edit/{0}'.format(fn[5:]))
Exemple #5
0
def push_cmd(cfg, parser, parsed, args):
    if not parsed.quiet:
        parser.print_byline()

    # Get username, api_url and api_key.

    username = get_from_config(cfg, 'username')
    api_url = get_from_config(cfg, 'api_url')

    update = parsed.update

    # Command line api_key overrides config-set api_key
    api_key = parsed.apikey
    if not api_key:
        try:
            api_key = cfg.get('project', 'api_key')
        except ConfigParser.NoOptionError:
            pass
    if not api_key:
        err('Specify an api key either in steve.ini, on command line, '
            'or in API_KEY file.')
        return 1

    if not username or not api_url or not api_key:
        return 1

    data = load_json_files(cfg)

    if args:
        data = [(fn, contents) for fn, contents in data if fn in args]

    # There are two modes:
    #
    # 1. User set category in configuration. Then the json files can
    #    either have no category set or they have to have the same
    #    category set.
    #
    # 2. User has NOT set category in configuration. Then the json
    #    files must all have the category set. The categories can be
    #    different.
    #
    # Go through and make sure there aren't any problems with
    # categories.

    api = steve.restapi.API(api_url)

    all_categories = dict(
        [(cat['title'], cat)
         for cat in steve.richardapi.get_all_categories(api_url)])

    try:
        category = cfg.get('project', 'category')
        category = category.strip()
        if category not in all_categories:
            err('Category "{0}" does not exist on server. Build it there '
                'first.'.format(category))
            return 1

    except ConfigParser.NoOptionError:
        category = None

    errors = False
    for fn, contents in data:
        if category is None:
            this_cat = contents.get('category')
            if not this_cat:
                err('No category set in configuration and {0} has no '
                    'category set.'.format(fn))
                errors = True
            elif this_cat != this_cat.strip():
                err('Category "{0}" has whitespace at beginning or '
                    'end.'.format(this_cat))
                return 1
            elif this_cat not in all_categories:
                err('Category "{0}" does not exist on server. '
                    'Build it there first.'.format(this_cat))
                return 1

        else:
            this_cat = contents.get('category')
            if this_cat is not None and str(this_cat).strip() != category:
                err('Category set in configuration ({0}), but {1} has '
                    'different category ({2}).'.format(
                    category, fn, this_cat))
                errors = True

    if update:
        for fn, contents in data:
            if not 'id' in contents:
                err('id not in contents for "{0}".'.format(fn))
                errors = True
                break

    if errors:
        err('Aborting.')
        return 1

    # Everything looks ok. So double-check with the user and push.

    out('Pushing to: {0}'.format(api_url))
    out('Username:   {0}'.format(username))
    out('api_key:    {0}'.format(api_key))
    out('update?:    {0}'.format(update))
    out('Once you push, you can not undo it. Push for realz? Y/N')
    if not raw_input().strip().lower().startswith('y'):
        err('Aborting.')
        return 1

    for fn, contents in data:
        contents['category'] = category or contents.get('category')

        if not update:
            # Nix any id field since that causes problems.
            if 'id' in contents:
                del contents['id']

            out('Pushing {0}'.format(fn))
            try:
                vid = steve.restapi.get_content(
                    api.video.post(contents, username=username,
                                   api_key=api_key))
                if 'id' in vid:
                    contents['id'] = vid['id']
                    out('   Now has id {0}'.format(vid['id']))
                else:
                    err('   Errors?: {0}'.format(vid))
            except steve.restapi.RestAPIException as exc:
                err('   Error?: {0}'.format(exc))
                err('   "{0}"'.format(exc.response.content))

        else:
            out('Updating {0} "{1}" ({2})'.format(
                contents['id'], contents['title'], fn))
            try:
                vid = steve.restapi.get_content(
                    api.video(contents['id']).put(
                        contents, username=username, api_key=api_key))
            except steve.restapi.RestAPIException as exc:
                err('   Error?: {0}'.format(exc))
                err('   "{0}"'.format(exc.response.content))

        save_json_file(cfg, fn, contents)

    return 0
Exemple #6
0
def push_cmd(cfg, parser, parsed, args):
    if not parsed.quiet:
        parser.print_byline()

    # Get username, api_url and api_key.

    username = get_from_config(cfg, 'username')
    api_url = get_from_config(cfg, 'api_url')

    update = parsed.update

    # Command line api_key overrides config-set api_key
    api_key = parsed.apikey
    if not api_key:
        try:
            api_key = cfg.get('project', 'api_key')
        except ConfigParser.NoOptionError:
            pass
    if not api_key:
        err('Specify an api key either in steve.ini, on command line, '
            'or in API_KEY file.')
        return 1

    if not username or not api_url or not api_key:
        return 1

    data = load_json_files(cfg)

    if args:
        data = [(fn, contents) for fn, contents in data if fn in args]

    # There are two modes:
    #
    # 1. User set category in configuration. Then the json files can
    #    either have no category set or they have to have the same
    #    category set.
    #
    # 2. User has NOT set category in configuration. Then the json
    #    files must all have the category set. The categories can be
    #    different.
    #
    # Go through and make sure there aren't any problems with
    # categories.

    all_categories = dict([
        (cat['title'], cat)
        for cat in steve.richardapi.get_all_categories(api_url)
    ])

    try:
        category = cfg.get('project', 'category')
        category = category.strip()
        if category not in all_categories:
            err('Category "{0}" does not exist on server. Build it there '
                'first.'.format(category))
            return 1
        else:
            out('Category {0} exists on site.'.format(category))
    except ConfigParser.NoOptionError:
        category = None

    errors = False
    for fn, contents in data:
        if category is None:
            this_cat = contents.get('category')
            if not this_cat:
                err('No category set in configuration and {0} has no '
                    'category set.'.format(fn))
                errors = True
            elif this_cat != this_cat.strip():
                err('Category "{0}" has whitespace at beginning or '
                    'end.'.format(this_cat))
                return 1
            elif this_cat not in all_categories:
                err('Category "{0}" does not exist on server. '
                    'Build it there first.'.format(this_cat))
                return 1

        else:
            this_cat = contents.get('category')
            if this_cat is not None and str(this_cat).strip() != category:
                err('Category set in configuration ({0}), but {1} has '
                    'different category ({2}).'.format(category, fn, this_cat))
                errors = True

    if update:
        for fn, contents in data:
            if not 'id' in contents:
                err('id not in contents for "{0}".'.format(fn))
                errors = True
                break

    if errors:
        err('Aborting.')
        return 1

    # Everything looks ok. So double-check with the user and push.

    out('Pushing to: {0}'.format(api_url))
    out('Username:   {0}'.format(username))
    out('api_key:    {0}'.format(api_key))
    out('update?:    {0}'.format(update))
    out('# videos:   {0}'.format(len(data)))
    out('Once you push, you can not undo it. Push for realz? Y/N')
    if not raw_input().strip().lower().startswith('y'):
        err('Aborting.')
        return 1

    for fn, contents in data:
        contents['category'] = category or contents.get('category')

        if not update:
            # Nix any id field since that causes problems.
            if 'id' in contents:
                if not parsed.overwrite:
                    print 'Skipping... already exists'
                    continue
                del contents['id']

            out('Pushing {0}'.format(fn))
            try:
                vid = steve.richardapi.create_video(api_url, api_key, contents)

                if 'id' in vid:
                    contents['id'] = vid['id']
                    out('   Now has id {0}'.format(vid['id']))
                else:
                    err('   Errors?: {0}'.format(vid))
            except steve.restapi.RestAPIException as exc:
                err('   Error?: {0}'.format(exc))
                err('   "{0}"'.format(exc.response.content))

        else:
            out('Updating {0} "{1}" ({2})'.format(contents['id'],
                                                  contents['title'], fn))
            try:
                vid = steve.richardapi.update_video(api_url, api_key,
                                                    contents['id'], contents)
            except steve.restapi.RestAPIException as exc:
                err('   Error?: {0}'.format(exc))
                err('   "{0}"'.format(exc.response.content))

        save_json_file(cfg, fn, contents)

    return 0
Exemple #7
0
def push(cfg, ctx, quiet, apikey, update, overwrite, files):
    """Pushes metadata to a richard instance."""
    if not quiet:
        click.echo(VERSION)

    # Get username, api_url and api_key.

    username = get_from_config(cfg, 'username')
    api_url = get_from_config(cfg, 'api_url')

    # Command line api_key overrides config-set api_key
    if not apikey:
        try:
            apikey = cfg.get('project', 'api_key')
        except NoOptionError:
            pass
    if not apikey:
        raise click.ClickException(
            u'Specify an api key either in {0}, on command line, '
            u'or in API_KEY file.'.format(get_project_config_file_name())
        )

    if not username or not api_url or not apikey:
        raise click.ClickException(u'Missing username, api_url or api_key.')

    data = load_json_files(cfg)

    if files:
        data = [(fn, contents) for fn, contents in data if fn in files]

    # There are two modes:
    #
    # 1. User set category in configuration. Then the json files can
    #    either have no category set or they have to have the same
    #    category set.
    #
    # 2. User has NOT set category in configuration. Then the json
    #    files must all have the category set. The categories can be
    #    different.
    #
    # Go through and make sure there aren't any problems with
    # categories.

    all_categories = dict(
        [(cat['title'], cat)
         for cat in steve.richardapi.get_all_categories(api_url)])

    try:
        category = cfg.get('project', 'category')
        category = category.strip()
        if category not in all_categories:
            raise click.ClickException(
                u'Category "{0}" does not exist on server. Build it there '
                u'first.'.format(category)
            )
        else:
            click.echo('Category {0} exists on site.'.format(category))
    except NoOptionError:
        category = None

    errors = []
    for fn, contents in data:
        if category is None:
            this_cat = contents.get('category')
            if not this_cat:
                errors.append(
                    u'No category set in configuration and {0} has no '
                    u'category set.'.format(fn)
                )
            elif this_cat != this_cat.strip():
                errors.append(
                    u'Category "{0}" has whitespace at beginning or '
                    u'end.'.format(this_cat)
                )
            elif this_cat not in all_categories:
                errors.append(
                    u'Category "{0}" does not exist on server. '
                    u'Build it there first.'.format(this_cat)
                )

        else:
            this_cat = contents.get('category')
            if this_cat is not None and str(this_cat).strip() != category:
                errors.append(
                    u'Category set in configuration ({0}), but {1} has '
                    u'different category ({2}).'.format(category, fn, this_cat)
                )

    if update:
        for fn, contents in data:
            if 'id' not in contents:
                errors.append(
                    u'id not in contents for "{0}".'.format(fn)
                )

    if errors:
        raise click.ClickException('\n'.join(errors))

    # Everything looks ok. So double-check with the user and push.

    click.echo('Pushing to: {0}'.format(api_url))
    click.echo('Username:   {0}'.format(username))
    click.echo('api_key:    {0}'.format(apikey))
    click.echo('update?:    {0}'.format(update))
    click.echo('# videos:   {0}'.format(len(data)))
    click.echo('Once you push, you can not undo it. Push for realz? Y/N')
    if not raw_input().strip().lower().startswith('y'):
        raise click.Abort()

    for fn, contents in data:
        contents['category'] = category or contents.get('category')

        if not update:
            # Nix any id field since that causes problems.
            if 'id' in contents:
                if not overwrite:
                    click.echo(u'Skipping... already exists.')
                    continue
                del contents['id']

            click.echo('Pushing {0}'.format(fn))
            try:
                vid = steve.richardapi.create_video(api_url, apikey, contents)

                if 'id' in vid:
                    contents['id'] = vid['id']
                    click.echo('   Now has id {0}'.format(vid['id']))
                else:
                    click.echo('   Errors?: {0}'.format(vid), err=True)
            except steve.restapi.RestAPIException as exc:
                click.echo('   Error?: {0}'.format(exc), err=True)
                click.echo('   "{0}"'.format(exc.response.content), err=True)

        else:
            click.echo('Updating {0} "{1}" ({2})'.format(
                contents['id'], contents['title'], fn))
            try:
                vid = steve.richardapi.update_video(
                    api_url, apikey, contents['id'], contents)
            except steve.restapi.RestAPIException as exc:
                click.err('   Error?: {0}'.format(exc))
                click.err('   "{0}"'.format(exc.response.content))

        save_json_file(cfg, fn, contents)
Exemple #8
0
def push(cfg, ctx, quiet, apikey, update, overwrite, files):
    """Pushes metadata to a richard instance."""
    if not quiet:
        click.echo(VERSION)

    # Get username, api_url and api_key.

    username = get_from_config(cfg, 'username')
    api_url = get_from_config(cfg, 'api_url')

    # Command line api_key overrides config-set api_key
    if not apikey:
        try:
            apikey = cfg.get('project', 'api_key')
        except ConfigParser.NoOptionError:
            pass
    if not apikey:
        raise click.ClickException(
            u'Specify an api key either in {0}, on command line, '
            u'or in API_KEY file.'.format(get_project_config_file_name())
        )

    if not username or not api_url or not apikey:
        raise click.ClickException(u'Missing username, api_url or api_key.')

    data = load_json_files(cfg)

    if files:
        data = [(fn, contents) for fn, contents in data if fn in files]

    # There are two modes:
    #
    # 1. User set category in configuration. Then the json files can
    #    either have no category set or they have to have the same
    #    category set.
    #
    # 2. User has NOT set category in configuration. Then the json
    #    files must all have the category set. The categories can be
    #    different.
    #
    # Go through and make sure there aren't any problems with
    # categories.

    all_categories = dict(
        [(cat['title'], cat)
         for cat in steve.richardapi.get_all_categories(api_url)])

    try:
        category = cfg.get('project', 'category')
        category = category.strip()
        if category not in all_categories:
            raise click.ClickException(
                u'Category "{0}" does not exist on server. Build it there '
                u'first.'.format(category)
            )
        else:
            click.echo('Category {0} exists on site.'.format(category))
    except ConfigParser.NoOptionError:
        category = None

    errors = []
    for fn, contents in data:
        if category is None:
            this_cat = contents.get('category')
            if not this_cat:
                errors.append(
                    u'No category set in configuration and {0} has no '
                    u'category set.'.format(fn)
                )
            elif this_cat != this_cat.strip():
                errors.append(
                    u'Category "{0}" has whitespace at beginning or '
                    u'end.'.format(this_cat)
                )
            elif this_cat not in all_categories:
                errors.append(
                    u'Category "{0}" does not exist on server. '
                    u'Build it there first.'.format(this_cat)
                )

        else:
            this_cat = contents.get('category')
            if this_cat is not None and str(this_cat).strip() != category:
                errors.append(
                    u'Category set in configuration ({0}), but {1} has '
                    u'different category ({2}).'.format(category, fn, this_cat)
                )

    if update:
        for fn, contents in data:
            if 'id' not in contents:
                errors.append(
                    u'id not in contents for "{0}".'.format(fn)
                )

    if errors:
        raise click.ClickException('\n'.join(errors))

    # Everything looks ok. So double-check with the user and push.

    click.echo('Pushing to: {0}'.format(api_url))
    click.echo('Username:   {0}'.format(username))
    click.echo('api_key:    {0}'.format(apikey))
    click.echo('update?:    {0}'.format(update))
    click.echo('# videos:   {0}'.format(len(data)))
    click.echo('Once you push, you can not undo it. Push for realz? Y/N')
    if not raw_input().strip().lower().startswith('y'):
        raise click.Abort()

    for fn, contents in data:
        contents['category'] = category or contents.get('category')

        if not update:
            # Nix any id field since that causes problems.
            if 'id' in contents:
                if not overwrite:
                    click.echo(u'Skipping... already exists.')
                    continue
                del contents['id']

            click.echo('Pushing {0}'.format(fn))
            try:
                vid = steve.richardapi.create_video(api_url, apikey, contents)

                if 'id' in vid:
                    contents['id'] = vid['id']
                    click.echo('   Now has id {0}'.format(vid['id']))
                else:
                    click.echo('   Errors?: {0}'.format(vid), err=True)
            except steve.restapi.RestAPIException as exc:
                click.echo('   Error?: {0}'.format(exc), err=True)
                click.echo('   "{0}"'.format(exc.response.content), err=True)

        else:
            click.echo('Updating {0} "{1}" ({2})'.format(
                contents['id'], contents['title'], fn))
            try:
                vid = steve.richardapi.update_video(
                    api_url, apikey, contents['id'], contents)
            except steve.restapi.RestAPIException as exc:
                click.echo('   Error?: {0}'.format(exc), err=True)
                click.echo('   "{0}"'.format(exc.response.content), err=True)

        save_json_file(cfg, fn, contents)