示例#1
0
def create_self_user_library_entry(username, listing_id, folder_name=None):
    """
    Create ApplicationLibrary Entry

    Args:
        username (str): the username to create a library entry for (Bookmark)
        listing_id (str): the id of the listing
        folder_name (str) optional: the name of the folder

    Return:
        ApplicationLibrary: New Entry of ApplicationLibrary

    Raise:
        Exception: if profile was not found based on username or
            or listing was not found based on listing_id
    """
    listing = listing_model_access.get_listing_by_id(username, listing_id)
    owner = generic_model_access.get_profile(username)

    if not listing or not owner:
        raise Exception('Listing or user not found')

    logger.debug('Adding bookmark for listing[{0!s}], user[{1!s}]'.format(listing.title, username), extra={'user': username})

    entry = models.ApplicationLibraryEntry(listing=listing, owner=owner, folder=folder_name)
    entry.save()
    return entry
示例#2
0
def create_library_entries(library_entries, object_cache):
    """
    Create Bookmarks for users

    Args:
        library_entries:
            [{'folder': None, 'listing_id': 8, 'owner': 'wsmith', 'position': 0},
             {'folder': None, 'listing_id': 5, 'owner': 'hodor', 'position': 0},...]
    """
    for current_entry in library_entries:
        current_profile = object_cache['Profile.{}'.format(
            current_entry['owner'])]
        current_listing = current_entry['listing_obj']
        library_entry = models.ApplicationLibraryEntry(
            owner=current_profile,
            listing=current_listing,
            folder=current_entry['folder'],
            position=current_entry['position'])
        library_entry.save()
示例#3
0
def create_library_entries(library_entries):
    """
    Create Bookmarks for users

    # library_entries = [{'folder': None, 'listing_id': 8, 'owner': 'wsmith', 'position': 0},
    #    {'folder': None, 'listing_id': 5, 'owner': 'hodor', 'position': 0},...]
    """
    print('Creating Library Entries...')
    for current_entry in library_entries:
        current_profile = models.Profile.objects.filter(
            user__username=current_entry['owner']).first()
        current_listing = models.Listing.objects.get(
            id=current_entry['listing_id'])
        library_entry = models.ApplicationLibraryEntry(
            owner=current_profile,
            listing=current_listing,
            folder=current_entry['folder'],
            position=current_entry['position'])
        library_entry.save()
        print('--[{}] creating bookmark for listing [{}]'.format(
            current_profile.user.username, current_listing.title))
    print('Finished Library Entries...')
def migrate_application_library_entry(profile_mapper, listing_mapper):
    logging.debug('migrating application_library_entry...')
    columns = get_columns('application_library_entry')
    # ['id', 'version', 'created_by_id', 'created_date', 'edited_by_id', 'edited_date', 'folder', 'listing_id', 'owner_id', 'application_library_idx']
    assert columns[0] == 'id'
    assert columns[1] == 'version'
    assert columns[2] == 'created_by_id'
    assert columns[3] == 'created_date'
    assert columns[4] == 'edited_by_id'
    assert columns[5] == 'edited_date'
    assert columns[6] == 'folder'
    assert columns[7] == 'listing_id'
    assert columns[8] == 'owner_id'
    values = get_values('application_library_entry', len(columns))
    # logging.debug('category columns: %s' % columns)
    logging.info('Application Library Entries to migrate: {0!s}'.format(
        len(values)))
    logging.info('==========================')
    for i in values:
        try:
            old_id = i[0]
            folder = i[6]
            listing_id = i[7]
            listing = models.Listing.objects.get(id=listing_mapper[listing_id])
            owner = models.Profile.objects.get(id=profile_mapper[i[8]])
            logging.info(
                'Adding application_library_entry for listing {0!s}, owner {1!s}'
                .format(listing.title, owner.user.username))
            entry = models.ApplicationLibraryEntry(folder=folder,
                                                   listing=listing,
                                                   owner=owner)
            entry.save()
        except Exception as e:
            logging.error(
                'Error adding library entry: {0!s}, values: {1!s}'.format(
                    str(e), i))
示例#5
0
def create_batch_library_entries(username, data):
    """
    Create Batch

    Args:
        username (str): username
        data (List<Dict>): Payload
            [
                {
                    "listing": {
                        "id": 1
                    },
                    "folder": "folderName" (or null),
                    "id": 2,
                    "position": 2
                },
                {
                    "listing": {
                        "id": 2
                    },
                    "folder": "folderName" (or null),
                    "id": 1,
                    "position": 1
                }
            ]

        Return:
            List<Dict>: payload data
    """
    owner = generic_model_access.get_profile(username)
    if not owner:
        return []

    validated_data = []
    # validate input
    for data_entry in data:
        error = False
        # Validates Listing
        new_data_entry = {}

        if 'listing' not in data_entry:
            error = True
        else:
            listing_id = data_entry.get('listing', {}).get('id')
            listing_obj = listing_model_access.get_listing_by_id(
                username, listing_id)
            if not listing_obj:
                error = True
            else:
                new_data_entry['listing'] = listing_obj

        if 'folder' not in data_entry:
            new_data_entry['folder'] = None
        else:
            new_data_entry['folder'] = data_entry['folder']

        if 'position' not in data_entry:
            new_data_entry['position'] = None
        else:
            try:
                position_value = int(data_entry['position'])
                new_data_entry['position'] = position_value
            except:
                new_data_entry['position'] = None

        if not error:
            validated_data.append(new_data_entry)

    output_entries = []
    for data_entry in validated_data:
        listing = data_entry.get('listing')
        folder_name = data_entry.get('folder')
        position = data_entry.get('position')

        if not listing:
            raise Exception('Listing not found')

        logger.debug('Adding bookmark for listing[{0!s}], user[{1!s}]'.format(
            listing.title, username),
                     extra={'user': username})

        entry = models.ApplicationLibraryEntry(listing=listing,
                                               owner=owner,
                                               folder=folder_name)
        if position:
            entry.position = position

        entry.save()

        output_entries.append(entry)

    return output_entries