def pending_list_add(options): with Session() as session: try: pending_list = plugin_pending_list.get_list_by_exact_name(options.list_name, session=session) except NoResultFound: console('Could not find a pending list with name `{}`, creating'.format(options.list_name)) pending_list = plugin_pending_list.PendingListList(name=options.list_name) session.add(pending_list) session.merge(pending_list) session.commit() title = options.entry_title entry = {'title': options.entry_title, 'url': options.url} db_entry = plugin_pending_list.get_entry_by_title(list_id=pending_list.id, title=title, session=session) if db_entry: console("Entry with the title `{}` already exist with list `{}`. Will replace identifiers if given".format( title, pending_list.name)) operation = 'updated' else: console("Adding entry with title `{}` to list `{}`".format(title, pending_list.name)) db_entry = plugin_pending_list.PendingListEntry(entry=entry, pending_list_id=pending_list.id) if options.approved: console('marking entry as approved') db_entry.approved = True session.add(db_entry) operation = 'added' if options.attributes: console('Adding attributes to entry `{}`'.format(title)) for identifier in options.attributes: for k, v in identifier.items(): entry[k] = v db_entry.entry = entry console('Successfully {} entry `{}` to pending list `{}` '.format(operation, title, pending_list.name))
def pending_list_show(options): with Session() as session: try: pending_list = plugin_pending_list.get_list_by_exact_name(options.list_name, session=session) except NoResultFound: console('Could not find pending list with name {}'.format(options.list_name)) return try: entry = plugin_pending_list.get_entry_by_id(pending_list.id, int(options.entry), session=session) except NoResultFound: console('Could not find matching pending entry with ID {} in list `{}`'.format(int(options.entry), options.list_name)) return except ValueError: entry = plugin_pending_list.get_entry_by_title(pending_list.id, options.entry, session=session) if not entry: console('Could not find matching pending entry with title `{}` in list `{}`'.format(options.entry, options.list_name)) return header = ['Field name', 'Value'] table_data = [header] for k, v in sorted(entry.entry.items()): table_data.append([k, str(v)]) table = TerminalTable(options.table_type, table_data, wrap_columns=[1]) table.table.justify_columns[0] = 'center' try: console(table.output) except TerminalTableError as e: console('ERROR: %s' % str(e))
def pending_list_approve(options, approve=None): with Session() as session: try: entry_list = plugin_pending_list.get_list_by_exact_name(options.list_name) except NoResultFound: console('Could not find pending list with name `{}`'.format(options.list_name)) return try: db_entry = plugin_pending_list.get_entry_by_id(entry_list.id, int(options.entry), session=session) except NoResultFound: console('Could not find matching entry with ID {} in list `{}`'.format(int(options.entry), options.list_name)) return except ValueError: db_entry = plugin_pending_list.get_entry_by_title(entry_list.id, options.entry, session=session) if not db_entry: console('Could not find matching entry with title `{}` in list `{}`'.format(options.entry, options.list_name)) return approve_text = 'approved' if approve else 'rejected' if (db_entry.approved is True and approve is True) or (db_entry.approved is False and approve is False): console('entry {} is already {}'.format(db_entry.title, approve_text)) return db_entry.approved = approve console('Successfully marked pending entry {} as {}'.format(db_entry.title, approve_text))
def pending_list_show(options): with Session() as session: try: pending_list = get_list_by_exact_name(options.list_name, session=session) except NoResultFound: console('Could not find pending list with name {}'.format(options.list_name)) return try: entry = get_entry_by_id(pending_list.id, int(options.entry), session=session) except NoResultFound: console('Could not find matching pending entry with ID {} in list `{}`'.format(int(options.entry), options.list_name)) return except ValueError: entry = get_entry_by_title(pending_list.id, options.entry, session=session) if not entry: console('Could not find matching pending entry with title `{}` in list `{}`'.format(options.entry, options.list_name)) return header = ['Field name', 'Value'] table_data = [header] for k, v in sorted(entry.entry.items()): table_data.append([k, str(v)]) table = TerminalTable(options.table_type, table_data, wrap_columns=[1]) table.table.justify_columns[0] = 'center' try: console(table.output) except TerminalTableError as e: console('ERROR: %s' % str(e))
def pending_list_approve(options, approve=None): with Session() as session: try: entry_list = get_list_by_exact_name(options.list_name) except NoResultFound: console('Could not find pending list with name `{}`'.format(options.list_name)) return try: db_entry = get_entry_by_id(entry_list.id, int(options.entry), session=session) except NoResultFound: console('Could not find matching entry with ID {} in list `{}`'.format(int(options.entry), options.list_name)) return except ValueError: db_entry = get_entry_by_title(entry_list.id, options.entry, session=session) if not db_entry: console('Could not find matching entry with title `{}` in list `{}`'.format(options.entry, options.list_name)) return approve_text = 'approved' if approve else 'rejected' if (db_entry.approved is True and approve is True) or (db_entry.approved is False and approve is False): console('entry {} is already {}'.format(db_entry.title, approve_text)) return db_entry.approved = approve console('Successfully marked pending entry {} as {}'.format(db_entry.title, approve_text))
def pending_list_add(options): with Session() as session: try: pending_list = get_list_by_exact_name(options.list_name, session=session) except NoResultFound: console('Could not find a pending list with name `{}`, creating'.format(options.list_name)) pending_list = PendingListList(name=options.list_name) session.add(pending_list) session.merge(pending_list) session.commit() title = options.entry_title entry = {'title': options.entry_title, 'url': options.url} db_entry = get_entry_by_title(list_id=pending_list.id, title=title, session=session) if db_entry: console("Entry with the title `{}` already exist with list `{}`. Will replace identifiers if given".format( title, pending_list.name)) operation = 'updated' else: console("Adding entry with title `{}` to list `{}`".format(title, pending_list.name)) db_entry = PendingListEntry(entry=entry, pending_list_id=pending_list.id) if options.approved: console('marking entry as approved') db_entry.approved = True session.add(db_entry) operation = 'added' if options.attributes: console('Adding attributes to entry `{}`'.format(title)) for identifier in options.attributes: for k, v in identifier.items(): entry[k] = v db_entry.entry = entry console('Successfully {} entry `{}` to pending list `{}` '.format(operation, title, pending_list.name))
def post(self, list_id, session=None): """ Create a new entry object""" try: get_list_by_id(list_id=list_id, session=session) except NoResultFound: raise NotFoundError('list_id %d does not exist' % list_id) data = request.json title = data.get('title') entry_object = get_entry_by_title(list_id=list_id, title=title, session=session) if entry_object: raise Conflict('entry with title \'%s\' already exists' % title) entry_object = PendingListEntry(entry=data, pending_list_id=list_id) if data.get('approved'): entry_object.approved = data['approved'] session.add(entry_object) session.commit() response = jsonify(entry_object.to_dict()) response.status_code = 201 return response
def post(self, list_id, session=None): """ Create a new entry object""" try: get_list_by_id(list_id=list_id, session=session) except NoResultFound: raise NotFoundError('list_id %d does not exist' % list_id) data = request.json title = data.get('title') entry_object = get_entry_by_title(list_id=list_id, title=title, session=session) if entry_object: raise Conflict('entry with title \'%s\' already exists' % title) entry_object = PendingListEntry(entry=data, pending_list_id=list_id) if data.get('approved'): entry_object.approved = data['approved'] session.add(entry_object) session.commit() response = jsonify(entry_object.to_dict()) response.status_code = 201 return response
def pending_list_del(options): with Session() as session: try: entry_list = plugin_pending_list.get_list_by_exact_name(options.list_name) except NoResultFound: console('Could not find pending list with name `{}`'.format(options.list_name)) return try: db_entry = plugin_pending_list.get_entry_by_id(entry_list.id, int(options.entry), session=session) except NoResultFound: console( 'Could not find matching entry with ID {} in list `{}`'.format(int(options.entry), options.list_name)) return except ValueError: db_entry = plugin_pending_list.get_entry_by_title(entry_list.id, options.entry, session=session) if not db_entry: console( 'Could not find matching entry with title `{}` in list `{}`'.format(options.entry, options.list_name)) return console('Removing entry `{}` from list {}'.format(db_entry.title, options.list_name)) session.delete(db_entry)
def pending_list_del(options): with Session() as session: try: entry_list = get_list_by_exact_name(options.list_name) except NoResultFound: console('Could not find pending list with name `{}`'.format(options.list_name)) return try: db_entry = get_entry_by_id(entry_list.id, int(options.entry), session=session) except NoResultFound: console( 'Could not find matching entry with ID {} in list `{}`'.format(int(options.entry), options.list_name)) return except ValueError: db_entry = get_entry_by_title(entry_list.id, options.entry, session=session) if not db_entry: console( 'Could not find matching entry with title `{}` in list `{}`'.format(options.entry, options.list_name)) return console('Removing entry `{}` from list {}'.format(db_entry.title, options.list_name)) session.delete(db_entry)